4 * Copyright (c) 2006 Junio C Hamano
6 #define USE_THE_REPOSITORY_VARIABLE
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
,
399 struct repository
*repo UNUSED
)
403 struct object_array ent
= OBJECT_ARRAY_INIT
;
404 int first_non_parent
= -1;
405 int blobs
= 0, paths
= 0;
406 struct object_array_entry
*blob
[2];
407 int nongit
= 0, no_index
= 0;
409 struct symdiff sdiff
;
412 * We could get N tree-ish in the rev.pending_objects list.
413 * Also there could be M blobs there, and P pathspecs. --cached may
417 * cache vs files (diff-files)
419 * N=0, M=0, --cached:
420 * HEAD vs cache (diff-index --cached)
423 * compare two random blobs. P must be zero.
426 * compare a blob with a working tree file.
429 * tree vs files (diff-index)
431 * N=1, M=0, --cached:
432 * tree vs cache (diff-index --cached)
435 * tree vs tree (diff-tree)
438 * compare two filesystem entities (aka --no-index).
440 * Other cases are errors.
443 /* Were we asked to do --no-index explicitly? */
444 for (i
= 1; i
< argc
; i
++) {
445 if (!strcmp(argv
[i
], "--")) {
449 if (!strcmp(argv
[i
], "--no-index"))
450 no_index
= DIFF_NO_INDEX_EXPLICIT
;
451 if (argv
[i
][0] != '-')
455 prefix
= setup_git_directory_gently(&nongit
);
458 prepare_repo_settings(the_repository
);
459 the_repository
->settings
.command_requires_full_index
= 0;
464 * Treat git diff with at least one path outside of the
465 * repo the same as if the command would have been executed
466 * outside of a git repository. In this case it behaves
467 * the same way as "git diff --no-index <a> <b>", which acts
468 * as a colourful "diff" replacement.
470 if (nongit
|| ((argc
== i
+ 2) &&
471 (!path_inside_repo(prefix
, argv
[i
]) ||
472 !path_inside_repo(prefix
, argv
[i
+ 1]))))
473 no_index
= DIFF_NO_INDEX_IMPLICIT
;
477 * When operating outside of a Git repository we need to have a hash
478 * algorithm at hand so that we can generate the blob hashes. We
479 * default to SHA1 here, but may eventually want to change this to be
480 * configurable via a command line option.
483 repo_set_hash_algo(the_repository
, GIT_HASH_SHA1
);
485 init_diff_ui_defaults();
486 git_config(git_diff_ui_config
, NULL
);
487 prefix
= precompose_argv_prefix(argc
, argv
, prefix
);
489 repo_init_revisions(the_repository
, &rev
, prefix
);
491 /* Set up defaults that will apply to both no-index and regular diffs. */
492 init_diffstat_widths(&rev
.diffopt
);
493 rev
.diffopt
.flags
.allow_external
= 1;
494 rev
.diffopt
.flags
.allow_textconv
= 1;
496 /* If this is a no-index diff, just run it and exit there. */
498 exit(diff_no_index(&rev
, no_index
== DIFF_NO_INDEX_IMPLICIT
,
503 * Otherwise, we are doing the usual "git" diff; set up any
504 * further defaults that apply to regular diffs.
506 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
509 * Default to intent-to-add entries invisible in the
510 * index. This makes them show up as new files in diff-files
511 * and not at all in diff-cached.
513 rev
.diffopt
.ita_invisible_in_index
= 1;
516 die(_("Not a git repository"));
517 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
518 if (!rev
.diffopt
.output_format
) {
519 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
520 diff_setup_done(&rev
.diffopt
);
523 rev
.diffopt
.flags
.recursive
= 1;
524 rev
.diffopt
.rotate_to_strict
= 1;
526 setup_diff_pager(&rev
.diffopt
);
529 * Do we have --cached and not have a pending object, then
530 * default to HEAD by hand. Eek.
532 if (!rev
.pending
.nr
) {
534 for (i
= 1; i
< argc
; i
++) {
535 const char *arg
= argv
[i
];
536 if (!strcmp(arg
, "--"))
538 else if (!strcmp(arg
, "--cached") ||
539 !strcmp(arg
, "--staged")) {
540 add_head_to_pending(&rev
);
541 if (!rev
.pending
.nr
) {
543 tree
= lookup_tree(the_repository
,
544 the_repository
->hash_algo
->empty_tree
);
545 add_pending_object(&rev
, &tree
->object
, "HEAD");
552 symdiff_prepare(&rev
, &sdiff
);
553 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
554 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
555 struct object
*obj
= entry
->item
;
556 const char *name
= entry
->name
;
557 int flags
= (obj
->flags
& UNINTERESTING
);
559 obj
= parse_object(the_repository
, &obj
->oid
);
560 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
562 die(_("invalid object '%s' given."), name
);
563 if (obj
->type
== OBJ_COMMIT
)
564 obj
= &repo_get_commit_tree(the_repository
,
565 ((struct commit
*)obj
))->object
;
567 if (obj
->type
== OBJ_TREE
) {
568 if (sdiff
.skip
&& bitmap_get(sdiff
.skip
, i
))
571 add_object_array(obj
, name
, &ent
);
572 if (first_non_parent
< 0 &&
573 (i
>= rev
.cmdline
.nr
|| /* HEAD by hand. */
574 rev
.cmdline
.rev
[i
].whence
!= REV_CMD_PARENTS_ONLY
))
575 first_non_parent
= ent
.nr
- 1;
576 } else if (obj
->type
== OBJ_BLOB
) {
578 die(_("more than two blobs given: '%s'"), name
);
583 die(_("unhandled object '%s' given."), name
);
586 if (rev
.prune_data
.nr
)
587 paths
+= rev
.prune_data
.nr
;
590 * Now, do the arguments look reasonable?
595 builtin_diff_files(&rev
, argc
, argv
);
599 usage(builtin_diff_usage
);
600 builtin_diff_b_f(&rev
, argc
, argv
, blob
);
604 usage(builtin_diff_usage
);
605 builtin_diff_blobs(&rev
, argc
, argv
, blob
);
608 usage(builtin_diff_usage
);
612 usage(builtin_diff_usage
);
613 else if (ent
.nr
== 1)
614 builtin_diff_index(&rev
, argc
, argv
);
615 else if (ent
.nr
== 2) {
617 warning(_("%s...%s: multiple merge bases, using %s"),
618 sdiff
.left
, sdiff
.right
, sdiff
.base
);
619 builtin_diff_tree(&rev
, argc
, argv
,
620 &ent
.objects
[0], &ent
.objects
[1]);
622 builtin_diff_combined(&rev
, argc
, argv
,
625 result
= diff_result_code(&rev
);
626 if (1 < rev
.diffopt
.skip_stat_unmatch
)
627 refresh_index_quietly();
628 release_revisions(&rev
);
629 object_array_clear(&ent
);
630 symdiff_release(&sdiff
);