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 int 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
);
115 static int builtin_diff_blobs(struct rev_info
*revs
,
116 int argc
, const char **argv UNUSED
,
117 struct object_array_entry
**blob
)
119 const unsigned mode
= canon_mode(S_IFREG
| 0644);
122 usage(builtin_diff_usage
);
124 if (blob
[0]->mode
== S_IFINVALID
)
125 blob
[0]->mode
= mode
;
127 if (blob
[1]->mode
== S_IFINVALID
)
128 blob
[1]->mode
= mode
;
130 stuff_change(&revs
->diffopt
,
131 blob
[0]->mode
, blob
[1]->mode
,
132 &blob
[0]->item
->oid
, &blob
[1]->item
->oid
,
134 blob_path(blob
[0]), blob_path(blob
[1]));
135 diffcore_std(&revs
->diffopt
);
136 diff_flush(&revs
->diffopt
);
140 static int builtin_diff_index(struct rev_info
*revs
,
141 int argc
, const char **argv
)
143 unsigned int option
= 0;
145 const char *arg
= argv
[1];
146 if (!strcmp(arg
, "--cached") || !strcmp(arg
, "--staged"))
147 option
|= DIFF_INDEX_CACHED
;
148 else if (!strcmp(arg
, "--merge-base"))
149 option
|= DIFF_INDEX_MERGE_BASE
;
151 usage(builtin_diff_usage
);
155 * Make sure there is one revision (i.e. pending object),
156 * and there is no revision filtering parameters.
158 if (revs
->pending
.nr
!= 1 ||
159 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
161 usage(builtin_diff_usage
);
162 if (!(option
& DIFF_INDEX_CACHED
)) {
164 if (repo_read_index_preload(the_repository
,
165 &revs
->diffopt
.pathspec
, 0) < 0) {
166 perror("repo_read_index_preload");
169 } else if (repo_read_index(the_repository
) < 0) {
170 perror("repo_read_cache");
173 return run_diff_index(revs
, option
);
176 static int builtin_diff_tree(struct rev_info
*revs
,
177 int argc
, const char **argv
,
178 struct object_array_entry
*ent0
,
179 struct object_array_entry
*ent1
)
181 const struct object_id
*(oid
[2]);
182 struct object_id mb_oid
;
186 const char *arg
= argv
[1];
187 if (!strcmp(arg
, "--merge-base"))
190 usage(builtin_diff_usage
);
195 diff_get_merge_base(revs
, &mb_oid
);
197 oid
[1] = &revs
->pending
.objects
[1].item
->oid
;
202 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
205 if (ent1
->item
->flags
& UNINTERESTING
)
207 oid
[swap
] = &ent0
->item
->oid
;
208 oid
[1 - swap
] = &ent1
->item
->oid
;
210 diff_tree_oid(oid
[0], oid
[1], "", &revs
->diffopt
);
211 log_tree_diff_flush(revs
);
215 static int builtin_diff_combined(struct rev_info
*revs
,
216 int argc
, const char **argv UNUSED
,
217 struct object_array_entry
*ent
,
218 int ents
, int first_non_parent
)
220 struct oid_array parents
= OID_ARRAY_INIT
;
224 usage(builtin_diff_usage
);
226 if (first_non_parent
< 0)
227 die(_("no merge given, only parents."));
228 if (first_non_parent
>= ents
)
229 BUG("first_non_parent out of range: %d", first_non_parent
);
231 diff_merges_set_dense_combined_if_unset(revs
);
233 for (i
= 0; i
< ents
; i
++) {
234 if (i
!= first_non_parent
)
235 oid_array_append(&parents
, &ent
[i
].item
->oid
);
237 diff_tree_combined(&ent
[first_non_parent
].item
->oid
, &parents
, revs
);
238 oid_array_clear(&parents
);
242 static void refresh_index_quietly(void)
244 struct lock_file lock_file
= LOCK_INIT
;
247 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
250 discard_index(&the_index
);
251 repo_read_index(the_repository
);
252 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
,
254 repo_update_index_if_able(the_repository
, &lock_file
);
257 static int builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
259 unsigned int options
= 0;
261 while (1 < argc
&& argv
[1][0] == '-') {
262 if (!strcmp(argv
[1], "--base"))
264 else if (!strcmp(argv
[1], "--ours"))
266 else if (!strcmp(argv
[1], "--theirs"))
268 else if (!strcmp(argv
[1], "-q"))
269 options
|= DIFF_SILENT_ON_REMOVED
;
270 else if (!strcmp(argv
[1], "-h"))
271 usage(builtin_diff_usage
);
273 return error(_("invalid option: %s"), argv
[1]);
278 * "diff --base" should not combine merges because it was not
279 * asked to. "diff -c" should not densify (if the user wants
280 * dense one, --cc can be explicitly asked for, or just rely
283 if (revs
->max_count
== -1 &&
284 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
285 diff_merges_set_dense_combined_if_unset(revs
);
288 if (repo_read_index_preload(the_repository
, &revs
->diffopt
.pathspec
,
290 perror("repo_read_index_preload");
293 return run_diff_files(revs
, options
);
299 const char *base
, *left
, *right
;
303 * Check for symmetric-difference arguments, and if present, arrange
304 * everything we need to know to handle them correctly. As a bonus,
305 * weed out all bogus range-based revision specifications, e.g.,
306 * "git diff A..B C..D" or "git diff A..B C" get rejected.
308 * For an actual symmetric diff, *symdiff is set this way:
310 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
311 * indices that the caller should ignore (extra merge bases, of
312 * which there might be many, and A in A...B). Note that the
313 * chosen merge base and right side are NOT marked.
314 * - warn is set if there are multiple merge bases.
315 * - base, left, and right point to the names to use in a
316 * warning about multiple merge bases.
318 * If there is no symmetric diff argument, sym->skip is NULL and
319 * sym->warn is cleared. The remaining fields are not set.
321 static void symdiff_prepare(struct rev_info
*rev
, struct symdiff
*sym
)
323 int i
, is_symdiff
= 0, basecount
= 0, othercount
= 0;
324 int lpos
= -1, rpos
= -1, basepos
= -1;
325 struct bitmap
*map
= NULL
;
328 * Use the whence fields to find merge bases and left and
329 * right parts of symmetric difference, so that we do not
330 * depend on the order that revisions are parsed. If there
331 * are any revs that aren't from these sources, we have a
332 * "git diff C A...B" or "git diff A...B C" case. Or we
333 * could even get "git diff A...B C...E", for instance.
335 * If we don't have just one merge base, we pick one
338 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
339 * so we must check for SYMMETRIC_LEFT too. The two arrays
340 * rev->pending.objects and rev->cmdline.rev are parallel.
342 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
343 struct object
*obj
= rev
->pending
.objects
[i
].item
;
344 switch (rev
->cmdline
.rev
[i
].whence
) {
345 case REV_CMD_MERGE_BASE
:
349 break; /* do mark all bases */
352 usage(builtin_diff_usage
);
354 if (obj
->flags
& SYMMETRIC_LEFT
) {
356 break; /* do mark A */
361 usage(builtin_diff_usage
);
363 continue; /* don't mark B */
364 case REV_CMD_PARENTS_ONLY
:
376 * Forbid any additional revs for both A...B and A..B.
378 if (lpos
>= 0 && othercount
> 0)
379 usage(builtin_diff_usage
);
388 sym
->left
= rev
->pending
.objects
[lpos
].name
;
389 sym
->right
= rev
->pending
.objects
[rpos
].name
;
391 die(_("%s...%s: no merge base"), sym
->left
, sym
->right
);
392 sym
->base
= rev
->pending
.objects
[basepos
].name
;
393 bitmap_unset(map
, basepos
); /* unmark the base we want */
394 sym
->warn
= basecount
> 1;
398 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
402 struct object_array ent
= OBJECT_ARRAY_INIT
;
403 int first_non_parent
= -1;
404 int blobs
= 0, paths
= 0;
405 struct object_array_entry
*blob
[2];
406 int nongit
= 0, no_index
= 0;
408 struct symdiff sdiff
;
411 * We could get N tree-ish in the rev.pending_objects list.
412 * Also there could be M blobs there, and P pathspecs. --cached may
416 * cache vs files (diff-files)
418 * N=0, M=0, --cached:
419 * HEAD vs cache (diff-index --cached)
422 * compare two random blobs. P must be zero.
425 * compare a blob with a working tree file.
428 * tree vs files (diff-index)
430 * N=1, M=0, --cached:
431 * tree vs cache (diff-index --cached)
434 * tree vs tree (diff-tree)
437 * compare two filesystem entities (aka --no-index).
439 * Other cases are errors.
442 /* Were we asked to do --no-index explicitly? */
443 for (i
= 1; i
< argc
; i
++) {
444 if (!strcmp(argv
[i
], "--")) {
448 if (!strcmp(argv
[i
], "--no-index"))
449 no_index
= DIFF_NO_INDEX_EXPLICIT
;
450 if (argv
[i
][0] != '-')
454 prefix
= setup_git_directory_gently(&nongit
);
457 prepare_repo_settings(the_repository
);
458 the_repository
->settings
.command_requires_full_index
= 0;
463 * Treat git diff with at least one path outside of the
464 * repo the same as if the command would have been executed
465 * outside of a git repository. In this case it behaves
466 * the same way as "git diff --no-index <a> <b>", which acts
467 * as a colourful "diff" replacement.
469 if (nongit
|| ((argc
== i
+ 2) &&
470 (!path_inside_repo(prefix
, argv
[i
]) ||
471 !path_inside_repo(prefix
, argv
[i
+ 1]))))
472 no_index
= DIFF_NO_INDEX_IMPLICIT
;
475 init_diff_ui_defaults();
476 git_config(git_diff_ui_config
, NULL
);
477 prefix
= precompose_argv_prefix(argc
, argv
, prefix
);
479 repo_init_revisions(the_repository
, &rev
, prefix
);
481 /* Set up defaults that will apply to both no-index and regular diffs. */
482 rev
.diffopt
.stat_width
= -1;
483 rev
.diffopt
.stat_graph_width
= -1;
484 rev
.diffopt
.flags
.allow_external
= 1;
485 rev
.diffopt
.flags
.allow_textconv
= 1;
487 /* If this is a no-index diff, just run it and exit there. */
489 exit(diff_no_index(&rev
, no_index
== DIFF_NO_INDEX_IMPLICIT
,
494 * Otherwise, we are doing the usual "git" diff; set up any
495 * further defaults that apply to regular diffs.
497 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
500 * Default to intent-to-add entries invisible in the
501 * index. This makes them show up as new files in diff-files
502 * and not at all in diff-cached.
504 rev
.diffopt
.ita_invisible_in_index
= 1;
507 die(_("Not a git repository"));
508 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
509 if (!rev
.diffopt
.output_format
) {
510 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
511 diff_setup_done(&rev
.diffopt
);
514 rev
.diffopt
.flags
.recursive
= 1;
515 rev
.diffopt
.rotate_to_strict
= 1;
517 setup_diff_pager(&rev
.diffopt
);
520 * Do we have --cached and not have a pending object, then
521 * default to HEAD by hand. Eek.
523 if (!rev
.pending
.nr
) {
525 for (i
= 1; i
< argc
; i
++) {
526 const char *arg
= argv
[i
];
527 if (!strcmp(arg
, "--"))
529 else if (!strcmp(arg
, "--cached") ||
530 !strcmp(arg
, "--staged")) {
531 add_head_to_pending(&rev
);
532 if (!rev
.pending
.nr
) {
534 tree
= lookup_tree(the_repository
,
535 the_repository
->hash_algo
->empty_tree
);
536 add_pending_object(&rev
, &tree
->object
, "HEAD");
543 symdiff_prepare(&rev
, &sdiff
);
544 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
545 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
546 struct object
*obj
= entry
->item
;
547 const char *name
= entry
->name
;
548 int flags
= (obj
->flags
& UNINTERESTING
);
550 obj
= parse_object(the_repository
, &obj
->oid
);
551 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
553 die(_("invalid object '%s' given."), name
);
554 if (obj
->type
== OBJ_COMMIT
)
555 obj
= &repo_get_commit_tree(the_repository
,
556 ((struct commit
*)obj
))->object
;
558 if (obj
->type
== OBJ_TREE
) {
559 if (sdiff
.skip
&& bitmap_get(sdiff
.skip
, i
))
562 add_object_array(obj
, name
, &ent
);
563 if (first_non_parent
< 0 &&
564 (i
>= rev
.cmdline
.nr
|| /* HEAD by hand. */
565 rev
.cmdline
.rev
[i
].whence
!= REV_CMD_PARENTS_ONLY
))
566 first_non_parent
= ent
.nr
- 1;
567 } else if (obj
->type
== OBJ_BLOB
) {
569 die(_("more than two blobs given: '%s'"), name
);
574 die(_("unhandled object '%s' given."), name
);
577 if (rev
.prune_data
.nr
)
578 paths
+= rev
.prune_data
.nr
;
581 * Now, do the arguments look reasonable?
586 result
= builtin_diff_files(&rev
, argc
, argv
);
590 usage(builtin_diff_usage
);
591 result
= builtin_diff_b_f(&rev
, argc
, argv
, blob
);
595 usage(builtin_diff_usage
);
596 result
= builtin_diff_blobs(&rev
, argc
, argv
, blob
);
599 usage(builtin_diff_usage
);
603 usage(builtin_diff_usage
);
604 else if (ent
.nr
== 1)
605 result
= builtin_diff_index(&rev
, argc
, argv
);
606 else if (ent
.nr
== 2) {
608 warning(_("%s...%s: multiple merge bases, using %s"),
609 sdiff
.left
, sdiff
.right
, sdiff
.base
);
610 result
= builtin_diff_tree(&rev
, argc
, argv
,
611 &ent
.objects
[0], &ent
.objects
[1]);
613 result
= builtin_diff_combined(&rev
, argc
, argv
,
616 result
= diff_result_code(&rev
.diffopt
, result
);
617 if (1 < rev
.diffopt
.skip_stat_unmatch
)
618 refresh_index_quietly();
619 release_revisions(&rev
);
620 object_array_clear(&ent
);