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"
27 #define DIFF_NO_INDEX_EXPLICIT 1
28 #define DIFF_NO_INDEX_IMPLICIT 2
30 static const char builtin_diff_usage
[] =
31 "git diff [<options>] [<commit>] [--] [<path>...]\n"
32 " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
33 " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
34 " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
35 " or: git diff [<options>] <blob> <blob>\n"
36 " or: git diff [<options>] --no-index [--] <path> <path>"
38 COMMON_DIFF_OPTIONS_HELP
;
40 static const char *blob_path(struct object_array_entry
*entry
)
42 return entry
->path
? entry
->path
: entry
->name
;
45 static void stuff_change(struct diff_options
*opt
,
46 unsigned old_mode
, unsigned new_mode
,
47 const struct object_id
*old_oid
,
48 const struct object_id
*new_oid
,
54 struct diff_filespec
*one
, *two
;
56 if (!is_null_oid(old_oid
) && !is_null_oid(new_oid
) &&
57 oideq(old_oid
, new_oid
) && (old_mode
== new_mode
))
60 if (opt
->flags
.reverse_diff
) {
61 SWAP(old_mode
, new_mode
);
62 SWAP(old_oid
, new_oid
);
63 SWAP(old_path
, new_path
);
67 (strncmp(old_path
, opt
->prefix
, opt
->prefix_length
) ||
68 strncmp(new_path
, opt
->prefix
, opt
->prefix_length
)))
71 one
= alloc_filespec(old_path
);
72 two
= alloc_filespec(new_path
);
73 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
74 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
76 diff_queue(&diff_queued_diff
, one
, two
);
79 static int builtin_diff_b_f(struct rev_info
*revs
,
80 int argc
, const char **argv UNUSED
,
81 struct object_array_entry
**blob
)
83 /* Blob vs file in the working tree*/
88 usage(builtin_diff_usage
);
90 GUARD_PATHSPEC(&revs
->prune_data
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
91 path
= revs
->prune_data
.items
[0].match
;
94 die_errno(_("failed to stat '%s'"), path
);
95 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
96 die(_("'%s': not a regular file or symlink"), path
);
98 diff_set_mnemonic_prefix(&revs
->diffopt
, "o/", "w/");
100 if (blob
[0]->mode
== S_IFINVALID
)
101 blob
[0]->mode
= canon_mode(st
.st_mode
);
103 stuff_change(&revs
->diffopt
,
104 blob
[0]->mode
, canon_mode(st
.st_mode
),
105 &blob
[0]->item
->oid
, null_oid(),
107 blob
[0]->path
? blob
[0]->path
: path
,
109 diffcore_std(&revs
->diffopt
);
110 diff_flush(&revs
->diffopt
);
114 static int 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
);
139 static int builtin_diff_index(struct rev_info
*revs
,
140 int argc
, const char **argv
)
142 unsigned int option
= 0;
144 const char *arg
= argv
[1];
145 if (!strcmp(arg
, "--cached") || !strcmp(arg
, "--staged"))
146 option
|= DIFF_INDEX_CACHED
;
147 else if (!strcmp(arg
, "--merge-base"))
148 option
|= DIFF_INDEX_MERGE_BASE
;
150 usage(builtin_diff_usage
);
154 * Make sure there is one revision (i.e. pending object),
155 * and there is no revision filtering parameters.
157 if (revs
->pending
.nr
!= 1 ||
158 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
160 usage(builtin_diff_usage
);
161 if (!(option
& DIFF_INDEX_CACHED
)) {
163 if (repo_read_index_preload(the_repository
,
164 &revs
->diffopt
.pathspec
, 0) < 0) {
165 perror("repo_read_index_preload");
168 } else if (repo_read_index(the_repository
) < 0) {
169 perror("repo_read_cache");
172 return run_diff_index(revs
, option
);
175 static int builtin_diff_tree(struct rev_info
*revs
,
176 int argc
, const char **argv
,
177 struct object_array_entry
*ent0
,
178 struct object_array_entry
*ent1
)
180 const struct object_id
*(oid
[2]);
181 struct object_id mb_oid
;
185 const char *arg
= argv
[1];
186 if (!strcmp(arg
, "--merge-base"))
189 usage(builtin_diff_usage
);
194 diff_get_merge_base(revs
, &mb_oid
);
196 oid
[1] = &revs
->pending
.objects
[1].item
->oid
;
201 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
204 if (ent1
->item
->flags
& UNINTERESTING
)
206 oid
[swap
] = &ent0
->item
->oid
;
207 oid
[1 - swap
] = &ent1
->item
->oid
;
209 diff_tree_oid(oid
[0], oid
[1], "", &revs
->diffopt
);
210 log_tree_diff_flush(revs
);
214 static int builtin_diff_combined(struct rev_info
*revs
,
215 int argc
, const char **argv UNUSED
,
216 struct object_array_entry
*ent
,
217 int ents
, int first_non_parent
)
219 struct oid_array parents
= OID_ARRAY_INIT
;
223 usage(builtin_diff_usage
);
225 if (first_non_parent
< 0)
226 die(_("no merge given, only parents."));
227 if (first_non_parent
>= ents
)
228 BUG("first_non_parent out of range: %d", first_non_parent
);
230 diff_merges_set_dense_combined_if_unset(revs
);
232 for (i
= 0; i
< ents
; i
++) {
233 if (i
!= first_non_parent
)
234 oid_array_append(&parents
, &ent
[i
].item
->oid
);
236 diff_tree_combined(&ent
[first_non_parent
].item
->oid
, &parents
, revs
);
237 oid_array_clear(&parents
);
241 static void refresh_index_quietly(void)
243 struct lock_file lock_file
= LOCK_INIT
;
246 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
249 discard_index(&the_index
);
250 repo_read_index(the_repository
);
251 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
,
253 repo_update_index_if_able(the_repository
, &lock_file
);
256 static int builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
258 unsigned int options
= 0;
260 while (1 < argc
&& argv
[1][0] == '-') {
261 if (!strcmp(argv
[1], "--base"))
263 else if (!strcmp(argv
[1], "--ours"))
265 else if (!strcmp(argv
[1], "--theirs"))
267 else if (!strcmp(argv
[1], "-q"))
268 options
|= DIFF_SILENT_ON_REMOVED
;
269 else if (!strcmp(argv
[1], "-h"))
270 usage(builtin_diff_usage
);
272 return error(_("invalid option: %s"), argv
[1]);
277 * "diff --base" should not combine merges because it was not
278 * asked to. "diff -c" should not densify (if the user wants
279 * dense one, --cc can be explicitly asked for, or just rely
282 if (revs
->max_count
== -1 &&
283 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
284 diff_merges_set_dense_combined_if_unset(revs
);
287 if (repo_read_index_preload(the_repository
, &revs
->diffopt
.pathspec
,
289 perror("repo_read_index_preload");
292 return run_diff_files(revs
, options
);
298 const char *base
, *left
, *right
;
302 * Check for symmetric-difference arguments, and if present, arrange
303 * everything we need to know to handle them correctly. As a bonus,
304 * weed out all bogus range-based revision specifications, e.g.,
305 * "git diff A..B C..D" or "git diff A..B C" get rejected.
307 * For an actual symmetric diff, *symdiff is set this way:
309 * - its skip is non-NULL and marks *all* rev->pending.objects[i]
310 * indices that the caller should ignore (extra merge bases, of
311 * which there might be many, and A in A...B). Note that the
312 * chosen merge base and right side are NOT marked.
313 * - warn is set if there are multiple merge bases.
314 * - base, left, and right point to the names to use in a
315 * warning about multiple merge bases.
317 * If there is no symmetric diff argument, sym->skip is NULL and
318 * sym->warn is cleared. The remaining fields are not set.
320 static void symdiff_prepare(struct rev_info
*rev
, struct symdiff
*sym
)
322 int i
, is_symdiff
= 0, basecount
= 0, othercount
= 0;
323 int lpos
= -1, rpos
= -1, basepos
= -1;
324 struct bitmap
*map
= NULL
;
327 * Use the whence fields to find merge bases and left and
328 * right parts of symmetric difference, so that we do not
329 * depend on the order that revisions are parsed. If there
330 * are any revs that aren't from these sources, we have a
331 * "git diff C A...B" or "git diff A...B C" case. Or we
332 * could even get "git diff A...B C...E", for instance.
334 * If we don't have just one merge base, we pick one
337 * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
338 * so we must check for SYMMETRIC_LEFT too. The two arrays
339 * rev->pending.objects and rev->cmdline.rev are parallel.
341 for (i
= 0; i
< rev
->cmdline
.nr
; i
++) {
342 struct object
*obj
= rev
->pending
.objects
[i
].item
;
343 switch (rev
->cmdline
.rev
[i
].whence
) {
344 case REV_CMD_MERGE_BASE
:
348 break; /* do mark all bases */
351 usage(builtin_diff_usage
);
353 if (obj
->flags
& SYMMETRIC_LEFT
) {
355 break; /* do mark A */
360 usage(builtin_diff_usage
);
362 continue; /* don't mark B */
363 case REV_CMD_PARENTS_ONLY
:
375 * Forbid any additional revs for both A...B and A..B.
377 if (lpos
>= 0 && othercount
> 0)
378 usage(builtin_diff_usage
);
387 sym
->left
= rev
->pending
.objects
[lpos
].name
;
388 sym
->right
= rev
->pending
.objects
[rpos
].name
;
390 die(_("%s...%s: no merge base"), sym
->left
, sym
->right
);
391 sym
->base
= rev
->pending
.objects
[basepos
].name
;
392 bitmap_unset(map
, basepos
); /* unmark the base we want */
393 sym
->warn
= basecount
> 1;
397 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
401 struct object_array ent
= OBJECT_ARRAY_INIT
;
402 int first_non_parent
= -1;
403 int blobs
= 0, paths
= 0;
404 struct object_array_entry
*blob
[2];
405 int nongit
= 0, no_index
= 0;
407 struct symdiff sdiff
;
410 * We could get N tree-ish in the rev.pending_objects list.
411 * Also there could be M blobs there, and P pathspecs. --cached may
415 * cache vs files (diff-files)
417 * N=0, M=0, --cached:
418 * HEAD vs cache (diff-index --cached)
421 * compare two random blobs. P must be zero.
424 * compare a blob with a working tree file.
427 * tree vs files (diff-index)
429 * N=1, M=0, --cached:
430 * tree vs cache (diff-index --cached)
433 * tree vs tree (diff-tree)
436 * compare two filesystem entities (aka --no-index).
438 * Other cases are errors.
441 /* Were we asked to do --no-index explicitly? */
442 for (i
= 1; i
< argc
; i
++) {
443 if (!strcmp(argv
[i
], "--")) {
447 if (!strcmp(argv
[i
], "--no-index"))
448 no_index
= DIFF_NO_INDEX_EXPLICIT
;
449 if (argv
[i
][0] != '-')
453 prefix
= setup_git_directory_gently(&nongit
);
456 prepare_repo_settings(the_repository
);
457 the_repository
->settings
.command_requires_full_index
= 0;
462 * Treat git diff with at least one path outside of the
463 * repo the same as if the command would have been executed
464 * outside of a git repository. In this case it behaves
465 * the same way as "git diff --no-index <a> <b>", which acts
466 * as a colourful "diff" replacement.
468 if (nongit
|| ((argc
== i
+ 2) &&
469 (!path_inside_repo(prefix
, argv
[i
]) ||
470 !path_inside_repo(prefix
, argv
[i
+ 1]))))
471 no_index
= DIFF_NO_INDEX_IMPLICIT
;
474 init_diff_ui_defaults();
475 git_config(git_diff_ui_config
, NULL
);
476 prefix
= precompose_argv_prefix(argc
, argv
, prefix
);
478 repo_init_revisions(the_repository
, &rev
, prefix
);
480 /* Set up defaults that will apply to both no-index and regular diffs. */
481 rev
.diffopt
.stat_width
= -1;
482 rev
.diffopt
.stat_graph_width
= -1;
483 rev
.diffopt
.flags
.allow_external
= 1;
484 rev
.diffopt
.flags
.allow_textconv
= 1;
486 /* If this is a no-index diff, just run it and exit there. */
488 exit(diff_no_index(&rev
, no_index
== DIFF_NO_INDEX_IMPLICIT
,
493 * Otherwise, we are doing the usual "git" diff; set up any
494 * further defaults that apply to regular diffs.
496 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
499 * Default to intent-to-add entries invisible in the
500 * index. This makes them show up as new files in diff-files
501 * and not at all in diff-cached.
503 rev
.diffopt
.ita_invisible_in_index
= 1;
506 die(_("Not a git repository"));
507 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
508 if (!rev
.diffopt
.output_format
) {
509 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
510 diff_setup_done(&rev
.diffopt
);
513 rev
.diffopt
.flags
.recursive
= 1;
514 rev
.diffopt
.rotate_to_strict
= 1;
516 setup_diff_pager(&rev
.diffopt
);
519 * Do we have --cached and not have a pending object, then
520 * default to HEAD by hand. Eek.
522 if (!rev
.pending
.nr
) {
524 for (i
= 1; i
< argc
; i
++) {
525 const char *arg
= argv
[i
];
526 if (!strcmp(arg
, "--"))
528 else if (!strcmp(arg
, "--cached") ||
529 !strcmp(arg
, "--staged")) {
530 add_head_to_pending(&rev
);
531 if (!rev
.pending
.nr
) {
533 tree
= lookup_tree(the_repository
,
534 the_repository
->hash_algo
->empty_tree
);
535 add_pending_object(&rev
, &tree
->object
, "HEAD");
542 symdiff_prepare(&rev
, &sdiff
);
543 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
544 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
545 struct object
*obj
= entry
->item
;
546 const char *name
= entry
->name
;
547 int flags
= (obj
->flags
& UNINTERESTING
);
549 obj
= parse_object(the_repository
, &obj
->oid
);
550 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
552 die(_("invalid object '%s' given."), name
);
553 if (obj
->type
== OBJ_COMMIT
)
554 obj
= &repo_get_commit_tree(the_repository
,
555 ((struct commit
*)obj
))->object
;
557 if (obj
->type
== OBJ_TREE
) {
558 if (sdiff
.skip
&& bitmap_get(sdiff
.skip
, i
))
561 add_object_array(obj
, name
, &ent
);
562 if (first_non_parent
< 0 &&
563 (i
>= rev
.cmdline
.nr
|| /* HEAD by hand. */
564 rev
.cmdline
.rev
[i
].whence
!= REV_CMD_PARENTS_ONLY
))
565 first_non_parent
= ent
.nr
- 1;
566 } else if (obj
->type
== OBJ_BLOB
) {
568 die(_("more than two blobs given: '%s'"), name
);
573 die(_("unhandled object '%s' given."), name
);
576 if (rev
.prune_data
.nr
)
577 paths
+= rev
.prune_data
.nr
;
580 * Now, do the arguments look reasonable?
585 result
= builtin_diff_files(&rev
, argc
, argv
);
589 usage(builtin_diff_usage
);
590 result
= builtin_diff_b_f(&rev
, argc
, argv
, blob
);
594 usage(builtin_diff_usage
);
595 result
= builtin_diff_blobs(&rev
, argc
, argv
, blob
);
598 usage(builtin_diff_usage
);
602 usage(builtin_diff_usage
);
603 else if (ent
.nr
== 1)
604 result
= builtin_diff_index(&rev
, argc
, argv
);
605 else if (ent
.nr
== 2) {
607 warning(_("%s...%s: multiple merge bases, using %s"),
608 sdiff
.left
, sdiff
.right
, sdiff
.base
);
609 result
= builtin_diff_tree(&rev
, argc
, argv
,
610 &ent
.objects
[0], &ent
.objects
[1]);
612 result
= builtin_diff_combined(&rev
, argc
, argv
,
615 result
= diff_result_code(&rev
.diffopt
, result
);
616 if (1 < rev
.diffopt
.skip_stat_unmatch
)
617 refresh_index_quietly();
618 release_revisions(&rev
);
619 object_array_clear(&ent
);