4 * Copyright (c) 2006 Junio C Hamano
17 #include "submodule.h"
18 #include "sha1-array.h"
20 #define DIFF_NO_INDEX_EXPLICIT 1
21 #define DIFF_NO_INDEX_IMPLICIT 2
23 static const char builtin_diff_usage
[] =
24 "git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
26 static void stuff_change(struct diff_options
*opt
,
27 unsigned old_mode
, unsigned new_mode
,
28 const struct object_id
*old_oid
,
29 const struct object_id
*new_oid
,
35 struct diff_filespec
*one
, *two
;
37 if (!is_null_oid(old_oid
) && !is_null_oid(new_oid
) &&
38 !oidcmp(old_oid
, new_oid
) && (old_mode
== new_mode
))
41 if (DIFF_OPT_TST(opt
, REVERSE_DIFF
)) {
42 SWAP(old_mode
, new_mode
);
43 SWAP(old_oid
, new_oid
);
44 SWAP(old_path
, new_path
);
48 (strncmp(old_path
, opt
->prefix
, opt
->prefix_length
) ||
49 strncmp(new_path
, opt
->prefix
, opt
->prefix_length
)))
52 one
= alloc_filespec(old_path
);
53 two
= alloc_filespec(new_path
);
54 fill_filespec(one
, old_oid
->hash
, old_oid_valid
, old_mode
);
55 fill_filespec(two
, new_oid
->hash
, new_oid_valid
, new_mode
);
57 diff_queue(&diff_queued_diff
, one
, two
);
60 static int builtin_diff_b_f(struct rev_info
*revs
,
61 int argc
, const char **argv
,
62 struct object_array_entry
**blob
)
64 /* Blob vs file in the working tree*/
69 usage(builtin_diff_usage
);
71 GUARD_PATHSPEC(&revs
->prune_data
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
72 path
= revs
->prune_data
.items
[0].match
;
75 die_errno(_("failed to stat '%s'"), path
);
76 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
77 die(_("'%s': not a regular file or symlink"), path
);
79 diff_set_mnemonic_prefix(&revs
->diffopt
, "o/", "w/");
81 if (blob
[0]->mode
== S_IFINVALID
)
82 blob
[0]->mode
= canon_mode(st
.st_mode
);
84 stuff_change(&revs
->diffopt
,
85 blob
[0]->mode
, canon_mode(st
.st_mode
),
86 &blob
[0]->item
->oid
, &null_oid
,
89 diffcore_std(&revs
->diffopt
);
90 diff_flush(&revs
->diffopt
);
94 static int builtin_diff_blobs(struct rev_info
*revs
,
95 int argc
, const char **argv
,
96 struct object_array_entry
**blob
)
98 unsigned mode
= canon_mode(S_IFREG
| 0644);
101 usage(builtin_diff_usage
);
103 if (blob
[0]->mode
== S_IFINVALID
)
104 blob
[0]->mode
= mode
;
106 if (blob
[1]->mode
== S_IFINVALID
)
107 blob
[1]->mode
= mode
;
109 stuff_change(&revs
->diffopt
,
110 blob
[0]->mode
, blob
[1]->mode
,
111 &blob
[0]->item
->oid
, &blob
[1]->item
->oid
,
113 blob
[0]->name
, blob
[1]->name
);
114 diffcore_std(&revs
->diffopt
);
115 diff_flush(&revs
->diffopt
);
119 static int builtin_diff_index(struct rev_info
*revs
,
120 int argc
, const char **argv
)
124 const char *arg
= argv
[1];
125 if (!strcmp(arg
, "--cached") || !strcmp(arg
, "--staged"))
128 usage(builtin_diff_usage
);
132 * Make sure there is one revision (i.e. pending object),
133 * and there is no revision filtering parameters.
135 if (revs
->pending
.nr
!= 1 ||
136 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
138 usage(builtin_diff_usage
);
141 if (read_cache_preload(&revs
->diffopt
.pathspec
) < 0) {
142 perror("read_cache_preload");
145 } else if (read_cache() < 0) {
146 perror("read_cache");
149 return run_diff_index(revs
, cached
);
152 static int builtin_diff_tree(struct rev_info
*revs
,
153 int argc
, const char **argv
,
154 struct object_array_entry
*ent0
,
155 struct object_array_entry
*ent1
)
157 const struct object_id
*(oid
[2]);
161 usage(builtin_diff_usage
);
164 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
167 if (ent1
->item
->flags
& UNINTERESTING
)
169 oid
[swap
] = &ent0
->item
->oid
;
170 oid
[1 - swap
] = &ent1
->item
->oid
;
171 diff_tree_sha1(oid
[0]->hash
, oid
[1]->hash
, "", &revs
->diffopt
);
172 log_tree_diff_flush(revs
);
176 static int builtin_diff_combined(struct rev_info
*revs
,
177 int argc
, const char **argv
,
178 struct object_array_entry
*ent
,
181 struct oid_array parents
= OID_ARRAY_INIT
;
185 usage(builtin_diff_usage
);
187 if (!revs
->dense_combined_merges
&& !revs
->combine_merges
)
188 revs
->dense_combined_merges
= revs
->combine_merges
= 1;
189 for (i
= 1; i
< ents
; i
++)
190 oid_array_append(&parents
, &ent
[i
].item
->oid
);
191 diff_tree_combined(ent
[0].item
->oid
.hash
, &parents
,
192 revs
->dense_combined_merges
, revs
);
193 oid_array_clear(&parents
);
197 static void refresh_index_quietly(void)
199 struct lock_file
*lock_file
;
202 lock_file
= xcalloc(1, sizeof(struct lock_file
));
203 fd
= hold_locked_index(lock_file
, 0);
208 refresh_cache(REFRESH_QUIET
|REFRESH_UNMERGED
);
209 update_index_if_able(&the_index
, lock_file
);
212 static int builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
214 unsigned int options
= 0;
216 while (1 < argc
&& argv
[1][0] == '-') {
217 if (!strcmp(argv
[1], "--base"))
219 else if (!strcmp(argv
[1], "--ours"))
221 else if (!strcmp(argv
[1], "--theirs"))
223 else if (!strcmp(argv
[1], "-q"))
224 options
|= DIFF_SILENT_ON_REMOVED
;
225 else if (!strcmp(argv
[1], "-h"))
226 usage(builtin_diff_usage
);
228 return error(_("invalid option: %s"), argv
[1]);
233 * "diff --base" should not combine merges because it was not
234 * asked to. "diff -c" should not densify (if the user wants
235 * dense one, --cc can be explicitly asked for, or just rely
238 if (revs
->max_count
== -1 && !revs
->combine_merges
&&
239 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
240 revs
->combine_merges
= revs
->dense_combined_merges
= 1;
243 if (read_cache_preload(&revs
->diffopt
.pathspec
) < 0) {
244 perror("read_cache_preload");
247 return run_diff_files(revs
, options
);
250 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
254 struct object_array ent
= OBJECT_ARRAY_INIT
;
255 int blobs
= 0, paths
= 0;
256 struct object_array_entry
*blob
[2];
257 int nongit
= 0, no_index
= 0;
261 * We could get N tree-ish in the rev.pending_objects list.
262 * Also there could be M blobs there, and P pathspecs.
265 * cache vs files (diff-files)
267 * compare two random blobs. P must be zero.
269 * compare a blob with a working tree file.
272 * tree vs cache (diff-index --cached)
275 * tree vs tree (diff-tree)
278 * compare two filesystem entities (aka --no-index).
280 * Other cases are errors.
283 /* Were we asked to do --no-index explicitly? */
284 for (i
= 1; i
< argc
; i
++) {
285 if (!strcmp(argv
[i
], "--")) {
289 if (!strcmp(argv
[i
], "--no-index"))
290 no_index
= DIFF_NO_INDEX_EXPLICIT
;
291 if (argv
[i
][0] != '-')
295 prefix
= setup_git_directory_gently(&nongit
);
299 * Treat git diff with at least one path outside of the
300 * repo the same as if the command would have been executed
301 * outside of a git repository. In this case it behaves
302 * the same way as "git diff --no-index <a> <b>", which acts
303 * as a colourful "diff" replacement.
305 if (nongit
|| ((argc
== i
+ 2) &&
306 (!path_inside_repo(prefix
, argv
[i
]) ||
307 !path_inside_repo(prefix
, argv
[i
+ 1]))))
308 no_index
= DIFF_NO_INDEX_IMPLICIT
;
313 init_diff_ui_defaults();
314 git_config(git_diff_ui_config
, NULL
);
315 precompose_argv(argc
, argv
);
317 init_revisions(&rev
, prefix
);
319 if (no_index
&& argc
!= i
+ 2) {
320 if (no_index
== DIFF_NO_INDEX_IMPLICIT
) {
322 * There was no --no-index and there were not two
323 * paths. It is possible that the user intended
324 * to do an inside-repository operation.
326 fprintf(stderr
, "Not a git repository\n");
328 "To compare two paths outside a working tree:\n");
330 /* Give the usage message for non-repository usage and exit. */
331 usagef("git diff %s <path> <path>",
332 no_index
== DIFF_NO_INDEX_EXPLICIT
?
333 "--no-index" : "[--no-index]");
337 /* If this is a no-index diff, just run it and exit there. */
338 diff_no_index(&rev
, argc
, argv
);
340 /* Otherwise, we are doing the usual "git" diff */
341 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
343 /* Scale to real terminal size and respect statGraphWidth config */
344 rev
.diffopt
.stat_width
= -1;
345 rev
.diffopt
.stat_graph_width
= -1;
347 /* Default to let external and textconv be used */
348 DIFF_OPT_SET(&rev
.diffopt
, ALLOW_EXTERNAL
);
349 DIFF_OPT_SET(&rev
.diffopt
, ALLOW_TEXTCONV
);
352 die(_("Not a git repository"));
353 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
354 if (!rev
.diffopt
.output_format
) {
355 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
356 diff_setup_done(&rev
.diffopt
);
359 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
361 setup_diff_pager(&rev
.diffopt
);
364 * Do we have --cached and not have a pending object, then
365 * default to HEAD by hand. Eek.
367 if (!rev
.pending
.nr
) {
369 for (i
= 1; i
< argc
; i
++) {
370 const char *arg
= argv
[i
];
371 if (!strcmp(arg
, "--"))
373 else if (!strcmp(arg
, "--cached") ||
374 !strcmp(arg
, "--staged")) {
375 add_head_to_pending(&rev
);
376 if (!rev
.pending
.nr
) {
378 tree
= lookup_tree(EMPTY_TREE_SHA1_BIN
);
379 add_pending_object(&rev
, &tree
->object
, "HEAD");
386 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
387 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
388 struct object
*obj
= entry
->item
;
389 const char *name
= entry
->name
;
390 int flags
= (obj
->flags
& UNINTERESTING
);
392 obj
= parse_object(obj
->oid
.hash
);
393 obj
= deref_tag(obj
, NULL
, 0);
395 die(_("invalid object '%s' given."), name
);
396 if (obj
->type
== OBJ_COMMIT
)
397 obj
= &((struct commit
*)obj
)->tree
->object
;
399 if (obj
->type
== OBJ_TREE
) {
401 add_object_array(obj
, name
, &ent
);
402 } else if (obj
->type
== OBJ_BLOB
) {
404 die(_("more than two blobs given: '%s'"), name
);
409 die(_("unhandled object '%s' given."), name
);
412 if (rev
.prune_data
.nr
)
413 paths
+= rev
.prune_data
.nr
;
416 * Now, do the arguments look reasonable?
421 result
= builtin_diff_files(&rev
, argc
, argv
);
425 usage(builtin_diff_usage
);
426 result
= builtin_diff_b_f(&rev
, argc
, argv
, blob
);
430 usage(builtin_diff_usage
);
431 result
= builtin_diff_blobs(&rev
, argc
, argv
, blob
);
434 usage(builtin_diff_usage
);
438 usage(builtin_diff_usage
);
439 else if (ent
.nr
== 1)
440 result
= builtin_diff_index(&rev
, argc
, argv
);
441 else if (ent
.nr
== 2)
442 result
= builtin_diff_tree(&rev
, argc
, argv
,
443 &ent
.objects
[0], &ent
.objects
[1]);
444 else if (ent
.objects
[0].item
->flags
& UNINTERESTING
) {
446 * diff A...B where there is at least one merge base
447 * between A and B. We have ent.objects[0] ==
448 * merge-base, ent.objects[ents-2] == A, and
449 * ent.objects[ents-1] == B. Show diff between the
450 * base and B. Note that we pick one merge base at
451 * random if there are more than one.
453 result
= builtin_diff_tree(&rev
, argc
, argv
,
455 &ent
.objects
[ent
.nr
-1]);
457 result
= builtin_diff_combined(&rev
, argc
, argv
,
458 ent
.objects
, ent
.nr
);
459 result
= diff_result_code(&rev
.diffopt
, result
);
460 if (1 < rev
.diffopt
.skip_stat_unmatch
)
461 refresh_index_quietly();