[PATCH] Deltification library work by Nicolas Pitre.
[git/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;
850e82d8
LT
207 if (!S_ISDIR(mode))
208 continue;
cb6c8ed2
LT
209 }
210
c5b42386
LT
211 if (strncmp(path, match, pathlen))
212 continue;
213
214 return 1;
215 }
216 return 0; /* No matches */
217}
218
ed1a368b
LT
219/* A whole sub-tree went away or appeared */
220static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
221{
222 while (size) {
223 if (interesting(tree, size, base))
224 show_file(prefix, tree, size, base);
225 update_tree_entry(&tree, &size);
226 }
227}
228
eeb79916 229static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
9174026c
LT
230{
231 while (size1 | size2) {
c5b42386
LT
232 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
233 update_tree_entry(&tree1, &size1);
234 continue;
235 }
236 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
237 update_tree_entry(&tree2, &size2);
238 continue;
239 }
9174026c 240 if (!size1) {
eeb79916 241 show_file("+", tree2, size2, base);
9174026c
LT
242 update_tree_entry(&tree2, &size2);
243 continue;
244 }
245 if (!size2) {
eeb79916 246 show_file("-", tree1, size1, base);
9174026c
LT
247 update_tree_entry(&tree1, &size1);
248 continue;
249 }
eeb79916 250 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
9174026c
LT
251 case -1:
252 update_tree_entry(&tree1, &size1);
253 continue;
254 case 0:
255 update_tree_entry(&tree1, &size1);
256 /* Fallthrough */
257 case 1:
258 update_tree_entry(&tree2, &size2);
259 continue;
260 }
2de381f9 261 die("diff-tree: internal error");
9174026c
LT
262 }
263 return 0;
264}
265
eeb79916 266static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
9174026c 267{
9174026c
LT
268 void *tree1, *tree2;
269 unsigned long size1, size2;
73134b6d 270 int retval;
9174026c 271
40469ee9 272 tree1 = read_object_with_reference(old, "tree", &size1, 0);
c1fdf2a6 273 if (!tree1)
2de381f9 274 die("unable to read source tree (%s)", sha1_to_hex(old));
40469ee9 275 tree2 = read_object_with_reference(new, "tree", &size2, 0);
c1fdf2a6 276 if (!tree2)
2de381f9 277 die("unable to read destination tree (%s)", sha1_to_hex(new));
eeb79916 278 retval = diff_tree(tree1, size1, tree2, size2, base);
73134b6d
LT
279 free(tree1);
280 free(tree2);
281 return retval;
282}
283
cee99d22
LT
284static int get_one_line(const char *msg, unsigned long len)
285{
286 int ret = 0;
287
288 while (len--) {
289 ret++;
290 if (*msg++ == '\n')
291 break;
292 }
293 return ret;
294}
295
a02ebff6
LT
296static int add_author_info(char *buf, const char *line, int len)
297{
298 char *date;
299 unsigned int namelen;
300 unsigned long time;
301 int tz;
302
303 line += strlen("author ");
304 date = strchr(line, '>');
305 if (!date)
306 return 0;
307 namelen = ++date - line;
308 time = strtoul(date, &date, 10);
309 tz = strtol(date, NULL, 10);
310
311 return sprintf(buf, "Author: %.*s\nDate: %s\n",
312 namelen, line,
313 show_date(time, tz));
314}
315
cee99d22
LT
316static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
317{
318 static char this_header[1000];
319 int offset;
320
321 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
322 if (verbose_header) {
323 int hdr = 1;
324
325 for (;;) {
326 const char *line = msg;
327 int linelen = get_one_line(msg, len);
328
329 if (!linelen)
330 break;
331 if (offset + linelen + 10 > sizeof(this_header))
332 break;
333
334 msg += linelen;
335 len -= linelen;
336 if (linelen == 1)
337 hdr = 0;
a02ebff6
LT
338 if (hdr) {
339 if (!memcmp(line, "author ", 7))
340 offset += add_author_info(this_header + offset, line, linelen);
cee99d22 341 continue;
a02ebff6 342 }
cee99d22
LT
343 memset(this_header + offset, ' ', 4);
344 memcpy(this_header + offset + 4, line, linelen);
345 offset += linelen + 4;
346 }
347 this_header[offset++] = '\n';
348 this_header[offset] = 0;
349 }
350
351 return this_header;
352}
353
b11645be 354static int diff_tree_commit(const unsigned char *commit, const char *name)
e0965d83 355{
e0965d83 356 unsigned long size, offset;
b11645be 357 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
e0965d83 358
e0965d83
LT
359 if (!buf)
360 return -1;
361
362 /* More than one parent? */
363 if (ignore_merges) {
364 if (!memcmp(buf + 46 + 48, "parent ", 7))
365 return 0;
366 }
367
73848892
LT
368 if (!name) {
369 static char commit_name[60];
370 strcpy(commit_name, sha1_to_hex(commit));
371 name = commit_name;
372 }
373
e0965d83
LT
374 offset = 46;
375 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
b11645be 376 unsigned char parent[20];
e0965d83
LT
377 if (get_sha1_hex(buf + offset + 7, parent))
378 return -1;
b11645be 379 header = generate_header(name, sha1_to_hex(parent), buf, size);
e0965d83 380 diff_tree_sha1(parent, commit, "");
cee99d22
LT
381 if (!header && verbose_header)
382 header_prefix = "\ndiff-tree ";
e0965d83
LT
383 offset += 48;
384 }
b11645be
LT
385 return 0;
386}
387
388static int diff_tree_stdin(char *line)
389{
390 int len = strlen(line);
391 unsigned char commit[20], parent[20];
392 static char this_header[1000];
393
394 if (!len || line[len-1] != '\n')
395 return -1;
396 line[len-1] = 0;
397 if (get_sha1_hex(line, commit))
398 return -1;
399 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
400 line[40] = 0;
401 line[81] = 0;
402 sprintf(this_header, "%s (from %s)\n", line, line+41);
403 header = this_header;
404 return diff_tree_sha1(parent, commit, "");
405 }
406 line[40] = 0;
407 return diff_tree_commit(commit, line);
e0965d83
LT
408}
409
5aad72f2
JH
410static char *diff_tree_usage =
411"diff-tree [-p] [-r] [-z] [--stdin] [-m] [-s] [-v] <tree sha1> <tree sha1>";
c5bac17a 412
73134b6d
LT
413int main(int argc, char **argv)
414{
0a8365a1 415 int nr_sha1;
e0965d83 416 char line[1000];
0a8365a1 417 unsigned char sha1[2][20];
73134b6d 418
0a8365a1 419 nr_sha1 = 0;
c5b42386 420 for (;;) {
e0965d83 421 char *arg;
c5b42386 422
e0965d83
LT
423 argv++;
424 argc--;
425 arg = *argv;
0a8365a1 426 if (!arg)
c5b42386
LT
427 break;
428
0a8365a1
LT
429 if (*arg != '-') {
430 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
431 nr_sha1++;
432 continue;
433 }
434 break;
435 }
436
437 if (!strcmp(arg, "--")) {
e0965d83
LT
438 argv++;
439 argc--;
440 break;
441 }
bf16c71e 442 if (!strcmp(arg, "-r")) {
73134b6d
LT
443 recursive = 1;
444 continue;
445 }
3ebfd4aa 446 if (!strcmp(arg, "-p")) {
3a663fd9 447 recursive = generate_patch = 1;
3ebfd4aa
JH
448 continue;
449 }
6cbd72f8
LT
450 if (!strcmp(arg, "-z")) {
451 line_termination = '\0';
452 continue;
453 }
e0965d83
LT
454 if (!strcmp(arg, "-m")) {
455 ignore_merges = 0;
456 continue;
457 }
f4f21ce3
LT
458 if (!strcmp(arg, "-s")) {
459 silent = 1;
460 continue;
461 }
cee99d22
LT
462 if (!strcmp(arg, "-v")) {
463 verbose_header = 1;
464 header_prefix = "diff-tree ";
465 continue;
466 }
e0965d83
LT
467 if (!strcmp(arg, "--stdin")) {
468 read_stdin = 1;
469 continue;
470 }
c5bac17a 471 usage(diff_tree_usage);
73134b6d
LT
472 }
473
e0965d83 474 if (argc > 0) {
c5b42386
LT
475 int i;
476
e0965d83
LT
477 paths = argv;
478 nr_paths = argc;
812666c8 479 pathlens = xmalloc(nr_paths * sizeof(int));
c5b42386
LT
480 for (i=0; i<nr_paths; i++)
481 pathlens[i] = strlen(paths[i]);
482 }
483
0a8365a1
LT
484 switch (nr_sha1) {
485 case 0:
486 if (!read_stdin)
487 usage(diff_tree_usage);
488 break;
489 case 1:
73848892 490 diff_tree_commit(sha1[0], NULL);
0a8365a1
LT
491 break;
492 case 2:
493 diff_tree_sha1(sha1[0], sha1[1], "");
494 break;
495 }
496
e0965d83 497 if (!read_stdin)
0a8365a1 498 return 0;
e0965d83
LT
499
500 while (fgets(line, sizeof(line), stdin))
501 diff_tree_stdin(line);
502
503 return 0;
9174026c 504}