4 * Copyright (c) 2006 Junio C Hamano
18 #include "submodule.h"
19 #include "sha1-array.h"
21 #define DIFF_NO_INDEX_EXPLICIT 1
22 #define DIFF_NO_INDEX_IMPLICIT 2
24 static const char builtin_diff_usage
[] =
25 "git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
27 static const char *blob_path(struct object_array_entry
*entry
)
29 return entry
->path
? entry
->path
: entry
->name
;
32 static void stuff_change(struct diff_options
*opt
,
33 unsigned old_mode
, unsigned new_mode
,
34 const struct object_id
*old_oid
,
35 const struct object_id
*new_oid
,
41 struct diff_filespec
*one
, *two
;
43 if (!is_null_oid(old_oid
) && !is_null_oid(new_oid
) &&
44 !oidcmp(old_oid
, new_oid
) && (old_mode
== new_mode
))
47 if (opt
->flags
.REVERSE_DIFF
) {
48 SWAP(old_mode
, new_mode
);
49 SWAP(old_oid
, new_oid
);
50 SWAP(old_path
, new_path
);
54 (strncmp(old_path
, opt
->prefix
, opt
->prefix_length
) ||
55 strncmp(new_path
, opt
->prefix
, opt
->prefix_length
)))
58 one
= alloc_filespec(old_path
);
59 two
= alloc_filespec(new_path
);
60 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
61 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
63 diff_queue(&diff_queued_diff
, one
, two
);
66 static int builtin_diff_b_f(struct rev_info
*revs
,
67 int argc
, const char **argv
,
68 struct object_array_entry
**blob
)
70 /* Blob vs file in the working tree*/
75 usage(builtin_diff_usage
);
77 GUARD_PATHSPEC(&revs
->prune_data
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
78 path
= revs
->prune_data
.items
[0].match
;
81 die_errno(_("failed to stat '%s'"), path
);
82 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
83 die(_("'%s': not a regular file or symlink"), path
);
85 diff_set_mnemonic_prefix(&revs
->diffopt
, "o/", "w/");
87 if (blob
[0]->mode
== S_IFINVALID
)
88 blob
[0]->mode
= canon_mode(st
.st_mode
);
90 stuff_change(&revs
->diffopt
,
91 blob
[0]->mode
, canon_mode(st
.st_mode
),
92 &blob
[0]->item
->oid
, &null_oid
,
94 blob
[0]->path
? blob
[0]->path
: path
,
96 diffcore_std(&revs
->diffopt
);
97 diff_flush(&revs
->diffopt
);
101 static int builtin_diff_blobs(struct rev_info
*revs
,
102 int argc
, const char **argv
,
103 struct object_array_entry
**blob
)
105 unsigned mode
= canon_mode(S_IFREG
| 0644);
108 usage(builtin_diff_usage
);
110 if (blob
[0]->mode
== S_IFINVALID
)
111 blob
[0]->mode
= mode
;
113 if (blob
[1]->mode
== S_IFINVALID
)
114 blob
[1]->mode
= mode
;
116 stuff_change(&revs
->diffopt
,
117 blob
[0]->mode
, blob
[1]->mode
,
118 &blob
[0]->item
->oid
, &blob
[1]->item
->oid
,
120 blob_path(blob
[0]), blob_path(blob
[1]));
121 diffcore_std(&revs
->diffopt
);
122 diff_flush(&revs
->diffopt
);
126 static int builtin_diff_index(struct rev_info
*revs
,
127 int argc
, const char **argv
)
131 const char *arg
= argv
[1];
132 if (!strcmp(arg
, "--cached") || !strcmp(arg
, "--staged"))
135 usage(builtin_diff_usage
);
139 * Make sure there is one revision (i.e. pending object),
140 * and there is no revision filtering parameters.
142 if (revs
->pending
.nr
!= 1 ||
143 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
145 usage(builtin_diff_usage
);
148 if (read_cache_preload(&revs
->diffopt
.pathspec
) < 0) {
149 perror("read_cache_preload");
152 } else if (read_cache() < 0) {
153 perror("read_cache");
156 return run_diff_index(revs
, cached
);
159 static int builtin_diff_tree(struct rev_info
*revs
,
160 int argc
, const char **argv
,
161 struct object_array_entry
*ent0
,
162 struct object_array_entry
*ent1
)
164 const struct object_id
*(oid
[2]);
168 usage(builtin_diff_usage
);
171 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
174 if (ent1
->item
->flags
& UNINTERESTING
)
176 oid
[swap
] = &ent0
->item
->oid
;
177 oid
[1 - swap
] = &ent1
->item
->oid
;
178 diff_tree_oid(oid
[0], oid
[1], "", &revs
->diffopt
);
179 log_tree_diff_flush(revs
);
183 static int builtin_diff_combined(struct rev_info
*revs
,
184 int argc
, const char **argv
,
185 struct object_array_entry
*ent
,
188 struct oid_array parents
= OID_ARRAY_INIT
;
192 usage(builtin_diff_usage
);
194 if (!revs
->dense_combined_merges
&& !revs
->combine_merges
)
195 revs
->dense_combined_merges
= revs
->combine_merges
= 1;
196 for (i
= 1; i
< ents
; i
++)
197 oid_array_append(&parents
, &ent
[i
].item
->oid
);
198 diff_tree_combined(&ent
[0].item
->oid
, &parents
,
199 revs
->dense_combined_merges
, revs
);
200 oid_array_clear(&parents
);
204 static void refresh_index_quietly(void)
206 struct lock_file
*lock_file
;
209 lock_file
= xcalloc(1, sizeof(struct lock_file
));
210 fd
= hold_locked_index(lock_file
, 0);
215 refresh_cache(REFRESH_QUIET
|REFRESH_UNMERGED
);
216 update_index_if_able(&the_index
, lock_file
);
219 static int builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
221 unsigned int options
= 0;
223 while (1 < argc
&& argv
[1][0] == '-') {
224 if (!strcmp(argv
[1], "--base"))
226 else if (!strcmp(argv
[1], "--ours"))
228 else if (!strcmp(argv
[1], "--theirs"))
230 else if (!strcmp(argv
[1], "-q"))
231 options
|= DIFF_SILENT_ON_REMOVED
;
232 else if (!strcmp(argv
[1], "-h"))
233 usage(builtin_diff_usage
);
235 return error(_("invalid option: %s"), argv
[1]);
240 * "diff --base" should not combine merges because it was not
241 * asked to. "diff -c" should not densify (if the user wants
242 * dense one, --cc can be explicitly asked for, or just rely
245 if (revs
->max_count
== -1 && !revs
->combine_merges
&&
246 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
247 revs
->combine_merges
= revs
->dense_combined_merges
= 1;
250 if (read_cache_preload(&revs
->diffopt
.pathspec
) < 0) {
251 perror("read_cache_preload");
254 return run_diff_files(revs
, options
);
257 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
261 struct object_array ent
= OBJECT_ARRAY_INIT
;
262 int blobs
= 0, paths
= 0;
263 struct object_array_entry
*blob
[2];
264 int nongit
= 0, no_index
= 0;
268 * We could get N tree-ish in the rev.pending_objects list.
269 * Also there could be M blobs there, and P pathspecs.
272 * cache vs files (diff-files)
274 * compare two random blobs. P must be zero.
276 * compare a blob with a working tree file.
279 * tree vs cache (diff-index --cached)
282 * tree vs tree (diff-tree)
285 * compare two filesystem entities (aka --no-index).
287 * Other cases are errors.
290 /* Were we asked to do --no-index explicitly? */
291 for (i
= 1; i
< argc
; i
++) {
292 if (!strcmp(argv
[i
], "--")) {
296 if (!strcmp(argv
[i
], "--no-index"))
297 no_index
= DIFF_NO_INDEX_EXPLICIT
;
298 if (argv
[i
][0] != '-')
302 prefix
= setup_git_directory_gently(&nongit
);
306 * Treat git diff with at least one path outside of the
307 * repo the same as if the command would have been executed
308 * outside of a git repository. In this case it behaves
309 * the same way as "git diff --no-index <a> <b>", which acts
310 * as a colourful "diff" replacement.
312 if (nongit
|| ((argc
== i
+ 2) &&
313 (!path_inside_repo(prefix
, argv
[i
]) ||
314 !path_inside_repo(prefix
, argv
[i
+ 1]))))
315 no_index
= DIFF_NO_INDEX_IMPLICIT
;
318 init_diff_ui_defaults();
319 git_config(git_diff_ui_config
, NULL
);
320 precompose_argv(argc
, argv
);
322 init_revisions(&rev
, prefix
);
324 if (no_index
&& argc
!= i
+ 2) {
325 if (no_index
== DIFF_NO_INDEX_IMPLICIT
) {
327 * There was no --no-index and there were not two
328 * paths. It is possible that the user intended
329 * to do an inside-repository operation.
331 fprintf(stderr
, "Not a git repository\n");
333 "To compare two paths outside a working tree:\n");
335 /* Give the usage message for non-repository usage and exit. */
336 usagef("git diff %s <path> <path>",
337 no_index
== DIFF_NO_INDEX_EXPLICIT
?
338 "--no-index" : "[--no-index]");
342 /* If this is a no-index diff, just run it and exit there. */
343 diff_no_index(&rev
, argc
, argv
);
345 /* Otherwise, we are doing the usual "git" diff */
346 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
348 /* Scale to real terminal size and respect statGraphWidth config */
349 rev
.diffopt
.stat_width
= -1;
350 rev
.diffopt
.stat_graph_width
= -1;
352 /* Default to let external and textconv be used */
353 rev
.diffopt
.flags
.ALLOW_EXTERNAL
= 1;
354 rev
.diffopt
.flags
.ALLOW_TEXTCONV
= 1;
357 die(_("Not a git repository"));
358 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
359 if (!rev
.diffopt
.output_format
) {
360 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
361 diff_setup_done(&rev
.diffopt
);
364 rev
.diffopt
.flags
.RECURSIVE
= 1;
366 setup_diff_pager(&rev
.diffopt
);
369 * Do we have --cached and not have a pending object, then
370 * default to HEAD by hand. Eek.
372 if (!rev
.pending
.nr
) {
374 for (i
= 1; i
< argc
; i
++) {
375 const char *arg
= argv
[i
];
376 if (!strcmp(arg
, "--"))
378 else if (!strcmp(arg
, "--cached") ||
379 !strcmp(arg
, "--staged")) {
380 add_head_to_pending(&rev
);
381 if (!rev
.pending
.nr
) {
383 tree
= lookup_tree(&empty_tree_oid
);
384 add_pending_object(&rev
, &tree
->object
, "HEAD");
391 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
392 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
393 struct object
*obj
= entry
->item
;
394 const char *name
= entry
->name
;
395 int flags
= (obj
->flags
& UNINTERESTING
);
397 obj
= parse_object(&obj
->oid
);
398 obj
= deref_tag(obj
, NULL
, 0);
400 die(_("invalid object '%s' given."), name
);
401 if (obj
->type
== OBJ_COMMIT
)
402 obj
= &((struct commit
*)obj
)->tree
->object
;
404 if (obj
->type
== OBJ_TREE
) {
406 add_object_array(obj
, name
, &ent
);
407 } else if (obj
->type
== OBJ_BLOB
) {
409 die(_("more than two blobs given: '%s'"), name
);
414 die(_("unhandled object '%s' given."), name
);
417 if (rev
.prune_data
.nr
)
418 paths
+= rev
.prune_data
.nr
;
421 * Now, do the arguments look reasonable?
426 result
= builtin_diff_files(&rev
, argc
, argv
);
430 usage(builtin_diff_usage
);
431 result
= builtin_diff_b_f(&rev
, argc
, argv
, blob
);
435 usage(builtin_diff_usage
);
436 result
= builtin_diff_blobs(&rev
, argc
, argv
, blob
);
439 usage(builtin_diff_usage
);
443 usage(builtin_diff_usage
);
444 else if (ent
.nr
== 1)
445 result
= builtin_diff_index(&rev
, argc
, argv
);
446 else if (ent
.nr
== 2)
447 result
= builtin_diff_tree(&rev
, argc
, argv
,
448 &ent
.objects
[0], &ent
.objects
[1]);
449 else if (ent
.objects
[0].item
->flags
& UNINTERESTING
) {
451 * diff A...B where there is at least one merge base
452 * between A and B. We have ent.objects[0] ==
453 * merge-base, ent.objects[ents-2] == A, and
454 * ent.objects[ents-1] == B. Show diff between the
455 * base and B. Note that we pick one merge base at
456 * random if there are more than one.
458 result
= builtin_diff_tree(&rev
, argc
, argv
,
460 &ent
.objects
[ent
.nr
-1]);
462 result
= builtin_diff_combined(&rev
, argc
, argv
,
463 ent
.objects
, ent
.nr
);
464 result
= diff_result_code(&rev
.diffopt
, result
);
465 if (1 < rev
.diffopt
.skip_stat_unmatch
)
466 refresh_index_quietly();