2 * Copyright (C) 2005 Junio C Hamano
4 #include "git-compat-util.h"
12 #include "object-name.h"
13 #include "read-cache.h"
15 #include "cache-tree.h"
16 #include "unpack-trees.h"
18 #include "repository.h"
19 #include "submodule.h"
23 #include "fsmonitor.h"
24 #include "commit-reach.h"
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
)
43 if (!(ce
->ce_flags
& CE_FSMONITOR_VALID
))
44 stat_err
= lstat(ce
->name
, st
);
46 stat_err
= fake_lstat(ce
, st
);
48 if (!is_missing_file_error(errno
))
53 if (has_symlink_leading_path(ce
->name
, ce_namelen(ce
)))
55 if (S_ISDIR(st
->st_mode
)) {
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
))
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
)
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
;
105 void run_diff_files(struct rev_info
*revs
, unsigned int option
)
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
];
125 unsigned dirty_submodule
= 0;
126 const struct object_id
*old_oid
, *new_oid
;
128 if (diff_can_quit_early(&revs
->diffopt
))
131 if (!ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
))
134 if (revs
->diffopt
.prefix
&&
135 strncmp(ce
->name
, revs
->diffopt
.prefix
, revs
->diffopt
.prefix_length
))
139 struct combine_diff_path
*dpath
;
140 struct diff_filepair
*pair
;
141 unsigned int wt_mode
= 0;
142 int num_compare_stages
= 0;
146 path_len
= ce_namelen(ce
);
148 dpath
= xmalloc(combine_diff_path_size(5, path_len
));
149 dpath
->path
= (char *) &(dpath
->parent
[5]);
152 memcpy(dpath
->path
, ce
->name
, path_len
);
153 dpath
->path
[path_len
] = '\0';
155 memset(&(dpath
->parent
[0]), 0,
156 sizeof(struct combine_diff_parent
)*5);
158 changed
= check_removed(ce
, &st
);
160 wt_mode
= ce_mode_from_stat(ce
, st
.st_mode
);
168 dpath
->mode
= wt_mode
;
170 while (i
< entries
) {
171 struct cache_entry
*nce
= istate
->cache
[i
];
174 if (strcmp(ce
->name
, nce
->name
))
177 /* Stage #2 (ours) is the first parent,
178 * stage #3 (theirs) is the second.
180 stage
= ce_stage(nce
);
182 int mode
= nce
->ce_mode
;
183 num_compare_stages
++;
184 oidcpy(&dpath
->parent
[stage
- 2].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
)
197 * Compensate for loop update
201 if (revs
->combine_merges
&& num_compare_stages
== 2) {
202 show_combined_diff(dpath
, 2, revs
);
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
);
214 pair
->two
->mode
= wt_mode
;
215 if (ce_stage(ce
) != diff_unmerged_stage
)
219 if (ce_uptodate(ce
) || ce_skip_worktree(ce
))
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
232 if (ce
->ce_flags
& (CE_VALID
| CE_FSMONITOR_VALID
)) {
234 newmode
= ce
->ce_mode
;
238 changed
= check_removed(ce
, &st
);
244 diff_addremove(&revs
->diffopt
, '-', ce
->ce_mode
,
246 !is_null_oid(&ce
->oid
),
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);
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
)
268 oldmode
= ce
->ce_mode
;
270 new_oid
= changed
? null_oid() : &ce
->oid
;
271 diff_change(&revs
->diffopt
, oldmode
, newmode
,
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");
287 /* A file entry went away or appeared */
288 static void diff_index_show_file(struct rev_info
*revs
,
290 const struct cache_entry
*ce
,
291 const struct object_id
*oid
, int oid_valid
,
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
,
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
)) {
312 changed
= check_removed(ce
, &st
);
323 changed
= match_stat_with_submodule(diffopt
, ce
, &st
,
326 mode
= ce_mode_from_stat(ce
, st
.st_mode
);
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
;
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
);
351 * New file in the index: it might actually be different in
354 if (get_stat_data(istate
, new_file
, &oid
, &mode
, cached
, match_missing
,
355 &dirty_submodule
, &revs
->diffopt
) < 0)
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
,
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
381 if (S_ISSPARSEDIR(new_entry
->ce_mode
)) {
382 diff_tree_oid(&old_entry
->oid
, &new_entry
->oid
, new_entry
->name
, &revs
->diffopt
);
386 if (get_stat_data(istate
, new_entry
, &oid
, &mode
, cached
, match_missing
,
387 &dirty_submodule
, &revs
->diffopt
) < 0) {
389 diff_index_show_file(revs
, "-", old_entry
,
390 &old_entry
->oid
, 1, old_entry
->ce_mode
,
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];
403 memcpy(p
->path
, new_entry
->name
, pathlen
);
404 p
->path
[pathlen
] = 0;
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
);
419 oldmode
= old_entry
->ce_mode
;
420 if (mode
== oldmode
&& oideq(oid
, &old_entry
->oid
) && !dirty_submodule
&&
421 !revs
->diffopt
.flags
.find_copies_harder
)
424 diff_change(&revs
->diffopt
, oldmode
, mode
,
425 &old_entry
->oid
, oid
, 1, !is_null_oid(oid
),
426 old_entry
->name
, 0, dirty_submodule
);
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)
448 revs
->diffopt
.ita_invisible_in_index
&&
449 idx
&& ce_intent_to_add(idx
)) {
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
);
465 fill_filespec(pair
->one
, &tree
->oid
, 1,
471 * Something added to the tree?
474 show_new_file(revs
, idx
, cached
, match_missing
);
479 * Something removed from the tree?
482 if (S_ISSPARSEDIR(tree
->ce_mode
)) {
483 diff_tree_oid(&tree
->oid
, NULL
, tree
->name
, &revs
->diffopt
);
487 diff_index_show_file(revs
, "-", tree
, &tree
->oid
, 1,
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
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
)
527 if (ce_path_match(revs
->diffopt
.repo
->index
,
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;
540 static int diff_cache(struct rev_info
*revs
,
541 const struct object_id
*tree_oid
,
542 const char *tree_name
,
547 struct unpack_trees_options opts
;
549 tree
= parse_tree_indirect(tree_oid
);
551 return error("bad tree object %s",
552 tree_name
? tree_name
: oid_to_hex(tree_oid
));
553 memset(&opts
, 0, sizeof(opts
));
555 opts
.index_only
= cached
;
556 opts
.diff_index_cached
= (cached
&&
557 !revs
->diffopt
.flags
.find_copies_harder
);
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
)
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
;
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]);
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
;
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
);
631 diff_get_merge_base(revs
, &oid
);
632 name
= oid_to_hex_r(merge_base_hex
, &oid
);
634 oidcpy(&oid
, &ent
->item
->oid
);
638 if (diff_cache(revs
, &oid
, name
, cached
))
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
);
657 if (diff_cache(&revs
, tree_oid
, NULL
, 1))
659 release_revisions(&revs
);
663 int index_differs_from(struct repository
*r
,
664 const char *def
, const struct diff_flags
*flags
,
665 int ita_invisible_in_index
)
668 struct setup_revision_opt opt
;
669 unsigned has_changes
;
671 repo_init_revisions(r
, &rev
, NULL
);
672 memset(&opt
, 0, sizeof(opt
));
674 setup_revisions(0, NULL
, &rev
, &opt
);
675 rev
.diffopt
.flags
.quick
= 1;
676 rev
.diffopt
.flags
.exit_with_status
= 1;
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
)
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
);
715 strbuf_release(&prefix
);