78729d7812fa0d350a963efc35ea31aa1ba35fdc
[git.git] / diff-stages.c
1 /*
2  * Copyright (c) 2005 Junio C Hamano
3  */
4
5 #include "cache.h"
6 #include "diff.h"
7
8 static int diff_output_format = DIFF_FORMAT_HUMAN;
9 static int detect_rename = 0;
10 static int diff_setup_opt = 0;
11 static int diff_score_opt = 0;
12 static const char *pickaxe = NULL;
13 static int pickaxe_opts = 0;
14 static int diff_break_opt = -1;
15 static const char *orderfile = NULL;
16 static const char *diff_filter = NULL;
17
18 static char *diff_stages_usage =
19 "git-diff-stages [-p] [-r] [-z] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] <stage1> <stage2> [<path>...]";
20
21 int main(int ac, const char **av)
22 {
23         int stage1, stage2, i;
24
25         read_cache();
26         while (1 < ac && av[1][0] == '-') {
27                 const char *arg = av[1];
28                 if (!strcmp(arg, "-r"))
29                         ; /* as usual */
30                 else if (!strcmp(arg, "-p"))
31                         diff_output_format = DIFF_FORMAT_PATCH;
32                 else if (!strncmp(arg, "-B", 2)) {
33                         if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
34                                 usage(diff_stages_usage);
35                 }
36                 else if (!strncmp(arg, "-M", 2)) {
37                         detect_rename = DIFF_DETECT_RENAME;
38                         if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
39                                 usage(diff_stages_usage);
40                 }
41                 else if (!strncmp(arg, "-C", 2)) {
42                         detect_rename = DIFF_DETECT_COPY;
43                         if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
44                                 usage(diff_stages_usage);
45                 }
46                 else if (!strcmp(arg, "-z"))
47                         diff_output_format = DIFF_FORMAT_MACHINE;
48                 else if (!strcmp(arg, "-R"))
49                         diff_setup_opt |= DIFF_SETUP_REVERSE;
50                 else if (!strncmp(arg, "-S", 2))
51                         pickaxe = arg + 2;
52                 else if (!strncmp(arg, "-O", 2))
53                         orderfile = arg + 2;
54                 else if (!strncmp(arg, "--diff-filter=", 14))
55                         diff_filter = arg + 14;
56                 else if (!strcmp(arg, "--pickaxe-all"))
57                         pickaxe_opts = DIFF_PICKAXE_ALL;
58                 else
59                         usage(diff_stages_usage);
60                 ac--; av++;
61         }
62
63         if (ac < 3 ||
64             sscanf(av[1], "%d", &stage1) != 1 ||
65             ! (0 <= stage1 && stage1 <= 3) ||
66             sscanf(av[2], "%d", &stage2) != 1 ||
67             ! (0 <= stage2 && stage2 <= 3))
68                 usage(diff_stages_usage);
69
70         av += 3; /* The rest from av[0] are for paths restriction. */
71         diff_setup(diff_setup_opt);
72
73         i = 0;
74         while (i < active_nr) {
75                 struct cache_entry *ce, *stages[4] = { NULL, };
76                 struct cache_entry *one, *two;
77                 const char *name;
78                 int len;
79                 ce = active_cache[i];
80                 len = ce_namelen(ce);
81                 name = ce->name;
82                 for (;;) {
83                         int stage = ce_stage(ce);
84                         stages[stage] = ce;
85                         if (active_nr <= ++i)
86                                 break;
87                         ce = active_cache[i];
88                         if (ce_namelen(ce) != len ||
89                             memcmp(name, ce->name, len))
90                                 break;
91                 }
92                 one = stages[stage1];
93                 two = stages[stage2];
94                 if (!one && !two)
95                         continue;
96                 if (!one)
97                         diff_addremove('+', ntohl(two->ce_mode),
98                                        two->sha1, name, NULL);
99                 else if (!two)
100                         diff_addremove('-', ntohl(one->ce_mode),
101                                        one->sha1, name, NULL);
102                 else if (memcmp(one->sha1, two->sha1, 20) ||
103                          (one->ce_mode != two->ce_mode))
104                          diff_change(ntohl(one->ce_mode), ntohl(two->ce_mode),
105                                      one->sha1, two->sha1, name, NULL);
106         }
107
108         diffcore_std(av,
109                      detect_rename, diff_score_opt,
110                      pickaxe, pickaxe_opts,
111                      diff_break_opt,
112                      orderfile,
113                      diff_filter);
114         diff_flush(diff_output_format);
115         return 0;
116 }