4 * Copyright (c) 2006 Junio C Hamano
16 #include "submodule.h"
17 #include "sha1-array.h"
20 unsigned char sha1
[20];
25 static const char builtin_diff_usage
[] =
26 "git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
28 static void stuff_change(struct diff_options
*opt
,
29 unsigned old_mode
, unsigned new_mode
,
30 const unsigned char *old_sha1
,
31 const unsigned char *new_sha1
,
37 struct diff_filespec
*one
, *two
;
39 if (!is_null_sha1(old_sha1
) && !is_null_sha1(new_sha1
) &&
40 !hashcmp(old_sha1
, new_sha1
) && (old_mode
== new_mode
))
43 if (DIFF_OPT_TST(opt
, REVERSE_DIFF
)) {
45 const unsigned char *tmp_u
;
47 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
48 tmp_u
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_u
;
49 tmp_c
= old_name
; old_name
= new_name
; new_name
= tmp_c
;
53 (strncmp(old_name
, opt
->prefix
, opt
->prefix_length
) ||
54 strncmp(new_name
, opt
->prefix
, opt
->prefix_length
)))
57 one
= alloc_filespec(old_name
);
58 two
= alloc_filespec(new_name
);
59 fill_filespec(one
, old_sha1
, old_sha1_valid
, old_mode
);
60 fill_filespec(two
, new_sha1
, new_sha1_valid
, new_mode
);
62 diff_queue(&diff_queued_diff
, one
, two
);
65 static int builtin_diff_b_f(struct rev_info
*revs
,
66 int argc
, const char **argv
,
67 struct blobinfo
*blob
)
69 /* Blob vs file in the working tree*/
74 usage(builtin_diff_usage
);
76 GUARD_PATHSPEC(&revs
->prune_data
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
77 path
= revs
->prune_data
.items
[0].match
;
80 die_errno(_("failed to stat '%s'"), path
);
81 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
82 die(_("'%s': not a regular file or symlink"), path
);
84 diff_set_mnemonic_prefix(&revs
->diffopt
, "o/", "w/");
86 if (blob
[0].mode
== S_IFINVALID
)
87 blob
[0].mode
= canon_mode(st
.st_mode
);
89 stuff_change(&revs
->diffopt
,
90 blob
[0].mode
, canon_mode(st
.st_mode
),
91 blob
[0].sha1
, null_sha1
,
94 diffcore_std(&revs
->diffopt
);
95 diff_flush(&revs
->diffopt
);
99 static int builtin_diff_blobs(struct rev_info
*revs
,
100 int argc
, const char **argv
,
101 struct blobinfo
*blob
)
103 unsigned mode
= canon_mode(S_IFREG
| 0644);
106 usage(builtin_diff_usage
);
108 if (blob
[0].mode
== S_IFINVALID
)
111 if (blob
[1].mode
== S_IFINVALID
)
114 stuff_change(&revs
->diffopt
,
115 blob
[0].mode
, blob
[1].mode
,
116 blob
[0].sha1
, blob
[1].sha1
,
118 blob
[0].name
, blob
[1].name
);
119 diffcore_std(&revs
->diffopt
);
120 diff_flush(&revs
->diffopt
);
124 static int builtin_diff_index(struct rev_info
*revs
,
125 int argc
, const char **argv
)
129 const char *arg
= argv
[1];
130 if (!strcmp(arg
, "--cached") || !strcmp(arg
, "--staged"))
133 usage(builtin_diff_usage
);
137 * Make sure there is one revision (i.e. pending object),
138 * and there is no revision filtering parameters.
140 if (revs
->pending
.nr
!= 1 ||
141 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
143 usage(builtin_diff_usage
);
146 if (read_cache_preload(&revs
->diffopt
.pathspec
) < 0) {
147 perror("read_cache_preload");
150 } else if (read_cache() < 0) {
151 perror("read_cache");
154 return run_diff_index(revs
, cached
);
157 static int builtin_diff_tree(struct rev_info
*revs
,
158 int argc
, const char **argv
,
159 struct object_array_entry
*ent0
,
160 struct object_array_entry
*ent1
)
162 const unsigned char *(sha1
[2]);
166 usage(builtin_diff_usage
);
169 * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
172 if (ent1
->item
->flags
& UNINTERESTING
)
174 sha1
[swap
] = ent0
->item
->sha1
;
175 sha1
[1 - swap
] = ent1
->item
->sha1
;
176 diff_tree_sha1(sha1
[0], sha1
[1], "", &revs
->diffopt
);
177 log_tree_diff_flush(revs
);
181 static int builtin_diff_combined(struct rev_info
*revs
,
182 int argc
, const char **argv
,
183 struct object_array_entry
*ent
,
186 struct sha1_array parents
= SHA1_ARRAY_INIT
;
190 usage(builtin_diff_usage
);
192 if (!revs
->dense_combined_merges
&& !revs
->combine_merges
)
193 revs
->dense_combined_merges
= revs
->combine_merges
= 1;
194 for (i
= 1; i
< ents
; i
++)
195 sha1_array_append(&parents
, ent
[i
].item
->sha1
);
196 diff_tree_combined(ent
[0].item
->sha1
, &parents
,
197 revs
->dense_combined_merges
, revs
);
198 sha1_array_clear(&parents
);
202 static void refresh_index_quietly(void)
204 struct lock_file
*lock_file
;
207 lock_file
= xcalloc(1, sizeof(struct lock_file
));
208 fd
= hold_locked_index(lock_file
, 0);
213 refresh_cache(REFRESH_QUIET
|REFRESH_UNMERGED
);
214 update_index_if_able(&the_index
, lock_file
);
217 static int builtin_diff_files(struct rev_info
*revs
, int argc
, const char **argv
)
219 unsigned int options
= 0;
221 while (1 < argc
&& argv
[1][0] == '-') {
222 if (!strcmp(argv
[1], "--base"))
224 else if (!strcmp(argv
[1], "--ours"))
226 else if (!strcmp(argv
[1], "--theirs"))
228 else if (!strcmp(argv
[1], "-q"))
229 options
|= DIFF_SILENT_ON_REMOVED
;
230 else if (!strcmp(argv
[1], "-h"))
231 usage(builtin_diff_usage
);
233 return error(_("invalid option: %s"), argv
[1]);
238 * "diff --base" should not combine merges because it was not
239 * asked to. "diff -c" should not densify (if the user wants
240 * dense one, --cc can be explicitly asked for, or just rely
243 if (revs
->max_count
== -1 && !revs
->combine_merges
&&
244 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
245 revs
->combine_merges
= revs
->dense_combined_merges
= 1;
248 if (read_cache_preload(&revs
->diffopt
.pathspec
) < 0) {
249 perror("read_cache_preload");
252 return run_diff_files(revs
, options
);
255 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
259 struct object_array ent
= OBJECT_ARRAY_INIT
;
260 int blobs
= 0, paths
= 0;
261 struct blobinfo blob
[2];
266 * We could get N tree-ish in the rev.pending_objects list.
267 * Also there could be M blobs there, and P pathspecs.
270 * cache vs files (diff-files)
272 * compare two random blobs. P must be zero.
274 * compare a blob with a working tree file.
277 * tree vs cache (diff-index --cached)
280 * tree vs tree (diff-tree)
283 * compare two filesystem entities (aka --no-index).
285 * Other cases are errors.
288 prefix
= setup_git_directory_gently(&nongit
);
290 git_config(git_diff_ui_config
, NULL
);
292 init_revisions(&rev
, prefix
);
294 /* If this is a no-index diff, just run it and exit there. */
295 diff_no_index(&rev
, argc
, argv
, nongit
, prefix
);
297 /* Otherwise, we are doing the usual "git" diff */
298 rev
.diffopt
.skip_stat_unmatch
= !!diff_auto_refresh_index
;
300 /* Scale to real terminal size and respect statGraphWidth config */
301 rev
.diffopt
.stat_width
= -1;
302 rev
.diffopt
.stat_graph_width
= -1;
304 /* Default to let external and textconv be used */
305 DIFF_OPT_SET(&rev
.diffopt
, ALLOW_EXTERNAL
);
306 DIFF_OPT_SET(&rev
.diffopt
, ALLOW_TEXTCONV
);
309 die(_("Not a git repository"));
310 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
311 if (!rev
.diffopt
.output_format
) {
312 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
313 diff_setup_done(&rev
.diffopt
);
316 DIFF_OPT_SET(&rev
.diffopt
, RECURSIVE
);
318 setup_diff_pager(&rev
.diffopt
);
321 * Do we have --cached and not have a pending object, then
322 * default to HEAD by hand. Eek.
324 if (!rev
.pending
.nr
) {
326 for (i
= 1; i
< argc
; i
++) {
327 const char *arg
= argv
[i
];
328 if (!strcmp(arg
, "--"))
330 else if (!strcmp(arg
, "--cached") ||
331 !strcmp(arg
, "--staged")) {
332 add_head_to_pending(&rev
);
333 if (!rev
.pending
.nr
) {
335 tree
= lookup_tree(EMPTY_TREE_SHA1_BIN
);
336 add_pending_object(&rev
, &tree
->object
, "HEAD");
343 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
344 struct object_array_entry
*entry
= &rev
.pending
.objects
[i
];
345 struct object
*obj
= entry
->item
;
346 const char *name
= entry
->name
;
347 int flags
= (obj
->flags
& UNINTERESTING
);
349 obj
= parse_object(obj
->sha1
);
350 obj
= deref_tag(obj
, NULL
, 0);
352 die(_("invalid object '%s' given."), name
);
353 if (obj
->type
== OBJ_COMMIT
)
354 obj
= &((struct commit
*)obj
)->tree
->object
;
356 if (obj
->type
== OBJ_TREE
) {
358 add_object_array(obj
, name
, &ent
);
359 } else if (obj
->type
== OBJ_BLOB
) {
361 die(_("more than two blobs given: '%s'"), name
);
362 hashcpy(blob
[blobs
].sha1
, obj
->sha1
);
363 blob
[blobs
].name
= name
;
364 blob
[blobs
].mode
= entry
->mode
;
368 die(_("unhandled object '%s' given."), name
);
371 if (rev
.prune_data
.nr
)
372 paths
+= rev
.prune_data
.nr
;
375 * Now, do the arguments look reasonable?
380 result
= builtin_diff_files(&rev
, argc
, argv
);
384 usage(builtin_diff_usage
);
385 result
= builtin_diff_b_f(&rev
, argc
, argv
, blob
);
389 usage(builtin_diff_usage
);
390 result
= builtin_diff_blobs(&rev
, argc
, argv
, blob
);
393 usage(builtin_diff_usage
);
397 usage(builtin_diff_usage
);
398 else if (ent
.nr
== 1)
399 result
= builtin_diff_index(&rev
, argc
, argv
);
400 else if (ent
.nr
== 2)
401 result
= builtin_diff_tree(&rev
, argc
, argv
,
402 &ent
.objects
[0], &ent
.objects
[1]);
403 else if (ent
.objects
[0].item
->flags
& UNINTERESTING
) {
405 * diff A...B where there is at least one merge base
406 * between A and B. We have ent.objects[0] ==
407 * merge-base, ent.objects[ents-2] == A, and
408 * ent.objects[ents-1] == B. Show diff between the
409 * base and B. Note that we pick one merge base at
410 * random if there are more than one.
412 result
= builtin_diff_tree(&rev
, argc
, argv
,
414 &ent
.objects
[ent
.nr
-1]);
416 result
= builtin_diff_combined(&rev
, argc
, argv
,
417 ent
.objects
, ent
.nr
);
418 result
= diff_result_code(&rev
.diffopt
, result
);
419 if (1 < rev
.diffopt
.skip_stat_unmatch
)
420 refresh_index_quietly();