4 * Copyright (c) 2006 Junio C Hamano
6 #define USE_THE_INDEX_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_index
);
243 repo_read_index(the_repository
);
244 refresh_index(&the_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 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
395 struct object_array ent
= OBJECT_ARRAY_INIT
;
396 int first_non_parent
= -1;
397 int blobs
= 0, paths
= 0;
398 struct object_array_entry
*blob
[2];
399 int nongit
= 0, no_index
= 0;
401 struct symdiff sdiff
;
404 * We could get N tree-ish in the rev.pending_objects list.
405 * Also there could be M blobs there, and P pathspecs. --cached may
409 * cache vs files (diff-files)
411 * N=0, M=0, --cached:
412 * HEAD vs cache (diff-index --cached)
415 * compare two random blobs. P must be zero.
418 * compare a blob with a working tree file.
421 * tree vs files (diff-index)
423 * N=1, M=0, --cached:
424 * tree vs cache (diff-index --cached)
427 * tree vs tree (diff-tree)
430 * compare two filesystem entities (aka --no-index).
432 * Other cases are errors.
435 /* Were we asked to do --no-index explicitly? */
436 for (i
= 1; i
< argc
; i
++) {
437 if (!strcmp(argv
[i
], "--")) {
441 if (!strcmp(argv
[i
], "--no-index"))
442 no_index
= DIFF_NO_INDEX_EXPLICIT
;
443 if (argv
[i
][0] != '-')
447 prefix
= setup_git_directory_gently(&nongit
);
450 prepare_repo_settings(the_repository
);
451 the_repository
->settings
.command_requires_full_index
= 0;
456 * Treat git diff with at least one path outside of the
457 * repo the same as if the command would have been executed
458 * outside of a git repository. In this case it behaves
459 * the same way as "git diff --no-index <a> <b>", which acts
460 * as a colourful "diff" replacement.
462 if (nongit
|| ((argc
== i
+ 2) &&
463 (!path_inside_repo(prefix
, argv
[i
]) ||
464 !path_inside_repo(prefix
, argv
[i
+ 1]))))
465 no_index
= DIFF_NO_INDEX_IMPLICIT
;
468 init_diff_ui_defaults();
469 git_config(git_diff_ui_config
, NULL
);
470 prefix
= precompose_argv_prefix(argc
, argv
, prefix
);
472 repo_init_revisions(the_repository
, &rev
, prefix
);
474 /* Set up defaults that will apply to both no-index and regular diffs. */
475 init_diffstat_widths(&rev
.diffopt
);
476 rev
.diffopt
.flags
.allow_external
= 1;
477 rev
.diffopt
.flags
.allow_textconv
= 1;
479 /* If this is a no-index diff, just run it and exit there. */
481 exit(diff_no_index(&rev
, no_index
== DIFF_NO_INDEX_IMPLICIT
,
486 * Otherwise, we are doing the usual "git" diff; set up any
487 * further defaults that apply to regular diffs.
489 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
492 * Default to intent-to-add entries invisible in the
493 * index. This makes them show up as new files in diff-files
494 * and not at all in diff-cached.
496 rev
.diffopt
.ita_invisible_in_index
= 1;
499 die(_("Not a git repository"));
500 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
501 if (!rev
.diffopt
.output_format
) {
502 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
503 diff_setup_done(&rev
.diffopt
);
506 rev
.diffopt
.flags
.recursive
= 1;
507 rev
.diffopt
.rotate_to_strict
= 1;
509 setup_diff_pager(&rev
.diffopt
);
512 * Do we have --cached and not have a pending object, then
513 * default to HEAD by hand. Eek.
515 if (!rev
.pending
.nr
) {
517 for (i
= 1; i
< argc
; i
++) {
518 const char *arg
= argv
[i
];
519 if (!strcmp(arg
, "--"))
521 else if (!strcmp(arg
, "--cached") ||
522 !strcmp(arg
, "--staged")) {
523 add_head_to_pending(&rev
);
524 if (!rev
.pending
.nr
) {
526 tree
= lookup_tree(the_repository
,
527 the_repository
->hash_algo
->empty_tree
);
528 add_pending_object(&rev
, &tree
->object
, "HEAD");
535 symdiff_prepare(&rev
, &sdiff
);
536 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
537 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
538 struct object
*obj
= entry
->item
;
539 const char *name
= entry
->name
;
540 int flags
= (obj
->flags
& UNINTERESTING
);
542 obj
= parse_object(the_repository
, &obj
->oid
);
543 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
545 die(_("invalid object '%s' given."), name
);
546 if (obj
->type
== OBJ_COMMIT
)
547 obj
= &repo_get_commit_tree(the_repository
,
548 ((struct commit
*)obj
))->object
;
550 if (obj
->type
== OBJ_TREE
) {
551 if (sdiff
.skip
&& bitmap_get(sdiff
.skip
, i
))
554 add_object_array(obj
, name
, &ent
);
555 if (first_non_parent
< 0 &&
556 (i
>= rev
.cmdline
.nr
|| /* HEAD by hand. */
557 rev
.cmdline
.rev
[i
].whence
!= REV_CMD_PARENTS_ONLY
))
558 first_non_parent
= ent
.nr
- 1;
559 } else if (obj
->type
== OBJ_BLOB
) {
561 die(_("more than two blobs given: '%s'"), name
);
566 die(_("unhandled object '%s' given."), name
);
569 if (rev
.prune_data
.nr
)
570 paths
+= rev
.prune_data
.nr
;
573 * Now, do the arguments look reasonable?
578 builtin_diff_files(&rev
, argc
, argv
);
582 usage(builtin_diff_usage
);
583 builtin_diff_b_f(&rev
, argc
, argv
, blob
);
587 usage(builtin_diff_usage
);
588 builtin_diff_blobs(&rev
, argc
, argv
, blob
);
591 usage(builtin_diff_usage
);
595 usage(builtin_diff_usage
);
596 else if (ent
.nr
== 1)
597 builtin_diff_index(&rev
, argc
, argv
);
598 else if (ent
.nr
== 2) {
600 warning(_("%s...%s: multiple merge bases, using %s"),
601 sdiff
.left
, sdiff
.right
, sdiff
.base
);
602 builtin_diff_tree(&rev
, argc
, argv
,
603 &ent
.objects
[0], &ent
.objects
[1]);
605 builtin_diff_combined(&rev
, argc
, argv
,
608 result
= diff_result_code(&rev
.diffopt
);
609 if (1 < rev
.diffopt
.skip_stat_unmatch
)
610 refresh_index_quietly();
611 release_revisions(&rev
);
612 object_array_clear(&ent
);