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