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#include "unpack-trees.h"
12#include "refs.h"
13
14/*
15 * diff-files
16 */
17
18/*
19 * Has the work tree entity been removed?
20 *
21 * Return 1 if it was removed from the work tree, 0 if an entity to be
22 * compared with the cache entry ce still exists (the latter includes
23 * the case where a directory that is not a submodule repository
24 * exists for ce that is a submodule -- it is a submodule that is not
25 * checked out). Return negative for an error.
26 */
27static int check_removed(const struct cache_entry *ce, struct stat *st)
28{
29 if (lstat(ce->name, st) < 0) {
30 if (errno != ENOENT && errno != ENOTDIR)
31 return -1;
32 return 1;
33 }
34 if (has_symlink_leading_path(ce->name, ce_namelen(ce)))
35 return 1;
36 if (S_ISDIR(st->st_mode)) {
37 unsigned char sub[20];
38
39 /*
40 * If ce is already a gitlink, we can have a plain
41 * directory (i.e. the submodule is not checked out),
42 * or a checked out submodule. Either case this is not
43 * a case where something was removed from the work tree,
44 * so we will return 0.
45 *
46 * Otherwise, if the directory is not a submodule
47 * repository, that means ce which was a blob turned into
48 * a directory --- the blob was removed!
49 */
50 if (!S_ISGITLINK(ce->ce_mode) &&
51 resolve_gitlink_ref(ce->name, "HEAD", sub))
52 return 1;
53 }
54 return 0;
55}
56
57int run_diff_files(struct rev_info *revs, unsigned int option)
58{
59 int entries, i;
60 int diff_unmerged_stage = revs->max_count;
61 int silent_on_removed = option & DIFF_SILENT_ON_REMOVED;
62 unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
63 ? CE_MATCH_RACY_IS_DIRTY : 0);
64
65 diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");
66
67 if (diff_unmerged_stage < 0)
68 diff_unmerged_stage = 2;
69 entries = active_nr;
70 for (i = 0; i < entries; i++) {
71 struct stat st;
72 unsigned int oldmode, newmode;
73 struct cache_entry *ce = active_cache[i];
74 int changed;
75
76 if (DIFF_OPT_TST(&revs->diffopt, QUIET) &&
77 DIFF_OPT_TST(&revs->diffopt, HAS_CHANGES))
78 break;
79
80 if (!ce_path_match(ce, revs->prune_data))
81 continue;
82
83 if (ce_stage(ce)) {
84 struct combine_diff_path *dpath;
85 int num_compare_stages = 0;
86 size_t path_len;
87
88 path_len = ce_namelen(ce);
89
90 dpath = xmalloc(combine_diff_path_size(5, path_len));
91 dpath->path = (char *) &(dpath->parent[5]);
92
93 dpath->next = NULL;
94 dpath->len = path_len;
95 memcpy(dpath->path, ce->name, path_len);
96 dpath->path[path_len] = '\0';
97 hashclr(dpath->sha1);
98 memset(&(dpath->parent[0]), 0,
99 sizeof(struct combine_diff_parent)*5);
100
101 changed = check_removed(ce, &st);
102 if (!changed)
103 dpath->mode = ce_mode_from_stat(ce, st.st_mode);
104 else {
105 if (changed < 0) {
106 perror(ce->name);
107 continue;
108 }
109 if (silent_on_removed)
110 continue;
111 }
112
113 while (i < entries) {
114 struct cache_entry *nce = active_cache[i];
115 int stage;
116
117 if (strcmp(ce->name, nce->name))
118 break;
119
120 /* Stage #2 (ours) is the first parent,
121 * stage #3 (theirs) is the second.
122 */
123 stage = ce_stage(nce);
124 if (2 <= stage) {
125 int mode = nce->ce_mode;
126 num_compare_stages++;
127 hashcpy(dpath->parent[stage-2].sha1, nce->sha1);
128 dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
129 dpath->parent[stage-2].status =
130 DIFF_STATUS_MODIFIED;
131 }
132
133 /* diff against the proper unmerged stage */
134 if (stage == diff_unmerged_stage)
135 ce = nce;
136 i++;
137 }
138 /*
139 * Compensate for loop update
140 */
141 i--;
142
143 if (revs->combine_merges && num_compare_stages == 2) {
144 show_combined_diff(dpath, 2,
145 revs->dense_combined_merges,
146 revs);
147 free(dpath);
148 continue;
149 }
150 free(dpath);
151 dpath = NULL;
152
153 /*
154 * Show the diff for the 'ce' if we found the one
155 * from the desired stage.
156 */
157 diff_unmerge(&revs->diffopt, ce->name, 0, null_sha1);
158 if (ce_stage(ce) != diff_unmerged_stage)
159 continue;
160 }
161
162 if (ce_uptodate(ce))
163 continue;
164
165 changed = check_removed(ce, &st);
166 if (changed) {
167 if (changed < 0) {
168 perror(ce->name);
169 continue;
170 }
171 if (silent_on_removed)
172 continue;
173 diff_addremove(&revs->diffopt, '-', ce->ce_mode,
174 ce->sha1, ce->name);
175 continue;
176 }
177 changed = ce_match_stat(ce, &st, ce_option);
178 if (!changed) {
179 ce_mark_uptodate(ce);
180 if (!DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
181 continue;
182 }
183 oldmode = ce->ce_mode;
184 newmode = ce_mode_from_stat(ce, st.st_mode);
185 diff_change(&revs->diffopt, oldmode, newmode,
186 ce->sha1, (changed ? null_sha1 : ce->sha1),
187 ce->name);
188
189 }
190 diffcore_std(&revs->diffopt);
191 diff_flush(&revs->diffopt);
192 return 0;
193}
194
195/*
196 * diff-index
197 */
198
199/* A file entry went away or appeared */
200static void diff_index_show_file(struct rev_info *revs,
201 const char *prefix,
202 struct cache_entry *ce,
203 const unsigned char *sha1, unsigned int mode)
204{
205 diff_addremove(&revs->diffopt, prefix[0], mode,
206 sha1, ce->name);
207}
208
209static int get_stat_data(struct cache_entry *ce,
210 const unsigned char **sha1p,
211 unsigned int *modep,
212 int cached, int match_missing)
213{
214 const unsigned char *sha1 = ce->sha1;
215 unsigned int mode = ce->ce_mode;
216
217 if (!cached && !ce_uptodate(ce)) {
218 int changed;
219 struct stat st;
220 changed = check_removed(ce, &st);
221 if (changed < 0)
222 return -1;
223 else if (changed) {
224 if (match_missing) {
225 *sha1p = sha1;
226 *modep = mode;
227 return 0;
228 }
229 return -1;
230 }
231 changed = ce_match_stat(ce, &st, 0);
232 if (changed) {
233 mode = ce_mode_from_stat(ce, st.st_mode);
234 sha1 = null_sha1;
235 }
236 }
237
238 *sha1p = sha1;
239 *modep = mode;
240 return 0;
241}
242
243static void show_new_file(struct rev_info *revs,
244 struct cache_entry *new,
245 int cached, int match_missing)
246{
247 const unsigned char *sha1;
248 unsigned int mode;
249
250 /*
251 * New file in the index: it might actually be different in
252 * the working copy.
253 */
254 if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0)
255 return;
256
257 diff_index_show_file(revs, "+", new, sha1, mode);
258}
259
260static int show_modified(struct rev_info *revs,
261 struct cache_entry *old,
262 struct cache_entry *new,
263 int report_missing,
264 int cached, int match_missing)
265{
266 unsigned int mode, oldmode;
267 const unsigned char *sha1;
268
269 if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0) {
270 if (report_missing)
271 diff_index_show_file(revs, "-", old,
272 old->sha1, old->ce_mode);
273 return -1;
274 }
275
276 if (revs->combine_merges && !cached &&
277 (hashcmp(sha1, old->sha1) || hashcmp(old->sha1, new->sha1))) {
278 struct combine_diff_path *p;
279 int pathlen = ce_namelen(new);
280
281 p = xmalloc(combine_diff_path_size(2, pathlen));
282 p->path = (char *) &p->parent[2];
283 p->next = NULL;
284 p->len = pathlen;
285 memcpy(p->path, new->name, pathlen);
286 p->path[pathlen] = 0;
287 p->mode = mode;
288 hashclr(p->sha1);
289 memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
290 p->parent[0].status = DIFF_STATUS_MODIFIED;
291 p->parent[0].mode = new->ce_mode;
292 hashcpy(p->parent[0].sha1, new->sha1);
293 p->parent[1].status = DIFF_STATUS_MODIFIED;
294 p->parent[1].mode = old->ce_mode;
295 hashcpy(p->parent[1].sha1, old->sha1);
296 show_combined_diff(p, 2, revs->dense_combined_merges, revs);
297 free(p);
298 return 0;
299 }
300
301 oldmode = old->ce_mode;
302 if (mode == oldmode && !hashcmp(sha1, old->sha1) &&
303 !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
304 return 0;
305
306 diff_change(&revs->diffopt, oldmode, mode,
307 old->sha1, sha1, old->name);
308 return 0;
309}
310
311/*
312 * This gets a mix of an existing index and a tree, one pathname entry
313 * at a time. The index entry may be a single stage-0 one, but it could
314 * also be multiple unmerged entries (in which case idx_pos/idx_nr will
315 * give you the position and number of entries in the index).
316 */
317static void do_oneway_diff(struct unpack_trees_options *o,
318 struct cache_entry *idx,
319 struct cache_entry *tree)
320{
321 struct rev_info *revs = o->unpack_data;
322 int match_missing, cached;
323
324 /*
325 * Backward compatibility wart - "diff-index -m" does
326 * not mean "do not ignore merges", but "match_missing".
327 *
328 * But with the revision flag parsing, that's found in
329 * "!revs->ignore_merges".
330 */
331 cached = o->index_only;
332 match_missing = !revs->ignore_merges;
333
334 if (cached && idx && ce_stage(idx)) {
335 diff_unmerge(&revs->diffopt, idx->name, idx->ce_mode,
336 idx->sha1);
337 return;
338 }
339
340 /*
341 * Something added to the tree?
342 */
343 if (!tree) {
344 show_new_file(revs, idx, cached, match_missing);
345 return;
346 }
347
348 /*
349 * Something removed from the tree?
350 */
351 if (!idx) {
352 diff_index_show_file(revs, "-", tree, tree->sha1, tree->ce_mode);
353 return;
354 }
355
356 /* Show difference between old and new */
357 show_modified(revs, tree, idx, 1, cached, match_missing);
358}
359
360static inline void skip_same_name(struct cache_entry *ce, struct unpack_trees_options *o)
361{
362 int len = ce_namelen(ce);
363 const struct index_state *index = o->src_index;
364
365 while (o->pos < index->cache_nr) {
366 struct cache_entry *next = index->cache[o->pos];
367 if (len != ce_namelen(next))
368 break;
369 if (memcmp(ce->name, next->name, len))
370 break;
371 o->pos++;
372 }
373}
374
375/*
376 * The unpack_trees() interface is designed for merging, so
377 * the different source entries are designed primarily for
378 * the source trees, with the old index being really mainly
379 * used for being replaced by the result.
380 *
381 * For diffing, the index is more important, and we only have a
382 * single tree.
383 *
384 * We're supposed to return how many index entries we want to skip.
385 *
386 * This wrapper makes it all more readable, and takes care of all
387 * the fairly complex unpack_trees() semantic requirements, including
388 * the skipping, the path matching, the type conflict cases etc.
389 */
390static int oneway_diff(struct cache_entry **src, struct unpack_trees_options *o)
391{
392 struct cache_entry *idx = src[0];
393 struct cache_entry *tree = src[1];
394 struct rev_info *revs = o->unpack_data;
395
396 if (idx && ce_stage(idx))
397 skip_same_name(idx, o);
398
399 /*
400 * Unpack-trees generates a DF/conflict entry if
401 * there was a directory in the index and a tree
402 * in the tree. From a diff standpoint, that's a
403 * delete of the tree and a create of the file.
404 */
405 if (tree == o->df_conflict_entry)
406 tree = NULL;
407
408 if (ce_path_match(idx ? idx : tree, revs->prune_data))
409 do_oneway_diff(o, idx, tree);
410
411 return 0;
412}
413
414int run_diff_index(struct rev_info *revs, int cached)
415{
416 struct object *ent;
417 struct tree *tree;
418 const char *tree_name;
419 struct unpack_trees_options opts;
420 struct tree_desc t;
421
422 ent = revs->pending.objects[0].item;
423 tree_name = revs->pending.objects[0].name;
424 tree = parse_tree_indirect(ent->sha1);
425 if (!tree)
426 return error("bad tree object %s", tree_name);
427
428 memset(&opts, 0, sizeof(opts));
429 opts.head_idx = 1;
430 opts.index_only = cached;
431 opts.diff_index_cached = (cached &&
432 !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER));
433 opts.merge = 1;
434 opts.fn = oneway_diff;
435 opts.unpack_data = revs;
436 opts.src_index = &the_index;
437 opts.dst_index = NULL;
438
439 init_tree_desc(&t, tree->buffer, tree->size);
440 if (unpack_trees(1, &t, &opts))
441 exit(128);
442
443 diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
444 diffcore_std(&revs->diffopt);
445 diff_flush(&revs->diffopt);
446 return 0;
447}
448
449int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
450{
451 struct tree *tree;
452 struct rev_info revs;
453 int i;
454 struct cache_entry **dst;
455 struct cache_entry *last = NULL;
456 struct unpack_trees_options opts;
457 struct tree_desc t;
458
459 /*
460 * This is used by git-blame to run diff-cache internally;
461 * it potentially needs to repeatedly run this, so we will
462 * start by removing the higher order entries the last round
463 * left behind.
464 */
465 dst = active_cache;
466 for (i = 0; i < active_nr; i++) {
467 struct cache_entry *ce = active_cache[i];
468 if (ce_stage(ce)) {
469 if (last && !strcmp(ce->name, last->name))
470 continue;
471 cache_tree_invalidate_path(active_cache_tree,
472 ce->name);
473 last = ce;
474 ce->ce_flags |= CE_REMOVE;
475 }
476 *dst++ = ce;
477 }
478 active_nr = dst - active_cache;
479
480 init_revisions(&revs, NULL);
481 revs.prune_data = opt->paths;
482 tree = parse_tree_indirect(tree_sha1);
483 if (!tree)
484 die("bad tree object %s", sha1_to_hex(tree_sha1));
485
486 memset(&opts, 0, sizeof(opts));
487 opts.head_idx = 1;
488 opts.index_only = 1;
489 opts.diff_index_cached = !DIFF_OPT_TST(opt, FIND_COPIES_HARDER);
490 opts.merge = 1;
491 opts.fn = oneway_diff;
492 opts.unpack_data = &revs;
493 opts.src_index = &the_index;
494 opts.dst_index = &the_index;
495
496 init_tree_desc(&t, tree->buffer, tree->size);
497 if (unpack_trees(1, &t, &opts))
498 exit(128);
499 return 0;
500}
501
502int index_differs_from(const char *def, int diff_flags)
503{
504 struct rev_info rev;
505
506 init_revisions(&rev, NULL);
507 setup_revisions(0, NULL, &rev, def);
508 DIFF_OPT_SET(&rev.diffopt, QUIET);
509 DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
510 rev.diffopt.flags |= diff_flags;
511 run_diff_index(&rev, 1);
512 if (rev.pending.alloc)
513 free(rev.pending.objects);
514 return (DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0);
515}