Merge branch 'tr' of github.com:bitigchi/git-po
[alt-git.git] / diff-lib.c
blob60e979dc1bdf1633ed8a62dee944c663f30fbe8b
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 "gettext.h"
10 #include "hex.h"
11 #include "object-name.h"
12 #include "revision.h"
13 #include "cache-tree.h"
14 #include "unpack-trees.h"
15 #include "refs.h"
16 #include "submodule.h"
17 #include "symlinks.h"
18 #include "trace.h"
19 #include "dir.h"
20 #include "fsmonitor.h"
21 #include "commit-reach.h"
24 * diff-files
28 * Has the work tree entity been removed?
30 * Return 1 if it was removed from the work tree, 0 if an entity to be
31 * compared with the cache entry ce still exists (the latter includes
32 * the case where a directory that is not a submodule repository
33 * exists for ce that is a submodule -- it is a submodule that is not
34 * checked out). Return negative for an error.
36 static int check_removed(const struct index_state *istate, const struct cache_entry *ce, struct stat *st)
38 assert(is_fsmonitor_refreshed(istate));
39 if (!(ce->ce_flags & CE_FSMONITOR_VALID) && lstat(ce->name, st) < 0) {
40 if (!is_missing_file_error(errno))
41 return -1;
42 return 1;
44 if (has_symlink_leading_path(ce->name, ce_namelen(ce)))
45 return 1;
46 if (S_ISDIR(st->st_mode)) {
47 struct object_id sub;
50 * If ce is already a gitlink, we can have a plain
51 * directory (i.e. the submodule is not checked out),
52 * or a checked out submodule. Either case this is not
53 * a case where something was removed from the work tree,
54 * so we will return 0.
56 * Otherwise, if the directory is not a submodule
57 * repository, that means ce which was a blob turned into
58 * a directory --- the blob was removed!
60 if (!S_ISGITLINK(ce->ce_mode) &&
61 resolve_gitlink_ref(ce->name, "HEAD", &sub))
62 return 1;
64 return 0;
68 * Has a file changed or has a submodule new commits or a dirty work tree?
70 * Return 1 when changes are detected, 0 otherwise. If the DIRTY_SUBMODULES
71 * option is set, the caller does not only want to know if a submodule is
72 * modified at all but wants to know all the conditions that are met (new
73 * commits, untracked content and/or modified content).
75 static int match_stat_with_submodule(struct diff_options *diffopt,
76 const struct cache_entry *ce,
77 struct stat *st, unsigned ce_option,
78 unsigned *dirty_submodule)
80 int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option);
81 if (S_ISGITLINK(ce->ce_mode)) {
82 struct diff_flags orig_flags = diffopt->flags;
83 if (!diffopt->flags.override_submodule_config)
84 set_diffopt_flags_from_submodule_config(diffopt, ce->name);
85 if (diffopt->flags.ignore_submodules)
86 changed = 0;
87 else if (!diffopt->flags.ignore_dirty_submodules &&
88 (!changed || diffopt->flags.dirty_submodules))
89 *dirty_submodule = is_submodule_modified(ce->name,
90 diffopt->flags.ignore_untracked_in_submodules);
91 diffopt->flags = orig_flags;
93 return changed;
96 int run_diff_files(struct rev_info *revs, unsigned int option)
98 int entries, i;
99 int diff_unmerged_stage = revs->max_count;
100 unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
101 ? CE_MATCH_RACY_IS_DIRTY : 0);
102 uint64_t start = getnanotime();
103 struct index_state *istate = revs->diffopt.repo->index;
105 diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");
107 refresh_fsmonitor(istate);
109 if (diff_unmerged_stage < 0)
110 diff_unmerged_stage = 2;
111 entries = istate->cache_nr;
112 for (i = 0; i < entries; i++) {
113 unsigned int oldmode, newmode;
114 struct cache_entry *ce = istate->cache[i];
115 int changed;
116 unsigned dirty_submodule = 0;
117 const struct object_id *old_oid, *new_oid;
119 if (diff_can_quit_early(&revs->diffopt))
120 break;
122 if (!ce_path_match(istate, ce, &revs->prune_data, NULL))
123 continue;
125 if (revs->diffopt.prefix &&
126 strncmp(ce->name, revs->diffopt.prefix, revs->diffopt.prefix_length))
127 continue;
129 if (ce_stage(ce)) {
130 struct combine_diff_path *dpath;
131 struct diff_filepair *pair;
132 unsigned int wt_mode = 0;
133 int num_compare_stages = 0;
134 size_t path_len;
135 struct stat st;
137 path_len = ce_namelen(ce);
139 dpath = xmalloc(combine_diff_path_size(5, path_len));
140 dpath->path = (char *) &(dpath->parent[5]);
142 dpath->next = NULL;
143 memcpy(dpath->path, ce->name, path_len);
144 dpath->path[path_len] = '\0';
145 oidclr(&dpath->oid);
146 memset(&(dpath->parent[0]), 0,
147 sizeof(struct combine_diff_parent)*5);
149 changed = check_removed(istate, ce, &st);
150 if (!changed)
151 wt_mode = ce_mode_from_stat(ce, st.st_mode);
152 else {
153 if (changed < 0) {
154 perror(ce->name);
155 continue;
157 wt_mode = 0;
159 dpath->mode = wt_mode;
161 while (i < entries) {
162 struct cache_entry *nce = istate->cache[i];
163 int stage;
165 if (strcmp(ce->name, nce->name))
166 break;
168 /* Stage #2 (ours) is the first parent,
169 * stage #3 (theirs) is the second.
171 stage = ce_stage(nce);
172 if (2 <= stage) {
173 int mode = nce->ce_mode;
174 num_compare_stages++;
175 oidcpy(&dpath->parent[stage - 2].oid,
176 &nce->oid);
177 dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
178 dpath->parent[stage-2].status =
179 DIFF_STATUS_MODIFIED;
182 /* diff against the proper unmerged stage */
183 if (stage == diff_unmerged_stage)
184 ce = nce;
185 i++;
188 * Compensate for loop update
190 i--;
192 if (revs->combine_merges && num_compare_stages == 2) {
193 show_combined_diff(dpath, 2, revs);
194 free(dpath);
195 continue;
197 FREE_AND_NULL(dpath);
200 * Show the diff for the 'ce' if we found the one
201 * from the desired stage.
203 pair = diff_unmerge(&revs->diffopt, ce->name);
204 if (wt_mode)
205 pair->two->mode = wt_mode;
206 if (ce_stage(ce) != diff_unmerged_stage)
207 continue;
210 if (ce_uptodate(ce) || ce_skip_worktree(ce))
211 continue;
214 * When CE_VALID is set (via "update-index --assume-unchanged"
215 * or via adding paths while core.ignorestat is set to true),
216 * the user has promised that the working tree file for that
217 * path will not be modified. When CE_FSMONITOR_VALID is true,
218 * the fsmonitor knows that the path hasn't been modified since
219 * we refreshed the cached stat information. In either case,
220 * we do not have to stat to see if the path has been removed
221 * or modified.
223 if (ce->ce_flags & (CE_VALID | CE_FSMONITOR_VALID)) {
224 changed = 0;
225 newmode = ce->ce_mode;
226 } else {
227 struct stat st;
229 changed = check_removed(istate, ce, &st);
230 if (changed) {
231 if (changed < 0) {
232 perror(ce->name);
233 continue;
235 diff_addremove(&revs->diffopt, '-', ce->ce_mode,
236 &ce->oid,
237 !is_null_oid(&ce->oid),
238 ce->name, 0);
239 continue;
240 } else if (revs->diffopt.ita_invisible_in_index &&
241 ce_intent_to_add(ce)) {
242 newmode = ce_mode_from_stat(ce, st.st_mode);
243 diff_addremove(&revs->diffopt, '+', newmode,
244 null_oid(), 0, ce->name, 0);
245 continue;
248 changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
249 ce_option, &dirty_submodule);
250 newmode = ce_mode_from_stat(ce, st.st_mode);
253 if (!changed && !dirty_submodule) {
254 ce_mark_uptodate(ce);
255 mark_fsmonitor_valid(istate, ce);
256 if (!revs->diffopt.flags.find_copies_harder)
257 continue;
259 oldmode = ce->ce_mode;
260 old_oid = &ce->oid;
261 new_oid = changed ? null_oid() : &ce->oid;
262 diff_change(&revs->diffopt, oldmode, newmode,
263 old_oid, new_oid,
264 !is_null_oid(old_oid),
265 !is_null_oid(new_oid),
266 ce->name, 0, dirty_submodule);
269 diffcore_std(&revs->diffopt);
270 diff_flush(&revs->diffopt);
271 trace_performance_since(start, "diff-files");
272 return 0;
276 * diff-index
279 /* A file entry went away or appeared */
280 static void diff_index_show_file(struct rev_info *revs,
281 const char *prefix,
282 const struct cache_entry *ce,
283 const struct object_id *oid, int oid_valid,
284 unsigned int mode,
285 unsigned dirty_submodule)
287 diff_addremove(&revs->diffopt, prefix[0], mode,
288 oid, oid_valid, ce->name, dirty_submodule);
291 static int get_stat_data(const struct index_state *istate,
292 const struct cache_entry *ce,
293 const struct object_id **oidp,
294 unsigned int *modep,
295 int cached, int match_missing,
296 unsigned *dirty_submodule, struct diff_options *diffopt)
298 const struct object_id *oid = &ce->oid;
299 unsigned int mode = ce->ce_mode;
301 if (!cached && !ce_uptodate(ce)) {
302 int changed;
303 struct stat st;
304 changed = check_removed(istate, ce, &st);
305 if (changed < 0)
306 return -1;
307 else if (changed) {
308 if (match_missing) {
309 *oidp = oid;
310 *modep = mode;
311 return 0;
313 return -1;
315 changed = match_stat_with_submodule(diffopt, ce, &st,
316 0, dirty_submodule);
317 if (changed) {
318 mode = ce_mode_from_stat(ce, st.st_mode);
319 oid = null_oid();
323 *oidp = oid;
324 *modep = mode;
325 return 0;
328 static void show_new_file(struct rev_info *revs,
329 const struct cache_entry *new_file,
330 int cached, int match_missing)
332 const struct object_id *oid;
333 unsigned int mode;
334 unsigned dirty_submodule = 0;
335 struct index_state *istate = revs->diffopt.repo->index;
337 if (new_file && S_ISSPARSEDIR(new_file->ce_mode)) {
338 diff_tree_oid(NULL, &new_file->oid, new_file->name, &revs->diffopt);
339 return;
343 * New file in the index: it might actually be different in
344 * the working tree.
346 if (get_stat_data(istate, new_file, &oid, &mode, cached, match_missing,
347 &dirty_submodule, &revs->diffopt) < 0)
348 return;
350 diff_index_show_file(revs, "+", new_file, oid, !is_null_oid(oid), mode, dirty_submodule);
353 static int show_modified(struct rev_info *revs,
354 const struct cache_entry *old_entry,
355 const struct cache_entry *new_entry,
356 int report_missing,
357 int cached, int match_missing)
359 unsigned int mode, oldmode;
360 const struct object_id *oid;
361 unsigned dirty_submodule = 0;
362 struct index_state *istate = revs->diffopt.repo->index;
364 assert(S_ISSPARSEDIR(old_entry->ce_mode) ==
365 S_ISSPARSEDIR(new_entry->ce_mode));
368 * If both are sparse directory entries, then expand the
369 * modifications to the file level. If only one was a sparse
370 * directory, then they appear as an add and delete instead of
371 * a modification.
373 if (S_ISSPARSEDIR(new_entry->ce_mode)) {
374 diff_tree_oid(&old_entry->oid, &new_entry->oid, new_entry->name, &revs->diffopt);
375 return 0;
378 if (get_stat_data(istate, new_entry, &oid, &mode, cached, match_missing,
379 &dirty_submodule, &revs->diffopt) < 0) {
380 if (report_missing)
381 diff_index_show_file(revs, "-", old_entry,
382 &old_entry->oid, 1, old_entry->ce_mode,
384 return -1;
387 if (revs->combine_merges && !cached &&
388 (!oideq(oid, &old_entry->oid) || !oideq(&old_entry->oid, &new_entry->oid))) {
389 struct combine_diff_path *p;
390 int pathlen = ce_namelen(new_entry);
392 p = xmalloc(combine_diff_path_size(2, pathlen));
393 p->path = (char *) &p->parent[2];
394 p->next = NULL;
395 memcpy(p->path, new_entry->name, pathlen);
396 p->path[pathlen] = 0;
397 p->mode = mode;
398 oidclr(&p->oid);
399 memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
400 p->parent[0].status = DIFF_STATUS_MODIFIED;
401 p->parent[0].mode = new_entry->ce_mode;
402 oidcpy(&p->parent[0].oid, &new_entry->oid);
403 p->parent[1].status = DIFF_STATUS_MODIFIED;
404 p->parent[1].mode = old_entry->ce_mode;
405 oidcpy(&p->parent[1].oid, &old_entry->oid);
406 show_combined_diff(p, 2, revs);
407 free(p);
408 return 0;
411 oldmode = old_entry->ce_mode;
412 if (mode == oldmode && oideq(oid, &old_entry->oid) && !dirty_submodule &&
413 !revs->diffopt.flags.find_copies_harder)
414 return 0;
416 diff_change(&revs->diffopt, oldmode, mode,
417 &old_entry->oid, oid, 1, !is_null_oid(oid),
418 old_entry->name, 0, dirty_submodule);
419 return 0;
423 * This gets a mix of an existing index and a tree, one pathname entry
424 * at a time. The index entry may be a single stage-0 one, but it could
425 * also be multiple unmerged entries (in which case idx_pos/idx_nr will
426 * give you the position and number of entries in the index).
428 static void do_oneway_diff(struct unpack_trees_options *o,
429 const struct cache_entry *idx,
430 const struct cache_entry *tree)
432 struct rev_info *revs = o->unpack_data;
433 int match_missing, cached;
436 * i-t-a entries do not actually exist in the index (if we're
437 * looking at its content)
439 if (o->index_only &&
440 revs->diffopt.ita_invisible_in_index &&
441 idx && ce_intent_to_add(idx)) {
442 idx = NULL;
443 if (!tree)
444 return; /* nothing to diff.. */
447 /* if the entry is not checked out, don't examine work tree */
448 cached = o->index_only ||
449 (idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx)));
451 match_missing = revs->match_missing;
453 if (cached && idx && ce_stage(idx)) {
454 struct diff_filepair *pair;
455 pair = diff_unmerge(&revs->diffopt, idx->name);
456 if (tree)
457 fill_filespec(pair->one, &tree->oid, 1,
458 tree->ce_mode);
459 return;
463 * Something added to the tree?
465 if (!tree) {
466 show_new_file(revs, idx, cached, match_missing);
467 return;
471 * Something removed from the tree?
473 if (!idx) {
474 if (S_ISSPARSEDIR(tree->ce_mode)) {
475 diff_tree_oid(&tree->oid, NULL, tree->name, &revs->diffopt);
476 return;
479 diff_index_show_file(revs, "-", tree, &tree->oid, 1,
480 tree->ce_mode, 0);
481 return;
484 /* Show difference between old and new */
485 show_modified(revs, tree, idx, 1, cached, match_missing);
489 * The unpack_trees() interface is designed for merging, so
490 * the different source entries are designed primarily for
491 * the source trees, with the old index being really mainly
492 * used for being replaced by the result.
494 * For diffing, the index is more important, and we only have a
495 * single tree.
497 * We're supposed to advance o->pos to skip what we have already processed.
499 * This wrapper makes it all more readable, and takes care of all
500 * the fairly complex unpack_trees() semantic requirements, including
501 * the skipping, the path matching, the type conflict cases etc.
503 static int oneway_diff(const struct cache_entry * const *src,
504 struct unpack_trees_options *o)
506 const struct cache_entry *idx = src[0];
507 const struct cache_entry *tree = src[1];
508 struct rev_info *revs = o->unpack_data;
511 * Unpack-trees generates a DF/conflict entry if
512 * there was a directory in the index and a tree
513 * in the tree. From a diff standpoint, that's a
514 * delete of the tree and a create of the file.
516 if (tree == o->df_conflict_entry)
517 tree = NULL;
519 if (ce_path_match(revs->diffopt.repo->index,
520 idx ? idx : tree,
521 &revs->prune_data, NULL)) {
522 do_oneway_diff(o, idx, tree);
523 if (diff_can_quit_early(&revs->diffopt)) {
524 o->exiting_early = 1;
525 return -1;
529 return 0;
532 static int diff_cache(struct rev_info *revs,
533 const struct object_id *tree_oid,
534 const char *tree_name,
535 int cached)
537 struct tree *tree;
538 struct tree_desc t;
539 struct unpack_trees_options opts;
541 tree = parse_tree_indirect(tree_oid);
542 if (!tree)
543 return error("bad tree object %s",
544 tree_name ? tree_name : oid_to_hex(tree_oid));
545 memset(&opts, 0, sizeof(opts));
546 opts.head_idx = 1;
547 opts.index_only = cached;
548 opts.diff_index_cached = (cached &&
549 !revs->diffopt.flags.find_copies_harder);
550 opts.merge = 1;
551 opts.fn = oneway_diff;
552 opts.unpack_data = revs;
553 opts.src_index = revs->diffopt.repo->index;
554 opts.dst_index = NULL;
555 opts.pathspec = &revs->diffopt.pathspec;
556 opts.pathspec->recursive = 1;
558 init_tree_desc(&t, tree->buffer, tree->size);
559 return unpack_trees(1, &t, &opts);
562 void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb)
564 int i;
565 struct commit *mb_child[2] = {0};
566 struct commit_list *merge_bases;
568 for (i = 0; i < revs->pending.nr; i++) {
569 struct object *obj = revs->pending.objects[i].item;
570 if (obj->flags)
571 die(_("--merge-base does not work with ranges"));
572 if (obj->type != OBJ_COMMIT)
573 die(_("--merge-base only works with commits"));
577 * This check must go after the for loop above because A...B
578 * ranges produce three pending commits, resulting in a
579 * misleading error message.
581 if (revs->pending.nr < 1 || revs->pending.nr > 2)
582 BUG("unexpected revs->pending.nr: %d", revs->pending.nr);
584 for (i = 0; i < revs->pending.nr; i++)
585 mb_child[i] = lookup_commit_reference(the_repository, &revs->pending.objects[i].item->oid);
586 if (revs->pending.nr == 1) {
587 struct object_id oid;
589 if (repo_get_oid(the_repository, "HEAD", &oid))
590 die(_("unable to get HEAD"));
592 mb_child[1] = lookup_commit_reference(the_repository, &oid);
595 merge_bases = repo_get_merge_bases(the_repository, mb_child[0], mb_child[1]);
596 if (!merge_bases)
597 die(_("no merge base found"));
598 if (merge_bases->next)
599 die(_("multiple merge bases found"));
601 oidcpy(mb, &merge_bases->item->object.oid);
603 free_commit_list(merge_bases);
606 int run_diff_index(struct rev_info *revs, unsigned int option)
608 struct object_array_entry *ent;
609 int cached = !!(option & DIFF_INDEX_CACHED);
610 int merge_base = !!(option & DIFF_INDEX_MERGE_BASE);
611 struct object_id oid;
612 const char *name;
613 char merge_base_hex[GIT_MAX_HEXSZ + 1];
614 struct index_state *istate = revs->diffopt.repo->index;
616 if (revs->pending.nr != 1)
617 BUG("run_diff_index must be passed exactly one tree");
619 trace_performance_enter();
620 ent = revs->pending.objects;
622 refresh_fsmonitor(istate);
624 if (merge_base) {
625 diff_get_merge_base(revs, &oid);
626 name = oid_to_hex_r(merge_base_hex, &oid);
627 } else {
628 oidcpy(&oid, &ent->item->oid);
629 name = ent->name;
632 if (diff_cache(revs, &oid, name, cached))
633 exit(128);
635 diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
636 diffcore_fix_diff_index();
637 diffcore_std(&revs->diffopt);
638 diff_flush(&revs->diffopt);
639 trace_performance_leave("diff-index");
640 return 0;
643 int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)
645 struct rev_info revs;
647 repo_init_revisions(opt->repo, &revs, NULL);
648 copy_pathspec(&revs.prune_data, &opt->pathspec);
649 diff_setup_done(&revs.diffopt);
650 revs.diffopt = *opt;
652 if (diff_cache(&revs, tree_oid, NULL, 1))
653 exit(128);
654 release_revisions(&revs);
655 return 0;
658 int index_differs_from(struct repository *r,
659 const char *def, const struct diff_flags *flags,
660 int ita_invisible_in_index)
662 struct rev_info rev;
663 struct setup_revision_opt opt;
664 unsigned has_changes;
666 repo_init_revisions(r, &rev, NULL);
667 memset(&opt, 0, sizeof(opt));
668 opt.def = def;
669 setup_revisions(0, NULL, &rev, &opt);
670 rev.diffopt.flags.quick = 1;
671 rev.diffopt.flags.exit_with_status = 1;
672 if (flags)
673 diff_flags_or(&rev.diffopt.flags, flags);
674 rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
675 run_diff_index(&rev, 1);
676 has_changes = rev.diffopt.flags.has_changes;
677 release_revisions(&rev);
678 return (has_changes != 0);
681 static struct strbuf *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
683 return data;
686 void show_interdiff(const struct object_id *oid1, const struct object_id *oid2,
687 int indent, struct diff_options *diffopt)
689 struct diff_options opts;
690 struct strbuf prefix = STRBUF_INIT;
692 memcpy(&opts, diffopt, sizeof(opts));
693 opts.output_format = DIFF_FORMAT_PATCH;
694 opts.output_prefix = idiff_prefix_cb;
695 strbuf_addchars(&prefix, ' ', indent);
696 opts.output_prefix_data = &prefix;
697 diff_setup_done(&opts);
699 diff_tree_oid(oid1, oid2, "", &opts);
700 diffcore_std(&opts);
701 diff_flush(&opts);
703 strbuf_release(&prefix);