4 * Copyright (c) 2006 Junio C Hamano
16 #include "diff-merges.h"
18 #include "preload-index.h"
19 #include "read-cache-ll.h"
23 #include "oid-array.h"
26 #define DIFF_NO_INDEX_EXPLICIT 1
27 #define DIFF_NO_INDEX_IMPLICIT 2
29 static const char builtin_diff_usage
[] =
30 "git diff [<options>] [<commit>] [--] [<path>...]\n"
31 " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
32 " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
33 " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
34 " or: git diff [<options>] <blob> <blob>\n"
35 " or: git diff [<options>] --no-index [--] <path> <path>"
37 COMMON_DIFF_OPTIONS_HELP
;
39 static const char *blob_path(struct object_array_entry
*entry
)
41 return entry
->path
? entry
->path
: entry
->name
;
44 static void stuff_change(struct diff_options
*opt
,
45 unsigned old_mode
, unsigned new_mode
,
46 const struct object_id
*old_oid
,
47 const struct object_id
*new_oid
,
53 struct diff_filespec
*one
, *two
;
55 if (!is_null_oid(old_oid
) && !is_null_oid(new_oid
) &&
56 oideq(old_oid
, new_oid
) && (old_mode
== new_mode
))
59 if (opt
->flags
.reverse_diff
) {
60 SWAP(old_mode
, new_mode
);
61 SWAP(old_oid
, new_oid
);
62 SWAP(old_path
, new_path
);
66 (strncmp(old_path
, opt
->prefix
, opt
->prefix_length
) ||
67 strncmp(new_path
, opt
->prefix
, opt
->prefix_length
)))
70 one
= alloc_filespec(old_path
);
71 two
= alloc_filespec(new_path
);
72 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
73 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
75 diff_queue(&diff_queued_diff
, one
, two
);
78 static void builtin_diff_b_f(struct rev_info
*revs
,
79 int argc
, const char **argv UNUSED
,
80 struct object_array_entry
**blob
)
82 /* Blob vs file in the working tree*/
87 usage(builtin_diff_usage
);
89 GUARD_PATHSPEC(&revs
->prune_data
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
90 path
= revs
->prune_data
.items
[0].match
;
93 die_errno(_("failed to stat '%s'"), path
);
94 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
95 die(_("'%s': not a regular file or symlink"), path
);
97 diff_set_mnemonic_prefix(&revs
->diffopt
, "o/", "w/");
99 if (blob
[0]->mode
== S_IFINVALID
)
100 blob
[0]->mode
= canon_mode(st
.st_mode
);
102 stuff_change(&revs
->diffopt
,
103 blob
[0]->mode
, canon_mode(st
.st_mode
),
104 &blob
[0]->item
->oid
, null_oid(),
106 blob
[0]->path
? blob
[0]->path
: path
,
108 diffcore_std(&revs
->diffopt
);
109 diff_flush(&revs
->diffopt
);
112 static void builtin_diff_blobs(struct rev_info
*revs
,
113 int argc
, const char **argv UNUSED
,
114 struct object_array_entry
**blob
)
116 const unsigned mode
= canon_mode(S_IFREG
| 0644);
119 usage(builtin_diff_usage
);
121 if (blob
[0]->mode
== S_IFINVALID
)
122 blob
[0]->mode
= mode
;
124 if (blob
[1]->mode
== S_IFINVALID
)
125 blob
[1]->mode
= mode
;
127 stuff_change(&revs
->diffopt
,
128 blob
[0]->mode
, blob
[1]->mode
,
129 &blob
[0]->item
->oid
, &blob
[1]->item
->oid
,
131 blob_path(blob
[0]), blob_path(blob
[1]));
132 diffcore_std(&revs
->diffopt
);
133 diff_flush(&revs
->diffopt
);
136 static void builtin_diff_index(struct rev_info
*revs
,
137 int argc
, const char **argv
)
139 unsigned int option
= 0;
141 const char *arg
= argv
[1];
142 if (!strcmp(arg
, "--cached") || !strcmp(arg
, "--staged"))
143 option
|= DIFF_INDEX_CACHED
;
144 else if (!strcmp(arg
, "--merge-base"))
145 option
|= DIFF_INDEX_MERGE_BASE
;
147 usage(builtin_diff_usage
);
151 * Make sure there is one revision (i.e. pending object),
152 * and there is no revision filtering parameters.
154 if (revs
->pending
.nr
!= 1 ||
155 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
157 usage(builtin_diff_usage
);
158 if (!(option
& DIFF_INDEX_CACHED
)) {
160 if (repo_read_index_preload(the_repository
,
161 &revs
->diffopt
.pathspec
, 0) < 0) {
162 die_errno("repo_read_index_preload");
164 } else if (repo_read_index(the_repository
) < 0) {
165 die_errno("repo_read_cache");
167 run_diff_index(revs
, option
);
170 static void builtin_diff_tree(struct rev_info
*revs
,
171 int argc
, const char **argv
,
172 struct object_array_entry
*ent0
,
173 struct object_array_entry
*ent1
)
175 const struct object_id
*(oid
[2]);
176 struct object_id mb_oid
;
180 const char *arg
= argv
[1];
181 if (!strcmp(arg
, "--merge-base"))
184 usage(builtin_diff_usage
);
189 diff_get_merge_base(revs
, &mb_oid
);
191 oid
[1] = &revs
->pending
.objects
[1].item
->oid
;
196 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
199 if (ent1
->item
->flags
& UNINTERESTING
)
201 oid
[swap
] = &ent0
->item
->oid
;
202 oid
[1 - swap
] = &ent1
->item
->oid
;
204 diff_tree_oid(oid
[0], oid
[1], "", &revs
->diffopt
);
205 log_tree_diff_flush(revs
);
208 static void builtin_diff_combined(struct rev_info
*revs
,
209 int argc
, const char **argv UNUSED
,
210 struct object_array_entry
*ent
,
211 int ents
, int first_non_parent
)
213 struct oid_array parents
= OID_ARRAY_INIT
;
217 usage(builtin_diff_usage
);
219 if (first_non_parent
< 0)
220 die(_("no merge given, only parents."));
221 if (first_non_parent
>= ents
)
222 BUG("first_non_parent out of range: %d", first_non_parent
);
224 diff_merges_set_dense_combined_if_unset(revs
);
226 for (i
= 0; i
< ents
; i
++) {
227 if (i
!= first_non_parent
)
228 oid_array_append(&parents
, &ent
[i
].item
->oid
);
230 diff_tree_combined(&ent
[first_non_parent
].item
->oid
, &parents
, revs
);
231 oid_array_clear(&parents
);
234 static void refresh_index_quietly(void)
236 struct lock_file lock_file
= LOCK_INIT
;
239 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
242 discard_index(the_repository
->index
);
243 repo_read_index(the_repository
);
244 refresh_index(the_repository
->index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
,
246 repo_update_index_if_able(the_repository
, &lock_file
);
249 static void builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
251 unsigned int options
= 0;
253 while (1 < argc
&& argv
[1][0] == '-') {
254 if (!strcmp(argv
[1], "--base"))
256 else if (!strcmp(argv
[1], "--ours"))
258 else if (!strcmp(argv
[1], "--theirs"))
260 else if (!strcmp(argv
[1], "-q"))
261 options
|= DIFF_SILENT_ON_REMOVED
;
262 else if (!strcmp(argv
[1], "-h"))
263 usage(builtin_diff_usage
);
265 error(_("invalid option: %s"), argv
[1]);
266 usage(builtin_diff_usage
);
272 * "diff --base" should not combine merges because it was not
273 * asked to. "diff -c" should not densify (if the user wants
274 * dense one, --cc can be explicitly asked for, or just rely
277 if (revs
->max_count
== -1 &&
278 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
279 diff_merges_set_dense_combined_if_unset(revs
);
282 if (repo_read_index_preload(the_repository
, &revs
->diffopt
.pathspec
,
284 die_errno("repo_read_index_preload");
286 run_diff_files(revs
, options
);
292 const char *base
, *left
, *right
;
296 * Check for symmetric-difference arguments, and if present, arrange
297 * everything we need to know to handle them correctly. As a bonus,
298 * weed out all bogus range-based revision specifications, e.g.,
299 * "git diff A..B C..D" or "git diff A..B C" get rejected.
301 * For an actual symmetric diff, *symdiff is set this way:
303 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
304 * indices that the caller should ignore (extra merge bases, of
305 * which there might be many, and A in A...B). Note that the
306 * chosen merge base and right side are NOT marked.
307 * - warn is set if there are multiple merge bases.
308 * - base, left, and right point to the names to use in a
309 * warning about multiple merge bases.
311 * If there is no symmetric diff argument, sym->skip is NULL and
312 * sym->warn is cleared. The remaining fields are not set.
314 static void symdiff_prepare(struct rev_info
*rev
, struct symdiff
*sym
)
316 int i
, is_symdiff
= 0, basecount
= 0, othercount
= 0;
317 int lpos
= -1, rpos
= -1, basepos
= -1;
318 struct bitmap
*map
= NULL
;
321 * Use the whence fields to find merge bases and left and
322 * right parts of symmetric difference, so that we do not
323 * depend on the order that revisions are parsed. If there
324 * are any revs that aren't from these sources, we have a
325 * "git diff C A...B" or "git diff A...B C" case. Or we
326 * could even get "git diff A...B C...E", for instance.
328 * If we don't have just one merge base, we pick one
331 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
332 * so we must check for SYMMETRIC_LEFT too. The two arrays
333 * rev->pending.objects and rev->cmdline.rev are parallel.
335 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
336 struct object
*obj
= rev
->pending
.objects
[i
].item
;
337 switch (rev
->cmdline
.rev
[i
].whence
) {
338 case REV_CMD_MERGE_BASE
:
342 break; /* do mark all bases */
345 usage(builtin_diff_usage
);
347 if (obj
->flags
& SYMMETRIC_LEFT
) {
349 break; /* do mark A */
354 usage(builtin_diff_usage
);
356 continue; /* don't mark B */
357 case REV_CMD_PARENTS_ONLY
:
369 * Forbid any additional revs for both A...B and A..B.
371 if (lpos
>= 0 && othercount
> 0)
372 usage(builtin_diff_usage
);
381 sym
->left
= rev
->pending
.objects
[lpos
].name
;
382 sym
->right
= rev
->pending
.objects
[rpos
].name
;
384 die(_("%s...%s: no merge base"), sym
->left
, sym
->right
);
385 sym
->base
= rev
->pending
.objects
[basepos
].name
;
386 bitmap_unset(map
, basepos
); /* unmark the base we want */
387 sym
->warn
= basecount
> 1;
391 static void symdiff_release(struct symdiff
*sdiff
)
393 bitmap_free(sdiff
->skip
);
396 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
400 struct object_array ent
= OBJECT_ARRAY_INIT
;
401 int first_non_parent
= -1;
402 int blobs
= 0, paths
= 0;
403 struct object_array_entry
*blob
[2];
404 int nongit
= 0, no_index
= 0;
406 struct symdiff sdiff
;
409 * We could get N tree-ish in the rev.pending_objects list.
410 * Also there could be M blobs there, and P pathspecs. --cached may
414 * cache vs files (diff-files)
416 * N=0, M=0, --cached:
417 * HEAD vs cache (diff-index --cached)
420 * compare two random blobs. P must be zero.
423 * compare a blob with a working tree file.
426 * tree vs files (diff-index)
428 * N=1, M=0, --cached:
429 * tree vs cache (diff-index --cached)
432 * tree vs tree (diff-tree)
435 * compare two filesystem entities (aka --no-index).
437 * Other cases are errors.
440 /* Were we asked to do --no-index explicitly? */
441 for (i
= 1; i
< argc
; i
++) {
442 if (!strcmp(argv
[i
], "--")) {
446 if (!strcmp(argv
[i
], "--no-index"))
447 no_index
= DIFF_NO_INDEX_EXPLICIT
;
448 if (argv
[i
][0] != '-')
452 prefix
= setup_git_directory_gently(&nongit
);
455 prepare_repo_settings(the_repository
);
456 the_repository
->settings
.command_requires_full_index
= 0;
461 * Treat git diff with at least one path outside of the
462 * repo the same as if the command would have been executed
463 * outside of a git repository. In this case it behaves
464 * the same way as "git diff --no-index <a> <b>", which acts
465 * as a colourful "diff" replacement.
467 if (nongit
|| ((argc
== i
+ 2) &&
468 (!path_inside_repo(prefix
, argv
[i
]) ||
469 !path_inside_repo(prefix
, argv
[i
+ 1]))))
470 no_index
= DIFF_NO_INDEX_IMPLICIT
;
474 * When operating outside of a Git repository we need to have a hash
475 * algorithm at hand so that we can generate the blob hashes. We
476 * default to SHA1 here, but may eventually want to change this to be
477 * configurable via a command line option.
480 repo_set_hash_algo(the_repository
, GIT_HASH_SHA1
);
482 init_diff_ui_defaults();
483 git_config(git_diff_ui_config
, NULL
);
484 prefix
= precompose_argv_prefix(argc
, argv
, prefix
);
486 repo_init_revisions(the_repository
, &rev
, prefix
);
488 /* Set up defaults that will apply to both no-index and regular diffs. */
489 init_diffstat_widths(&rev
.diffopt
);
490 rev
.diffopt
.flags
.allow_external
= 1;
491 rev
.diffopt
.flags
.allow_textconv
= 1;
493 /* If this is a no-index diff, just run it and exit there. */
495 exit(diff_no_index(&rev
, no_index
== DIFF_NO_INDEX_IMPLICIT
,
500 * Otherwise, we are doing the usual "git" diff; set up any
501 * further defaults that apply to regular diffs.
503 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
506 * Default to intent-to-add entries invisible in the
507 * index. This makes them show up as new files in diff-files
508 * and not at all in diff-cached.
510 rev
.diffopt
.ita_invisible_in_index
= 1;
513 die(_("Not a git repository"));
514 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
515 if (!rev
.diffopt
.output_format
) {
516 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
517 diff_setup_done(&rev
.diffopt
);
520 rev
.diffopt
.flags
.recursive
= 1;
521 rev
.diffopt
.rotate_to_strict
= 1;
523 setup_diff_pager(&rev
.diffopt
);
526 * Do we have --cached and not have a pending object, then
527 * default to HEAD by hand. Eek.
529 if (!rev
.pending
.nr
) {
531 for (i
= 1; i
< argc
; i
++) {
532 const char *arg
= argv
[i
];
533 if (!strcmp(arg
, "--"))
535 else if (!strcmp(arg
, "--cached") ||
536 !strcmp(arg
, "--staged")) {
537 add_head_to_pending(&rev
);
538 if (!rev
.pending
.nr
) {
540 tree
= lookup_tree(the_repository
,
541 the_repository
->hash_algo
->empty_tree
);
542 add_pending_object(&rev
, &tree
->object
, "HEAD");
549 symdiff_prepare(&rev
, &sdiff
);
550 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
551 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
552 struct object
*obj
= entry
->item
;
553 const char *name
= entry
->name
;
554 int flags
= (obj
->flags
& UNINTERESTING
);
556 obj
= parse_object(the_repository
, &obj
->oid
);
557 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
559 die(_("invalid object '%s' given."), name
);
560 if (obj
->type
== OBJ_COMMIT
)
561 obj
= &repo_get_commit_tree(the_repository
,
562 ((struct commit
*)obj
))->object
;
564 if (obj
->type
== OBJ_TREE
) {
565 if (sdiff
.skip
&& bitmap_get(sdiff
.skip
, i
))
568 add_object_array(obj
, name
, &ent
);
569 if (first_non_parent
< 0 &&
570 (i
>= rev
.cmdline
.nr
|| /* HEAD by hand. */
571 rev
.cmdline
.rev
[i
].whence
!= REV_CMD_PARENTS_ONLY
))
572 first_non_parent
= ent
.nr
- 1;
573 } else if (obj
->type
== OBJ_BLOB
) {
575 die(_("more than two blobs given: '%s'"), name
);
580 die(_("unhandled object '%s' given."), name
);
583 if (rev
.prune_data
.nr
)
584 paths
+= rev
.prune_data
.nr
;
587 * Now, do the arguments look reasonable?
592 builtin_diff_files(&rev
, argc
, argv
);
596 usage(builtin_diff_usage
);
597 builtin_diff_b_f(&rev
, argc
, argv
, blob
);
601 usage(builtin_diff_usage
);
602 builtin_diff_blobs(&rev
, argc
, argv
, blob
);
605 usage(builtin_diff_usage
);
609 usage(builtin_diff_usage
);
610 else if (ent
.nr
== 1)
611 builtin_diff_index(&rev
, argc
, argv
);
612 else if (ent
.nr
== 2) {
614 warning(_("%s...%s: multiple merge bases, using %s"),
615 sdiff
.left
, sdiff
.right
, sdiff
.base
);
616 builtin_diff_tree(&rev
, argc
, argv
,
617 &ent
.objects
[0], &ent
.objects
[1]);
619 builtin_diff_combined(&rev
, argc
, argv
,
622 result
= diff_result_code(&rev
);
623 if (1 < rev
.diffopt
.skip_stat_unmatch
)
624 refresh_index_quietly();
625 release_revisions(&rev
);
626 object_array_clear(&ent
);
627 symdiff_release(&sdiff
);