4 * Copyright (c) 2006 Junio C Hamano
6 #define USE_THE_INDEX_VARIABLE
17 #include "diff-merges.h"
23 #include "submodule.h"
24 #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 int builtin_diff_b_f(struct rev_info
*revs
,
79 int argc
, const char **argv
,
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
);
113 static int builtin_diff_blobs(struct rev_info
*revs
,
114 int argc
, const char **argv
,
115 struct object_array_entry
**blob
)
117 const unsigned mode
= canon_mode(S_IFREG
| 0644);
120 usage(builtin_diff_usage
);
122 if (blob
[0]->mode
== S_IFINVALID
)
123 blob
[0]->mode
= mode
;
125 if (blob
[1]->mode
== S_IFINVALID
)
126 blob
[1]->mode
= mode
;
128 stuff_change(&revs
->diffopt
,
129 blob
[0]->mode
, blob
[1]->mode
,
130 &blob
[0]->item
->oid
, &blob
[1]->item
->oid
,
132 blob_path(blob
[0]), blob_path(blob
[1]));
133 diffcore_std(&revs
->diffopt
);
134 diff_flush(&revs
->diffopt
);
138 static int builtin_diff_index(struct rev_info
*revs
,
139 int argc
, const char **argv
)
141 unsigned int option
= 0;
143 const char *arg
= argv
[1];
144 if (!strcmp(arg
, "--cached") || !strcmp(arg
, "--staged"))
145 option
|= DIFF_INDEX_CACHED
;
146 else if (!strcmp(arg
, "--merge-base"))
147 option
|= DIFF_INDEX_MERGE_BASE
;
149 usage(builtin_diff_usage
);
153 * Make sure there is one revision (i.e. pending object),
154 * and there is no revision filtering parameters.
156 if (revs
->pending
.nr
!= 1 ||
157 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
159 usage(builtin_diff_usage
);
160 if (!(option
& DIFF_INDEX_CACHED
)) {
162 if (repo_read_index_preload(the_repository
,
163 &revs
->diffopt
.pathspec
, 0) < 0) {
164 perror("repo_read_index_preload");
167 } else if (repo_read_index(the_repository
) < 0) {
168 perror("repo_read_cache");
171 return run_diff_index(revs
, option
);
174 static int builtin_diff_tree(struct rev_info
*revs
,
175 int argc
, const char **argv
,
176 struct object_array_entry
*ent0
,
177 struct object_array_entry
*ent1
)
179 const struct object_id
*(oid
[2]);
180 struct object_id mb_oid
;
184 const char *arg
= argv
[1];
185 if (!strcmp(arg
, "--merge-base"))
188 usage(builtin_diff_usage
);
193 diff_get_merge_base(revs
, &mb_oid
);
195 oid
[1] = &revs
->pending
.objects
[1].item
->oid
;
200 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
203 if (ent1
->item
->flags
& UNINTERESTING
)
205 oid
[swap
] = &ent0
->item
->oid
;
206 oid
[1 - swap
] = &ent1
->item
->oid
;
208 diff_tree_oid(oid
[0], oid
[1], "", &revs
->diffopt
);
209 log_tree_diff_flush(revs
);
213 static int builtin_diff_combined(struct rev_info
*revs
,
214 int argc
, const char **argv
,
215 struct object_array_entry
*ent
,
216 int ents
, int first_non_parent
)
218 struct oid_array parents
= OID_ARRAY_INIT
;
222 usage(builtin_diff_usage
);
224 if (first_non_parent
< 0)
225 die(_("no merge given, only parents."));
226 if (first_non_parent
>= ents
)
227 BUG("first_non_parent out of range: %d", first_non_parent
);
229 diff_merges_set_dense_combined_if_unset(revs
);
231 for (i
= 0; i
< ents
; i
++) {
232 if (i
!= first_non_parent
)
233 oid_array_append(&parents
, &ent
[i
].item
->oid
);
235 diff_tree_combined(&ent
[first_non_parent
].item
->oid
, &parents
, revs
);
236 oid_array_clear(&parents
);
240 static void refresh_index_quietly(void)
242 struct lock_file lock_file
= LOCK_INIT
;
245 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
248 discard_index(&the_index
);
249 repo_read_index(the_repository
);
250 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
,
252 repo_update_index_if_able(the_repository
, &lock_file
);
255 static int builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
257 unsigned int options
= 0;
259 while (1 < argc
&& argv
[1][0] == '-') {
260 if (!strcmp(argv
[1], "--base"))
262 else if (!strcmp(argv
[1], "--ours"))
264 else if (!strcmp(argv
[1], "--theirs"))
266 else if (!strcmp(argv
[1], "-q"))
267 options
|= DIFF_SILENT_ON_REMOVED
;
268 else if (!strcmp(argv
[1], "-h"))
269 usage(builtin_diff_usage
);
271 return error(_("invalid option: %s"), argv
[1]);
276 * "diff --base" should not combine merges because it was not
277 * asked to. "diff -c" should not densify (if the user wants
278 * dense one, --cc can be explicitly asked for, or just rely
281 if (revs
->max_count
== -1 &&
282 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
283 diff_merges_set_dense_combined_if_unset(revs
);
286 if (repo_read_index_preload(the_repository
, &revs
->diffopt
.pathspec
,
288 perror("repo_read_index_preload");
291 return run_diff_files(revs
, options
);
297 const char *base
, *left
, *right
;
301 * Check for symmetric-difference arguments, and if present, arrange
302 * everything we need to know to handle them correctly. As a bonus,
303 * weed out all bogus range-based revision specifications, e.g.,
304 * "git diff A..B C..D" or "git diff A..B C" get rejected.
306 * For an actual symmetric diff, *symdiff is set this way:
308 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
309 * indices that the caller should ignore (extra merge bases, of
310 * which there might be many, and A in A...B). Note that the
311 * chosen merge base and right side are NOT marked.
312 * - warn is set if there are multiple merge bases.
313 * - base, left, and right point to the names to use in a
314 * warning about multiple merge bases.
316 * If there is no symmetric diff argument, sym->skip is NULL and
317 * sym->warn is cleared. The remaining fields are not set.
319 static void symdiff_prepare(struct rev_info
*rev
, struct symdiff
*sym
)
321 int i
, is_symdiff
= 0, basecount
= 0, othercount
= 0;
322 int lpos
= -1, rpos
= -1, basepos
= -1;
323 struct bitmap
*map
= NULL
;
326 * Use the whence fields to find merge bases and left and
327 * right parts of symmetric difference, so that we do not
328 * depend on the order that revisions are parsed. If there
329 * are any revs that aren't from these sources, we have a
330 * "git diff C A...B" or "git diff A...B C" case. Or we
331 * could even get "git diff A...B C...E", for instance.
333 * If we don't have just one merge base, we pick one
336 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
337 * so we must check for SYMMETRIC_LEFT too. The two arrays
338 * rev->pending.objects and rev->cmdline.rev are parallel.
340 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
341 struct object
*obj
= rev
->pending
.objects
[i
].item
;
342 switch (rev
->cmdline
.rev
[i
].whence
) {
343 case REV_CMD_MERGE_BASE
:
347 break; /* do mark all bases */
350 usage(builtin_diff_usage
);
352 if (obj
->flags
& SYMMETRIC_LEFT
) {
354 break; /* do mark A */
359 usage(builtin_diff_usage
);
361 continue; /* don't mark B */
362 case REV_CMD_PARENTS_ONLY
:
374 * Forbid any additional revs for both A...B and A..B.
376 if (lpos
>= 0 && othercount
> 0)
377 usage(builtin_diff_usage
);
386 sym
->left
= rev
->pending
.objects
[lpos
].name
;
387 sym
->right
= rev
->pending
.objects
[rpos
].name
;
389 die(_("%s...%s: no merge base"), sym
->left
, sym
->right
);
390 sym
->base
= rev
->pending
.objects
[basepos
].name
;
391 bitmap_unset(map
, basepos
); /* unmark the base we want */
392 sym
->warn
= basecount
> 1;
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
;
473 init_diff_ui_defaults();
474 git_config(git_diff_ui_config
, NULL
);
475 prefix
= precompose_argv_prefix(argc
, argv
, prefix
);
477 repo_init_revisions(the_repository
, &rev
, prefix
);
479 /* Set up defaults that will apply to both no-index and regular diffs. */
480 rev
.diffopt
.stat_width
= -1;
481 rev
.diffopt
.stat_graph_width
= -1;
482 rev
.diffopt
.flags
.allow_external
= 1;
483 rev
.diffopt
.flags
.allow_textconv
= 1;
485 /* If this is a no-index diff, just run it and exit there. */
487 exit(diff_no_index(&rev
, no_index
== DIFF_NO_INDEX_IMPLICIT
,
492 * Otherwise, we are doing the usual "git" diff; set up any
493 * further defaults that apply to regular diffs.
495 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
498 * Default to intent-to-add entries invisible in the
499 * index. This makes them show up as new files in diff-files
500 * and not at all in diff-cached.
502 rev
.diffopt
.ita_invisible_in_index
= 1;
505 die(_("Not a git repository"));
506 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
507 if (!rev
.diffopt
.output_format
) {
508 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
509 diff_setup_done(&rev
.diffopt
);
512 rev
.diffopt
.flags
.recursive
= 1;
513 rev
.diffopt
.rotate_to_strict
= 1;
515 setup_diff_pager(&rev
.diffopt
);
518 * Do we have --cached and not have a pending object, then
519 * default to HEAD by hand. Eek.
521 if (!rev
.pending
.nr
) {
523 for (i
= 1; i
< argc
; i
++) {
524 const char *arg
= argv
[i
];
525 if (!strcmp(arg
, "--"))
527 else if (!strcmp(arg
, "--cached") ||
528 !strcmp(arg
, "--staged")) {
529 add_head_to_pending(&rev
);
530 if (!rev
.pending
.nr
) {
532 tree
= lookup_tree(the_repository
,
533 the_repository
->hash_algo
->empty_tree
);
534 add_pending_object(&rev
, &tree
->object
, "HEAD");
541 symdiff_prepare(&rev
, &sdiff
);
542 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
543 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
544 struct object
*obj
= entry
->item
;
545 const char *name
= entry
->name
;
546 int flags
= (obj
->flags
& UNINTERESTING
);
548 obj
= parse_object(the_repository
, &obj
->oid
);
549 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
551 die(_("invalid object '%s' given."), name
);
552 if (obj
->type
== OBJ_COMMIT
)
553 obj
= &get_commit_tree(((struct commit
*)obj
))->object
;
555 if (obj
->type
== OBJ_TREE
) {
556 if (sdiff
.skip
&& bitmap_get(sdiff
.skip
, i
))
559 add_object_array(obj
, name
, &ent
);
560 if (first_non_parent
< 0 &&
561 (i
>= rev
.cmdline
.nr
|| /* HEAD by hand. */
562 rev
.cmdline
.rev
[i
].whence
!= REV_CMD_PARENTS_ONLY
))
563 first_non_parent
= ent
.nr
- 1;
564 } else if (obj
->type
== OBJ_BLOB
) {
566 die(_("more than two blobs given: '%s'"), name
);
571 die(_("unhandled object '%s' given."), name
);
574 if (rev
.prune_data
.nr
)
575 paths
+= rev
.prune_data
.nr
;
578 * Now, do the arguments look reasonable?
583 result
= builtin_diff_files(&rev
, argc
, argv
);
587 usage(builtin_diff_usage
);
588 result
= builtin_diff_b_f(&rev
, argc
, argv
, blob
);
592 usage(builtin_diff_usage
);
593 result
= builtin_diff_blobs(&rev
, argc
, argv
, blob
);
596 usage(builtin_diff_usage
);
600 usage(builtin_diff_usage
);
601 else if (ent
.nr
== 1)
602 result
= builtin_diff_index(&rev
, argc
, argv
);
603 else if (ent
.nr
== 2) {
605 warning(_("%s...%s: multiple merge bases, using %s"),
606 sdiff
.left
, sdiff
.right
, sdiff
.base
);
607 result
= builtin_diff_tree(&rev
, argc
, argv
,
608 &ent
.objects
[0], &ent
.objects
[1]);
610 result
= builtin_diff_combined(&rev
, argc
, argv
,
613 result
= diff_result_code(&rev
.diffopt
, result
);
614 if (1 < rev
.diffopt
.skip_stat_unmatch
)
615 refresh_index_quietly();
616 release_revisions(&rev
);
617 object_array_clear(&ent
);