diff-tree: fix and extend argument parsing
[git.git] / diff-tree.c
blob8cfe990023bbae14ff36a23bed1923513a9b022e
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
5 static int silent = 0;
6 static int verbose_header = 0;
7 static int ignore_merges = 1;
8 static int recursive = 0;
9 static int read_stdin = 0;
10 static int line_termination = '\n';
11 static int generate_patch = 0;
12 static const char *header = NULL;
13 static const char *header_prefix = "";
15 // What paths are we interested in?
16 static int nr_paths = 0;
17 static char **paths = NULL;
18 static int *pathlens = NULL;
20 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
22 static void update_tree_entry(void **bufp, unsigned long *sizep)
24 void *buf = *bufp;
25 unsigned long size = *sizep;
26 int len = strlen(buf) + 1 + 20;
28 if (size < len)
29 die("corrupt tree file");
30 *bufp = buf + len;
31 *sizep = size - len;
34 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
36 int len = strlen(tree)+1;
37 const unsigned char *sha1 = tree + len;
38 const char *path = strchr(tree, ' ');
40 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
41 die("corrupt tree file");
42 *pathp = path+1;
43 return sha1;
46 static char *malloc_base(const char *base, const char *path, int pathlen)
48 int baselen = strlen(base);
49 char *newbase = xmalloc(baselen + pathlen + 2);
50 memcpy(newbase, base, baselen);
51 memcpy(newbase + baselen, path, pathlen);
52 memcpy(newbase + baselen + pathlen, "/", 2);
53 return newbase;
56 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
58 /* A whole sub-tree went away or appeared */
59 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
61 while (size) {
62 show_file(prefix, tree, size, base);
63 update_tree_entry(&tree, &size);
67 /* A file entry went away or appeared */
68 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
70 unsigned mode;
71 const char *path;
72 const unsigned char *sha1 = extract(tree, size, &path, &mode);
74 if (header) {
75 printf("%s", header);
76 header = NULL;
79 if (silent)
80 return;
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;
88 tree = read_sha1_file(sha1, type, &size);
89 if (!tree || strcmp(type, "tree"))
90 die("corrupt tree sha %s", sha1_to_hex(sha1));
92 show_tree(prefix, tree, size, newbase);
94 free(tree);
95 free(newbase);
96 return;
99 if (generate_patch) {
100 if (!S_ISDIR(mode))
101 diff_addremove(prefix[0], mode, sha1, base, path);
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);
110 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
112 unsigned mode1, mode2;
113 const char *path1, *path2;
114 const unsigned char *sha1, *sha2;
115 int cmp, pathlen1, pathlen2;
116 char old_sha1_hex[50];
118 sha1 = extract(tree1, size1, &path1, &mode1);
119 sha2 = extract(tree2, size2, &path2, &mode2);
121 pathlen1 = strlen(path1);
122 pathlen2 = strlen(path2);
123 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
124 if (cmp < 0) {
125 show_file("-", tree1, size1, base);
126 return -1;
128 if (cmp > 0) {
129 show_file("+", tree2, size2, base);
130 return 1;
132 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
133 return 0;
136 * If the filemode has changed to/from a directory from/to a regular
137 * file, we need to consider it a remove and an add.
139 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
140 show_file("-", tree1, size1, base);
141 show_file("+", tree2, size2, base);
142 return 0;
145 if (recursive && S_ISDIR(mode1)) {
146 int retval;
147 char *newbase = malloc_base(base, path1, pathlen1);
148 retval = diff_tree_sha1(sha1, sha2, newbase);
149 free(newbase);
150 return retval;
153 if (header) {
154 printf("%s", header);
155 header = NULL;
157 if (silent)
158 return 0;
160 if (generate_patch) {
161 if (!S_ISDIR(mode1))
162 diff_change(mode1, mode2, sha1, sha2, base, path1);
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);
171 return 0;
174 static int interesting(void *tree, unsigned long size, const char *base)
176 const char *path;
177 unsigned mode;
178 int i;
179 int baselen, pathlen;
181 if (!nr_paths)
182 return 1;
184 (void)extract(tree, size, &path, &mode);
186 pathlen = strlen(path);
187 baselen = strlen(base);
189 for (i=0; i < nr_paths; i++) {
190 const char *match = paths[i];
191 int matchlen = pathlens[i];
193 if (baselen >= matchlen) {
194 /* If it doesn't match, move along... */
195 if (strncmp(base, match, matchlen))
196 continue;
198 /* The base is a subdirectory of a path which was specified. */
199 return 1;
202 /* Does the base match? */
203 if (strncmp(base, match, baselen))
204 continue;
206 match += baselen;
207 matchlen -= baselen;
209 if (pathlen > matchlen)
210 continue;
212 if (strncmp(path, match, pathlen))
213 continue;
215 return 1;
217 return 0; /* No matches */
220 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
222 while (size1 | size2) {
223 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
224 update_tree_entry(&tree1, &size1);
225 continue;
227 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
228 update_tree_entry(&tree2, &size2);
229 continue;
231 if (!size1) {
232 show_file("+", tree2, size2, base);
233 update_tree_entry(&tree2, &size2);
234 continue;
236 if (!size2) {
237 show_file("-", tree1, size1, base);
238 update_tree_entry(&tree1, &size1);
239 continue;
241 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
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;
252 die("diff-tree: internal error");
254 return 0;
257 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
259 void *tree1, *tree2;
260 unsigned long size1, size2;
261 int retval;
263 tree1 = read_object_with_reference(old, "tree", &size1, 0);
264 if (!tree1)
265 die("unable to read source tree (%s)", sha1_to_hex(old));
266 tree2 = read_object_with_reference(new, "tree", &size2, 0);
267 if (!tree2)
268 die("unable to read destination tree (%s)", sha1_to_hex(new));
269 retval = diff_tree(tree1, size1, tree2, size2, base);
270 free(tree1);
271 free(tree2);
272 return retval;
275 static int get_one_line(const char *msg, unsigned long len)
277 int ret = 0;
279 while (len--) {
280 ret++;
281 if (*msg++ == '\n')
282 break;
284 return ret;
287 static int add_author_info(char *buf, const char *line, int len)
289 char *date;
290 unsigned int namelen;
291 unsigned long time;
292 int tz;
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);
302 return sprintf(buf, "Author: %.*s\nDate: %s\n",
303 namelen, line,
304 show_date(time, tz));
307 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
309 static char this_header[1000];
310 int offset;
312 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
313 if (verbose_header) {
314 int hdr = 1;
316 for (;;) {
317 const char *line = msg;
318 int linelen = get_one_line(msg, len);
320 if (!linelen)
321 break;
322 if (offset + linelen + 10 > sizeof(this_header))
323 break;
325 msg += linelen;
326 len -= linelen;
327 if (linelen == 1)
328 hdr = 0;
329 if (hdr) {
330 if (!memcmp(line, "author ", 7))
331 offset += add_author_info(this_header + offset, line, linelen);
332 continue;
334 memset(this_header + offset, ' ', 4);
335 memcpy(this_header + offset + 4, line, linelen);
336 offset += linelen + 4;
338 this_header[offset++] = '\n';
339 this_header[offset] = 0;
342 return this_header;
345 static int diff_tree_commit(const unsigned char *commit, const char *name)
347 unsigned long size, offset;
348 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
350 if (!buf)
351 return -1;
353 /* More than one parent? */
354 if (ignore_merges) {
355 if (!memcmp(buf + 46 + 48, "parent ", 7))
356 return 0;
359 offset = 46;
360 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
361 unsigned char parent[20];
362 if (get_sha1_hex(buf + offset + 7, parent))
363 return -1;
364 header = generate_header(name, sha1_to_hex(parent), buf, size);
365 diff_tree_sha1(parent, commit, "");
366 if (!header && verbose_header)
367 header_prefix = "\ndiff-tree ";
368 offset += 48;
370 return 0;
373 static int diff_tree_stdin(char *line)
375 int len = strlen(line);
376 unsigned char commit[20], parent[20];
377 static char this_header[1000];
379 if (!len || line[len-1] != '\n')
380 return -1;
381 line[len-1] = 0;
382 if (get_sha1_hex(line, commit))
383 return -1;
384 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
385 line[40] = 0;
386 line[81] = 0;
387 sprintf(this_header, "%s (from %s)\n", line, line+41);
388 header = this_header;
389 return diff_tree_sha1(parent, commit, "");
391 line[40] = 0;
392 return diff_tree_commit(commit, line);
395 static char *diff_tree_usage =
396 "diff-tree [-p] [-r] [-z] [--stdin] [-m] [-s] [-v] <tree sha1> <tree sha1>";
398 int main(int argc, char **argv)
400 int nr_sha1;
401 char line[1000];
402 unsigned char sha1[2][20];
404 nr_sha1 = 0;
405 for (;;) {
406 char *arg;
408 argv++;
409 argc--;
410 arg = *argv;
411 if (!arg)
412 break;
414 if (*arg != '-') {
415 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
416 nr_sha1++;
417 continue;
419 break;
422 if (!strcmp(arg, "--")) {
423 argv++;
424 argc--;
425 break;
427 if (!strcmp(arg, "-r")) {
428 recursive = 1;
429 continue;
431 if (!strcmp(arg, "-p")) {
432 recursive = generate_patch = 1;
433 continue;
435 if (!strcmp(arg, "-z")) {
436 line_termination = '\0';
437 continue;
439 if (!strcmp(arg, "-m")) {
440 ignore_merges = 0;
441 continue;
443 if (!strcmp(arg, "-s")) {
444 silent = 1;
445 continue;
447 if (!strcmp(arg, "-v")) {
448 verbose_header = 1;
449 header_prefix = "diff-tree ";
450 continue;
452 if (!strcmp(arg, "--stdin")) {
453 read_stdin = 1;
454 continue;
456 usage(diff_tree_usage);
459 if (argc > 0) {
460 int i;
462 paths = argv;
463 nr_paths = argc;
464 pathlens = xmalloc(nr_paths * sizeof(int));
465 for (i=0; i<nr_paths; i++)
466 pathlens[i] = strlen(paths[i]);
469 switch (nr_sha1) {
470 case 0:
471 if (!read_stdin)
472 usage(diff_tree_usage);
473 break;
474 case 1:
475 diff_tree_commit(sha1[0], sha1_to_hex(sha1[0]));
476 break;
477 case 2:
478 diff_tree_sha1(sha1[0], sha1[1], "");
479 break;
482 if (!read_stdin)
483 return 0;
485 while (fgets(line, sizeof(line), stdin))
486 diff_tree_stdin(line);
488 return 0;