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 (!is_null_sha1(old_sha1
) && !is_null_sha1(new_sha1
) &&
72 !hashcmp(old_sha1
, new_sha1
))
75 if (opt
->reverse_diff
) {
77 const unsigned char *tmp_u
;
79 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
80 tmp_u
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_u
;
81 tmp_c
= old_name
; old_name
= new_name
; new_name
= tmp_c
;
83 one
= alloc_filespec(old_name
);
84 two
= alloc_filespec(new_name
);
85 fill_filespec(one
, old_sha1
, old_mode
);
86 fill_filespec(two
, new_sha1
, new_mode
);
88 /* NEEDSWORK: shouldn't this part of diffopt??? */
89 diff_queue(&diff_queued_diff
, one
, two
);
92 static int builtin_diff_b_f(struct rev_info
*revs
,
93 int argc
, const char **argv
,
94 struct blobinfo
*blob
,
97 /* Blob vs file in the working tree*/
101 usage(builtin_diff_usage
);
103 if (lstat(path
, &st
))
104 die("'%s': %s", path
, strerror(errno
));
105 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
106 die("'%s': not a regular file or symlink", path
);
107 stuff_change(&revs
->diffopt
,
108 canon_mode(st
.st_mode
), canon_mode(st
.st_mode
),
109 blob
[0].sha1
, null_sha1
,
111 diffcore_std(&revs
->diffopt
);
112 diff_flush(&revs
->diffopt
);
116 static int builtin_diff_blobs(struct rev_info
*revs
,
117 int argc
, const char **argv
,
118 struct blobinfo
*blob
)
120 unsigned mode
= canon_mode(S_IFREG
| 0644);
123 usage(builtin_diff_usage
);
125 stuff_change(&revs
->diffopt
,
127 blob
[0].sha1
, blob
[1].sha1
,
128 blob
[0].name
, blob
[1].name
);
129 diffcore_std(&revs
->diffopt
);
130 diff_flush(&revs
->diffopt
);
134 static int builtin_diff_index(struct rev_info
*revs
,
135 int argc
, const char **argv
)
139 const char *arg
= argv
[1];
140 if (!strcmp(arg
, "--cached"))
143 usage(builtin_diff_usage
);
147 * Make sure there is one revision (i.e. pending object),
148 * and there is no revision filtering parameters.
150 if (revs
->pending
.nr
!= 1 ||
151 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
153 usage(builtin_diff_usage
);
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
*ent
)
161 const unsigned char *(sha1
[2]);
165 usage(builtin_diff_usage
);
167 /* We saw two trees, ent[0] and ent[1].
168 * if ent[1] is uninteresting, they are swapped
170 if (ent
[1].item
->flags
& UNINTERESTING
)
172 sha1
[swap
] = ent
[0].item
->sha1
;
173 sha1
[1-swap
] = ent
[1].item
->sha1
;
174 diff_tree_sha1(sha1
[0], sha1
[1], "", &revs
->diffopt
);
175 log_tree_diff_flush(revs
);
179 static int builtin_diff_combined(struct rev_info
*revs
,
180 int argc
, const char **argv
,
181 struct object_array_entry
*ent
,
184 const unsigned char (*parent
)[20];
188 usage(builtin_diff_usage
);
190 if (!revs
->dense_combined_merges
&& !revs
->combine_merges
)
191 revs
->dense_combined_merges
= revs
->combine_merges
= 1;
192 parent
= xmalloc(ents
* sizeof(*parent
));
193 /* Again, the revs are all reverse */
194 for (i
= 0; i
< ents
; i
++)
195 hashcpy((unsigned char*)parent
+ i
, ent
[ents
- 1 - i
].item
->sha1
);
196 diff_tree_combined(parent
[0], parent
+ 1, ents
- 1,
197 revs
->dense_combined_merges
, revs
);
201 void add_head(struct rev_info
*revs
)
203 unsigned char sha1
[20];
205 if (get_sha1("HEAD", sha1
))
207 obj
= parse_object(sha1
);
210 add_pending_object(revs
, obj
, "HEAD");
213 int cmd_diff(int argc
, const char **argv
, const char *prefix
)
217 struct object_array_entry ent
[100];
218 int ents
= 0, blobs
= 0, paths
= 0;
219 const char *path
= NULL
;
220 struct blobinfo blob
[2];
223 * We could get N tree-ish in the rev.pending_objects list.
224 * Also there could be M blobs there, and P pathspecs.
227 * cache vs files (diff-files)
229 * compare two random blobs. P must be zero.
231 * compare a blob with a working tree file.
234 * tree vs cache (diff-index --cached)
237 * tree vs tree (diff-tree)
239 * Other cases are errors.
242 git_config(git_diff_ui_config
);
243 init_revisions(&rev
, prefix
);
245 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
246 if (!rev
.diffopt
.output_format
) {
247 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
248 if (diff_setup_done(&rev
.diffopt
) < 0)
249 die("diff_setup_done failed");
252 /* Do we have --cached and not have a pending object, then
253 * default to HEAD by hand. Eek.
255 if (!rev
.pending
.nr
) {
257 for (i
= 1; i
< argc
; i
++) {
258 const char *arg
= argv
[i
];
259 if (!strcmp(arg
, "--"))
261 else if (!strcmp(arg
, "--cached")) {
268 for (i
= 0; i
< rev
.pending
.nr
; i
++) {
269 struct object_array_entry
*list
= rev
.pending
.objects
+i
;
270 struct object
*obj
= list
->item
;
271 const char *name
= list
->name
;
272 int flags
= (obj
->flags
& UNINTERESTING
);
274 obj
= parse_object(obj
->sha1
);
275 obj
= deref_tag(obj
, NULL
, 0);
277 die("invalid object '%s' given.", name
);
278 if (obj
->type
== OBJ_COMMIT
)
279 obj
= &((struct commit
*)obj
)->tree
->object
;
280 if (obj
->type
== OBJ_TREE
) {
281 if (ARRAY_SIZE(ent
) <= ents
)
282 die("more than %d trees given: '%s'",
283 (int) ARRAY_SIZE(ent
), name
);
285 ent
[ents
].item
= obj
;
286 ent
[ents
].name
= name
;
290 if (obj
->type
== OBJ_BLOB
) {
292 die("more than two blobs given: '%s'", name
);
293 hashcpy(blob
[blobs
].sha1
, obj
->sha1
);
294 blob
[blobs
].name
= name
;
299 die("unhandled object '%s' given.", name
);
301 if (rev
.prune_data
) {
302 const char **pathspec
= rev
.prune_data
;
312 * Now, do the arguments look reasonable?
317 return builtin_diff_files(&rev
, argc
, argv
);
321 usage(builtin_diff_usage
);
322 return builtin_diff_b_f(&rev
, argc
, argv
, blob
, path
);
326 usage(builtin_diff_usage
);
327 return builtin_diff_blobs(&rev
, argc
, argv
, blob
);
330 usage(builtin_diff_usage
);
334 usage(builtin_diff_usage
);
336 return builtin_diff_index(&rev
, argc
, argv
);
338 return builtin_diff_tree(&rev
, argc
, argv
, ent
);
339 else if ((ents
== 3) && (ent
[0].item
->flags
& UNINTERESTING
)) {
340 /* diff A...B where there is one sane merge base between
341 * A and B. We have ent[0] == merge-base, ent[1] == A,
342 * and ent[2] == B. Show diff between the base and B.
345 return builtin_diff_tree(&rev
, argc
, argv
, ent
);
348 return builtin_diff_combined(&rev
, argc
, argv
,
350 usage(builtin_diff_usage
);