Fix cvsimport warning when called without --no-cvs-direct
[git/jnareb-git.git] / diff-tree.c
blob851722037df936c2b4e0ab6a9389291e74d5ae60
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
5 static int show_root_diff = 0;
6 static int verbose_header = 0;
7 static int ignore_merges = 1;
8 static int recursive = 0;
9 static int show_tree_entry_in_recursive = 0;
10 static int read_stdin = 0;
12 static struct diff_options diff_options;
14 static const char *header = NULL;
15 static const char *header_prefix = "";
16 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
18 // What paths are we interested in?
19 static int nr_paths = 0;
20 static const 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)
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;
37 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
39 int len = strlen(tree)+1;
40 const unsigned char *sha1 = tree + len;
41 const char *path = strchr(tree, ' ');
42 unsigned int mode;
44 if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
45 die("corrupt tree file");
46 *pathp = path+1;
47 *modep = DIFF_FILE_CANON_MODE(mode);
48 return sha1;
51 static char *malloc_base(const char *base, const char *path, int pathlen)
53 int baselen = strlen(base);
54 char *newbase = xmalloc(baselen + pathlen + 2);
55 memcpy(newbase, base, baselen);
56 memcpy(newbase + baselen, path, pathlen);
57 memcpy(newbase + baselen + pathlen, "/", 2);
58 return newbase;
61 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
62 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
64 /* A file entry went away or appeared */
65 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
67 unsigned mode;
68 const char *path;
69 const unsigned char *sha1 = extract(tree, size, &path, &mode);
71 if (recursive && S_ISDIR(mode)) {
72 char type[20];
73 unsigned long size;
74 char *newbase = malloc_base(base, path, strlen(path));
75 void *tree;
77 tree = read_sha1_file(sha1, type, &size);
78 if (!tree || strcmp(type, "tree"))
79 die("corrupt tree sha %s", sha1_to_hex(sha1));
81 show_tree(prefix, tree, size, newbase);
83 free(tree);
84 free(newbase);
85 return;
88 diff_addremove(&diff_options, prefix[0], mode, sha1, base, path);
91 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
93 unsigned mode1, mode2;
94 const char *path1, *path2;
95 const unsigned char *sha1, *sha2;
96 int cmp, pathlen1, pathlen2;
98 sha1 = extract(tree1, size1, &path1, &mode1);
99 sha2 = extract(tree2, size2, &path2, &mode2);
101 pathlen1 = strlen(path1);
102 pathlen2 = strlen(path2);
103 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
104 if (cmp < 0) {
105 show_file("-", tree1, size1, base);
106 return -1;
108 if (cmp > 0) {
109 show_file("+", tree2, size2, base);
110 return 1;
112 if (!diff_options.find_copies_harder &&
113 !memcmp(sha1, sha2, 20) && mode1 == mode2)
114 return 0;
117 * If the filemode has changed to/from a directory from/to a regular
118 * file, we need to consider it a remove and an add.
120 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
121 show_file("-", tree1, size1, base);
122 show_file("+", tree2, size2, base);
123 return 0;
126 if (recursive && S_ISDIR(mode1)) {
127 int retval;
128 char *newbase = malloc_base(base, path1, pathlen1);
129 if (show_tree_entry_in_recursive)
130 diff_change(&diff_options, mode1, mode2,
131 sha1, sha2, base, path1);
132 retval = diff_tree_sha1(sha1, sha2, newbase);
133 free(newbase);
134 return retval;
137 diff_change(&diff_options, mode1, mode2, sha1, sha2, base, path1);
138 return 0;
141 static int interesting(void *tree, unsigned long size, const char *base)
143 const char *path;
144 unsigned mode;
145 int i;
146 int baselen, pathlen;
148 if (!nr_paths)
149 return 1;
151 (void)extract(tree, size, &path, &mode);
153 pathlen = strlen(path);
154 baselen = strlen(base);
156 for (i=0; i < nr_paths; i++) {
157 const char *match = paths[i];
158 int matchlen = pathlens[i];
160 if (baselen >= matchlen) {
161 /* If it doesn't match, move along... */
162 if (strncmp(base, match, matchlen))
163 continue;
165 /* The base is a subdirectory of a path which was specified. */
166 return 1;
169 /* Does the base match? */
170 if (strncmp(base, match, baselen))
171 continue;
173 match += baselen;
174 matchlen -= baselen;
176 if (pathlen > matchlen)
177 continue;
179 if (matchlen > pathlen) {
180 if (match[pathlen] != '/')
181 continue;
182 if (!S_ISDIR(mode))
183 continue;
186 if (strncmp(path, match, pathlen))
187 continue;
189 return 1;
191 return 0; /* No matches */
194 /* A whole sub-tree went away or appeared */
195 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
197 while (size) {
198 if (interesting(tree, size, base))
199 show_file(prefix, tree, size, base);
200 update_tree_entry(&tree, &size);
204 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
206 while (size1 | size2) {
207 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
208 update_tree_entry(&tree1, &size1);
209 continue;
211 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
212 update_tree_entry(&tree2, &size2);
213 continue;
215 if (!size1) {
216 show_file("+", tree2, size2, base);
217 update_tree_entry(&tree2, &size2);
218 continue;
220 if (!size2) {
221 show_file("-", tree1, size1, base);
222 update_tree_entry(&tree1, &size1);
223 continue;
225 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
226 case -1:
227 update_tree_entry(&tree1, &size1);
228 continue;
229 case 0:
230 update_tree_entry(&tree1, &size1);
231 /* Fallthrough */
232 case 1:
233 update_tree_entry(&tree2, &size2);
234 continue;
236 die("git-diff-tree: internal error");
238 return 0;
241 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
243 void *tree1, *tree2;
244 unsigned long size1, size2;
245 int retval;
247 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
248 if (!tree1)
249 die("unable to read source tree (%s)", sha1_to_hex(old));
250 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
251 if (!tree2)
252 die("unable to read destination tree (%s)", sha1_to_hex(new));
253 retval = diff_tree(tree1, size1, tree2, size2, base);
254 free(tree1);
255 free(tree2);
256 return retval;
259 static void call_diff_setup_done(void)
261 diff_setup_done(&diff_options);
264 static int call_diff_flush(void)
266 diffcore_std(&diff_options);
267 if (diff_queue_is_empty()) {
268 int saved_fmt = diff_options.output_format;
269 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
270 diff_flush(&diff_options);
271 diff_options.output_format = saved_fmt;
272 return 0;
274 if (header) {
275 printf("%s%c", header, diff_options.line_termination);
276 header = NULL;
278 diff_flush(&diff_options);
279 return 1;
282 static int diff_tree_sha1_top(const unsigned char *old,
283 const unsigned char *new, const char *base)
285 int ret;
287 call_diff_setup_done();
288 ret = diff_tree_sha1(old, new, base);
289 call_diff_flush();
290 return ret;
293 static int diff_root_tree(const unsigned char *new, const char *base)
295 int retval;
296 void *tree;
297 unsigned long size;
299 call_diff_setup_done();
300 tree = read_object_with_reference(new, "tree", &size, NULL);
301 if (!tree)
302 die("unable to read root tree (%s)", sha1_to_hex(new));
303 retval = diff_tree("", 0, tree, size, base);
304 free(tree);
305 call_diff_flush();
306 return retval;
309 static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
311 static char this_header[16384];
312 int offset;
314 if (!verbose_header)
315 return commit;
317 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
318 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
319 return this_header;
322 static int diff_tree_commit(const unsigned char *commit, const char *name)
324 unsigned long size, offset;
325 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
327 if (!buf)
328 return -1;
330 if (!name) {
331 static char commit_name[60];
332 strcpy(commit_name, sha1_to_hex(commit));
333 name = commit_name;
336 /* Root commit? */
337 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
338 header = generate_header(name, "root", buf, size);
339 diff_root_tree(commit, "");
342 /* More than one parent? */
343 if (ignore_merges) {
344 if (!memcmp(buf + 46 + 48, "parent ", 7))
345 return 0;
348 offset = 46;
349 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
350 unsigned char parent[20];
351 if (get_sha1_hex(buf + offset + 7, parent))
352 return -1;
353 header = generate_header(name, sha1_to_hex(parent), buf, size);
354 diff_tree_sha1_top(parent, commit, "");
355 if (!header && verbose_header) {
356 header_prefix = "\ndiff-tree ";
358 * Don't print multiple merge entries if we
359 * don't print the diffs.
362 offset += 48;
364 free(buf);
365 return 0;
368 static int diff_tree_stdin(char *line)
370 int len = strlen(line);
371 unsigned char commit[20], parent[20];
372 static char this_header[1000];
374 if (!len || line[len-1] != '\n')
375 return -1;
376 line[len-1] = 0;
377 if (get_sha1_hex(line, commit))
378 return -1;
379 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
380 line[40] = 0;
381 line[81] = 0;
382 sprintf(this_header, "%s (from %s)\n", line, line+41);
383 header = this_header;
384 return diff_tree_sha1_top(parent, commit, "");
386 line[40] = 0;
387 return diff_tree_commit(commit, line);
390 static int count_paths(const char **paths)
392 int i = 0;
393 while (*paths++)
394 i++;
395 return i;
398 static const char diff_tree_usage[] =
399 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] "
400 "[<common diff options>] <tree-ish> <tree-ish>"
401 COMMON_DIFF_OPTIONS_HELP;
403 int main(int argc, const char **argv)
405 int nr_sha1;
406 char line[1000];
407 unsigned char sha1[2][20];
408 const char *prefix = setup_git_directory();
410 git_config(git_default_config);
411 nr_sha1 = 0;
412 diff_setup(&diff_options);
414 for (;;) {
415 int diff_opt_cnt;
416 const char *arg;
418 argv++;
419 argc--;
420 arg = *argv;
421 if (!arg)
422 break;
424 if (*arg != '-') {
425 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
426 nr_sha1++;
427 continue;
429 break;
432 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
433 if (diff_opt_cnt < 0)
434 usage(diff_tree_usage);
435 else if (diff_opt_cnt) {
436 argv += diff_opt_cnt - 1;
437 argc -= diff_opt_cnt - 1;
438 continue;
442 if (!strcmp(arg, "--")) {
443 argv++;
444 argc--;
445 break;
447 if (!strcmp(arg, "-r")) {
448 recursive = 1;
449 continue;
451 if (!strcmp(arg, "-t")) {
452 recursive = show_tree_entry_in_recursive = 1;
453 continue;
455 if (!strcmp(arg, "-m")) {
456 ignore_merges = 0;
457 continue;
459 if (!strcmp(arg, "-v")) {
460 verbose_header = 1;
461 header_prefix = "diff-tree ";
462 continue;
464 if (!strncmp(arg, "--pretty", 8)) {
465 verbose_header = 1;
466 header_prefix = "diff-tree ";
467 commit_format = get_commit_format(arg+8);
468 continue;
470 if (!strcmp(arg, "--stdin")) {
471 read_stdin = 1;
472 continue;
474 if (!strcmp(arg, "--root")) {
475 show_root_diff = 1;
476 continue;
478 usage(diff_tree_usage);
480 if (diff_options.output_format == DIFF_FORMAT_PATCH)
481 recursive = 1;
483 paths = get_pathspec(prefix, argv);
484 if (paths) {
485 int i;
487 nr_paths = count_paths(paths);
488 pathlens = xmalloc(nr_paths * sizeof(int));
489 for (i=0; i<nr_paths; i++)
490 pathlens[i] = strlen(paths[i]);
493 switch (nr_sha1) {
494 case 0:
495 if (!read_stdin)
496 usage(diff_tree_usage);
497 break;
498 case 1:
499 diff_tree_commit(sha1[0], NULL);
500 break;
501 case 2:
502 diff_tree_sha1_top(sha1[0], sha1[1], "");
503 break;
506 if (!read_stdin)
507 return 0;
509 if (diff_options.detect_rename)
510 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
511 DIFF_SETUP_USE_CACHE);
512 while (fgets(line, sizeof(line), stdin))
513 diff_tree_stdin(line);
515 return 0;