Merge branch 'jc/retire-cas-opt-name-constant'
[git.git] / diff-lib.c
blob92aa13789d7192544cfc8d21cefec528ea7445ff
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "git-compat-util.h"
5 #include "quote.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "diffcore.h"
9 #include "gettext.h"
10 #include "hash.h"
11 #include "hex.h"
12 #include "object-name.h"
13 #include "read-cache.h"
14 #include "revision.h"
15 #include "cache-tree.h"
16 #include "unpack-trees.h"
17 #include "refs.h"
18 #include "repository.h"
19 #include "submodule.h"
20 #include "symlinks.h"
21 #include "trace.h"
22 #include "dir.h"
23 #include "fsmonitor.h"
24 #include "commit-reach.h"
27 * diff-files
31 * Has the work tree entity been removed?
33 * Return 1 if it was removed from the work tree, 0 if an entity to be
34 * compared with the cache entry ce still exists (the latter includes
35 * the case where a directory that is not a submodule repository
36 * exists for ce that is a submodule -- it is a submodule that is not
37 * checked out). Return negative for an error.
39 static int check_removed(const struct cache_entry *ce, struct stat *st)
41 int stat_err;
43 if (!(ce->ce_flags & CE_FSMONITOR_VALID))
44 stat_err = lstat(ce->name, st);
45 else
46 stat_err = fake_lstat(ce, st);
47 if (stat_err < 0) {
48 if (!is_missing_file_error(errno))
49 return -1;
50 return 1;
53 if (has_symlink_leading_path(ce->name, ce_namelen(ce)))
54 return 1;
55 if (S_ISDIR(st->st_mode)) {
56 struct object_id sub;
59 * If ce is already a gitlink, we can have a plain
60 * directory (i.e. the submodule is not checked out),
61 * or a checked out submodule. Either case this is not
62 * a case where something was removed from the work tree,
63 * so we will return 0.
65 * Otherwise, if the directory is not a submodule
66 * repository, that means ce which was a blob turned into
67 * a directory --- the blob was removed!
69 if (!S_ISGITLINK(ce->ce_mode) &&
70 resolve_gitlink_ref(ce->name, "HEAD", &sub))
71 return 1;
73 return 0;
77 * Has a file changed or has a submodule new commits or a dirty work tree?
79 * Return 1 when changes are detected, 0 otherwise. If the DIRTY_SUBMODULES
80 * option is set, the caller does not only want to know if a submodule is
81 * modified at all but wants to know all the conditions that are met (new
82 * commits, untracked content and/or modified content).
84 static int match_stat_with_submodule(struct diff_options *diffopt,
85 const struct cache_entry *ce,
86 struct stat *st, unsigned ce_option,
87 unsigned *dirty_submodule)
89 int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option);
90 if (S_ISGITLINK(ce->ce_mode)) {
91 struct diff_flags orig_flags = diffopt->flags;
92 if (!diffopt->flags.override_submodule_config)
93 set_diffopt_flags_from_submodule_config(diffopt, ce->name);
94 if (diffopt->flags.ignore_submodules)
95 changed = 0;
96 else if (!diffopt->flags.ignore_dirty_submodules &&
97 (!changed || diffopt->flags.dirty_submodules))
98 *dirty_submodule = is_submodule_modified(ce->name,
99 diffopt->flags.ignore_untracked_in_submodules);
100 diffopt->flags = orig_flags;
102 return changed;
105 void run_diff_files(struct rev_info *revs, unsigned int option)
107 int entries, i;
108 int diff_unmerged_stage = revs->max_count;
109 unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
110 ? CE_MATCH_RACY_IS_DIRTY : 0);
111 uint64_t start = getnanotime();
112 struct index_state *istate = revs->diffopt.repo->index;
114 diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");
116 refresh_fsmonitor(istate);
118 if (diff_unmerged_stage < 0)
119 diff_unmerged_stage = 2;
120 entries = istate->cache_nr;
121 for (i = 0; i < entries; i++) {
122 unsigned int oldmode, newmode;
123 struct cache_entry *ce = istate->cache[i];
124 int changed;
125 unsigned dirty_submodule = 0;
126 const struct object_id *old_oid, *new_oid;
128 if (diff_can_quit_early(&revs->diffopt))
129 break;
131 if (!ce_path_match(istate, ce, &revs->prune_data, NULL))
132 continue;
134 if (revs->diffopt.prefix &&
135 strncmp(ce->name, revs->diffopt.prefix, revs->diffopt.prefix_length))
136 continue;
138 if (ce_stage(ce)) {
139 struct combine_diff_path *dpath;
140 struct diff_filepair *pair;
141 unsigned int wt_mode = 0;
142 int num_compare_stages = 0;
143 size_t path_len;
144 struct stat st;
146 path_len = ce_namelen(ce);
148 dpath = xmalloc(combine_diff_path_size(5, path_len));
149 dpath->path = (char *) &(dpath->parent[5]);
151 dpath->next = NULL;
152 memcpy(dpath->path, ce->name, path_len);
153 dpath->path[path_len] = '\0';
154 oidclr(&dpath->oid);
155 memset(&(dpath->parent[0]), 0,
156 sizeof(struct combine_diff_parent)*5);
158 changed = check_removed(ce, &st);
159 if (!changed)
160 wt_mode = ce_mode_from_stat(ce, st.st_mode);
161 else {
162 if (changed < 0) {
163 perror(ce->name);
164 continue;
166 wt_mode = 0;
168 dpath->mode = wt_mode;
170 while (i < entries) {
171 struct cache_entry *nce = istate->cache[i];
172 int stage;
174 if (strcmp(ce->name, nce->name))
175 break;
177 /* Stage #2 (ours) is the first parent,
178 * stage #3 (theirs) is the second.
180 stage = ce_stage(nce);
181 if (2 <= stage) {
182 int mode = nce->ce_mode;
183 num_compare_stages++;
184 oidcpy(&dpath->parent[stage - 2].oid,
185 &nce->oid);
186 dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
187 dpath->parent[stage-2].status =
188 DIFF_STATUS_MODIFIED;
191 /* diff against the proper unmerged stage */
192 if (stage == diff_unmerged_stage)
193 ce = nce;
194 i++;
197 * Compensate for loop update
199 i--;
201 if (revs->combine_merges && num_compare_stages == 2) {
202 show_combined_diff(dpath, 2, revs);
203 free(dpath);
204 continue;
206 FREE_AND_NULL(dpath);
209 * Show the diff for the 'ce' if we found the one
210 * from the desired stage.
212 pair = diff_unmerge(&revs->diffopt, ce->name);
213 if (wt_mode)
214 pair->two->mode = wt_mode;
215 if (ce_stage(ce) != diff_unmerged_stage)
216 continue;
219 if (ce_uptodate(ce) || ce_skip_worktree(ce))
220 continue;
223 * When CE_VALID is set (via "update-index --assume-unchanged"
224 * or via adding paths while core.ignorestat is set to true),
225 * the user has promised that the working tree file for that
226 * path will not be modified. When CE_FSMONITOR_VALID is true,
227 * the fsmonitor knows that the path hasn't been modified since
228 * we refreshed the cached stat information. In either case,
229 * we do not have to stat to see if the path has been removed
230 * or modified.
232 if (ce->ce_flags & (CE_VALID | CE_FSMONITOR_VALID)) {
233 changed = 0;
234 newmode = ce->ce_mode;
235 } else {
236 struct stat st;
238 changed = check_removed(ce, &st);
239 if (changed) {
240 if (changed < 0) {
241 perror(ce->name);
242 continue;
244 diff_addremove(&revs->diffopt, '-', ce->ce_mode,
245 &ce->oid,
246 !is_null_oid(&ce->oid),
247 ce->name, 0);
248 continue;
249 } else if (revs->diffopt.ita_invisible_in_index &&
250 ce_intent_to_add(ce)) {
251 newmode = ce_mode_from_stat(ce, st.st_mode);
252 diff_addremove(&revs->diffopt, '+', newmode,
253 null_oid(), 0, ce->name, 0);
254 continue;
257 changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
258 ce_option, &dirty_submodule);
259 newmode = ce_mode_from_stat(ce, st.st_mode);
262 if (!changed && !dirty_submodule) {
263 ce_mark_uptodate(ce);
264 mark_fsmonitor_valid(istate, ce);
265 if (!revs->diffopt.flags.find_copies_harder)
266 continue;
268 oldmode = ce->ce_mode;
269 old_oid = &ce->oid;
270 new_oid = changed ? null_oid() : &ce->oid;
271 diff_change(&revs->diffopt, oldmode, newmode,
272 old_oid, new_oid,
273 !is_null_oid(old_oid),
274 !is_null_oid(new_oid),
275 ce->name, 0, dirty_submodule);
278 diffcore_std(&revs->diffopt);
279 diff_flush(&revs->diffopt);
280 trace_performance_since(start, "diff-files");
284 * diff-index
287 /* A file entry went away or appeared */
288 static void diff_index_show_file(struct rev_info *revs,
289 const char *prefix,
290 const struct cache_entry *ce,
291 const struct object_id *oid, int oid_valid,
292 unsigned int mode,
293 unsigned dirty_submodule)
295 diff_addremove(&revs->diffopt, prefix[0], mode,
296 oid, oid_valid, ce->name, dirty_submodule);
299 static int get_stat_data(const struct index_state *istate,
300 const struct cache_entry *ce,
301 const struct object_id **oidp,
302 unsigned int *modep,
303 int cached, int match_missing,
304 unsigned *dirty_submodule, struct diff_options *diffopt)
306 const struct object_id *oid = &ce->oid;
307 unsigned int mode = ce->ce_mode;
309 if (!cached && !ce_uptodate(ce)) {
310 int changed;
311 struct stat st;
312 changed = check_removed(ce, &st);
313 if (changed < 0)
314 return -1;
315 else if (changed) {
316 if (match_missing) {
317 *oidp = oid;
318 *modep = mode;
319 return 0;
321 return -1;
323 changed = match_stat_with_submodule(diffopt, ce, &st,
324 0, dirty_submodule);
325 if (changed) {
326 mode = ce_mode_from_stat(ce, st.st_mode);
327 oid = null_oid();
331 *oidp = oid;
332 *modep = mode;
333 return 0;
336 static void show_new_file(struct rev_info *revs,
337 const struct cache_entry *new_file,
338 int cached, int match_missing)
340 const struct object_id *oid;
341 unsigned int mode;
342 unsigned dirty_submodule = 0;
343 struct index_state *istate = revs->diffopt.repo->index;
345 if (new_file && S_ISSPARSEDIR(new_file->ce_mode)) {
346 diff_tree_oid(NULL, &new_file->oid, new_file->name, &revs->diffopt);
347 return;
351 * New file in the index: it might actually be different in
352 * the working tree.
354 if (get_stat_data(istate, new_file, &oid, &mode, cached, match_missing,
355 &dirty_submodule, &revs->diffopt) < 0)
356 return;
358 diff_index_show_file(revs, "+", new_file, oid, !is_null_oid(oid), mode, dirty_submodule);
361 static int show_modified(struct rev_info *revs,
362 const struct cache_entry *old_entry,
363 const struct cache_entry *new_entry,
364 int report_missing,
365 int cached, int match_missing)
367 unsigned int mode, oldmode;
368 const struct object_id *oid;
369 unsigned dirty_submodule = 0;
370 struct index_state *istate = revs->diffopt.repo->index;
372 assert(S_ISSPARSEDIR(old_entry->ce_mode) ==
373 S_ISSPARSEDIR(new_entry->ce_mode));
376 * If both are sparse directory entries, then expand the
377 * modifications to the file level. If only one was a sparse
378 * directory, then they appear as an add and delete instead of
379 * a modification.
381 if (S_ISSPARSEDIR(new_entry->ce_mode)) {
382 diff_tree_oid(&old_entry->oid, &new_entry->oid, new_entry->name, &revs->diffopt);
383 return 0;
386 if (get_stat_data(istate, new_entry, &oid, &mode, cached, match_missing,
387 &dirty_submodule, &revs->diffopt) < 0) {
388 if (report_missing)
389 diff_index_show_file(revs, "-", old_entry,
390 &old_entry->oid, 1, old_entry->ce_mode,
392 return -1;
395 if (revs->combine_merges && !cached &&
396 (!oideq(oid, &old_entry->oid) || !oideq(&old_entry->oid, &new_entry->oid))) {
397 struct combine_diff_path *p;
398 int pathlen = ce_namelen(new_entry);
400 p = xmalloc(combine_diff_path_size(2, pathlen));
401 p->path = (char *) &p->parent[2];
402 p->next = NULL;
403 memcpy(p->path, new_entry->name, pathlen);
404 p->path[pathlen] = 0;
405 p->mode = mode;
406 oidclr(&p->oid);
407 memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
408 p->parent[0].status = DIFF_STATUS_MODIFIED;
409 p->parent[0].mode = new_entry->ce_mode;
410 oidcpy(&p->parent[0].oid, &new_entry->oid);
411 p->parent[1].status = DIFF_STATUS_MODIFIED;
412 p->parent[1].mode = old_entry->ce_mode;
413 oidcpy(&p->parent[1].oid, &old_entry->oid);
414 show_combined_diff(p, 2, revs);
415 free(p);
416 return 0;
419 oldmode = old_entry->ce_mode;
420 if (mode == oldmode && oideq(oid, &old_entry->oid) && !dirty_submodule &&
421 !revs->diffopt.flags.find_copies_harder)
422 return 0;
424 diff_change(&revs->diffopt, oldmode, mode,
425 &old_entry->oid, oid, 1, !is_null_oid(oid),
426 old_entry->name, 0, dirty_submodule);
427 return 0;
431 * This gets a mix of an existing index and a tree, one pathname entry
432 * at a time. The index entry may be a single stage-0 one, but it could
433 * also be multiple unmerged entries (in which case idx_pos/idx_nr will
434 * give you the position and number of entries in the index).
436 static void do_oneway_diff(struct unpack_trees_options *o,
437 const struct cache_entry *idx,
438 const struct cache_entry *tree)
440 struct rev_info *revs = o->unpack_data;
441 int match_missing, cached;
444 * i-t-a entries do not actually exist in the index (if we're
445 * looking at its content)
447 if (o->index_only &&
448 revs->diffopt.ita_invisible_in_index &&
449 idx && ce_intent_to_add(idx)) {
450 idx = NULL;
451 if (!tree)
452 return; /* nothing to diff.. */
455 /* if the entry is not checked out, don't examine work tree */
456 cached = o->index_only ||
457 (idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx)));
459 match_missing = revs->match_missing;
461 if (cached && idx && ce_stage(idx)) {
462 struct diff_filepair *pair;
463 pair = diff_unmerge(&revs->diffopt, idx->name);
464 if (tree)
465 fill_filespec(pair->one, &tree->oid, 1,
466 tree->ce_mode);
467 return;
471 * Something added to the tree?
473 if (!tree) {
474 show_new_file(revs, idx, cached, match_missing);
475 return;
479 * Something removed from the tree?
481 if (!idx) {
482 if (S_ISSPARSEDIR(tree->ce_mode)) {
483 diff_tree_oid(&tree->oid, NULL, tree->name, &revs->diffopt);
484 return;
487 diff_index_show_file(revs, "-", tree, &tree->oid, 1,
488 tree->ce_mode, 0);
489 return;
492 /* Show difference between old and new */
493 show_modified(revs, tree, idx, 1, cached, match_missing);
497 * The unpack_trees() interface is designed for merging, so
498 * the different source entries are designed primarily for
499 * the source trees, with the old index being really mainly
500 * used for being replaced by the result.
502 * For diffing, the index is more important, and we only have a
503 * single tree.
505 * We're supposed to advance o->pos to skip what we have already processed.
507 * This wrapper makes it all more readable, and takes care of all
508 * the fairly complex unpack_trees() semantic requirements, including
509 * the skipping, the path matching, the type conflict cases etc.
511 static int oneway_diff(const struct cache_entry * const *src,
512 struct unpack_trees_options *o)
514 const struct cache_entry *idx = src[0];
515 const struct cache_entry *tree = src[1];
516 struct rev_info *revs = o->unpack_data;
519 * Unpack-trees generates a DF/conflict entry if
520 * there was a directory in the index and a tree
521 * in the tree. From a diff standpoint, that's a
522 * delete of the tree and a create of the file.
524 if (tree == o->df_conflict_entry)
525 tree = NULL;
527 if (ce_path_match(revs->diffopt.repo->index,
528 idx ? idx : tree,
529 &revs->prune_data, NULL)) {
530 do_oneway_diff(o, idx, tree);
531 if (diff_can_quit_early(&revs->diffopt)) {
532 o->exiting_early = 1;
533 return -1;
537 return 0;
540 static int diff_cache(struct rev_info *revs,
541 const struct object_id *tree_oid,
542 const char *tree_name,
543 int cached)
545 struct tree *tree;
546 struct tree_desc t;
547 struct unpack_trees_options opts;
549 tree = parse_tree_indirect(tree_oid);
550 if (!tree)
551 return error("bad tree object %s",
552 tree_name ? tree_name : oid_to_hex(tree_oid));
553 memset(&opts, 0, sizeof(opts));
554 opts.head_idx = 1;
555 opts.index_only = cached;
556 opts.diff_index_cached = (cached &&
557 !revs->diffopt.flags.find_copies_harder);
558 opts.merge = 1;
559 opts.fn = oneway_diff;
560 opts.unpack_data = revs;
561 opts.src_index = revs->diffopt.repo->index;
562 opts.dst_index = NULL;
563 opts.pathspec = &revs->diffopt.pathspec;
564 opts.pathspec->recursive = 1;
566 init_tree_desc(&t, tree->buffer, tree->size);
567 return unpack_trees(1, &t, &opts);
570 void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb)
572 int i;
573 struct commit *mb_child[2] = {0};
574 struct commit_list *merge_bases;
576 for (i = 0; i < revs->pending.nr; i++) {
577 struct object *obj = revs->pending.objects[i].item;
578 if (obj->flags)
579 die(_("--merge-base does not work with ranges"));
583 * This check must go after the for loop above because A...B
584 * ranges produce three pending commits, resulting in a
585 * misleading error message.
587 if (revs->pending.nr < 1 || revs->pending.nr > 2)
588 BUG("unexpected revs->pending.nr: %d", revs->pending.nr);
590 for (i = 0; i < revs->pending.nr; i++)
591 mb_child[i] = lookup_commit_reference(the_repository, &revs->pending.objects[i].item->oid);
592 if (revs->pending.nr == 1) {
593 struct object_id oid;
595 if (repo_get_oid(the_repository, "HEAD", &oid))
596 die(_("unable to get HEAD"));
598 mb_child[1] = lookup_commit_reference(the_repository, &oid);
601 merge_bases = repo_get_merge_bases(the_repository, mb_child[0], mb_child[1]);
602 if (!merge_bases)
603 die(_("no merge base found"));
604 if (merge_bases->next)
605 die(_("multiple merge bases found"));
607 oidcpy(mb, &merge_bases->item->object.oid);
609 free_commit_list(merge_bases);
612 void run_diff_index(struct rev_info *revs, unsigned int option)
614 struct object_array_entry *ent;
615 int cached = !!(option & DIFF_INDEX_CACHED);
616 int merge_base = !!(option & DIFF_INDEX_MERGE_BASE);
617 struct object_id oid;
618 const char *name;
619 char merge_base_hex[GIT_MAX_HEXSZ + 1];
620 struct index_state *istate = revs->diffopt.repo->index;
622 if (revs->pending.nr != 1)
623 BUG("run_diff_index must be passed exactly one tree");
625 trace_performance_enter();
626 ent = revs->pending.objects;
628 refresh_fsmonitor(istate);
630 if (merge_base) {
631 diff_get_merge_base(revs, &oid);
632 name = oid_to_hex_r(merge_base_hex, &oid);
633 } else {
634 oidcpy(&oid, &ent->item->oid);
635 name = ent->name;
638 if (diff_cache(revs, &oid, name, cached))
639 exit(128);
641 diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
642 diffcore_fix_diff_index();
643 diffcore_std(&revs->diffopt);
644 diff_flush(&revs->diffopt);
645 trace_performance_leave("diff-index");
648 int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)
650 struct rev_info revs;
652 repo_init_revisions(opt->repo, &revs, NULL);
653 copy_pathspec(&revs.prune_data, &opt->pathspec);
654 diff_setup_done(&revs.diffopt);
655 revs.diffopt = *opt;
657 if (diff_cache(&revs, tree_oid, NULL, 1))
658 exit(128);
659 release_revisions(&revs);
660 return 0;
663 int index_differs_from(struct repository *r,
664 const char *def, const struct diff_flags *flags,
665 int ita_invisible_in_index)
667 struct rev_info rev;
668 struct setup_revision_opt opt;
669 unsigned has_changes;
671 repo_init_revisions(r, &rev, NULL);
672 memset(&opt, 0, sizeof(opt));
673 opt.def = def;
674 setup_revisions(0, NULL, &rev, &opt);
675 rev.diffopt.flags.quick = 1;
676 rev.diffopt.flags.exit_with_status = 1;
677 if (flags) {
678 diff_flags_or(&rev.diffopt.flags, flags);
680 * Now that flags are merged, honor override_submodule_config
681 * and ignore_submodules from passed flags.
683 if (flags->override_submodule_config)
684 rev.diffopt.flags.ignore_submodules = flags->ignore_submodules;
686 rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
687 run_diff_index(&rev, DIFF_INDEX_CACHED);
688 has_changes = rev.diffopt.flags.has_changes;
689 release_revisions(&rev);
690 return (has_changes != 0);
693 static struct strbuf *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
695 return data;
698 void show_interdiff(const struct object_id *oid1, const struct object_id *oid2,
699 int indent, struct diff_options *diffopt)
701 struct diff_options opts;
702 struct strbuf prefix = STRBUF_INIT;
704 memcpy(&opts, diffopt, sizeof(opts));
705 opts.output_format = DIFF_FORMAT_PATCH;
706 opts.output_prefix = idiff_prefix_cb;
707 strbuf_addchars(&prefix, ' ', indent);
708 opts.output_prefix_data = &prefix;
709 diff_setup_done(&opts);
711 diff_tree_oid(oid1, oid2, "", &opts);
712 diffcore_std(&opts);
713 diff_flush(&opts);
715 strbuf_release(&prefix);