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 "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"))
42 else if (!strcmp(arg
, "--raw"))
43 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
45 usage(builtin_diff_usage
);
49 * Make sure there are NO revision (i.e. pending object) parameter,
50 * specified rev.max_count is reasonable (0 <= n <= 3), and
51 * there is no other revision filtering parameter.
53 if (revs
->pending_objects
||
54 revs
->min_age
!= -1 ||
55 revs
->max_age
!= -1 ||
57 usage(builtin_diff_usage
);
58 if (revs
->max_count
< 0 &&
59 (revs
->diffopt
.output_format
== DIFF_FORMAT_PATCH
))
60 revs
->combine_merges
= revs
->dense_combined_merges
= 1;
62 * Backward compatibility wart - "diff-files -s" used to
63 * defeat the common diff option "-s" which asked for
64 * DIFF_FORMAT_NO_OUTPUT.
66 if (revs
->diffopt
.output_format
== DIFF_FORMAT_NO_OUTPUT
)
67 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
68 return run_diff_files(revs
, silent
);
71 static void stuff_change(struct diff_options
*opt
,
72 unsigned old_mode
, unsigned new_mode
,
73 const unsigned char *old_sha1
,
74 const unsigned char *new_sha1
,
78 struct diff_filespec
*one
, *two
;
80 if (memcmp(null_sha1
, old_sha1
, 20) &&
81 memcmp(null_sha1
, new_sha1
, 20) &&
82 !memcmp(old_sha1
, new_sha1
, 20))
85 if (opt
->reverse_diff
) {
88 const unsigned char *tmp_u
;
90 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
91 tmp_u
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_u
;
92 tmp_c
= old_name
; old_name
= new_name
; new_name
= tmp_c
;
94 one
= alloc_filespec(old_name
);
95 two
= alloc_filespec(new_name
);
96 fill_filespec(one
, old_sha1
, old_mode
);
97 fill_filespec(two
, new_sha1
, new_mode
);
99 /* NEEDSWORK: shouldn't this part of diffopt??? */
100 diff_queue(&diff_queued_diff
, one
, two
);
103 static int builtin_diff_b_f(struct rev_info
*revs
,
104 int argc
, const char **argv
,
105 struct blobinfo
*blob
,
108 /* Blob vs file in the working tree*/
112 const char *arg
= argv
[1];
113 if (!strcmp(arg
, "--raw"))
114 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
116 usage(builtin_diff_usage
);
119 if (lstat(path
, &st
))
120 die("'%s': %s", path
, strerror(errno
));
121 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
122 die("'%s': not a regular file or symlink", path
);
123 stuff_change(&revs
->diffopt
,
124 canon_mode(st
.st_mode
), canon_mode(st
.st_mode
),
125 blob
[0].sha1
, null_sha1
,
127 diffcore_std(&revs
->diffopt
);
128 diff_flush(&revs
->diffopt
);
132 static int builtin_diff_blobs(struct rev_info
*revs
,
133 int argc
, const char **argv
,
134 struct blobinfo
*blob
)
137 unsigned mode
= canon_mode(S_IFREG
| 0644);
140 const char *arg
= argv
[1];
141 if (!strcmp(arg
, "--raw"))
142 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
144 usage(builtin_diff_usage
);
147 stuff_change(&revs
->diffopt
,
149 blob
[0].sha1
, blob
[1].sha1
,
150 blob
[1].name
, blob
[1].name
);
151 diffcore_std(&revs
->diffopt
);
152 diff_flush(&revs
->diffopt
);
156 static int builtin_diff_index(struct rev_info
*revs
,
157 int argc
, const char **argv
)
161 const char *arg
= argv
[1];
162 if (!strcmp(arg
, "--cached"))
164 else if (!strcmp(arg
, "--raw"))
165 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
167 usage(builtin_diff_usage
);
171 * Make sure there is one revision (i.e. pending object),
172 * and there is no revision filtering parameters.
174 if (!revs
->pending_objects
|| revs
->pending_objects
->next
||
175 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
177 usage(builtin_diff_usage
);
178 return run_diff_index(revs
, cached
);
181 static int builtin_diff_tree(struct rev_info
*revs
,
182 int argc
, const char **argv
,
183 struct object_list
*ent
)
185 const unsigned char *(sha1
[2]);
188 const char *arg
= argv
[1];
189 if (!strcmp(arg
, "--raw"))
190 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
192 usage(builtin_diff_usage
);
196 /* We saw two trees, ent[0] and ent[1].
197 * unless ent[0] is unintesting, they are swapped
199 if (ent
[0].item
->flags
& UNINTERESTING
)
201 sha1
[swap
] = ent
[0].item
->sha1
;
202 sha1
[1-swap
] = ent
[1].item
->sha1
;
203 diff_tree_sha1(sha1
[0], sha1
[1], "", &revs
->diffopt
);
204 log_tree_diff_flush(revs
);
208 static int builtin_diff_combined(struct rev_info
*revs
,
209 int argc
, const char **argv
,
210 struct object_list
*ent
,
213 const unsigned char (*parent
)[20];
217 const char *arg
= argv
[1];
218 if (!strcmp(arg
, "--raw"))
219 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
221 usage(builtin_diff_usage
);
224 if (!revs
->dense_combined_merges
&& !revs
->combine_merges
)
225 revs
->dense_combined_merges
= revs
->combine_merges
= 1;
226 parent
= xmalloc(ents
* sizeof(*parent
));
227 /* Again, the revs are all reverse */
228 for (i
= 0; i
< ents
; i
++)
229 memcpy(parent
+ i
, ent
[ents
- 1 - i
].item
->sha1
, 20);
230 diff_tree_combined(parent
[0], parent
+ 1, ents
- 1,
231 revs
->dense_combined_merges
, revs
);
235 static void add_head(struct rev_info
*revs
)
237 unsigned char sha1
[20];
239 if (get_sha1("HEAD", sha1
))
241 obj
= parse_object(sha1
);
244 add_object(obj
, &revs
->pending_objects
, NULL
, "HEAD");
247 int cmd_diff(int argc
, const char **argv
, char **envp
)
250 struct object_list
*list
, ent
[100];
251 int ents
= 0, blobs
= 0, paths
= 0;
252 const char *path
= NULL
;
253 struct blobinfo blob
[2];
256 * We could get N tree-ish in the rev.pending_objects list.
257 * Also there could be M blobs there, and P pathspecs.
260 * cache vs files (diff-files)
262 * compare two random blobs. P must be zero.
264 * compare a blob with a working tree file.
267 * tree vs cache (diff-index --cached)
270 * tree vs tree (diff-tree)
272 * Other cases are errors.
275 git_config(git_diff_config
);
276 init_revisions(&rev
);
277 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
279 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
280 /* Do we have --cached and not have a pending object, then
281 * default to HEAD by hand. Eek.
283 if (!rev
.pending_objects
) {
285 for (i
= 1; i
< argc
; i
++) {
286 const char *arg
= argv
[i
];
287 if (!strcmp(arg
, "--"))
289 else if (!strcmp(arg
, "--cached")) {
296 for (list
= rev
.pending_objects
; list
; list
= list
->next
) {
297 struct object
*obj
= list
->item
;
298 const char *name
= list
->name
;
299 int flags
= (obj
->flags
& UNINTERESTING
);
301 obj
= parse_object(obj
->sha1
);
302 obj
= deref_tag(obj
, NULL
, 0);
304 die("invalid object '%s' given.", name
);
305 if (!strcmp(obj
->type
, commit_type
))
306 obj
= &((struct commit
*)obj
)->tree
->object
;
307 if (!strcmp(obj
->type
, tree_type
)) {
308 if (ARRAY_SIZE(ent
) <= ents
)
309 die("more than %d trees given: '%s'",
310 (int) ARRAY_SIZE(ent
), name
);
312 ent
[ents
].item
= obj
;
313 ent
[ents
].name
= name
;
317 if (!strcmp(obj
->type
, blob_type
)) {
319 die("more than two blobs given: '%s'", name
);
320 memcpy(blob
[blobs
].sha1
, obj
->sha1
, 20);
321 blob
[blobs
].name
= name
;
326 die("unhandled object '%s' given.", name
);
328 if (rev
.prune_data
) {
329 const char **pathspec
= rev
.prune_data
;
339 * Now, do the arguments look reasonable?
344 return builtin_diff_files(&rev
, argc
, argv
);
348 usage(builtin_diff_usage
);
349 return builtin_diff_b_f(&rev
, argc
, argv
, blob
, path
);
353 usage(builtin_diff_usage
);
354 return builtin_diff_blobs(&rev
, argc
, argv
, blob
);
357 usage(builtin_diff_usage
);
361 usage(builtin_diff_usage
);
363 return builtin_diff_index(&rev
, argc
, argv
);
365 return builtin_diff_tree(&rev
, argc
, argv
, ent
);
367 return builtin_diff_combined(&rev
, argc
, argv
, ent
, ents
);
368 usage(builtin_diff_usage
);