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