4 * Copyright (c) 2006 Junio C Hamano
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
20 #include "submodule.h"
21 #include "oid-array.h"
23 #define DIFF_NO_INDEX_EXPLICIT 1
24 #define DIFF_NO_INDEX_IMPLICIT 2
26 static const char builtin_diff_usage
[] =
27 "git diff [<options>] [<commit>] [--] [<path>...]\n"
28 " or: git diff [<options>] --cached [<commit>] [--] [<path>...]\n"
29 " or: git diff [<options>] <commit> [<commit>...] <commit> [--] [<path>...]\n"
30 " or: git diff [<options>] <commit>...<commit>] [--] [<path>...]\n"
31 " or: git diff [<options>] <blob> <blob>]\n"
32 " or: git diff [<options>] --no-index [--] <path> <path>]\n"
33 COMMON_DIFF_OPTIONS_HELP
;
35 static const char *blob_path(struct object_array_entry
*entry
)
37 return entry
->path
? entry
->path
: entry
->name
;
40 static void stuff_change(struct diff_options
*opt
,
41 unsigned old_mode
, unsigned new_mode
,
42 const struct object_id
*old_oid
,
43 const struct object_id
*new_oid
,
49 struct diff_filespec
*one
, *two
;
51 if (!is_null_oid(old_oid
) && !is_null_oid(new_oid
) &&
52 oideq(old_oid
, new_oid
) && (old_mode
== new_mode
))
55 if (opt
->flags
.reverse_diff
) {
56 SWAP(old_mode
, new_mode
);
57 SWAP(old_oid
, new_oid
);
58 SWAP(old_path
, new_path
);
62 (strncmp(old_path
, opt
->prefix
, opt
->prefix_length
) ||
63 strncmp(new_path
, opt
->prefix
, opt
->prefix_length
)))
66 one
= alloc_filespec(old_path
);
67 two
= alloc_filespec(new_path
);
68 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
69 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
71 diff_queue(&diff_queued_diff
, one
, two
);
74 static int builtin_diff_b_f(struct rev_info
*revs
,
75 int argc
, const char **argv
,
76 struct object_array_entry
**blob
)
78 /* Blob vs file in the working tree*/
83 usage(builtin_diff_usage
);
85 GUARD_PATHSPEC(&revs
->prune_data
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
86 path
= revs
->prune_data
.items
[0].match
;
89 die_errno(_("failed to stat '%s'"), path
);
90 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
91 die(_("'%s': not a regular file or symlink"), path
);
93 diff_set_mnemonic_prefix(&revs
->diffopt
, "o/", "w/");
95 if (blob
[0]->mode
== S_IFINVALID
)
96 blob
[0]->mode
= canon_mode(st
.st_mode
);
98 stuff_change(&revs
->diffopt
,
99 blob
[0]->mode
, canon_mode(st
.st_mode
),
100 &blob
[0]->item
->oid
, &null_oid
,
102 blob
[0]->path
? blob
[0]->path
: path
,
104 diffcore_std(&revs
->diffopt
);
105 diff_flush(&revs
->diffopt
);
109 static int builtin_diff_blobs(struct rev_info
*revs
,
110 int argc
, const char **argv
,
111 struct object_array_entry
**blob
)
113 const unsigned mode
= canon_mode(S_IFREG
| 0644);
116 usage(builtin_diff_usage
);
118 if (blob
[0]->mode
== S_IFINVALID
)
119 blob
[0]->mode
= mode
;
121 if (blob
[1]->mode
== S_IFINVALID
)
122 blob
[1]->mode
= mode
;
124 stuff_change(&revs
->diffopt
,
125 blob
[0]->mode
, blob
[1]->mode
,
126 &blob
[0]->item
->oid
, &blob
[1]->item
->oid
,
128 blob_path(blob
[0]), blob_path(blob
[1]));
129 diffcore_std(&revs
->diffopt
);
130 diff_flush(&revs
->diffopt
);
134 static int builtin_diff_index(struct rev_info
*revs
,
135 int argc
, const char **argv
)
139 const char *arg
= argv
[1];
140 if (!strcmp(arg
, "--cached") || !strcmp(arg
, "--staged"))
143 usage(builtin_diff_usage
);
147 * Make sure there is one revision (i.e. pending object),
148 * and there is no revision filtering parameters.
150 if (revs
->pending
.nr
!= 1 ||
151 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
153 usage(builtin_diff_usage
);
156 if (read_cache_preload(&revs
->diffopt
.pathspec
) < 0) {
157 perror("read_cache_preload");
160 } else if (read_cache() < 0) {
161 perror("read_cache");
164 return run_diff_index(revs
, cached
);
167 static int builtin_diff_tree(struct rev_info
*revs
,
168 int argc
, const char **argv
,
169 struct object_array_entry
*ent0
,
170 struct object_array_entry
*ent1
)
172 const struct object_id
*(oid
[2]);
176 usage(builtin_diff_usage
);
179 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
182 if (ent1
->item
->flags
& UNINTERESTING
)
184 oid
[swap
] = &ent0
->item
->oid
;
185 oid
[1 - swap
] = &ent1
->item
->oid
;
186 diff_tree_oid(oid
[0], oid
[1], "", &revs
->diffopt
);
187 log_tree_diff_flush(revs
);
191 static int builtin_diff_combined(struct rev_info
*revs
,
192 int argc
, const char **argv
,
193 struct object_array_entry
*ent
,
196 struct oid_array parents
= OID_ARRAY_INIT
;
200 usage(builtin_diff_usage
);
202 if (!revs
->dense_combined_merges
&& !revs
->combine_merges
)
203 revs
->dense_combined_merges
= revs
->combine_merges
= 1;
204 for (i
= 1; i
< ents
; i
++)
205 oid_array_append(&parents
, &ent
[i
].item
->oid
);
206 diff_tree_combined(&ent
[0].item
->oid
, &parents
,
207 revs
->dense_combined_merges
, revs
);
208 oid_array_clear(&parents
);
212 static void refresh_index_quietly(void)
214 struct lock_file lock_file
= LOCK_INIT
;
217 fd
= hold_locked_index(&lock_file
, 0);
222 refresh_cache(REFRESH_QUIET
|REFRESH_UNMERGED
);
223 repo_update_index_if_able(the_repository
, &lock_file
);
226 static int builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
228 unsigned int options
= 0;
230 while (1 < argc
&& argv
[1][0] == '-') {
231 if (!strcmp(argv
[1], "--base"))
233 else if (!strcmp(argv
[1], "--ours"))
235 else if (!strcmp(argv
[1], "--theirs"))
237 else if (!strcmp(argv
[1], "-q"))
238 options
|= DIFF_SILENT_ON_REMOVED
;
239 else if (!strcmp(argv
[1], "-h"))
240 usage(builtin_diff_usage
);
242 return error(_("invalid option: %s"), argv
[1]);
247 * "diff --base" should not combine merges because it was not
248 * asked to. "diff -c" should not densify (if the user wants
249 * dense one, --cc can be explicitly asked for, or just rely
252 if (revs
->max_count
== -1 && !revs
->combine_merges
&&
253 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
254 revs
->combine_merges
= revs
->dense_combined_merges
= 1;
257 if (read_cache_preload(&revs
->diffopt
.pathspec
) < 0) {
258 perror("read_cache_preload");
261 return run_diff_files(revs
, options
);
267 const char *base
, *left
, *right
;
271 * Check for symmetric-difference arguments, and if present, arrange
272 * everything we need to know to handle them correctly. As a bonus,
273 * weed out all bogus range-based revision specifications, e.g.,
274 * "git diff A..B C..D" or "git diff A..B C" get rejected.
276 * For an actual symmetric diff, *symdiff is set this way:
278 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
279 * indices that the caller should ignore (extra merge bases, of
280 * which there might be many, and A in A...B). Note that the
281 * chosen merge base and right side are NOT marked.
282 * - warn is set if there are multiple merge bases.
283 * - base, left, and right point to the names to use in a
284 * warning about multiple merge bases.
286 * If there is no symmetric diff argument, sym->skip is NULL and
287 * sym->warn is cleared. The remaining fields are not set.
289 static void symdiff_prepare(struct rev_info
*rev
, struct symdiff
*sym
)
291 int i
, is_symdiff
= 0, basecount
= 0, othercount
= 0;
292 int lpos
= -1, rpos
= -1, basepos
= -1;
293 struct bitmap
*map
= NULL
;
296 * Use the whence fields to find merge bases and left and
297 * right parts of symmetric difference, so that we do not
298 * depend on the order that revisions are parsed. If there
299 * are any revs that aren't from these sources, we have a
300 * "git diff C A...B" or "git diff A...B C" case. Or we
301 * could even get "git diff A...B C...E", for instance.
303 * If we don't have just one merge base, we pick one
306 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
307 * so we must check for SYMMETRIC_LEFT too. The two arrays
308 * rev->pending.objects and rev->cmdline.rev are parallel.
310 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
311 struct object
*obj
= rev
->pending
.objects
[i
].item
;
312 switch (rev
->cmdline
.rev
[i
].whence
) {
313 case REV_CMD_MERGE_BASE
:
317 break; /* do mark all bases */
320 usage(builtin_diff_usage
);
322 if (obj
->flags
& SYMMETRIC_LEFT
) {
324 break; /* do mark A */
329 usage(builtin_diff_usage
);
331 continue; /* don't mark B */
332 case REV_CMD_PARENTS_ONLY
:
344 * Forbid any additional revs for both A...B and A..B.
346 if (lpos
>= 0 && othercount
> 0)
347 usage(builtin_diff_usage
);
356 sym
->left
= rev
->pending
.objects
[lpos
].name
;
357 sym
->right
= rev
->pending
.objects
[rpos
].name
;
359 die(_("%s...%s: no merge base"), sym
->left
, sym
->right
);
360 sym
->base
= rev
->pending
.objects
[basepos
].name
;
361 bitmap_unset(map
, basepos
); /* unmark the base we want */
362 sym
->warn
= basecount
> 1;
366 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
370 struct object_array ent
= OBJECT_ARRAY_INIT
;
371 int blobs
= 0, paths
= 0;
372 struct object_array_entry
*blob
[2];
373 int nongit
= 0, no_index
= 0;
375 struct symdiff sdiff
;
378 * We could get N tree-ish in the rev.pending_objects list.
379 * Also there could be M blobs there, and P pathspecs. --cached may
383 * cache vs files (diff-files)
385 * N=0, M=0, --cached:
386 * HEAD vs cache (diff-index --cached)
389 * compare two random blobs. P must be zero.
392 * compare a blob with a working tree file.
395 * tree vs files (diff-index)
397 * N=1, M=0, --cached:
398 * tree vs cache (diff-index --cached)
401 * tree vs tree (diff-tree)
404 * compare two filesystem entities (aka --no-index).
406 * Other cases are errors.
409 /* Were we asked to do --no-index explicitly? */
410 for (i
= 1; i
< argc
; i
++) {
411 if (!strcmp(argv
[i
], "--")) {
415 if (!strcmp(argv
[i
], "--no-index"))
416 no_index
= DIFF_NO_INDEX_EXPLICIT
;
417 if (argv
[i
][0] != '-')
421 prefix
= setup_git_directory_gently(&nongit
);
425 * Treat git diff with at least one path outside of the
426 * repo the same as if the command would have been executed
427 * outside of a git repository. In this case it behaves
428 * the same way as "git diff --no-index <a> <b>", which acts
429 * as a colourful "diff" replacement.
431 if (nongit
|| ((argc
== i
+ 2) &&
432 (!path_inside_repo(prefix
, argv
[i
]) ||
433 !path_inside_repo(prefix
, argv
[i
+ 1]))))
434 no_index
= DIFF_NO_INDEX_IMPLICIT
;
437 init_diff_ui_defaults();
438 git_config(git_diff_ui_config
, NULL
);
439 precompose_argv(argc
, argv
);
441 repo_init_revisions(the_repository
, &rev
, prefix
);
443 /* Set up defaults that will apply to both no-index and regular diffs. */
444 rev
.diffopt
.stat_width
= -1;
445 rev
.diffopt
.stat_graph_width
= -1;
446 rev
.diffopt
.flags
.allow_external
= 1;
447 rev
.diffopt
.flags
.allow_textconv
= 1;
449 /* If this is a no-index diff, just run it and exit there. */
451 exit(diff_no_index(&rev
, no_index
== DIFF_NO_INDEX_IMPLICIT
,
456 * Otherwise, we are doing the usual "git" diff; set up any
457 * further defaults that apply to regular diffs.
459 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
462 * Default to intent-to-add entries invisible in the
463 * index. This makes them show up as new files in diff-files
464 * and not at all in diff-cached.
466 rev
.diffopt
.ita_invisible_in_index
= 1;
469 die(_("Not a git repository"));
470 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
471 if (!rev
.diffopt
.output_format
) {
472 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
473 diff_setup_done(&rev
.diffopt
);
476 rev
.diffopt
.flags
.recursive
= 1;
478 setup_diff_pager(&rev
.diffopt
);
481 * Do we have --cached and not have a pending object, then
482 * default to HEAD by hand. Eek.
484 if (!rev
.pending
.nr
) {
486 for (i
= 1; i
< argc
; i
++) {
487 const char *arg
= argv
[i
];
488 if (!strcmp(arg
, "--"))
490 else if (!strcmp(arg
, "--cached") ||
491 !strcmp(arg
, "--staged")) {
492 add_head_to_pending(&rev
);
493 if (!rev
.pending
.nr
) {
495 tree
= lookup_tree(the_repository
,
496 the_repository
->hash_algo
->empty_tree
);
497 add_pending_object(&rev
, &tree
->object
, "HEAD");
504 symdiff_prepare(&rev
, &sdiff
);
505 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
506 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
507 struct object
*obj
= entry
->item
;
508 const char *name
= entry
->name
;
509 int flags
= (obj
->flags
& UNINTERESTING
);
511 obj
= parse_object(the_repository
, &obj
->oid
);
512 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
514 die(_("invalid object '%s' given."), name
);
515 if (obj
->type
== OBJ_COMMIT
)
516 obj
= &get_commit_tree(((struct commit
*)obj
))->object
;
518 if (obj
->type
== OBJ_TREE
) {
519 if (sdiff
.skip
&& bitmap_get(sdiff
.skip
, i
))
522 add_object_array(obj
, name
, &ent
);
523 } else if (obj
->type
== OBJ_BLOB
) {
525 die(_("more than two blobs given: '%s'"), name
);
530 die(_("unhandled object '%s' given."), name
);
533 if (rev
.prune_data
.nr
)
534 paths
+= rev
.prune_data
.nr
;
537 * Now, do the arguments look reasonable?
542 result
= builtin_diff_files(&rev
, argc
, argv
);
546 usage(builtin_diff_usage
);
547 result
= builtin_diff_b_f(&rev
, argc
, argv
, blob
);
551 usage(builtin_diff_usage
);
552 result
= builtin_diff_blobs(&rev
, argc
, argv
, blob
);
555 usage(builtin_diff_usage
);
559 usage(builtin_diff_usage
);
560 else if (ent
.nr
== 1)
561 result
= builtin_diff_index(&rev
, argc
, argv
);
562 else if (ent
.nr
== 2) {
564 warning(_("%s...%s: multiple merge bases, using %s"),
565 sdiff
.left
, sdiff
.right
, sdiff
.base
);
566 result
= builtin_diff_tree(&rev
, argc
, argv
,
567 &ent
.objects
[0], &ent
.objects
[1]);
569 result
= builtin_diff_combined(&rev
, argc
, argv
,
570 ent
.objects
, ent
.nr
);
571 result
= diff_result_code(&rev
.diffopt
, result
);
572 if (1 < rev
.diffopt
.skip_stat_unmatch
)
573 refresh_index_quietly();