4 * Copyright (c) 2006 Junio C Hamano
7 #define USE_THE_REPOSITORY_VARIABLE
8 #define DISABLE_SIGN_COMPARE_WARNINGS
12 #include "ewah/ewok.h"
19 #include "diff-merges.h"
21 #include "preload-index.h"
22 #include "read-cache-ll.h"
26 #include "oid-array.h"
29 #define DIFF_NO_INDEX_EXPLICIT 1
30 #define DIFF_NO_INDEX_IMPLICIT 2
32 static const char builtin_diff_usage
[] =
33 "git diff [<options>] [<commit>] [--] [<path>...]\n"
34 " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
35 " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
36 " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
37 " or: git diff [<options>] <blob> <blob>\n"
38 " or: git diff [<options>] --no-index [--] <path> <path> [<pathspec>...]"
40 COMMON_DIFF_OPTIONS_HELP
;
42 static const char *blob_path(struct object_array_entry
*entry
)
44 return entry
->path
? entry
->path
: entry
->name
;
47 static void stuff_change(struct diff_options
*opt
,
48 unsigned old_mode
, unsigned new_mode
,
49 const struct object_id
*old_oid
,
50 const struct object_id
*new_oid
,
56 struct diff_filespec
*one
, *two
;
58 if (!is_null_oid(old_oid
) && !is_null_oid(new_oid
) &&
59 oideq(old_oid
, new_oid
) && (old_mode
== new_mode
))
62 if (opt
->flags
.reverse_diff
) {
63 SWAP(old_mode
, new_mode
);
64 SWAP(old_oid
, new_oid
);
65 SWAP(old_path
, new_path
);
69 (strncmp(old_path
, opt
->prefix
, opt
->prefix_length
) ||
70 strncmp(new_path
, opt
->prefix
, opt
->prefix_length
)))
73 one
= alloc_filespec(old_path
);
74 two
= alloc_filespec(new_path
);
75 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
76 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
78 diff_queue(&diff_queued_diff
, one
, two
);
81 static void builtin_diff_b_f(struct rev_info
*revs
,
82 int argc
, const char **argv UNUSED
,
83 struct object_array_entry
**blob
)
85 /* Blob vs file in the working tree*/
90 usage(builtin_diff_usage
);
92 GUARD_PATHSPEC(&revs
->prune_data
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
93 path
= revs
->prune_data
.items
[0].match
;
96 die_errno(_("failed to stat '%s'"), path
);
97 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
98 die(_("'%s': not a regular file or symlink"), path
);
100 diff_set_mnemonic_prefix(&revs
->diffopt
, "o/", "w/");
102 if (blob
[0]->mode
== S_IFINVALID
)
103 blob
[0]->mode
= canon_mode(st
.st_mode
);
105 stuff_change(&revs
->diffopt
,
106 blob
[0]->mode
, canon_mode(st
.st_mode
),
107 &blob
[0]->item
->oid
, null_oid(the_hash_algo
),
109 blob
[0]->path
? blob
[0]->path
: path
,
111 diffcore_std(&revs
->diffopt
);
112 diff_flush(&revs
->diffopt
);
115 static void 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
);
139 static void 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 die_errno("repo_read_index_preload");
167 } else if (repo_read_index(the_repository
) < 0) {
168 die_errno("repo_read_cache");
170 run_diff_index(revs
, option
);
173 static void builtin_diff_tree(struct rev_info
*revs
,
174 int argc
, const char **argv
,
175 struct object_array_entry
*ent0
,
176 struct object_array_entry
*ent1
)
178 const struct object_id
*(oid
[2]);
179 struct object_id mb_oid
;
183 const char *arg
= argv
[1];
184 if (!strcmp(arg
, "--merge-base"))
187 usage(builtin_diff_usage
);
192 diff_get_merge_base(revs
, &mb_oid
);
194 oid
[1] = &revs
->pending
.objects
[1].item
->oid
;
199 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
202 if (ent1
->item
->flags
& UNINTERESTING
)
204 oid
[swap
] = &ent0
->item
->oid
;
205 oid
[1 - swap
] = &ent1
->item
->oid
;
207 diff_tree_oid(oid
[0], oid
[1], "", &revs
->diffopt
);
208 log_tree_diff_flush(revs
);
211 static void builtin_diff_combined(struct rev_info
*revs
,
212 int argc
, const char **argv UNUSED
,
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
);
237 static void refresh_index_quietly(void)
239 struct lock_file lock_file
= LOCK_INIT
;
242 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
245 discard_index(the_repository
->index
);
246 repo_read_index(the_repository
);
247 refresh_index(the_repository
->index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
,
249 repo_update_index_if_able(the_repository
, &lock_file
);
252 static void builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
254 unsigned int options
= 0;
256 while (1 < argc
&& argv
[1][0] == '-') {
257 if (!strcmp(argv
[1], "--base"))
259 else if (!strcmp(argv
[1], "--ours"))
261 else if (!strcmp(argv
[1], "--theirs"))
263 else if (!strcmp(argv
[1], "-q"))
264 options
|= DIFF_SILENT_ON_REMOVED
;
265 else if (!strcmp(argv
[1], "-h"))
266 usage(builtin_diff_usage
);
268 error(_("invalid option: %s"), argv
[1]);
269 usage(builtin_diff_usage
);
275 * "diff --base" should not combine merges because it was not
276 * asked to. "diff -c" should not densify (if the user wants
277 * dense one, --cc can be explicitly asked for, or just rely
280 if (revs
->max_count
== -1 &&
281 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
282 diff_merges_set_dense_combined_if_unset(revs
);
285 if (repo_read_index_preload(the_repository
, &revs
->diffopt
.pathspec
,
287 die_errno("repo_read_index_preload");
289 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 static void symdiff_release(struct symdiff
*sdiff
)
396 bitmap_free(sdiff
->skip
);
399 int cmd_diff(int argc
,
402 struct repository
*repo UNUSED
)
406 struct object_array ent
= OBJECT_ARRAY_INIT
;
407 int first_non_parent
= -1;
408 int blobs
= 0, paths
= 0;
409 struct object_array_entry
*blob
[2];
410 int nongit
= 0, no_index
= 0;
412 struct symdiff sdiff
;
415 * We could get N tree-ish in the rev.pending_objects list.
416 * Also there could be M blobs there, and P pathspecs. --cached may
420 * cache vs files (diff-files)
422 * N=0, M=0, --cached:
423 * HEAD vs cache (diff-index --cached)
426 * compare two random blobs. P must be zero.
429 * compare a blob with a working tree file.
432 * tree vs files (diff-index)
434 * N=1, M=0, --cached:
435 * tree vs cache (diff-index --cached)
438 * tree vs tree (diff-tree)
441 * compare two filesystem entities (aka --no-index).
443 * Other cases are errors.
446 /* Were we asked to do --no-index explicitly? */
447 for (i
= 1; i
< argc
; i
++) {
448 if (!strcmp(argv
[i
], "--")) {
452 if (!strcmp(argv
[i
], "--no-index"))
453 no_index
= DIFF_NO_INDEX_EXPLICIT
;
454 if (argv
[i
][0] != '-')
458 prefix
= setup_git_directory_gently(&nongit
);
461 prepare_repo_settings(the_repository
);
462 the_repository
->settings
.command_requires_full_index
= 0;
467 * Treat git diff with at least one path outside of the
468 * repo the same as if the command would have been executed
469 * outside of a git repository. In this case it behaves
470 * the same way as "git diff --no-index <a> <b>", which acts
471 * as a colourful "diff" replacement.
473 if (nongit
|| ((argc
== i
+ 2) &&
474 (!path_inside_repo(prefix
, argv
[i
]) ||
475 !path_inside_repo(prefix
, argv
[i
+ 1]))))
476 no_index
= DIFF_NO_INDEX_IMPLICIT
;
480 * When operating outside of a Git repository we need to have a hash
481 * algorithm at hand so that we can generate the blob hashes. We
482 * default to SHA1 here, but may eventually want to change this to be
483 * configurable via a command line option.
486 repo_set_hash_algo(the_repository
, GIT_HASH_SHA1
);
488 init_diff_ui_defaults();
489 git_config(git_diff_ui_config
, NULL
);
490 prefix
= precompose_argv_prefix(argc
, argv
, prefix
);
492 repo_init_revisions(the_repository
, &rev
, prefix
);
494 /* Set up defaults that will apply to both no-index and regular diffs. */
495 init_diffstat_widths(&rev
.diffopt
);
496 rev
.diffopt
.flags
.allow_external
= 1;
497 rev
.diffopt
.flags
.allow_textconv
= 1;
499 /* If this is a no-index diff, just run it and exit there. */
501 exit(diff_no_index(&rev
, the_repository
->hash_algo
,
502 no_index
== DIFF_NO_INDEX_IMPLICIT
,
507 * Otherwise, we are doing the usual "git" diff; set up any
508 * further defaults that apply to regular diffs.
510 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
513 * Default to intent-to-add entries invisible in the
514 * index. This makes them show up as new files in diff-files
515 * and not at all in diff-cached.
517 rev
.diffopt
.ita_invisible_in_index
= 1;
520 die(_("Not a git repository"));
521 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
522 if (!rev
.diffopt
.output_format
) {
523 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
524 diff_setup_done(&rev
.diffopt
);
527 rev
.diffopt
.flags
.recursive
= 1;
528 rev
.diffopt
.rotate_to_strict
= 1;
530 setup_diff_pager(&rev
.diffopt
);
533 * Do we have --cached and not have a pending object, then
534 * default to HEAD by hand. Eek.
536 if (!rev
.pending
.nr
) {
538 for (i
= 1; i
< argc
; i
++) {
539 const char *arg
= argv
[i
];
540 if (!strcmp(arg
, "--"))
542 else if (!strcmp(arg
, "--cached") ||
543 !strcmp(arg
, "--staged")) {
544 add_head_to_pending(&rev
);
545 if (!rev
.pending
.nr
) {
547 tree
= lookup_tree(the_repository
,
548 the_repository
->hash_algo
->empty_tree
);
549 add_pending_object(&rev
, &tree
->object
, "HEAD");
556 symdiff_prepare(&rev
, &sdiff
);
557 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
558 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
559 struct object
*obj
= entry
->item
;
560 const char *name
= entry
->name
;
561 int flags
= (obj
->flags
& UNINTERESTING
);
563 obj
= parse_object(the_repository
, &obj
->oid
);
564 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
566 die(_("invalid object '%s' given."), name
);
567 if (obj
->type
== OBJ_COMMIT
)
568 obj
= &repo_get_commit_tree(the_repository
,
569 ((struct commit
*)obj
))->object
;
571 if (obj
->type
== OBJ_TREE
) {
572 if (sdiff
.skip
&& bitmap_get(sdiff
.skip
, i
))
575 add_object_array(obj
, name
, &ent
);
576 if (first_non_parent
< 0 &&
577 (i
>= rev
.cmdline
.nr
|| /* HEAD by hand. */
578 rev
.cmdline
.rev
[i
].whence
!= REV_CMD_PARENTS_ONLY
))
579 first_non_parent
= ent
.nr
- 1;
580 } else if (obj
->type
== OBJ_BLOB
) {
582 die(_("more than two blobs given: '%s'"), name
);
587 die(_("unhandled object '%s' given."), name
);
590 if (rev
.prune_data
.nr
)
591 paths
+= rev
.prune_data
.nr
;
594 * Now, do the arguments look reasonable?
599 builtin_diff_files(&rev
, argc
, argv
);
603 usage(builtin_diff_usage
);
604 builtin_diff_b_f(&rev
, argc
, argv
, blob
);
608 usage(builtin_diff_usage
);
609 builtin_diff_blobs(&rev
, argc
, argv
, blob
);
612 usage(builtin_diff_usage
);
616 usage(builtin_diff_usage
);
617 else if (ent
.nr
== 1)
618 builtin_diff_index(&rev
, argc
, argv
);
619 else if (ent
.nr
== 2) {
621 warning(_("%s...%s: multiple merge bases, using %s"),
622 sdiff
.left
, sdiff
.right
, sdiff
.base
);
623 builtin_diff_tree(&rev
, argc
, argv
,
624 &ent
.objects
[0], &ent
.objects
[1]);
626 builtin_diff_combined(&rev
, argc
, argv
,
629 result
= diff_result_code(&rev
);
630 if (1 < rev
.diffopt
.skip_stat_unmatch
)
631 refresh_index_quietly();
632 release_revisions(&rev
);
633 object_array_clear(&ent
);
634 symdiff_release(&sdiff
);