4 * Copyright (c) 2006 Junio C Hamano
6 #define USE_THE_INDEX_VARIABLE
16 #include "diff-merges.h"
21 #include "submodule.h"
22 #include "oid-array.h"
24 #define DIFF_NO_INDEX_EXPLICIT 1
25 #define DIFF_NO_INDEX_IMPLICIT 2
27 static const char builtin_diff_usage
[] =
28 "git diff [<options>] [<commit>] [--] [<path>...]\n"
29 " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
30 " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
31 " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
32 " or: git diff [<options>] <blob> <blob>\n"
33 " or: git diff [<options>] --no-index [--] <path> <path>"
35 COMMON_DIFF_OPTIONS_HELP
;
37 static const char *blob_path(struct object_array_entry
*entry
)
39 return entry
->path
? entry
->path
: entry
->name
;
42 static void stuff_change(struct diff_options
*opt
,
43 unsigned old_mode
, unsigned new_mode
,
44 const struct object_id
*old_oid
,
45 const struct object_id
*new_oid
,
51 struct diff_filespec
*one
, *two
;
53 if (!is_null_oid(old_oid
) && !is_null_oid(new_oid
) &&
54 oideq(old_oid
, new_oid
) && (old_mode
== new_mode
))
57 if (opt
->flags
.reverse_diff
) {
58 SWAP(old_mode
, new_mode
);
59 SWAP(old_oid
, new_oid
);
60 SWAP(old_path
, new_path
);
64 (strncmp(old_path
, opt
->prefix
, opt
->prefix_length
) ||
65 strncmp(new_path
, opt
->prefix
, opt
->prefix_length
)))
68 one
= alloc_filespec(old_path
);
69 two
= alloc_filespec(new_path
);
70 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
71 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
73 diff_queue(&diff_queued_diff
, one
, two
);
76 static int builtin_diff_b_f(struct rev_info
*revs
,
77 int argc
, const char **argv
,
78 struct object_array_entry
**blob
)
80 /* Blob vs file in the working tree*/
85 usage(builtin_diff_usage
);
87 GUARD_PATHSPEC(&revs
->prune_data
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
88 path
= revs
->prune_data
.items
[0].match
;
91 die_errno(_("failed to stat '%s'"), path
);
92 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
93 die(_("'%s': not a regular file or symlink"), path
);
95 diff_set_mnemonic_prefix(&revs
->diffopt
, "o/", "w/");
97 if (blob
[0]->mode
== S_IFINVALID
)
98 blob
[0]->mode
= canon_mode(st
.st_mode
);
100 stuff_change(&revs
->diffopt
,
101 blob
[0]->mode
, canon_mode(st
.st_mode
),
102 &blob
[0]->item
->oid
, null_oid(),
104 blob
[0]->path
? blob
[0]->path
: path
,
106 diffcore_std(&revs
->diffopt
);
107 diff_flush(&revs
->diffopt
);
111 static int builtin_diff_blobs(struct rev_info
*revs
,
112 int argc
, const char **argv
,
113 struct object_array_entry
**blob
)
115 const unsigned mode
= canon_mode(S_IFREG
| 0644);
118 usage(builtin_diff_usage
);
120 if (blob
[0]->mode
== S_IFINVALID
)
121 blob
[0]->mode
= mode
;
123 if (blob
[1]->mode
== S_IFINVALID
)
124 blob
[1]->mode
= mode
;
126 stuff_change(&revs
->diffopt
,
127 blob
[0]->mode
, blob
[1]->mode
,
128 &blob
[0]->item
->oid
, &blob
[1]->item
->oid
,
130 blob_path(blob
[0]), blob_path(blob
[1]));
131 diffcore_std(&revs
->diffopt
);
132 diff_flush(&revs
->diffopt
);
136 static int 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 perror("repo_read_index_preload");
165 } else if (repo_read_index(the_repository
) < 0) {
166 perror("repo_read_cache");
169 return run_diff_index(revs
, option
);
172 static int 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
);
211 static int builtin_diff_combined(struct rev_info
*revs
,
212 int argc
, const char **argv
,
213 struct object_array_entry
*ent
,
214 int ents
, int first_non_parent
)
216 struct oid_array parents
= OID_ARRAY_INIT
;
220 usage(builtin_diff_usage
);
222 if (first_non_parent
< 0)
223 die(_("no merge given, only parents."));
224 if (first_non_parent
>= ents
)
225 BUG("first_non_parent out of range: %d", first_non_parent
);
227 diff_merges_set_dense_combined_if_unset(revs
);
229 for (i
= 0; i
< ents
; i
++) {
230 if (i
!= first_non_parent
)
231 oid_array_append(&parents
, &ent
[i
].item
->oid
);
233 diff_tree_combined(&ent
[first_non_parent
].item
->oid
, &parents
, revs
);
234 oid_array_clear(&parents
);
238 static void refresh_index_quietly(void)
240 struct lock_file lock_file
= LOCK_INIT
;
243 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
246 discard_index(&the_index
);
247 repo_read_index(the_repository
);
248 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
,
250 repo_update_index_if_able(the_repository
, &lock_file
);
253 static int builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
255 unsigned int options
= 0;
257 while (1 < argc
&& argv
[1][0] == '-') {
258 if (!strcmp(argv
[1], "--base"))
260 else if (!strcmp(argv
[1], "--ours"))
262 else if (!strcmp(argv
[1], "--theirs"))
264 else if (!strcmp(argv
[1], "-q"))
265 options
|= DIFF_SILENT_ON_REMOVED
;
266 else if (!strcmp(argv
[1], "-h"))
267 usage(builtin_diff_usage
);
269 return error(_("invalid option: %s"), argv
[1]);
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 perror("repo_read_index_preload");
289 return run_diff_files(revs
, options
);
295 const char *base
, *left
, *right
;
299 * Check for symmetric-difference arguments, and if present, arrange
300 * everything we need to know to handle them correctly. As a bonus,
301 * weed out all bogus range-based revision specifications, e.g.,
302 * "git diff A..B C..D" or "git diff A..B C" get rejected.
304 * For an actual symmetric diff, *symdiff is set this way:
306 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
307 * indices that the caller should ignore (extra merge bases, of
308 * which there might be many, and A in A...B). Note that the
309 * chosen merge base and right side are NOT marked.
310 * - warn is set if there are multiple merge bases.
311 * - base, left, and right point to the names to use in a
312 * warning about multiple merge bases.
314 * If there is no symmetric diff argument, sym->skip is NULL and
315 * sym->warn is cleared. The remaining fields are not set.
317 static void symdiff_prepare(struct rev_info
*rev
, struct symdiff
*sym
)
319 int i
, is_symdiff
= 0, basecount
= 0, othercount
= 0;
320 int lpos
= -1, rpos
= -1, basepos
= -1;
321 struct bitmap
*map
= NULL
;
324 * Use the whence fields to find merge bases and left and
325 * right parts of symmetric difference, so that we do not
326 * depend on the order that revisions are parsed. If there
327 * are any revs that aren't from these sources, we have a
328 * "git diff C A...B" or "git diff A...B C" case. Or we
329 * could even get "git diff A...B C...E", for instance.
331 * If we don't have just one merge base, we pick one
334 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
335 * so we must check for SYMMETRIC_LEFT too. The two arrays
336 * rev->pending.objects and rev->cmdline.rev are parallel.
338 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
339 struct object
*obj
= rev
->pending
.objects
[i
].item
;
340 switch (rev
->cmdline
.rev
[i
].whence
) {
341 case REV_CMD_MERGE_BASE
:
345 break; /* do mark all bases */
348 usage(builtin_diff_usage
);
350 if (obj
->flags
& SYMMETRIC_LEFT
) {
352 break; /* do mark A */
357 usage(builtin_diff_usage
);
359 continue; /* don't mark B */
360 case REV_CMD_PARENTS_ONLY
:
372 * Forbid any additional revs for both A...B and A..B.
374 if (lpos
>= 0 && othercount
> 0)
375 usage(builtin_diff_usage
);
384 sym
->left
= rev
->pending
.objects
[lpos
].name
;
385 sym
->right
= rev
->pending
.objects
[rpos
].name
;
387 die(_("%s...%s: no merge base"), sym
->left
, sym
->right
);
388 sym
->base
= rev
->pending
.objects
[basepos
].name
;
389 bitmap_unset(map
, basepos
); /* unmark the base we want */
390 sym
->warn
= basecount
> 1;
394 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
398 struct object_array ent
= OBJECT_ARRAY_INIT
;
399 int first_non_parent
= -1;
400 int blobs
= 0, paths
= 0;
401 struct object_array_entry
*blob
[2];
402 int nongit
= 0, no_index
= 0;
404 struct symdiff sdiff
;
407 * We could get N tree-ish in the rev.pending_objects list.
408 * Also there could be M blobs there, and P pathspecs. --cached may
412 * cache vs files (diff-files)
414 * N=0, M=0, --cached:
415 * HEAD vs cache (diff-index --cached)
418 * compare two random blobs. P must be zero.
421 * compare a blob with a working tree file.
424 * tree vs files (diff-index)
426 * N=1, M=0, --cached:
427 * tree vs cache (diff-index --cached)
430 * tree vs tree (diff-tree)
433 * compare two filesystem entities (aka --no-index).
435 * Other cases are errors.
438 /* Were we asked to do --no-index explicitly? */
439 for (i
= 1; i
< argc
; i
++) {
440 if (!strcmp(argv
[i
], "--")) {
444 if (!strcmp(argv
[i
], "--no-index"))
445 no_index
= DIFF_NO_INDEX_EXPLICIT
;
446 if (argv
[i
][0] != '-')
450 prefix
= setup_git_directory_gently(&nongit
);
453 prepare_repo_settings(the_repository
);
454 the_repository
->settings
.command_requires_full_index
= 0;
459 * Treat git diff with at least one path outside of the
460 * repo the same as if the command would have been executed
461 * outside of a git repository. In this case it behaves
462 * the same way as "git diff --no-index <a> <b>", which acts
463 * as a colourful "diff" replacement.
465 if (nongit
|| ((argc
== i
+ 2) &&
466 (!path_inside_repo(prefix
, argv
[i
]) ||
467 !path_inside_repo(prefix
, argv
[i
+ 1]))))
468 no_index
= DIFF_NO_INDEX_IMPLICIT
;
471 init_diff_ui_defaults();
472 git_config(git_diff_ui_config
, NULL
);
473 prefix
= precompose_argv_prefix(argc
, argv
, prefix
);
475 repo_init_revisions(the_repository
, &rev
, prefix
);
477 /* Set up defaults that will apply to both no-index and regular diffs. */
478 rev
.diffopt
.stat_width
= -1;
479 rev
.diffopt
.stat_graph_width
= -1;
480 rev
.diffopt
.flags
.allow_external
= 1;
481 rev
.diffopt
.flags
.allow_textconv
= 1;
483 /* If this is a no-index diff, just run it and exit there. */
485 exit(diff_no_index(&rev
, no_index
== DIFF_NO_INDEX_IMPLICIT
,
490 * Otherwise, we are doing the usual "git" diff; set up any
491 * further defaults that apply to regular diffs.
493 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
496 * Default to intent-to-add entries invisible in the
497 * index. This makes them show up as new files in diff-files
498 * and not at all in diff-cached.
500 rev
.diffopt
.ita_invisible_in_index
= 1;
503 die(_("Not a git repository"));
504 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
505 if (!rev
.diffopt
.output_format
) {
506 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
507 diff_setup_done(&rev
.diffopt
);
510 rev
.diffopt
.flags
.recursive
= 1;
511 rev
.diffopt
.rotate_to_strict
= 1;
513 setup_diff_pager(&rev
.diffopt
);
516 * Do we have --cached and not have a pending object, then
517 * default to HEAD by hand. Eek.
519 if (!rev
.pending
.nr
) {
521 for (i
= 1; i
< argc
; i
++) {
522 const char *arg
= argv
[i
];
523 if (!strcmp(arg
, "--"))
525 else if (!strcmp(arg
, "--cached") ||
526 !strcmp(arg
, "--staged")) {
527 add_head_to_pending(&rev
);
528 if (!rev
.pending
.nr
) {
530 tree
= lookup_tree(the_repository
,
531 the_repository
->hash_algo
->empty_tree
);
532 add_pending_object(&rev
, &tree
->object
, "HEAD");
539 symdiff_prepare(&rev
, &sdiff
);
540 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
541 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
542 struct object
*obj
= entry
->item
;
543 const char *name
= entry
->name
;
544 int flags
= (obj
->flags
& UNINTERESTING
);
546 obj
= parse_object(the_repository
, &obj
->oid
);
547 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
549 die(_("invalid object '%s' given."), name
);
550 if (obj
->type
== OBJ_COMMIT
)
551 obj
= &get_commit_tree(((struct commit
*)obj
))->object
;
553 if (obj
->type
== OBJ_TREE
) {
554 if (sdiff
.skip
&& bitmap_get(sdiff
.skip
, i
))
557 add_object_array(obj
, name
, &ent
);
558 if (first_non_parent
< 0 &&
559 (i
>= rev
.cmdline
.nr
|| /* HEAD by hand. */
560 rev
.cmdline
.rev
[i
].whence
!= REV_CMD_PARENTS_ONLY
))
561 first_non_parent
= ent
.nr
- 1;
562 } else if (obj
->type
== OBJ_BLOB
) {
564 die(_("more than two blobs given: '%s'"), name
);
569 die(_("unhandled object '%s' given."), name
);
572 if (rev
.prune_data
.nr
)
573 paths
+= rev
.prune_data
.nr
;
576 * Now, do the arguments look reasonable?
581 result
= builtin_diff_files(&rev
, argc
, argv
);
585 usage(builtin_diff_usage
);
586 result
= builtin_diff_b_f(&rev
, argc
, argv
, blob
);
590 usage(builtin_diff_usage
);
591 result
= builtin_diff_blobs(&rev
, argc
, argv
, blob
);
594 usage(builtin_diff_usage
);
598 usage(builtin_diff_usage
);
599 else if (ent
.nr
== 1)
600 result
= builtin_diff_index(&rev
, argc
, argv
);
601 else if (ent
.nr
== 2) {
603 warning(_("%s...%s: multiple merge bases, using %s"),
604 sdiff
.left
, sdiff
.right
, sdiff
.base
);
605 result
= builtin_diff_tree(&rev
, argc
, argv
,
606 &ent
.objects
[0], &ent
.objects
[1]);
608 result
= builtin_diff_combined(&rev
, argc
, argv
,
611 result
= diff_result_code(&rev
.diffopt
, result
);
612 if (1 < rev
.diffopt
.skip_stat_unmatch
)
613 refresh_index_quietly();
614 release_revisions(&rev
);
615 object_array_clear(&ent
);