4 * Copyright (c) 2006 Junio C Hamano
16 /* NEEDSWORK: struct object has place for name but we _do_
17 * know mode when we extracted the blob out of a tree, which
21 unsigned char sha1
[20];
25 static const char builtin_diff_usage
[] =
26 "git-diff <options> <rev>{0,2} -- <path>*";
28 static int builtin_diff_files(struct rev_info
*revs
,
29 int argc
, const char **argv
)
33 const char *arg
= argv
[1];
34 if (!strcmp(arg
, "--base"))
36 else if (!strcmp(arg
, "--ours"))
38 else if (!strcmp(arg
, "--theirs"))
40 else if (!strcmp(arg
, "-q"))
43 usage(builtin_diff_usage
);
47 * Make sure there are NO revision (i.e. pending object) parameter,
48 * specified rev.max_count is reasonable (0 <= n <= 3), and
49 * there is no other revision filtering parameter.
51 if (revs
->pending
.nr
||
52 revs
->min_age
!= -1 ||
53 revs
->max_age
!= -1 ||
55 usage(builtin_diff_usage
);
56 if (revs
->max_count
< 0 &&
57 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
58 revs
->combine_merges
= revs
->dense_combined_merges
= 1;
59 return run_diff_files(revs
, silent
);
62 static void stuff_change(struct diff_options
*opt
,
63 unsigned old_mode
, unsigned new_mode
,
64 const unsigned char *old_sha1
,
65 const unsigned char *new_sha1
,
69 struct diff_filespec
*one
, *two
;
71 if (memcmp(null_sha1
, old_sha1
, 20) &&
72 memcmp(null_sha1
, new_sha1
, 20) &&
73 !memcmp(old_sha1
, new_sha1
, 20))
76 if (opt
->reverse_diff
) {
78 const unsigned char *tmp_u
;
80 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
81 tmp_u
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_u
;
82 tmp_c
= old_name
; old_name
= new_name
; new_name
= tmp_c
;
84 one
= alloc_filespec(old_name
);
85 two
= alloc_filespec(new_name
);
86 fill_filespec(one
, old_sha1
, old_mode
);
87 fill_filespec(two
, new_sha1
, new_mode
);
89 /* NEEDSWORK: shouldn't this part of diffopt??? */
90 diff_queue(&diff_queued_diff
, one
, two
);
93 static int builtin_diff_b_f(struct rev_info
*revs
,
94 int argc
, const char **argv
,
95 struct blobinfo
*blob
,
98 /* Blob vs file in the working tree*/
102 usage(builtin_diff_usage
);
104 if (lstat(path
, &st
))
105 die("'%s': %s", path
, strerror(errno
));
106 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
107 die("'%s': not a regular file or symlink", path
);
108 stuff_change(&revs
->diffopt
,
109 canon_mode(st
.st_mode
), canon_mode(st
.st_mode
),
110 blob
[0].sha1
, null_sha1
,
112 diffcore_std(&revs
->diffopt
);
113 diff_flush(&revs
->diffopt
);
117 static int builtin_diff_blobs(struct rev_info
*revs
,
118 int argc
, const char **argv
,
119 struct blobinfo
*blob
)
121 unsigned mode
= canon_mode(S_IFREG
| 0644);
124 usage(builtin_diff_usage
);
126 stuff_change(&revs
->diffopt
,
128 blob
[0].sha1
, blob
[1].sha1
,
129 blob
[0].name
, blob
[1].name
);
130 diffcore_std(&revs
->diffopt
);
131 diff_flush(&revs
->diffopt
);
135 static int builtin_diff_index(struct rev_info
*revs
,
136 int argc
, const char **argv
)
140 const char *arg
= argv
[1];
141 if (!strcmp(arg
, "--cached"))
144 usage(builtin_diff_usage
);
148 * Make sure there is one revision (i.e. pending object),
149 * and there is no revision filtering parameters.
151 if (revs
->pending
.nr
!= 1 ||
152 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
154 usage(builtin_diff_usage
);
155 return run_diff_index(revs
, cached
);
158 static int builtin_diff_tree(struct rev_info
*revs
,
159 int argc
, const char **argv
,
160 struct object_array_entry
*ent
)
162 const unsigned char *(sha1
[2]);
166 usage(builtin_diff_usage
);
168 /* We saw two trees, ent[0] and ent[1].
169 * if ent[1] is uninteresting, they are swapped
171 if (ent
[1].item
->flags
& UNINTERESTING
)
173 sha1
[swap
] = ent
[0].item
->sha1
;
174 sha1
[1-swap
] = ent
[1].item
->sha1
;
175 diff_tree_sha1(sha1
[0], sha1
[1], "", &revs
->diffopt
);
176 log_tree_diff_flush(revs
);
180 static int builtin_diff_combined(struct rev_info
*revs
,
181 int argc
, const char **argv
,
182 struct object_array_entry
*ent
,
185 const unsigned char (*parent
)[20];
189 usage(builtin_diff_usage
);
191 if (!revs
->dense_combined_merges
&& !revs
->combine_merges
)
192 revs
->dense_combined_merges
= revs
->combine_merges
= 1;
193 parent
= xmalloc(ents
* sizeof(*parent
));
194 /* Again, the revs are all reverse */
195 for (i
= 0; i
< ents
; i
++)
196 memcpy(parent
+ i
, ent
[ents
- 1 - i
].item
->sha1
, 20);
197 diff_tree_combined(parent
[0], parent
+ 1, ents
- 1,
198 revs
->dense_combined_merges
, revs
);
202 void add_head(struct rev_info
*revs
)
204 unsigned char sha1
[20];
206 if (get_sha1("HEAD", sha1
))
208 obj
= parse_object(sha1
);
211 add_pending_object(revs
, obj
, "HEAD");
214 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
218 struct object_array_entry ent
[100];
219 int ents
= 0, blobs
= 0, paths
= 0;
220 const char *path
= NULL
;
221 struct blobinfo blob
[2];
224 * We could get N tree-ish in the rev.pending_objects list.
225 * Also there could be M blobs there, and P pathspecs.
228 * cache vs files (diff-files)
230 * compare two random blobs. P must be zero.
232 * compare a blob with a working tree file.
235 * tree vs cache (diff-index --cached)
238 * tree vs tree (diff-tree)
240 * Other cases are errors.
243 git_config(git_diff_ui_config
);
244 init_revisions(&rev
, prefix
);
246 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
247 if (!rev
.diffopt
.output_format
) {
248 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
249 if (diff_setup_done(&rev
.diffopt
) < 0)
250 die("diff_setup_done failed");
253 /* Do we have --cached and not have a pending object, then
254 * default to HEAD by hand. Eek.
256 if (!rev
.pending
.nr
) {
258 for (i
= 1; i
< argc
; i
++) {
259 const char *arg
= argv
[i
];
260 if (!strcmp(arg
, "--"))
262 else if (!strcmp(arg
, "--cached")) {
269 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
270 struct object_array_entry
*list
= rev
.pending
.objects
+i
;
271 struct object
*obj
= list
->item
;
272 const char *name
= list
->name
;
273 int flags
= (obj
->flags
& UNINTERESTING
);
275 obj
= parse_object(obj
->sha1
);
276 obj
= deref_tag(obj
, NULL
, 0);
278 die("invalid object '%s' given.", name
);
279 if (obj
->type
== OBJ_COMMIT
)
280 obj
= &((struct commit
*)obj
)->tree
->object
;
281 if (obj
->type
== OBJ_TREE
) {
282 if (ARRAY_SIZE(ent
) <= ents
)
283 die("more than %d trees given: '%s'",
284 (int) ARRAY_SIZE(ent
), name
);
286 ent
[ents
].item
= obj
;
287 ent
[ents
].name
= name
;
291 if (obj
->type
== OBJ_BLOB
) {
293 die("more than two blobs given: '%s'", name
);
294 memcpy(blob
[blobs
].sha1
, obj
->sha1
, 20);
295 blob
[blobs
].name
= name
;
300 die("unhandled object '%s' given.", name
);
302 if (rev
.prune_data
) {
303 const char **pathspec
= rev
.prune_data
;
313 * Now, do the arguments look reasonable?
318 return builtin_diff_files(&rev
, argc
, argv
);
322 usage(builtin_diff_usage
);
323 return builtin_diff_b_f(&rev
, argc
, argv
, blob
, path
);
327 usage(builtin_diff_usage
);
328 return builtin_diff_blobs(&rev
, argc
, argv
, blob
);
331 usage(builtin_diff_usage
);
335 usage(builtin_diff_usage
);
337 return builtin_diff_index(&rev
, argc
, argv
);
339 return builtin_diff_tree(&rev
, argc
, argv
, ent
);
340 else if ((ents
== 3) && (ent
[0].item
->flags
& UNINTERESTING
)) {
341 /* diff A...B where there is one sane merge base between
342 * A and B. We have ent[0] == merge-base, ent[1] == A,
343 * and ent[2] == B. Show diff between the base and B.
346 return builtin_diff_tree(&rev
, argc
, argv
, ent
);
349 return builtin_diff_combined(&rev
, argc
, argv
,
351 usage(builtin_diff_usage
);