run_diff_{files,index}(): update calling convention.
[git] / diff-lib.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include "cache.h"
5 #include "quote.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "diffcore.h"
9 #include "revision.h"
10 #include "cache-tree.h"
11
12 /*
13  * diff-files
14  */
15
16 int run_diff_files(struct rev_info *revs, int silent_on_removed)
17 {
18         int entries, i;
19         int diff_unmerged_stage = revs->max_count;
20
21         if (diff_unmerged_stage < 0)
22                 diff_unmerged_stage = 2;
23         entries = active_nr;
24         for (i = 0; i < entries; i++) {
25                 struct stat st;
26                 unsigned int oldmode, newmode;
27                 struct cache_entry *ce = active_cache[i];
28                 int changed;
29
30                 if (!ce_path_match(ce, revs->prune_data))
31                         continue;
32
33                 if (ce_stage(ce)) {
34                         struct combine_diff_path *dpath;
35                         int num_compare_stages = 0;
36                         size_t path_len;
37
38                         path_len = ce_namelen(ce);
39
40                         dpath = xmalloc (combine_diff_path_size (5, path_len));
41                         dpath->path = (char *) &(dpath->parent[5]);
42
43                         dpath->next = NULL;
44                         dpath->len = path_len;
45                         memcpy(dpath->path, ce->name, path_len);
46                         dpath->path[path_len] = '\0';
47                         dpath->mode = 0;
48                         hashclr(dpath->sha1);
49                         memset(&(dpath->parent[0]), 0,
50                                         sizeof(struct combine_diff_parent)*5);
51
52                         while (i < entries) {
53                                 struct cache_entry *nce = active_cache[i];
54                                 int stage;
55
56                                 if (strcmp(ce->name, nce->name))
57                                         break;
58
59                                 /* Stage #2 (ours) is the first parent,
60                                  * stage #3 (theirs) is the second.
61                                  */
62                                 stage = ce_stage(nce);
63                                 if (2 <= stage) {
64                                         int mode = ntohl(nce->ce_mode);
65                                         num_compare_stages++;
66                                         hashcpy(dpath->parent[stage-2].sha1, nce->sha1);
67                                         dpath->parent[stage-2].mode =
68                                                 canon_mode(mode);
69                                         dpath->parent[stage-2].status =
70                                                 DIFF_STATUS_MODIFIED;
71                                 }
72
73                                 /* diff against the proper unmerged stage */
74                                 if (stage == diff_unmerged_stage)
75                                         ce = nce;
76                                 i++;
77                         }
78                         /*
79                          * Compensate for loop update
80                          */
81                         i--;
82
83                         if (revs->combine_merges && num_compare_stages == 2) {
84                                 show_combined_diff(dpath, 2,
85                                                    revs->dense_combined_merges,
86                                                    revs);
87                                 free(dpath);
88                                 continue;
89                         }
90                         free(dpath);
91                         dpath = NULL;
92
93                         /*
94                          * Show the diff for the 'ce' if we found the one
95                          * from the desired stage.
96                          */
97                         diff_unmerge(&revs->diffopt, ce->name, 0, null_sha1);
98                         if (ce_stage(ce) != diff_unmerged_stage)
99                                 continue;
100                 }
101
102                 if (lstat(ce->name, &st) < 0) {
103                         if (errno != ENOENT && errno != ENOTDIR) {
104                                 perror(ce->name);
105                                 continue;
106                         }
107                         if (silent_on_removed)
108                                 continue;
109                         diff_addremove(&revs->diffopt, '-', ntohl(ce->ce_mode),
110                                        ce->sha1, ce->name, NULL);
111                         continue;
112                 }
113                 changed = ce_match_stat(ce, &st, 0);
114                 if (!changed && !revs->diffopt.find_copies_harder)
115                         continue;
116                 oldmode = ntohl(ce->ce_mode);
117
118                 newmode = canon_mode(st.st_mode);
119                 if (!trust_executable_bit &&
120                     S_ISREG(newmode) && S_ISREG(oldmode) &&
121                     ((newmode ^ oldmode) == 0111))
122                         newmode = oldmode;
123                 diff_change(&revs->diffopt, oldmode, newmode,
124                             ce->sha1, (changed ? null_sha1 : ce->sha1),
125                             ce->name, NULL);
126
127         }
128         diffcore_std(&revs->diffopt);
129         diff_flush(&revs->diffopt);
130         return 0;
131 }
132
133 /*
134  * diff-index
135  */
136
137 /* A file entry went away or appeared */
138 static void diff_index_show_file(struct rev_info *revs,
139                                  const char *prefix,
140                                  struct cache_entry *ce,
141                                  unsigned char *sha1, unsigned int mode)
142 {
143         diff_addremove(&revs->diffopt, prefix[0], ntohl(mode),
144                        sha1, ce->name, NULL);
145 }
146
147 static int get_stat_data(struct cache_entry *ce,
148                          unsigned char **sha1p,
149                          unsigned int *modep,
150                          int cached, int match_missing)
151 {
152         unsigned char *sha1 = ce->sha1;
153         unsigned int mode = ce->ce_mode;
154
155         if (!cached) {
156                 static unsigned char no_sha1[20];
157                 int changed;
158                 struct stat st;
159                 if (lstat(ce->name, &st) < 0) {
160                         if (errno == ENOENT && match_missing) {
161                                 *sha1p = sha1;
162                                 *modep = mode;
163                                 return 0;
164                         }
165                         return -1;
166                 }
167                 changed = ce_match_stat(ce, &st, 0);
168                 if (changed) {
169                         mode = create_ce_mode(st.st_mode);
170                         if (!trust_executable_bit && S_ISREG(st.st_mode))
171                                 mode = ce->ce_mode;
172                         sha1 = no_sha1;
173                 }
174         }
175
176         *sha1p = sha1;
177         *modep = mode;
178         return 0;
179 }
180
181 static void show_new_file(struct rev_info *revs,
182                           struct cache_entry *new,
183                           int cached, int match_missing)
184 {
185         unsigned char *sha1;
186         unsigned int mode;
187
188         /* New file in the index: it might actually be different in
189          * the working copy.
190          */
191         if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0)
192                 return;
193
194         diff_index_show_file(revs, "+", new, sha1, mode);
195 }
196
197 static int show_modified(struct rev_info *revs,
198                          struct cache_entry *old,
199                          struct cache_entry *new,
200                          int report_missing,
201                          int cached, int match_missing)
202 {
203         unsigned int mode, oldmode;
204         unsigned char *sha1;
205
206         if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0) {
207                 if (report_missing)
208                         diff_index_show_file(revs, "-", old,
209                                              old->sha1, old->ce_mode);
210                 return -1;
211         }
212
213         if (revs->combine_merges && !cached &&
214             (hashcmp(sha1, old->sha1) || hashcmp(old->sha1, new->sha1))) {
215                 struct combine_diff_path *p;
216                 int pathlen = ce_namelen(new);
217
218                 p = xmalloc(combine_diff_path_size(2, pathlen));
219                 p->path = (char *) &p->parent[2];
220                 p->next = NULL;
221                 p->len = pathlen;
222                 memcpy(p->path, new->name, pathlen);
223                 p->path[pathlen] = 0;
224                 p->mode = ntohl(mode);
225                 hashclr(p->sha1);
226                 memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
227                 p->parent[0].status = DIFF_STATUS_MODIFIED;
228                 p->parent[0].mode = ntohl(new->ce_mode);
229                 hashcpy(p->parent[0].sha1, new->sha1);
230                 p->parent[1].status = DIFF_STATUS_MODIFIED;
231                 p->parent[1].mode = ntohl(old->ce_mode);
232                 hashcpy(p->parent[1].sha1, old->sha1);
233                 show_combined_diff(p, 2, revs->dense_combined_merges, revs);
234                 free(p);
235                 return 0;
236         }
237
238         oldmode = old->ce_mode;
239         if (mode == oldmode && !hashcmp(sha1, old->sha1) &&
240             !revs->diffopt.find_copies_harder)
241                 return 0;
242
243         mode = ntohl(mode);
244         oldmode = ntohl(oldmode);
245
246         diff_change(&revs->diffopt, oldmode, mode,
247                     old->sha1, sha1, old->name, NULL);
248         return 0;
249 }
250
251 static int diff_cache(struct rev_info *revs,
252                       struct cache_entry **ac, int entries,
253                       const char **pathspec,
254                       int cached, int match_missing)
255 {
256         while (entries) {
257                 struct cache_entry *ce = *ac;
258                 int same = (entries > 1) && ce_same_name(ce, ac[1]);
259
260                 if (!ce_path_match(ce, pathspec))
261                         goto skip_entry;
262
263                 switch (ce_stage(ce)) {
264                 case 0:
265                         /* No stage 1 entry? That means it's a new file */
266                         if (!same) {
267                                 show_new_file(revs, ce, cached, match_missing);
268                                 break;
269                         }
270                         /* Show difference between old and new */
271                         show_modified(revs, ac[1], ce, 1,
272                                       cached, match_missing);
273                         break;
274                 case 1:
275                         /* No stage 3 (merge) entry?
276                          * That means it's been deleted.
277                          */
278                         if (!same) {
279                                 diff_index_show_file(revs, "-", ce,
280                                                      ce->sha1, ce->ce_mode);
281                                 break;
282                         }
283                         /* We come here with ce pointing at stage 1
284                          * (original tree) and ac[1] pointing at stage
285                          * 3 (unmerged).  show-modified with
286                          * report-missing set to false does not say the
287                          * file is deleted but reports true if work
288                          * tree does not have it, in which case we
289                          * fall through to report the unmerged state.
290                          * Otherwise, we show the differences between
291                          * the original tree and the work tree.
292                          */
293                         if (!cached &&
294                             !show_modified(revs, ce, ac[1], 0,
295                                            cached, match_missing))
296                                 break;
297                         diff_unmerge(&revs->diffopt, ce->name,
298                                      ntohl(ce->ce_mode), ce->sha1);
299                         break;
300                 case 3:
301                         diff_unmerge(&revs->diffopt, ce->name,
302                                      0, null_sha1);
303                         break;
304
305                 default:
306                         die("impossible cache entry stage");
307                 }
308
309 skip_entry:
310                 /*
311                  * Ignore all the different stages for this file,
312                  * we've handled the relevant cases now.
313                  */
314                 do {
315                         ac++;
316                         entries--;
317                 } while (entries && ce_same_name(ce, ac[0]));
318         }
319         return 0;
320 }
321
322 /*
323  * This turns all merge entries into "stage 3". That guarantees that
324  * when we read in the new tree (into "stage 1"), we won't lose sight
325  * of the fact that we had unmerged entries.
326  */
327 static void mark_merge_entries(void)
328 {
329         int i;
330         for (i = 0; i < active_nr; i++) {
331                 struct cache_entry *ce = active_cache[i];
332                 if (!ce_stage(ce))
333                         continue;
334                 ce->ce_flags |= htons(CE_STAGEMASK);
335         }
336 }
337
338 int run_diff_index(struct rev_info *revs, int cached)
339 {
340         int ret;
341         struct object *ent;
342         struct tree *tree;
343         const char *tree_name;
344         int match_missing = 0;
345
346         /* 
347          * Backward compatibility wart - "diff-index -m" does
348          * not mean "do not ignore merges", but totally different.
349          */
350         if (!revs->ignore_merges)
351                 match_missing = 1;
352
353         mark_merge_entries();
354
355         ent = revs->pending.objects[0].item;
356         tree_name = revs->pending.objects[0].name;
357         tree = parse_tree_indirect(ent->sha1);
358         if (!tree)
359                 return error("bad tree object %s", tree_name);
360         if (read_tree(tree, 1, revs->prune_data))
361                 return error("unable to read tree object %s", tree_name);
362         ret = diff_cache(revs, active_cache, active_nr, revs->prune_data,
363                          cached, match_missing);
364         diffcore_std(&revs->diffopt);
365         diff_flush(&revs->diffopt);
366         return ret;
367 }
368
369 int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
370 {
371         struct tree *tree;
372         struct rev_info revs;
373         int i;
374         struct cache_entry **dst;
375         struct cache_entry *last = NULL;
376
377         /*
378          * This is used by git-blame to run diff-cache internally;
379          * it potentially needs to repeatedly run this, so we will
380          * start by removing the higher order entries the last round
381          * left behind.
382          */
383         dst = active_cache;
384         for (i = 0; i < active_nr; i++) {
385                 struct cache_entry *ce = active_cache[i];
386                 if (ce_stage(ce)) {
387                         if (last && !strcmp(ce->name, last->name))
388                                 continue;
389                         cache_tree_invalidate_path(active_cache_tree,
390                                                    ce->name);
391                         last = ce;
392                         ce->ce_mode = 0;
393                         ce->ce_flags &= ~htons(CE_STAGEMASK);
394                 }
395                 *dst++ = ce;
396         }
397         active_nr = dst - active_cache;
398
399         init_revisions(&revs, NULL);
400         revs.prune_data = opt->paths;
401         tree = parse_tree_indirect(tree_sha1);
402         if (!tree)
403                 die("bad tree object %s", sha1_to_hex(tree_sha1));
404         if (read_tree(tree, 1, opt->paths))
405                 return error("unable to read tree %s", sha1_to_hex(tree_sha1));
406         return diff_cache(&revs, active_cache, active_nr, revs.prune_data,
407                           1, 0);
408 }