]>
Commit | Line | Data |
---|---|---|
e0965d83 | 1 | #include <ctype.h> |
9174026c | 2 | #include "cache.h" |
3ebfd4aa | 3 | #include "diff.h" |
e3bc7a3b | 4 | #include "commit.h" |
9174026c | 5 | |
dc26bd89 | 6 | static int show_root_diff = 0; |
cee99d22 | 7 | static int verbose_header = 0; |
e0965d83 | 8 | static int ignore_merges = 1; |
73134b6d | 9 | static int recursive = 0; |
4cae1a96 | 10 | static int show_tree_entry_in_recursive = 0; |
e0965d83 | 11 | static int read_stdin = 0; |
6b5ee137 JH |
12 | |
13 | static struct diff_options diff_options; | |
14 | ||
f4f21ce3 | 15 | static const char *header = NULL; |
cee99d22 | 16 | static const char *header_prefix = ""; |
000182ea | 17 | static enum cmit_fmt commit_format = CMIT_FMT_RAW; |
9174026c | 18 | |
c5b42386 LT |
19 | // What paths are we interested in? |
20 | static int nr_paths = 0; | |
6b14d7fa | 21 | static const char **paths = NULL; |
c5b42386 LT |
22 | static int *pathlens = NULL; |
23 | ||
eeb79916 | 24 | static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base); |
73134b6d LT |
25 | |
26 | static void update_tree_entry(void **bufp, unsigned long *sizep) | |
9174026c LT |
27 | { |
28 | void *buf = *bufp; | |
29 | unsigned long size = *sizep; | |
30 | int len = strlen(buf) + 1 + 20; | |
31 | ||
32 | if (size < len) | |
2de381f9 | 33 | die("corrupt tree file"); |
9174026c LT |
34 | *bufp = buf + len; |
35 | *sizep = size - len; | |
9174026c LT |
36 | } |
37 | ||
38 | static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep) | |
39 | { | |
40 | int len = strlen(tree)+1; | |
41 | const unsigned char *sha1 = tree + len; | |
42 | const char *path = strchr(tree, ' '); | |
67574c40 | 43 | unsigned int mode; |
9174026c | 44 | |
67574c40 | 45 | if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1) |
2de381f9 | 46 | die("corrupt tree file"); |
9174026c | 47 | *pathp = path+1; |
67574c40 | 48 | *modep = DIFF_FILE_CANON_MODE(mode); |
9174026c LT |
49 | return sha1; |
50 | } | |
51 | ||
262e82b4 LT |
52 | static char *malloc_base(const char *base, const char *path, int pathlen) |
53 | { | |
54 | int baselen = strlen(base); | |
812666c8 | 55 | char *newbase = xmalloc(baselen + pathlen + 2); |
262e82b4 LT |
56 | memcpy(newbase, base, baselen); |
57 | memcpy(newbase + baselen, path, pathlen); | |
58 | memcpy(newbase + baselen + pathlen, "/", 2); | |
59 | return newbase; | |
60 | } | |
61 | ||
62 | static void show_file(const char *prefix, void *tree, unsigned long size, const char *base); | |
ed1a368b | 63 | static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base); |
262e82b4 LT |
64 | |
65 | /* A file entry went away or appeared */ | |
73134b6d | 66 | static void show_file(const char *prefix, void *tree, unsigned long size, const char *base) |
9174026c LT |
67 | { |
68 | unsigned mode; | |
69 | const char *path; | |
70 | const unsigned char *sha1 = extract(tree, size, &path, &mode); | |
262e82b4 LT |
71 | |
72 | if (recursive && S_ISDIR(mode)) { | |
73 | char type[20]; | |
74 | unsigned long size; | |
75 | char *newbase = malloc_base(base, path, strlen(path)); | |
76 | void *tree; | |
77 | ||
78 | tree = read_sha1_file(sha1, type, &size); | |
79 | if (!tree || strcmp(type, "tree")) | |
2de381f9 | 80 | die("corrupt tree sha %s", sha1_to_hex(sha1)); |
262e82b4 LT |
81 | |
82 | show_tree(prefix, tree, size, newbase); | |
57fe64a4 | 83 | |
262e82b4 LT |
84 | free(tree); |
85 | free(newbase); | |
86 | return; | |
87 | } | |
88 | ||
6b5ee137 | 89 | diff_addremove(&diff_options, prefix[0], mode, sha1, base, path); |
9174026c LT |
90 | } |
91 | ||
eeb79916 | 92 | static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base) |
9174026c LT |
93 | { |
94 | unsigned mode1, mode2; | |
95 | const char *path1, *path2; | |
96 | const unsigned char *sha1, *sha2; | |
73134b6d | 97 | int cmp, pathlen1, pathlen2; |
9174026c LT |
98 | |
99 | sha1 = extract(tree1, size1, &path1, &mode1); | |
100 | sha2 = extract(tree2, size2, &path2, &mode2); | |
101 | ||
73134b6d LT |
102 | pathlen1 = strlen(path1); |
103 | pathlen2 = strlen(path2); | |
e46091d5 | 104 | cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2); |
9174026c | 105 | if (cmp < 0) { |
eeb79916 | 106 | show_file("-", tree1, size1, base); |
9174026c LT |
107 | return -1; |
108 | } | |
109 | if (cmp > 0) { | |
eeb79916 | 110 | show_file("+", tree2, size2, base); |
9174026c LT |
111 | return 1; |
112 | } | |
6b5ee137 JH |
113 | if (!diff_options.find_copies_harder && |
114 | !memcmp(sha1, sha2, 20) && mode1 == mode2) | |
9174026c | 115 | return 0; |
262e82b4 LT |
116 | |
117 | /* | |
118 | * If the filemode has changed to/from a directory from/to a regular | |
aebb2679 | 119 | * file, we need to consider it a remove and an add. |
262e82b4 LT |
120 | */ |
121 | if (S_ISDIR(mode1) != S_ISDIR(mode2)) { | |
122 | show_file("-", tree1, size1, base); | |
123 | show_file("+", tree2, size2, base); | |
124 | return 0; | |
125 | } | |
126 | ||
127 | if (recursive && S_ISDIR(mode1)) { | |
eeb79916 | 128 | int retval; |
262e82b4 | 129 | char *newbase = malloc_base(base, path1, pathlen1); |
4cae1a96 | 130 | if (show_tree_entry_in_recursive) |
6b5ee137 JH |
131 | diff_change(&diff_options, mode1, mode2, |
132 | sha1, sha2, base, path1); | |
eeb79916 LT |
133 | retval = diff_tree_sha1(sha1, sha2, newbase); |
134 | free(newbase); | |
135 | return retval; | |
73134b6d LT |
136 | } |
137 | ||
6b5ee137 | 138 | diff_change(&diff_options, mode1, mode2, sha1, sha2, base, path1); |
9174026c LT |
139 | return 0; |
140 | } | |
141 | ||
c5b42386 LT |
142 | static int interesting(void *tree, unsigned long size, const char *base) |
143 | { | |
144 | const char *path; | |
145 | unsigned mode; | |
146 | int i; | |
147 | int baselen, pathlen; | |
148 | ||
149 | if (!nr_paths) | |
150 | return 1; | |
151 | ||
152 | (void)extract(tree, size, &path, &mode); | |
153 | ||
154 | pathlen = strlen(path); | |
155 | baselen = strlen(base); | |
156 | ||
157 | for (i=0; i < nr_paths; i++) { | |
158 | const char *match = paths[i]; | |
159 | int matchlen = pathlens[i]; | |
160 | ||
161 | if (baselen >= matchlen) { | |
162 | /* If it doesn't match, move along... */ | |
163 | if (strncmp(base, match, matchlen)) | |
164 | continue; | |
165 | ||
166 | /* The base is a subdirectory of a path which was specified. */ | |
167 | return 1; | |
168 | } | |
169 | ||
170 | /* Does the base match? */ | |
171 | if (strncmp(base, match, baselen)) | |
172 | continue; | |
173 | ||
174 | match += baselen; | |
175 | matchlen -= baselen; | |
176 | ||
177 | if (pathlen > matchlen) | |
178 | continue; | |
179 | ||
cb6c8ed2 LT |
180 | if (matchlen > pathlen) { |
181 | if (match[pathlen] != '/') | |
182 | continue; | |
850e82d8 LT |
183 | if (!S_ISDIR(mode)) |
184 | continue; | |
cb6c8ed2 LT |
185 | } |
186 | ||
c5b42386 LT |
187 | if (strncmp(path, match, pathlen)) |
188 | continue; | |
189 | ||
190 | return 1; | |
191 | } | |
192 | return 0; /* No matches */ | |
193 | } | |
194 | ||
ed1a368b LT |
195 | /* A whole sub-tree went away or appeared */ |
196 | static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base) | |
197 | { | |
198 | while (size) { | |
4727f640 | 199 | if (interesting(tree, size, base)) |
ed1a368b LT |
200 | show_file(prefix, tree, size, base); |
201 | update_tree_entry(&tree, &size); | |
202 | } | |
203 | } | |
204 | ||
eeb79916 | 205 | static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base) |
9174026c LT |
206 | { |
207 | while (size1 | size2) { | |
c5b42386 LT |
208 | if (nr_paths && size1 && !interesting(tree1, size1, base)) { |
209 | update_tree_entry(&tree1, &size1); | |
210 | continue; | |
211 | } | |
212 | if (nr_paths && size2 && !interesting(tree2, size2, base)) { | |
213 | update_tree_entry(&tree2, &size2); | |
214 | continue; | |
215 | } | |
9174026c | 216 | if (!size1) { |
eeb79916 | 217 | show_file("+", tree2, size2, base); |
9174026c LT |
218 | update_tree_entry(&tree2, &size2); |
219 | continue; | |
220 | } | |
221 | if (!size2) { | |
eeb79916 | 222 | show_file("-", tree1, size1, base); |
9174026c LT |
223 | update_tree_entry(&tree1, &size1); |
224 | continue; | |
225 | } | |
eeb79916 | 226 | switch (compare_tree_entry(tree1, size1, tree2, size2, base)) { |
9174026c LT |
227 | case -1: |
228 | update_tree_entry(&tree1, &size1); | |
229 | continue; | |
230 | case 0: | |
231 | update_tree_entry(&tree1, &size1); | |
232 | /* Fallthrough */ | |
233 | case 1: | |
234 | update_tree_entry(&tree2, &size2); | |
235 | continue; | |
236 | } | |
667bb59b | 237 | die("git-diff-tree: internal error"); |
9174026c LT |
238 | } |
239 | return 0; | |
240 | } | |
241 | ||
eeb79916 | 242 | static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base) |
9174026c | 243 | { |
9174026c LT |
244 | void *tree1, *tree2; |
245 | unsigned long size1, size2; | |
73134b6d | 246 | int retval; |
9174026c | 247 | |
e99d59ff | 248 | tree1 = read_object_with_reference(old, "tree", &size1, NULL); |
c1fdf2a6 | 249 | if (!tree1) |
2de381f9 | 250 | die("unable to read source tree (%s)", sha1_to_hex(old)); |
e99d59ff | 251 | tree2 = read_object_with_reference(new, "tree", &size2, NULL); |
c1fdf2a6 | 252 | if (!tree2) |
2de381f9 | 253 | die("unable to read destination tree (%s)", sha1_to_hex(new)); |
eeb79916 | 254 | retval = diff_tree(tree1, size1, tree2, size2, base); |
73134b6d LT |
255 | free(tree1); |
256 | free(tree2); | |
257 | return retval; | |
258 | } | |
259 | ||
6b5ee137 | 260 | static void call_diff_setup_done(void) |
38c6f780 | 261 | { |
6b5ee137 | 262 | diff_setup_done(&diff_options); |
38c6f780 JH |
263 | } |
264 | ||
09d74b3b | 265 | static int call_diff_flush(void) |
38c6f780 | 266 | { |
6b5ee137 | 267 | diffcore_std(&diff_options); |
9ab55bd2 | 268 | if (diff_queue_is_empty()) { |
6b5ee137 JH |
269 | int saved_fmt = diff_options.output_format; |
270 | diff_options.output_format = DIFF_FORMAT_NO_OUTPUT; | |
271 | diff_flush(&diff_options); | |
272 | diff_options.output_format = saved_fmt; | |
9ab55bd2 | 273 | return 0; |
6b14d7fa | 274 | } |
6b14d7fa | 275 | if (header) { |
6b5ee137 | 276 | printf("%s%c", header, diff_options.line_termination); |
6b14d7fa JH |
277 | header = NULL; |
278 | } | |
6b5ee137 | 279 | diff_flush(&diff_options); |
6b14d7fa | 280 | return 1; |
38c6f780 JH |
281 | } |
282 | ||
5c97558c JH |
283 | static int diff_tree_sha1_top(const unsigned char *old, |
284 | const unsigned char *new, const char *base) | |
285 | { | |
286 | int ret; | |
57fe64a4 | 287 | |
6b5ee137 | 288 | call_diff_setup_done(); |
5c97558c | 289 | ret = diff_tree_sha1(old, new, base); |
38c6f780 | 290 | call_diff_flush(); |
5c97558c JH |
291 | return ret; |
292 | } | |
293 | ||
dc26bd89 LT |
294 | static int diff_root_tree(const unsigned char *new, const char *base) |
295 | { | |
296 | int retval; | |
297 | void *tree; | |
298 | unsigned long size; | |
299 | ||
6b5ee137 | 300 | call_diff_setup_done(); |
e99d59ff | 301 | tree = read_object_with_reference(new, "tree", &size, NULL); |
dc26bd89 LT |
302 | if (!tree) |
303 | die("unable to read root tree (%s)", sha1_to_hex(new)); | |
304 | retval = diff_tree("", 0, tree, size, base); | |
305 | free(tree); | |
38c6f780 | 306 | call_diff_flush(); |
dc26bd89 LT |
307 | return retval; |
308 | } | |
309 | ||
18092663 | 310 | static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len) |
cee99d22 | 311 | { |
3258c902 | 312 | static char this_header[16384]; |
cee99d22 LT |
313 | int offset; |
314 | ||
18092663 LT |
315 | if (!verbose_header) |
316 | return commit; | |
cee99d22 | 317 | |
18092663 LT |
318 | offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent); |
319 | offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset); | |
cee99d22 LT |
320 | return this_header; |
321 | } | |
322 | ||
b11645be | 323 | static int diff_tree_commit(const unsigned char *commit, const char *name) |
e0965d83 | 324 | { |
e0965d83 | 325 | unsigned long size, offset; |
b11645be | 326 | char *buf = read_object_with_reference(commit, "commit", &size, NULL); |
e0965d83 | 327 | |
e0965d83 LT |
328 | if (!buf) |
329 | return -1; | |
330 | ||
73848892 LT |
331 | if (!name) { |
332 | static char commit_name[60]; | |
333 | strcpy(commit_name, sha1_to_hex(commit)); | |
334 | name = commit_name; | |
335 | } | |
336 | ||
dc26bd89 LT |
337 | /* Root commit? */ |
338 | if (show_root_diff && memcmp(buf + 46, "parent ", 7)) { | |
339 | header = generate_header(name, "root", buf, size); | |
340 | diff_root_tree(commit, ""); | |
341 | } | |
342 | ||
343 | /* More than one parent? */ | |
344 | if (ignore_merges) { | |
345 | if (!memcmp(buf + 46 + 48, "parent ", 7)) | |
346 | return 0; | |
347 | } | |
348 | ||
e0965d83 LT |
349 | offset = 46; |
350 | while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) { | |
b11645be | 351 | unsigned char parent[20]; |
e0965d83 LT |
352 | if (get_sha1_hex(buf + offset + 7, parent)) |
353 | return -1; | |
b11645be | 354 | header = generate_header(name, sha1_to_hex(parent), buf, size); |
5c97558c | 355 | diff_tree_sha1_top(parent, commit, ""); |
d6db0107 | 356 | if (!header && verbose_header) { |
cee99d22 | 357 | header_prefix = "\ndiff-tree "; |
d6db0107 LT |
358 | /* |
359 | * Don't print multiple merge entries if we | |
360 | * don't print the diffs. | |
361 | */ | |
d6db0107 | 362 | } |
e0965d83 LT |
363 | offset += 48; |
364 | } | |
5098bafb | 365 | free(buf); |
b11645be LT |
366 | return 0; |
367 | } | |
368 | ||
369 | static int diff_tree_stdin(char *line) | |
370 | { | |
371 | int len = strlen(line); | |
372 | unsigned char commit[20], parent[20]; | |
373 | static char this_header[1000]; | |
374 | ||
375 | if (!len || line[len-1] != '\n') | |
376 | return -1; | |
377 | line[len-1] = 0; | |
378 | if (get_sha1_hex(line, commit)) | |
379 | return -1; | |
380 | if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) { | |
381 | line[40] = 0; | |
382 | line[81] = 0; | |
383 | sprintf(this_header, "%s (from %s)\n", line, line+41); | |
384 | header = this_header; | |
5c97558c | 385 | return diff_tree_sha1_top(parent, commit, ""); |
b11645be LT |
386 | } |
387 | line[40] = 0; | |
388 | return diff_tree_commit(commit, line); | |
e0965d83 LT |
389 | } |
390 | ||
d288a700 LT |
391 | static int count_paths(const char **paths) |
392 | { | |
393 | int i = 0; | |
394 | while (*paths++) | |
395 | i++; | |
396 | return i; | |
397 | } | |
398 | ||
4d1f1190 | 399 | static const char diff_tree_usage[] = |
dda2d79a JH |
400 | "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] " |
401 | "[<common diff options>] <tree-ish> <tree-ish>" | |
402 | COMMON_DIFF_OPTIONS_HELP; | |
a8db165e | 403 | |
6b5ee137 | 404 | int main(int argc, const char **argv) |
73134b6d | 405 | { |
0a8365a1 | 406 | int nr_sha1; |
e0965d83 | 407 | char line[1000]; |
0a8365a1 | 408 | unsigned char sha1[2][20]; |
d288a700 | 409 | const char *prefix = setup_git_directory(); |
73134b6d | 410 | |
0a8365a1 | 411 | nr_sha1 = 0; |
6b5ee137 JH |
412 | diff_setup(&diff_options); |
413 | ||
c5b42386 | 414 | for (;;) { |
6b5ee137 | 415 | int diff_opt_cnt; |
6b14d7fa | 416 | const char *arg; |
c5b42386 | 417 | |
e0965d83 LT |
418 | argv++; |
419 | argc--; | |
420 | arg = *argv; | |
0a8365a1 | 421 | if (!arg) |
c5b42386 LT |
422 | break; |
423 | ||
0a8365a1 LT |
424 | if (*arg != '-') { |
425 | if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) { | |
426 | nr_sha1++; | |
427 | continue; | |
428 | } | |
429 | break; | |
430 | } | |
431 | ||
6b5ee137 JH |
432 | diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc); |
433 | if (diff_opt_cnt < 0) | |
434 | usage(diff_tree_usage); | |
435 | else if (diff_opt_cnt) { | |
436 | argv += diff_opt_cnt - 1; | |
437 | argc -= diff_opt_cnt - 1; | |
438 | continue; | |
439 | } | |
440 | ||
441 | ||
0a8365a1 | 442 | if (!strcmp(arg, "--")) { |
e0965d83 LT |
443 | argv++; |
444 | argc--; | |
445 | break; | |
446 | } | |
bf16c71e | 447 | if (!strcmp(arg, "-r")) { |
73134b6d LT |
448 | recursive = 1; |
449 | continue; | |
450 | } | |
4cae1a96 JH |
451 | if (!strcmp(arg, "-t")) { |
452 | recursive = show_tree_entry_in_recursive = 1; | |
453 | continue; | |
454 | } | |
e0965d83 LT |
455 | if (!strcmp(arg, "-m")) { |
456 | ignore_merges = 0; | |
457 | continue; | |
458 | } | |
cee99d22 LT |
459 | if (!strcmp(arg, "-v")) { |
460 | verbose_header = 1; | |
461 | header_prefix = "diff-tree "; | |
462 | continue; | |
463 | } | |
a8db165e JH |
464 | if (!strncmp(arg, "--pretty", 8)) { |
465 | verbose_header = 1; | |
ba88e54b | 466 | header_prefix = "diff-tree "; |
a8db165e JH |
467 | commit_format = get_commit_format(arg+8); |
468 | continue; | |
469 | } | |
e0965d83 LT |
470 | if (!strcmp(arg, "--stdin")) { |
471 | read_stdin = 1; | |
472 | continue; | |
473 | } | |
dc26bd89 LT |
474 | if (!strcmp(arg, "--root")) { |
475 | show_root_diff = 1; | |
476 | continue; | |
477 | } | |
c5bac17a | 478 | usage(diff_tree_usage); |
73134b6d | 479 | } |
6b5ee137 JH |
480 | if (diff_options.output_format == DIFF_FORMAT_PATCH) |
481 | recursive = 1; | |
73134b6d | 482 | |
d288a700 LT |
483 | paths = get_pathspec(prefix, argv); |
484 | if (paths) { | |
c5b42386 LT |
485 | int i; |
486 | ||
d288a700 | 487 | nr_paths = count_paths(paths); |
812666c8 | 488 | pathlens = xmalloc(nr_paths * sizeof(int)); |
c5b42386 LT |
489 | for (i=0; i<nr_paths; i++) |
490 | pathlens[i] = strlen(paths[i]); | |
491 | } | |
492 | ||
0a8365a1 LT |
493 | switch (nr_sha1) { |
494 | case 0: | |
495 | if (!read_stdin) | |
496 | usage(diff_tree_usage); | |
497 | break; | |
498 | case 1: | |
73848892 | 499 | diff_tree_commit(sha1[0], NULL); |
0a8365a1 LT |
500 | break; |
501 | case 2: | |
5c97558c | 502 | diff_tree_sha1_top(sha1[0], sha1[1], ""); |
0a8365a1 LT |
503 | break; |
504 | } | |
505 | ||
e0965d83 | 506 | if (!read_stdin) |
0a8365a1 | 507 | return 0; |
e0965d83 | 508 | |
6b5ee137 JH |
509 | if (diff_options.detect_rename) |
510 | diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE | | |
511 | DIFF_SETUP_USE_CACHE); | |
e0965d83 LT |
512 | while (fgets(line, sizeof(line), stdin)) |
513 | diff_tree_stdin(line); | |
514 | ||
515 | return 0; | |
9174026c | 516 | } |