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