]>
Commit | Line | Data |
---|---|---|
65056021 JH |
1 | /* |
2 | * Builtin "git diff" | |
3 | * | |
4 | * Copyright (c) 2006 Junio C Hamano | |
5 | */ | |
6 | #include "cache.h" | |
7 | #include "commit.h" | |
8 | #include "blob.h" | |
9 | #include "tag.h" | |
10 | #include "diff.h" | |
11 | #include "diffcore.h" | |
12 | #include "revision.h" | |
13 | #include "log-tree.h" | |
14 | #include "builtin.h" | |
15 | ||
16 | /* NEEDSWORK: struct object has place for name but we _do_ | |
17 | * know mode when we extracted the blob out of a tree, which | |
18 | * we currently lose. | |
19 | */ | |
20 | struct blobinfo { | |
21 | unsigned char sha1[20]; | |
22 | const char *name; | |
23 | }; | |
24 | ||
25 | static const char builtin_diff_usage[] = | |
15e593e4 | 26 | "git-diff <options> <rev>{0,2} -- <path>*"; |
65056021 | 27 | |
65056021 JH |
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, | |
32 | const char *old_name, | |
33 | const char *new_name) | |
34 | { | |
35 | struct diff_filespec *one, *two; | |
36 | ||
0bef57ee | 37 | if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) && |
a89fccd2 | 38 | !hashcmp(old_sha1, new_sha1)) |
65056021 JH |
39 | return; |
40 | ||
41 | if (opt->reverse_diff) { | |
42 | unsigned tmp; | |
d92f1dc6 | 43 | const unsigned char *tmp_u; |
65056021 JH |
44 | const char *tmp_c; |
45 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; | |
46 | tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u; | |
47 | tmp_c = old_name; old_name = new_name; new_name = tmp_c; | |
48 | } | |
49 | one = alloc_filespec(old_name); | |
50 | two = alloc_filespec(new_name); | |
51 | fill_filespec(one, old_sha1, old_mode); | |
52 | fill_filespec(two, new_sha1, new_mode); | |
53 | ||
54 | /* NEEDSWORK: shouldn't this part of diffopt??? */ | |
55 | diff_queue(&diff_queued_diff, one, two); | |
56 | } | |
57 | ||
58 | static int builtin_diff_b_f(struct rev_info *revs, | |
59 | int argc, const char **argv, | |
60 | struct blobinfo *blob, | |
61 | const char *path) | |
62 | { | |
63 | /* Blob vs file in the working tree*/ | |
64 | struct stat st; | |
65 | ||
a610786f TH |
66 | if (argc > 1) |
67 | usage(builtin_diff_usage); | |
68 | ||
65056021 JH |
69 | if (lstat(path, &st)) |
70 | die("'%s': %s", path, strerror(errno)); | |
71 | if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) | |
72 | die("'%s': not a regular file or symlink", path); | |
73 | stuff_change(&revs->diffopt, | |
74 | canon_mode(st.st_mode), canon_mode(st.st_mode), | |
75 | blob[0].sha1, null_sha1, | |
065e0b12 | 76 | path, path); |
65056021 JH |
77 | diffcore_std(&revs->diffopt); |
78 | diff_flush(&revs->diffopt); | |
79 | return 0; | |
80 | } | |
81 | ||
82 | static int builtin_diff_blobs(struct rev_info *revs, | |
83 | int argc, const char **argv, | |
84 | struct blobinfo *blob) | |
85 | { | |
65056021 JH |
86 | unsigned mode = canon_mode(S_IFREG | 0644); |
87 | ||
a610786f TH |
88 | if (argc > 1) |
89 | usage(builtin_diff_usage); | |
90 | ||
65056021 JH |
91 | stuff_change(&revs->diffopt, |
92 | mode, mode, | |
f82cd3c6 | 93 | blob[0].sha1, blob[1].sha1, |
53dd8a9c | 94 | blob[0].name, blob[1].name); |
65056021 JH |
95 | diffcore_std(&revs->diffopt); |
96 | diff_flush(&revs->diffopt); | |
97 | return 0; | |
98 | } | |
99 | ||
100 | static int builtin_diff_index(struct rev_info *revs, | |
101 | int argc, const char **argv) | |
102 | { | |
103 | int cached = 0; | |
104 | while (1 < argc) { | |
105 | const char *arg = argv[1]; | |
f2dd1c9a | 106 | if (!strcmp(arg, "--cached")) |
65056021 | 107 | cached = 1; |
65056021 JH |
108 | else |
109 | usage(builtin_diff_usage); | |
110 | argv++; argc--; | |
111 | } | |
112 | /* | |
113 | * Make sure there is one revision (i.e. pending object), | |
114 | * and there is no revision filtering parameters. | |
115 | */ | |
1f1e895f | 116 | if (revs->pending.nr != 1 || |
65056021 JH |
117 | revs->max_count != -1 || revs->min_age != -1 || |
118 | revs->max_age != -1) | |
119 | usage(builtin_diff_usage); | |
b4e1e4a7 JH |
120 | if (read_cache() < 0) { |
121 | perror("read_cache"); | |
122 | return -1; | |
123 | } | |
65056021 JH |
124 | return run_diff_index(revs, cached); |
125 | } | |
126 | ||
127 | static int builtin_diff_tree(struct rev_info *revs, | |
128 | int argc, const char **argv, | |
1f1e895f | 129 | struct object_array_entry *ent) |
65056021 | 130 | { |
65056021 | 131 | const unsigned char *(sha1[2]); |
1f1e895f | 132 | int swap = 0; |
a610786f TH |
133 | |
134 | if (argc > 1) | |
135 | usage(builtin_diff_usage); | |
0fe7c1de JH |
136 | |
137 | /* We saw two trees, ent[0] and ent[1]. | |
82e5a82f | 138 | * if ent[1] is uninteresting, they are swapped |
0fe7c1de | 139 | */ |
1f1e895f LT |
140 | if (ent[1].item->flags & UNINTERESTING) |
141 | swap = 1; | |
65056021 JH |
142 | sha1[swap] = ent[0].item->sha1; |
143 | sha1[1-swap] = ent[1].item->sha1; | |
144 | diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt); | |
145 | log_tree_diff_flush(revs); | |
146 | return 0; | |
147 | } | |
148 | ||
0fe7c1de JH |
149 | static int builtin_diff_combined(struct rev_info *revs, |
150 | int argc, const char **argv, | |
1f1e895f | 151 | struct object_array_entry *ent, |
0fe7c1de JH |
152 | int ents) |
153 | { | |
154 | const unsigned char (*parent)[20]; | |
155 | int i; | |
156 | ||
a610786f TH |
157 | if (argc > 1) |
158 | usage(builtin_diff_usage); | |
159 | ||
0fe7c1de JH |
160 | if (!revs->dense_combined_merges && !revs->combine_merges) |
161 | revs->dense_combined_merges = revs->combine_merges = 1; | |
162 | parent = xmalloc(ents * sizeof(*parent)); | |
163 | /* Again, the revs are all reverse */ | |
164 | for (i = 0; i < ents; i++) | |
75b62b48 JS |
165 | hashcpy((unsigned char *)(parent + i), |
166 | ent[ents - 1 - i].item->sha1); | |
0fe7c1de JH |
167 | diff_tree_combined(parent[0], parent + 1, ents - 1, |
168 | revs->dense_combined_merges, revs); | |
169 | return 0; | |
170 | } | |
171 | ||
e686eb98 | 172 | void add_head(struct rev_info *revs) |
65056021 JH |
173 | { |
174 | unsigned char sha1[20]; | |
175 | struct object *obj; | |
176 | if (get_sha1("HEAD", sha1)) | |
177 | return; | |
178 | obj = parse_object(sha1); | |
179 | if (!obj) | |
180 | return; | |
1f1e895f | 181 | add_pending_object(revs, obj, "HEAD"); |
65056021 JH |
182 | } |
183 | ||
a633fca0 | 184 | int cmd_diff(int argc, const char **argv, const char *prefix) |
65056021 | 185 | { |
1f1e895f | 186 | int i; |
65056021 | 187 | struct rev_info rev; |
1f1e895f | 188 | struct object_array_entry ent[100]; |
65056021 | 189 | int ents = 0, blobs = 0, paths = 0; |
a633fca0 | 190 | const char *path = NULL; |
65056021 | 191 | struct blobinfo blob[2]; |
d516c2d1 | 192 | int nongit = 0; |
41bbf9d5 | 193 | int result = 0; |
65056021 JH |
194 | |
195 | /* | |
196 | * We could get N tree-ish in the rev.pending_objects list. | |
197 | * Also there could be M blobs there, and P pathspecs. | |
198 | * | |
199 | * N=0, M=0: | |
200 | * cache vs files (diff-files) | |
201 | * N=0, M=2: | |
202 | * compare two random blobs. P must be zero. | |
203 | * N=0, M=1, P=1: | |
204 | * compare a blob with a working tree file. | |
205 | * | |
206 | * N=1, M=0: | |
207 | * tree vs cache (diff-index --cached) | |
208 | * | |
209 | * N=2, M=0: | |
210 | * tree vs tree (diff-tree) | |
211 | * | |
212 | * Other cases are errors. | |
213 | */ | |
230f544e | 214 | |
d516c2d1 | 215 | prefix = setup_git_directory_gently(&nongit); |
ef1d9c5a | 216 | git_config(git_diff_ui_config); |
db6296a5 | 217 | init_revisions(&rev, prefix); |
65056021 | 218 | |
fcfa33ec JS |
219 | if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix)) |
220 | argc = 0; | |
221 | else | |
222 | argc = setup_revisions(argc, argv, &rev, NULL); | |
047fbe90 | 223 | if (!rev.diffopt.output_format) { |
c9b5ef99 | 224 | rev.diffopt.output_format = DIFF_FORMAT_PATCH; |
72ee96c0 JH |
225 | if (diff_setup_done(&rev.diffopt) < 0) |
226 | die("diff_setup_done failed"); | |
047fbe90 | 227 | } |
c9b5ef99 | 228 | |
65056021 JH |
229 | /* Do we have --cached and not have a pending object, then |
230 | * default to HEAD by hand. Eek. | |
231 | */ | |
1f1e895f | 232 | if (!rev.pending.nr) { |
65056021 JH |
233 | int i; |
234 | for (i = 1; i < argc; i++) { | |
235 | const char *arg = argv[i]; | |
236 | if (!strcmp(arg, "--")) | |
237 | break; | |
238 | else if (!strcmp(arg, "--cached")) { | |
239 | add_head(&rev); | |
6c09c451 JH |
240 | if (!rev.pending.nr) |
241 | die("No HEAD commit to compare with (yet)"); | |
65056021 JH |
242 | break; |
243 | } | |
244 | } | |
245 | } | |
246 | ||
1f1e895f LT |
247 | for (i = 0; i < rev.pending.nr; i++) { |
248 | struct object_array_entry *list = rev.pending.objects+i; | |
65056021 JH |
249 | struct object *obj = list->item; |
250 | const char *name = list->name; | |
251 | int flags = (obj->flags & UNINTERESTING); | |
252 | if (!obj->parsed) | |
253 | obj = parse_object(obj->sha1); | |
254 | obj = deref_tag(obj, NULL, 0); | |
255 | if (!obj) | |
256 | die("invalid object '%s' given.", name); | |
1974632c | 257 | if (obj->type == OBJ_COMMIT) |
65056021 | 258 | obj = &((struct commit *)obj)->tree->object; |
1974632c | 259 | if (obj->type == OBJ_TREE) { |
0fe7c1de JH |
260 | if (ARRAY_SIZE(ent) <= ents) |
261 | die("more than %d trees given: '%s'", | |
334b506a | 262 | (int) ARRAY_SIZE(ent), name); |
65056021 JH |
263 | obj->flags |= flags; |
264 | ent[ents].item = obj; | |
265 | ent[ents].name = name; | |
266 | ents++; | |
267 | continue; | |
268 | } | |
1974632c | 269 | if (obj->type == OBJ_BLOB) { |
65056021 JH |
270 | if (2 <= blobs) |
271 | die("more than two blobs given: '%s'", name); | |
e702496e | 272 | hashcpy(blob[blobs].sha1, obj->sha1); |
65056021 JH |
273 | blob[blobs].name = name; |
274 | blobs++; | |
275 | continue; | |
230f544e | 276 | |
65056021 JH |
277 | } |
278 | die("unhandled object '%s' given.", name); | |
279 | } | |
280 | if (rev.prune_data) { | |
281 | const char **pathspec = rev.prune_data; | |
282 | while (*pathspec) { | |
283 | if (!path) | |
284 | path = *pathspec; | |
285 | paths++; | |
286 | pathspec++; | |
287 | } | |
288 | } | |
289 | ||
290 | /* | |
291 | * Now, do the arguments look reasonable? | |
292 | */ | |
293 | if (!ents) { | |
294 | switch (blobs) { | |
295 | case 0: | |
41bbf9d5 | 296 | result = run_diff_files_cmd(&rev, argc, argv); |
65056021 JH |
297 | break; |
298 | case 1: | |
299 | if (paths != 1) | |
300 | usage(builtin_diff_usage); | |
41bbf9d5 | 301 | result = builtin_diff_b_f(&rev, argc, argv, blob, path); |
65056021 JH |
302 | break; |
303 | case 2: | |
0fe7c1de JH |
304 | if (paths) |
305 | usage(builtin_diff_usage); | |
41bbf9d5 | 306 | result = builtin_diff_blobs(&rev, argc, argv, blob); |
65056021 JH |
307 | break; |
308 | default: | |
309 | usage(builtin_diff_usage); | |
310 | } | |
311 | } | |
312 | else if (blobs) | |
313 | usage(builtin_diff_usage); | |
314 | else if (ents == 1) | |
41bbf9d5 | 315 | result = builtin_diff_index(&rev, argc, argv); |
65056021 | 316 | else if (ents == 2) |
41bbf9d5 | 317 | result = builtin_diff_tree(&rev, argc, argv, ent); |
9919f41c JH |
318 | else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) { |
319 | /* diff A...B where there is one sane merge base between | |
320 | * A and B. We have ent[0] == merge-base, ent[1] == A, | |
321 | * and ent[2] == B. Show diff between the base and B. | |
322 | */ | |
306ea2df | 323 | ent[1] = ent[2]; |
41bbf9d5 | 324 | result = builtin_diff_tree(&rev, argc, argv, ent); |
9919f41c | 325 | } |
0fe7c1de | 326 | else |
41bbf9d5 | 327 | result = builtin_diff_combined(&rev, argc, argv, |
9919f41c | 328 | ent, ents); |
41bbf9d5 AR |
329 | if (rev.diffopt.exit_with_status) |
330 | result = rev.diffopt.has_changes; | |
331 | return result; | |
65056021 | 332 | } |