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 * rev.max_count is reasonable (0 <= n <= 3),
51 * there is no other revision filtering parameters.
53 if (revs
->pending_objects
||
54 revs
->min_age
!= -1 ||
56 usage(builtin_diff_usage
);
58 * Backward compatibility wart - "diff-files -s" used to
59 * defeat the common diff option "-s" which asked for
60 * DIFF_FORMAT_NO_OUTPUT.
62 if (revs
->diffopt
.output_format
== DIFF_FORMAT_NO_OUTPUT
)
63 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
64 return run_diff_files(revs
, silent
);
67 static void stuff_change(struct diff_options
*opt
,
68 unsigned old_mode
, unsigned new_mode
,
69 const unsigned char *old_sha1
,
70 const unsigned char *new_sha1
,
74 struct diff_filespec
*one
, *two
;
76 if (memcmp(null_sha1
, old_sha1
, 20) &&
77 memcmp(null_sha1
, new_sha1
, 20) &&
78 !memcmp(old_sha1
, new_sha1
, 20))
81 if (opt
->reverse_diff
) {
84 const unsigned char *tmp_u
;
86 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
87 tmp_u
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_u
;
88 tmp_c
= old_name
; old_name
= new_name
; new_name
= tmp_c
;
90 one
= alloc_filespec(old_name
);
91 two
= alloc_filespec(new_name
);
92 fill_filespec(one
, old_sha1
, old_mode
);
93 fill_filespec(two
, new_sha1
, new_mode
);
95 /* NEEDSWORK: shouldn't this part of diffopt??? */
96 diff_queue(&diff_queued_diff
, one
, two
);
99 static int builtin_diff_b_f(struct rev_info
*revs
,
100 int argc
, const char **argv
,
101 struct blobinfo
*blob
,
104 /* Blob vs file in the working tree*/
108 const char *arg
= argv
[1];
109 if (!strcmp(arg
, "--raw"))
110 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
112 usage(builtin_diff_usage
);
115 if (lstat(path
, &st
))
116 die("'%s': %s", path
, strerror(errno
));
117 if (!(S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)))
118 die("'%s': not a regular file or symlink", path
);
119 stuff_change(&revs
->diffopt
,
120 canon_mode(st
.st_mode
), canon_mode(st
.st_mode
),
121 blob
[0].sha1
, null_sha1
,
123 diffcore_std(&revs
->diffopt
);
124 diff_flush(&revs
->diffopt
);
128 static int builtin_diff_blobs(struct rev_info
*revs
,
129 int argc
, const char **argv
,
130 struct blobinfo
*blob
)
133 unsigned mode
= canon_mode(S_IFREG
| 0644);
136 const char *arg
= argv
[1];
137 if (!strcmp(arg
, "--raw"))
138 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
140 usage(builtin_diff_usage
);
143 stuff_change(&revs
->diffopt
,
145 blob
[0].sha1
, blob
[1].sha1
,
146 blob
[1].name
, blob
[1].name
);
147 diffcore_std(&revs
->diffopt
);
148 diff_flush(&revs
->diffopt
);
152 static int builtin_diff_index(struct rev_info
*revs
,
153 int argc
, const char **argv
)
157 const char *arg
= argv
[1];
158 if (!strcmp(arg
, "--cached"))
160 else if (!strcmp(arg
, "--raw"))
161 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
163 usage(builtin_diff_usage
);
167 * Make sure there is one revision (i.e. pending object),
168 * and there is no revision filtering parameters.
170 if (!revs
->pending_objects
|| revs
->pending_objects
->next
||
171 revs
->max_count
!= -1 || revs
->min_age
!= -1 ||
173 usage(builtin_diff_usage
);
174 return run_diff_index(revs
, cached
);
177 static int builtin_diff_tree(struct rev_info
*revs
,
178 int argc
, const char **argv
,
179 struct object_list
*ent
)
181 /* We saw two trees, ent[0] and ent[1].
182 * unless ent[0] is unintesting, they are swapped
184 const unsigned char *(sha1
[2]);
187 const char *arg
= argv
[1];
188 if (!strcmp(arg
, "--raw"))
189 revs
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
191 usage(builtin_diff_usage
);
194 if (ent
[0].item
->flags
& UNINTERESTING
)
196 sha1
[swap
] = ent
[0].item
->sha1
;
197 sha1
[1-swap
] = ent
[1].item
->sha1
;
198 diff_tree_sha1(sha1
[0], sha1
[1], "", &revs
->diffopt
);
199 log_tree_diff_flush(revs
);
203 static void add_head(struct rev_info
*revs
)
205 unsigned char sha1
[20];
207 if (get_sha1("HEAD", sha1
))
209 obj
= parse_object(sha1
);
212 add_object(obj
, &revs
->pending_objects
, NULL
, "HEAD");
215 int cmd_diff(int argc
, const char **argv
, char **envp
)
218 struct object_list
*list
, ent
[2];
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_config
);
244 init_revisions(&rev
);
245 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
247 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
248 /* Do we have --cached and not have a pending object, then
249 * default to HEAD by hand. Eek.
251 if (!rev
.pending_objects
) {
253 for (i
= 1; i
< argc
; i
++) {
254 const char *arg
= argv
[i
];
255 if (!strcmp(arg
, "--"))
257 else if (!strcmp(arg
, "--cached")) {
264 for (list
= rev
.pending_objects
; list
; list
= list
->next
) {
265 struct object
*obj
= list
->item
;
266 const char *name
= list
->name
;
267 int flags
= (obj
->flags
& UNINTERESTING
);
269 obj
= parse_object(obj
->sha1
);
270 obj
= deref_tag(obj
, NULL
, 0);
272 die("invalid object '%s' given.", name
);
273 if (!strcmp(obj
->type
, commit_type
))
274 obj
= &((struct commit
*)obj
)->tree
->object
;
275 if (!strcmp(obj
->type
, tree_type
)) {
277 die("more than two trees given: '%s'", name
);
279 ent
[ents
].item
= obj
;
280 ent
[ents
].name
= name
;
284 if (!strcmp(obj
->type
, blob_type
)) {
286 die("more than two blobs given: '%s'", name
);
287 memcpy(blob
[blobs
].sha1
, obj
->sha1
, 20);
288 blob
[blobs
].name
= name
;
293 die("unhandled object '%s' given.", name
);
295 if (rev
.prune_data
) {
296 const char **pathspec
= rev
.prune_data
;
306 * Now, do the arguments look reasonable?
311 return builtin_diff_files(&rev
, argc
, argv
);
315 usage(builtin_diff_usage
);
316 return builtin_diff_b_f(&rev
, argc
, argv
, blob
, path
);
319 return builtin_diff_blobs(&rev
, argc
, argv
, blob
);
322 usage(builtin_diff_usage
);
326 usage(builtin_diff_usage
);
328 return builtin_diff_index(&rev
, argc
, argv
);
330 return builtin_diff_tree(&rev
, argc
, argv
, ent
);
331 usage(builtin_diff_usage
);