[PATCH] Update git-tag-script to create the .git/refs/tags if it does not already...
[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_RAW;
9 static int diff_line_termination = '\n';
10 static int detect_rename = 0;
11 static int find_copies_harder = 0;
12 static int diff_setup_opt = 0;
13 static int diff_score_opt = 0;
14 static const char *pickaxe = NULL;
15 static int pickaxe_opts = 0;
16 static int diff_break_opt = -1;
17 static const char *orderfile = NULL;
18 static const char *diff_filter = NULL;
19
20 static char *diff_stages_usage =
21 "git-diff-stages [<common diff options>] <stage1> <stage2> [<path>...]"
22 COMMON_DIFF_OPTIONS_HELP;
23
24 static void diff_stages(int stage1, int stage2)
25 {
26         int i = 0;
27         while (i < active_nr) {
28                 struct cache_entry *ce, *stages[4] = { NULL, };
29                 struct cache_entry *one, *two;
30                 const char *name;
31                 int len;
32                 ce = active_cache[i];
33                 len = ce_namelen(ce);
34                 name = ce->name;
35                 for (;;) {
36                         int stage = ce_stage(ce);
37                         stages[stage] = ce;
38                         if (active_nr <= ++i)
39                                 break;
40                         ce = active_cache[i];
41                         if (ce_namelen(ce) != len ||
42                             memcmp(name, ce->name, len))
43                                 break;
44                 }
45                 one = stages[stage1];
46                 two = stages[stage2];
47                 if (!one && !two)
48                         continue;
49                 if (!one)
50                         diff_addremove('+', ntohl(two->ce_mode),
51                                        two->sha1, name, NULL);
52                 else if (!two)
53                         diff_addremove('-', ntohl(one->ce_mode),
54                                        one->sha1, name, NULL);
55                 else if (memcmp(one->sha1, two->sha1, 20) ||
56                          (one->ce_mode != two->ce_mode) ||
57                          find_copies_harder)
58                         diff_change(ntohl(one->ce_mode), ntohl(two->ce_mode),
59                                     one->sha1, two->sha1, name, NULL);
60         }
61 }
62
63 int main(int ac, const char **av)
64 {
65         int stage1, stage2;
66
67         read_cache();
68         while (1 < ac && av[1][0] == '-') {
69                 const char *arg = av[1];
70                 if (!strcmp(arg, "-r"))
71                         ; /* as usual */
72                 else if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
73                         diff_output_format = DIFF_FORMAT_PATCH;
74                 else if (!strncmp(arg, "-B", 2)) {
75                         if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
76                                 usage(diff_stages_usage);
77                 }
78                 else if (!strncmp(arg, "-M", 2)) {
79                         detect_rename = DIFF_DETECT_RENAME;
80                         if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
81                                 usage(diff_stages_usage);
82                 }
83                 else if (!strncmp(arg, "-C", 2)) {
84                         detect_rename = DIFF_DETECT_COPY;
85                         if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
86                                 usage(diff_stages_usage);
87                 }
88                 else if (!strcmp(arg, "--find-copies-harder"))
89                         find_copies_harder = 1;
90                 else if (!strcmp(arg, "-z"))
91                         diff_line_termination = 0;
92                 else if (!strcmp(arg, "--name-only"))
93                         diff_output_format = DIFF_FORMAT_NAME;
94                 else if (!strcmp(arg, "-R"))
95                         diff_setup_opt |= DIFF_SETUP_REVERSE;
96                 else if (!strncmp(arg, "-S", 2))
97                         pickaxe = arg + 2;
98                 else if (!strncmp(arg, "-O", 2))
99                         orderfile = arg + 2;
100                 else if (!strncmp(arg, "--diff-filter=", 14))
101                         diff_filter = arg + 14;
102                 else if (!strcmp(arg, "--pickaxe-all"))
103                         pickaxe_opts = DIFF_PICKAXE_ALL;
104                 else
105                         usage(diff_stages_usage);
106                 ac--; av++;
107         }
108
109         if (ac < 3 ||
110             sscanf(av[1], "%d", &stage1) != 1 ||
111             ! (0 <= stage1 && stage1 <= 3) ||
112             sscanf(av[2], "%d", &stage2) != 1 ||
113             ! (0 <= stage2 && stage2 <= 3) ||
114             (find_copies_harder && detect_rename != DIFF_DETECT_COPY))
115                 usage(diff_stages_usage);
116
117         av += 3; /* The rest from av[0] are for paths restriction. */
118         diff_setup(diff_setup_opt);
119
120         diff_stages(stage1, stage2);
121
122         diffcore_std(av,
123                      detect_rename, diff_score_opt,
124                      pickaxe, pickaxe_opts,
125                      diff_break_opt,
126                      orderfile,
127                      diff_filter);
128         diff_flush(diff_output_format, diff_line_termination);
129         return 0;
130 }