[PATCH] diff overhaul
[git.git] / diff-cache.c
1 #include "cache.h"
2 #include "diff.h"
3
4 static int cached_only = 0;
5 static int generate_patch = 0;
6 static int match_nonexisting = 0;
7 static int line_termination = '\n';
8 static int detect_rename = 0;
9 static int reverse_diff = 0;
10 static int diff_score_opt = 0;
11
12 /* A file entry went away or appeared */
13 static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
14 {
15         diff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);
16 }
17
18 static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned int *modep)
19 {
20         unsigned char *sha1 = ce->sha1;
21         unsigned int mode = ce->ce_mode;
22
23         if (!cached_only) {
24                 static unsigned char no_sha1[20];
25                 int changed;
26                 struct stat st;
27                 if (lstat(ce->name, &st) < 0) {
28                         if (errno == ENOENT && match_nonexisting) {
29                                 *sha1p = sha1;
30                                 *modep = mode;
31                                 return 0;
32                         }
33                         return -1;
34                 }
35                 changed = ce_match_stat(ce, &st);
36                 if (changed) {
37                         mode = create_ce_mode(st.st_mode);
38                         sha1 = no_sha1;
39                 }
40         }
41
42         *sha1p = sha1;
43         *modep = mode;
44         return 0;
45 }
46
47 static void show_new_file(struct cache_entry *new)
48 {
49         unsigned char *sha1;
50         unsigned int mode;
51
52         /* New file in the index: it might actually be different in the working copy */
53         if (get_stat_data(new, &sha1, &mode) < 0)
54                 return;
55
56         show_file("+", new, sha1, mode);
57 }
58
59 static int show_modified(struct cache_entry *old,
60                          struct cache_entry *new,
61                          int report_missing)
62 {
63         unsigned int mode, oldmode;
64         unsigned char *sha1;
65
66         if (get_stat_data(new, &sha1, &mode) < 0) {
67                 if (report_missing)
68                         show_file("-", old, old->sha1, old->ce_mode);
69                 return -1;
70         }
71
72         oldmode = old->ce_mode;
73         if (mode == oldmode && !memcmp(sha1, old->sha1, 20))
74                 return 0;
75
76         mode = ntohl(mode);
77         oldmode = ntohl(oldmode);
78
79         diff_change(oldmode, mode,
80                     old->sha1, sha1, old->name, NULL);
81         return 0;
82 }
83
84 static int diff_cache(struct cache_entry **ac, int entries)
85 {
86         while (entries) {
87                 struct cache_entry *ce = *ac;
88                 int same = (entries > 1) && ce_same_name(ce, ac[1]);
89
90                 switch (ce_stage(ce)) {
91                 case 0:
92                         /* No stage 1 entry? That means it's a new file */
93                         if (!same) {
94                                 show_new_file(ce);
95                                 break;
96                         }
97                         /* Show difference between old and new */
98                         show_modified(ac[1], ce, 1);
99                         break;
100                 case 1:
101                         /* No stage 3 (merge) entry? That means it's been deleted */
102                         if (!same) {
103                                 show_file("-", ce, ce->sha1, ce->ce_mode);
104                                 break;
105                         }
106                         /* We come here with ce pointing at stage 1
107                          * (original tree) and ac[1] pointing at stage
108                          * 3 (unmerged).  show-modified with
109                          * report-mising set to false does not say the
110                          * file is deleted but reports true if work
111                          * tree does not have it, in which case we
112                          * fall through to report the unmerged state.
113                          * Otherwise, we show the differences between
114                          * the original tree and the work tree.
115                          */
116                         if (!cached_only && !show_modified(ce, ac[1], 0))
117                                 break;
118                         /* fallthru */
119                 case 3:
120                         diff_unmerge(ce->name);
121                         break;
122
123                 default:
124                         die("impossible cache entry stage");
125                 }
126
127                 /*
128                  * Ignore all the different stages for this file,
129                  * we've handled the relevant cases now.
130                  */
131                 do {
132                         ac++;
133                         entries--;
134                 } while (entries && ce_same_name(ce, ac[0]));
135         }
136         return 0;
137 }
138
139 /*
140  * This turns all merge entries into "stage 3". That guarantees that
141  * when we read in the new tree (into "stage 1"), we won't lose sight
142  * of the fact that we had unmerged entries.
143  */
144 static void mark_merge_entries(void)
145 {
146         int i;
147         for (i = 0; i < active_nr; i++) {
148                 struct cache_entry *ce = active_cache[i];
149                 if (!ce_stage(ce))
150                         continue;
151                 ce->ce_flags |= htons(CE_STAGEMASK);
152         }
153 }
154
155 static char *diff_cache_usage =
156 "git-diff-cache [-p] [-r] [-z] [-m] [-M] [-R] [--cached] <tree-ish>";
157
158 int main(int argc, char **argv)
159 {
160         unsigned char tree_sha1[20];
161         void *tree;
162         unsigned long size;
163         int ret;
164
165         read_cache();
166         while (argc > 2) {
167                 char *arg = argv[1];
168                 argv++;
169                 argc--;
170                 if (!strcmp(arg, "-r")) {
171                         /* We accept the -r flag just to look like git-diff-tree */
172                         continue;
173                 }
174                 if (!strcmp(arg, "-p")) {
175                         generate_patch = 1;
176                         continue;
177                 }
178                 if (!strncmp(arg, "-M", 2)) {
179                         generate_patch = detect_rename = 1;
180                         diff_score_opt = diff_scoreopt_parse(arg);
181                         continue;
182                 }
183                 if (!strcmp(arg, "-z")) {
184                         line_termination = '\0';
185                         continue;
186                 }
187                 if (!strcmp(arg, "-R")) {
188                         reverse_diff = 1;
189                         continue;
190                 }
191                 if (!strcmp(arg, "-m")) {
192                         match_nonexisting = 1;
193                         continue;
194                 }
195                 if (!strcmp(arg, "--cached")) {
196                         cached_only = 1;
197                         continue;
198                 }
199                 usage(diff_cache_usage);
200         }
201
202         if (argc != 2 || get_sha1(argv[1], tree_sha1))
203                 usage(diff_cache_usage);
204
205         diff_setup(detect_rename, diff_score_opt, reverse_diff,
206                    (generate_patch ? -1 : line_termination),
207                    0, 0);
208
209         mark_merge_entries();
210
211         tree = read_object_with_reference(tree_sha1, "tree", &size, 0);
212         if (!tree)
213                 die("bad tree object %s", argv[1]);
214         if (read_tree(tree, size, 1))
215                 die("unable to read tree object %s", argv[1]);
216
217         ret = diff_cache(active_cache, active_nr);
218         diff_flush();
219         return ret;
220 }