4 * Copyright (c) 2006 Junio C Hamano
6 #define USE_THE_INDEX_VARIABLE
17 #include "diff-merges.h"
19 #include "preload-index.h"
20 #include "read-cache-ll.h"
24 #include "submodule.h"
25 #include "oid-array.h"
28 #define DIFF_NO_INDEX_EXPLICIT 1
29 #define DIFF_NO_INDEX_IMPLICIT 2
31 static const char builtin_diff_usage
[] =
32 "git diff [<options>] [<commit>] [--] [<path>...]\n"
33 " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
34 " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
35 " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
36 " or: git diff [<options>] <blob> <blob>\n"
37 " or: git diff [<options>] --no-index [--] <path> <path>"
39 COMMON_DIFF_OPTIONS_HELP
;
41 static const char *blob_path(struct object_array_entry
*entry
)
43 return entry
->path
? entry
->path
: entry
->name
;
46 static void stuff_change(struct diff_options
*opt
,
47 unsigned old_mode
, unsigned new_mode
,
48 const struct object_id
*old_oid
,
49 const struct object_id
*new_oid
,
55 struct diff_filespec
*one
, *two
;
57 if (!is_null_oid(old_oid
) && !is_null_oid(new_oid
) &&
58 oideq(old_oid
, new_oid
) && (old_mode
== new_mode
))
61 if (opt
->flags
.reverse_diff
) {
62 SWAP(old_mode
, new_mode
);
63 SWAP(old_oid
, new_oid
);
64 SWAP(old_path
, new_path
);
68 (strncmp(old_path
, opt
->prefix
, opt
->prefix_length
) ||
69 strncmp(new_path
, opt
->prefix
, opt
->prefix_length
)))
72 one
= alloc_filespec(old_path
);
73 two
= alloc_filespec(new_path
);
74 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
75 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
77 diff_queue(&diff_queued_diff
, one
, two
);
80 static void builtin_diff_b_f(struct rev_info
*revs
,
81 int argc
, const char **argv UNUSED
,
82 struct object_array_entry
**blob
)
84 /* Blob vs file in the working tree*/
89 usage(builtin_diff_usage
);
91 GUARD_PATHSPEC(&revs
->prune_data
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
92 path
= revs
->prune_data
.items
[0].match
;
95 die_errno(_("failed to stat '%s'"), path
);
96 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
97 die(_("'%s': not a regular file or symlink"), path
);
99 diff_set_mnemonic_prefix(&revs
->diffopt
, "o/", "w/");
101 if (blob
[0]->mode
== S_IFINVALID
)
102 blob
[0]->mode
= canon_mode(st
.st_mode
);
104 stuff_change(&revs
->diffopt
,
105 blob
[0]->mode
, canon_mode(st
.st_mode
),
106 &blob
[0]->item
->oid
, null_oid(),
108 blob
[0]->path
? blob
[0]->path
: path
,
110 diffcore_std(&revs
->diffopt
);
111 diff_flush(&revs
->diffopt
);
114 static void builtin_diff_blobs(struct rev_info
*revs
,
115 int argc
, const char **argv UNUSED
,
116 struct object_array_entry
**blob
)
118 const unsigned mode
= canon_mode(S_IFREG
| 0644);
121 usage(builtin_diff_usage
);
123 if (blob
[0]->mode
== S_IFINVALID
)
124 blob
[0]->mode
= mode
;
126 if (blob
[1]->mode
== S_IFINVALID
)
127 blob
[1]->mode
= mode
;
129 stuff_change(&revs
->diffopt
,
130 blob
[0]->mode
, blob
[1]->mode
,
131 &blob
[0]->item
->oid
, &blob
[1]->item
->oid
,
133 blob_path(blob
[0]), blob_path(blob
[1]));
134 diffcore_std(&revs
->diffopt
);
135 diff_flush(&revs
->diffopt
);
138 static void 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 die_errno("repo_read_index_preload");
166 } else if (repo_read_index(the_repository
) < 0) {
167 die_errno("repo_read_cache");
169 run_diff_index(revs
, option
);
172 static void builtin_diff_tree(struct rev_info
*revs
,
173 int argc
, const char **argv
,
174 struct object_array_entry
*ent0
,
175 struct object_array_entry
*ent1
)
177 const struct object_id
*(oid
[2]);
178 struct object_id mb_oid
;
182 const char *arg
= argv
[1];
183 if (!strcmp(arg
, "--merge-base"))
186 usage(builtin_diff_usage
);
191 diff_get_merge_base(revs
, &mb_oid
);
193 oid
[1] = &revs
->pending
.objects
[1].item
->oid
;
198 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
201 if (ent1
->item
->flags
& UNINTERESTING
)
203 oid
[swap
] = &ent0
->item
->oid
;
204 oid
[1 - swap
] = &ent1
->item
->oid
;
206 diff_tree_oid(oid
[0], oid
[1], "", &revs
->diffopt
);
207 log_tree_diff_flush(revs
);
210 static void builtin_diff_combined(struct rev_info
*revs
,
211 int argc
, const char **argv UNUSED
,
212 struct object_array_entry
*ent
,
213 int ents
, int first_non_parent
)
215 struct oid_array parents
= OID_ARRAY_INIT
;
219 usage(builtin_diff_usage
);
221 if (first_non_parent
< 0)
222 die(_("no merge given, only parents."));
223 if (first_non_parent
>= ents
)
224 BUG("first_non_parent out of range: %d", first_non_parent
);
226 diff_merges_set_dense_combined_if_unset(revs
);
228 for (i
= 0; i
< ents
; i
++) {
229 if (i
!= first_non_parent
)
230 oid_array_append(&parents
, &ent
[i
].item
->oid
);
232 diff_tree_combined(&ent
[first_non_parent
].item
->oid
, &parents
, revs
);
233 oid_array_clear(&parents
);
236 static void refresh_index_quietly(void)
238 struct lock_file lock_file
= LOCK_INIT
;
241 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
244 discard_index(&the_index
);
245 repo_read_index(the_repository
);
246 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
,
248 repo_update_index_if_able(the_repository
, &lock_file
);
251 static void builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
253 unsigned int options
= 0;
255 while (1 < argc
&& argv
[1][0] == '-') {
256 if (!strcmp(argv
[1], "--base"))
258 else if (!strcmp(argv
[1], "--ours"))
260 else if (!strcmp(argv
[1], "--theirs"))
262 else if (!strcmp(argv
[1], "-q"))
263 options
|= DIFF_SILENT_ON_REMOVED
;
264 else if (!strcmp(argv
[1], "-h"))
265 usage(builtin_diff_usage
);
267 error(_("invalid option: %s"), argv
[1]);
268 usage(builtin_diff_usage
);
274 * "diff --base" should not combine merges because it was not
275 * asked to. "diff -c" should not densify (if the user wants
276 * dense one, --cc can be explicitly asked for, or just rely
279 if (revs
->max_count
== -1 &&
280 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
281 diff_merges_set_dense_combined_if_unset(revs
);
284 if (repo_read_index_preload(the_repository
, &revs
->diffopt
.pathspec
,
286 die_errno("repo_read_index_preload");
288 run_diff_files(revs
, options
);
294 const char *base
, *left
, *right
;
298 * Check for symmetric-difference arguments, and if present, arrange
299 * everything we need to know to handle them correctly. As a bonus,
300 * weed out all bogus range-based revision specifications, e.g.,
301 * "git diff A..B C..D" or "git diff A..B C" get rejected.
303 * For an actual symmetric diff, *symdiff is set this way:
305 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
306 * indices that the caller should ignore (extra merge bases, of
307 * which there might be many, and A in A...B). Note that the
308 * chosen merge base and right side are NOT marked.
309 * - warn is set if there are multiple merge bases.
310 * - base, left, and right point to the names to use in a
311 * warning about multiple merge bases.
313 * If there is no symmetric diff argument, sym->skip is NULL and
314 * sym->warn is cleared. The remaining fields are not set.
316 static void symdiff_prepare(struct rev_info
*rev
, struct symdiff
*sym
)
318 int i
, is_symdiff
= 0, basecount
= 0, othercount
= 0;
319 int lpos
= -1, rpos
= -1, basepos
= -1;
320 struct bitmap
*map
= NULL
;
323 * Use the whence fields to find merge bases and left and
324 * right parts of symmetric difference, so that we do not
325 * depend on the order that revisions are parsed. If there
326 * are any revs that aren't from these sources, we have a
327 * "git diff C A...B" or "git diff A...B C" case. Or we
328 * could even get "git diff A...B C...E", for instance.
330 * If we don't have just one merge base, we pick one
333 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
334 * so we must check for SYMMETRIC_LEFT too. The two arrays
335 * rev->pending.objects and rev->cmdline.rev are parallel.
337 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
338 struct object
*obj
= rev
->pending
.objects
[i
].item
;
339 switch (rev
->cmdline
.rev
[i
].whence
) {
340 case REV_CMD_MERGE_BASE
:
344 break; /* do mark all bases */
347 usage(builtin_diff_usage
);
349 if (obj
->flags
& SYMMETRIC_LEFT
) {
351 break; /* do mark A */
356 usage(builtin_diff_usage
);
358 continue; /* don't mark B */
359 case REV_CMD_PARENTS_ONLY
:
371 * Forbid any additional revs for both A...B and A..B.
373 if (lpos
>= 0 && othercount
> 0)
374 usage(builtin_diff_usage
);
383 sym
->left
= rev
->pending
.objects
[lpos
].name
;
384 sym
->right
= rev
->pending
.objects
[rpos
].name
;
386 die(_("%s...%s: no merge base"), sym
->left
, sym
->right
);
387 sym
->base
= rev
->pending
.objects
[basepos
].name
;
388 bitmap_unset(map
, basepos
); /* unmark the base we want */
389 sym
->warn
= basecount
> 1;
393 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
397 struct object_array ent
= OBJECT_ARRAY_INIT
;
398 int first_non_parent
= -1;
399 int blobs
= 0, paths
= 0;
400 struct object_array_entry
*blob
[2];
401 int nongit
= 0, no_index
= 0;
403 struct symdiff sdiff
;
406 * We could get N tree-ish in the rev.pending_objects list.
407 * Also there could be M blobs there, and P pathspecs. --cached may
411 * cache vs files (diff-files)
413 * N=0, M=0, --cached:
414 * HEAD vs cache (diff-index --cached)
417 * compare two random blobs. P must be zero.
420 * compare a blob with a working tree file.
423 * tree vs files (diff-index)
425 * N=1, M=0, --cached:
426 * tree vs cache (diff-index --cached)
429 * tree vs tree (diff-tree)
432 * compare two filesystem entities (aka --no-index).
434 * Other cases are errors.
437 /* Were we asked to do --no-index explicitly? */
438 for (i
= 1; i
< argc
; i
++) {
439 if (!strcmp(argv
[i
], "--")) {
443 if (!strcmp(argv
[i
], "--no-index"))
444 no_index
= DIFF_NO_INDEX_EXPLICIT
;
445 if (argv
[i
][0] != '-')
449 prefix
= setup_git_directory_gently(&nongit
);
452 prepare_repo_settings(the_repository
);
453 the_repository
->settings
.command_requires_full_index
= 0;
458 * Treat git diff with at least one path outside of the
459 * repo the same as if the command would have been executed
460 * outside of a git repository. In this case it behaves
461 * the same way as "git diff --no-index <a> <b>", which acts
462 * as a colourful "diff" replacement.
464 if (nongit
|| ((argc
== i
+ 2) &&
465 (!path_inside_repo(prefix
, argv
[i
]) ||
466 !path_inside_repo(prefix
, argv
[i
+ 1]))))
467 no_index
= DIFF_NO_INDEX_IMPLICIT
;
470 init_diff_ui_defaults();
471 git_config(git_diff_ui_config
, NULL
);
472 prefix
= precompose_argv_prefix(argc
, argv
, prefix
);
474 repo_init_revisions(the_repository
, &rev
, prefix
);
476 /* Set up defaults that will apply to both no-index and regular diffs. */
477 init_diffstat_widths(&rev
.diffopt
);
478 rev
.diffopt
.flags
.allow_external
= 1;
479 rev
.diffopt
.flags
.allow_textconv
= 1;
481 /* If this is a no-index diff, just run it and exit there. */
483 exit(diff_no_index(&rev
, no_index
== DIFF_NO_INDEX_IMPLICIT
,
488 * Otherwise, we are doing the usual "git" diff; set up any
489 * further defaults that apply to regular diffs.
491 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
494 * Default to intent-to-add entries invisible in the
495 * index. This makes them show up as new files in diff-files
496 * and not at all in diff-cached.
498 rev
.diffopt
.ita_invisible_in_index
= 1;
501 die(_("Not a git repository"));
502 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
503 if (!rev
.diffopt
.output_format
) {
504 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
505 diff_setup_done(&rev
.diffopt
);
508 rev
.diffopt
.flags
.recursive
= 1;
509 rev
.diffopt
.rotate_to_strict
= 1;
511 setup_diff_pager(&rev
.diffopt
);
514 * Do we have --cached and not have a pending object, then
515 * default to HEAD by hand. Eek.
517 if (!rev
.pending
.nr
) {
519 for (i
= 1; i
< argc
; i
++) {
520 const char *arg
= argv
[i
];
521 if (!strcmp(arg
, "--"))
523 else if (!strcmp(arg
, "--cached") ||
524 !strcmp(arg
, "--staged")) {
525 add_head_to_pending(&rev
);
526 if (!rev
.pending
.nr
) {
528 tree
= lookup_tree(the_repository
,
529 the_repository
->hash_algo
->empty_tree
);
530 add_pending_object(&rev
, &tree
->object
, "HEAD");
537 symdiff_prepare(&rev
, &sdiff
);
538 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
539 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
540 struct object
*obj
= entry
->item
;
541 const char *name
= entry
->name
;
542 int flags
= (obj
->flags
& UNINTERESTING
);
544 obj
= parse_object(the_repository
, &obj
->oid
);
545 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
547 die(_("invalid object '%s' given."), name
);
548 if (obj
->type
== OBJ_COMMIT
)
549 obj
= &repo_get_commit_tree(the_repository
,
550 ((struct commit
*)obj
))->object
;
552 if (obj
->type
== OBJ_TREE
) {
553 if (sdiff
.skip
&& bitmap_get(sdiff
.skip
, i
))
556 add_object_array(obj
, name
, &ent
);
557 if (first_non_parent
< 0 &&
558 (i
>= rev
.cmdline
.nr
|| /* HEAD by hand. */
559 rev
.cmdline
.rev
[i
].whence
!= REV_CMD_PARENTS_ONLY
))
560 first_non_parent
= ent
.nr
- 1;
561 } else if (obj
->type
== OBJ_BLOB
) {
563 die(_("more than two blobs given: '%s'"), name
);
568 die(_("unhandled object '%s' given."), name
);
571 if (rev
.prune_data
.nr
)
572 paths
+= rev
.prune_data
.nr
;
575 * Now, do the arguments look reasonable?
580 builtin_diff_files(&rev
, argc
, argv
);
584 usage(builtin_diff_usage
);
585 builtin_diff_b_f(&rev
, argc
, argv
, blob
);
589 usage(builtin_diff_usage
);
590 builtin_diff_blobs(&rev
, argc
, argv
, blob
);
593 usage(builtin_diff_usage
);
597 usage(builtin_diff_usage
);
598 else if (ent
.nr
== 1)
599 builtin_diff_index(&rev
, argc
, argv
);
600 else if (ent
.nr
== 2) {
602 warning(_("%s...%s: multiple merge bases, using %s"),
603 sdiff
.left
, sdiff
.right
, sdiff
.base
);
604 builtin_diff_tree(&rev
, argc
, argv
,
605 &ent
.objects
[0], &ent
.objects
[1]);
607 builtin_diff_combined(&rev
, argc
, argv
,
610 result
= diff_result_code(&rev
.diffopt
);
611 if (1 < rev
.diffopt
.skip_stat_unmatch
)
612 refresh_index_quietly();
613 release_revisions(&rev
);
614 object_array_clear(&ent
);