]> git.ipfire.org Git - thirdparty/git.git/blame - diff-tree.c
Allow git-merge not to commit.
[thirdparty/git.git] / diff-tree.c
CommitLineData
9174026c 1#include "cache.h"
3ebfd4aa 2#include "diff.h"
e3bc7a3b 3#include "commit.h"
9174026c 4
dc26bd89 5static int show_root_diff = 0;
cee99d22 6static int verbose_header = 0;
e0965d83 7static int ignore_merges = 1;
73134b6d 8static int recursive = 0;
4cae1a96 9static int show_tree_entry_in_recursive = 0;
e0965d83 10static int read_stdin = 0;
6b5ee137
JH
11
12static struct diff_options diff_options;
13
f4f21ce3 14static const char *header = NULL;
cee99d22 15static const char *header_prefix = "";
000182ea 16static enum cmit_fmt commit_format = CMIT_FMT_RAW;
9174026c 17
c5b42386
LT
18// What paths are we interested in?
19static int nr_paths = 0;
6b14d7fa 20static const char **paths = NULL;
c5b42386
LT
21static int *pathlens = NULL;
22
eeb79916 23static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
73134b6d
LT
24
25static void update_tree_entry(void **bufp, unsigned long *sizep)
9174026c
LT
26{
27 void *buf = *bufp;
28 unsigned long size = *sizep;
29 int len = strlen(buf) + 1 + 20;
30
31 if (size < len)
2de381f9 32 die("corrupt tree file");
9174026c
LT
33 *bufp = buf + len;
34 *sizep = size - len;
9174026c
LT
35}
36
37static 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, ' ');
67574c40 42 unsigned int mode;
9174026c 43
67574c40 44 if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
2de381f9 45 die("corrupt tree file");
9174026c 46 *pathp = path+1;
67574c40 47 *modep = DIFF_FILE_CANON_MODE(mode);
9174026c
LT
48 return sha1;
49}
50
262e82b4
LT
51static char *malloc_base(const char *base, const char *path, int pathlen)
52{
53 int baselen = strlen(base);
812666c8 54 char *newbase = xmalloc(baselen + pathlen + 2);
262e82b4
LT
55 memcpy(newbase, base, baselen);
56 memcpy(newbase + baselen, path, pathlen);
57 memcpy(newbase + baselen + pathlen, "/", 2);
58 return newbase;
59}
60
61static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
ed1a368b 62static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
262e82b4
LT
63
64/* A file entry went away or appeared */
73134b6d 65static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
9174026c
LT
66{
67 unsigned mode;
68 const char *path;
69 const unsigned char *sha1 = extract(tree, size, &path, &mode);
262e82b4
LT
70
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;
76
77 tree = read_sha1_file(sha1, type, &size);
78 if (!tree || strcmp(type, "tree"))
2de381f9 79 die("corrupt tree sha %s", sha1_to_hex(sha1));
262e82b4
LT
80
81 show_tree(prefix, tree, size, newbase);
57fe64a4 82
262e82b4
LT
83 free(tree);
84 free(newbase);
85 return;
86 }
87
6b5ee137 88 diff_addremove(&diff_options, prefix[0], mode, sha1, base, path);
9174026c
LT
89}
90
eeb79916 91static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
9174026c
LT
92{
93 unsigned mode1, mode2;
94 const char *path1, *path2;
95 const unsigned char *sha1, *sha2;
73134b6d 96 int cmp, pathlen1, pathlen2;
9174026c
LT
97
98 sha1 = extract(tree1, size1, &path1, &mode1);
99 sha2 = extract(tree2, size2, &path2, &mode2);
100
73134b6d
LT
101 pathlen1 = strlen(path1);
102 pathlen2 = strlen(path2);
e46091d5 103 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
9174026c 104 if (cmp < 0) {
eeb79916 105 show_file("-", tree1, size1, base);
9174026c
LT
106 return -1;
107 }
108 if (cmp > 0) {
eeb79916 109 show_file("+", tree2, size2, base);
9174026c
LT
110 return 1;
111 }
6b5ee137
JH
112 if (!diff_options.find_copies_harder &&
113 !memcmp(sha1, sha2, 20) && mode1 == mode2)
9174026c 114 return 0;
262e82b4
LT
115
116 /*
117 * If the filemode has changed to/from a directory from/to a regular
aebb2679 118 * file, we need to consider it a remove and an add.
262e82b4
LT
119 */
120 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
121 show_file("-", tree1, size1, base);
122 show_file("+", tree2, size2, base);
123 return 0;
124 }
125
126 if (recursive && S_ISDIR(mode1)) {
eeb79916 127 int retval;
262e82b4 128 char *newbase = malloc_base(base, path1, pathlen1);
4cae1a96 129 if (show_tree_entry_in_recursive)
6b5ee137
JH
130 diff_change(&diff_options, mode1, mode2,
131 sha1, sha2, base, path1);
eeb79916
LT
132 retval = diff_tree_sha1(sha1, sha2, newbase);
133 free(newbase);
134 return retval;
73134b6d
LT
135 }
136
6b5ee137 137 diff_change(&diff_options, mode1, mode2, sha1, sha2, base, path1);
9174026c
LT
138 return 0;
139}
140
c5b42386
LT
141static int interesting(void *tree, unsigned long size, const char *base)
142{
143 const char *path;
144 unsigned mode;
145 int i;
146 int baselen, pathlen;
147
148 if (!nr_paths)
149 return 1;
150
151 (void)extract(tree, size, &path, &mode);
152
153 pathlen = strlen(path);
154 baselen = strlen(base);
155
156 for (i=0; i < nr_paths; i++) {
157 const char *match = paths[i];
158 int matchlen = pathlens[i];
159
160 if (baselen >= matchlen) {
161 /* If it doesn't match, move along... */
162 if (strncmp(base, match, matchlen))
163 continue;
164
165 /* The base is a subdirectory of a path which was specified. */
166 return 1;
167 }
168
169 /* Does the base match? */
170 if (strncmp(base, match, baselen))
171 continue;
172
173 match += baselen;
174 matchlen -= baselen;
175
176 if (pathlen > matchlen)
177 continue;
178
cb6c8ed2
LT
179 if (matchlen > pathlen) {
180 if (match[pathlen] != '/')
181 continue;
850e82d8
LT
182 if (!S_ISDIR(mode))
183 continue;
cb6c8ed2
LT
184 }
185
c5b42386
LT
186 if (strncmp(path, match, pathlen))
187 continue;
188
189 return 1;
190 }
191 return 0; /* No matches */
192}
193
ed1a368b
LT
194/* A whole sub-tree went away or appeared */
195static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
196{
197 while (size) {
4727f640 198 if (interesting(tree, size, base))
ed1a368b
LT
199 show_file(prefix, tree, size, base);
200 update_tree_entry(&tree, &size);
201 }
202}
203
eeb79916 204static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
9174026c
LT
205{
206 while (size1 | size2) {
c5b42386
LT
207 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
208 update_tree_entry(&tree1, &size1);
209 continue;
210 }
211 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
212 update_tree_entry(&tree2, &size2);
213 continue;
214 }
9174026c 215 if (!size1) {
eeb79916 216 show_file("+", tree2, size2, base);
9174026c
LT
217 update_tree_entry(&tree2, &size2);
218 continue;
219 }
220 if (!size2) {
eeb79916 221 show_file("-", tree1, size1, base);
9174026c
LT
222 update_tree_entry(&tree1, &size1);
223 continue;
224 }
eeb79916 225 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
9174026c
LT
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;
235 }
667bb59b 236 die("git-diff-tree: internal error");
9174026c
LT
237 }
238 return 0;
239}
240
eeb79916 241static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
9174026c 242{
9174026c
LT
243 void *tree1, *tree2;
244 unsigned long size1, size2;
73134b6d 245 int retval;
9174026c 246
e99d59ff 247 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
c1fdf2a6 248 if (!tree1)
2de381f9 249 die("unable to read source tree (%s)", sha1_to_hex(old));
e99d59ff 250 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
c1fdf2a6 251 if (!tree2)
2de381f9 252 die("unable to read destination tree (%s)", sha1_to_hex(new));
eeb79916 253 retval = diff_tree(tree1, size1, tree2, size2, base);
73134b6d
LT
254 free(tree1);
255 free(tree2);
256 return retval;
257}
258
6b5ee137 259static void call_diff_setup_done(void)
38c6f780 260{
6b5ee137 261 diff_setup_done(&diff_options);
38c6f780
JH
262}
263
09d74b3b 264static int call_diff_flush(void)
38c6f780 265{
6b5ee137 266 diffcore_std(&diff_options);
9ab55bd2 267 if (diff_queue_is_empty()) {
6b5ee137
JH
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;
9ab55bd2 272 return 0;
6b14d7fa 273 }
6b14d7fa 274 if (header) {
6b5ee137 275 printf("%s%c", header, diff_options.line_termination);
6b14d7fa
JH
276 header = NULL;
277 }
6b5ee137 278 diff_flush(&diff_options);
6b14d7fa 279 return 1;
38c6f780
JH
280}
281
5c97558c
JH
282static int diff_tree_sha1_top(const unsigned char *old,
283 const unsigned char *new, const char *base)
284{
285 int ret;
57fe64a4 286
6b5ee137 287 call_diff_setup_done();
5c97558c 288 ret = diff_tree_sha1(old, new, base);
38c6f780 289 call_diff_flush();
5c97558c
JH
290 return ret;
291}
292
dc26bd89
LT
293static int diff_root_tree(const unsigned char *new, const char *base)
294{
295 int retval;
296 void *tree;
297 unsigned long size;
298
6b5ee137 299 call_diff_setup_done();
e99d59ff 300 tree = read_object_with_reference(new, "tree", &size, NULL);
dc26bd89
LT
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);
38c6f780 305 call_diff_flush();
dc26bd89
LT
306 return retval;
307}
308
18092663 309static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
cee99d22 310{
3258c902 311 static char this_header[16384];
cee99d22
LT
312 int offset;
313
18092663
LT
314 if (!verbose_header)
315 return commit;
cee99d22 316
18092663
LT
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);
cee99d22
LT
319 return this_header;
320}
321
b11645be 322static int diff_tree_commit(const unsigned char *commit, const char *name)
e0965d83 323{
e0965d83 324 unsigned long size, offset;
b11645be 325 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
e0965d83 326
e0965d83
LT
327 if (!buf)
328 return -1;
329
73848892
LT
330 if (!name) {
331 static char commit_name[60];
332 strcpy(commit_name, sha1_to_hex(commit));
333 name = commit_name;
334 }
335
dc26bd89
LT
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, "");
340 }
341
342 /* More than one parent? */
343 if (ignore_merges) {
344 if (!memcmp(buf + 46 + 48, "parent ", 7))
345 return 0;
346 }
347
e0965d83
LT
348 offset = 46;
349 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
b11645be 350 unsigned char parent[20];
e0965d83
LT
351 if (get_sha1_hex(buf + offset + 7, parent))
352 return -1;
b11645be 353 header = generate_header(name, sha1_to_hex(parent), buf, size);
5c97558c 354 diff_tree_sha1_top(parent, commit, "");
d6db0107 355 if (!header && verbose_header) {
cee99d22 356 header_prefix = "\ndiff-tree ";
d6db0107
LT
357 /*
358 * Don't print multiple merge entries if we
359 * don't print the diffs.
360 */
d6db0107 361 }
e0965d83
LT
362 offset += 48;
363 }
5098bafb 364 free(buf);
b11645be
LT
365 return 0;
366}
367
368static int diff_tree_stdin(char *line)
369{
370 int len = strlen(line);
371 unsigned char commit[20], parent[20];
372 static char this_header[1000];
373
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;
5c97558c 384 return diff_tree_sha1_top(parent, commit, "");
b11645be
LT
385 }
386 line[40] = 0;
387 return diff_tree_commit(commit, line);
e0965d83
LT
388}
389
d288a700
LT
390static int count_paths(const char **paths)
391{
392 int i = 0;
393 while (*paths++)
394 i++;
395 return i;
396}
397
4d1f1190 398static const char diff_tree_usage[] =
dda2d79a
JH
399"git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] "
400"[<common diff options>] <tree-ish> <tree-ish>"
401COMMON_DIFF_OPTIONS_HELP;
a8db165e 402
6b5ee137 403int main(int argc, const char **argv)
73134b6d 404{
0a8365a1 405 int nr_sha1;
e0965d83 406 char line[1000];
0a8365a1 407 unsigned char sha1[2][20];
d288a700 408 const char *prefix = setup_git_directory();
73134b6d 409
17712991 410 git_config(git_default_config);
0a8365a1 411 nr_sha1 = 0;
6b5ee137
JH
412 diff_setup(&diff_options);
413
c5b42386 414 for (;;) {
6b5ee137 415 int diff_opt_cnt;
6b14d7fa 416 const char *arg;
c5b42386 417
e0965d83
LT
418 argv++;
419 argc--;
420 arg = *argv;
0a8365a1 421 if (!arg)
c5b42386
LT
422 break;
423
0a8365a1
LT
424 if (*arg != '-') {
425 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
426 nr_sha1++;
427 continue;
428 }
429 break;
430 }
431
6b5ee137
JH
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;
439 }
440
441
0a8365a1 442 if (!strcmp(arg, "--")) {
e0965d83
LT
443 argv++;
444 argc--;
445 break;
446 }
bf16c71e 447 if (!strcmp(arg, "-r")) {
73134b6d
LT
448 recursive = 1;
449 continue;
450 }
4cae1a96
JH
451 if (!strcmp(arg, "-t")) {
452 recursive = show_tree_entry_in_recursive = 1;
453 continue;
454 }
e0965d83
LT
455 if (!strcmp(arg, "-m")) {
456 ignore_merges = 0;
457 continue;
458 }
cee99d22
LT
459 if (!strcmp(arg, "-v")) {
460 verbose_header = 1;
461 header_prefix = "diff-tree ";
462 continue;
463 }
a8db165e
JH
464 if (!strncmp(arg, "--pretty", 8)) {
465 verbose_header = 1;
ba88e54b 466 header_prefix = "diff-tree ";
a8db165e
JH
467 commit_format = get_commit_format(arg+8);
468 continue;
469 }
e0965d83
LT
470 if (!strcmp(arg, "--stdin")) {
471 read_stdin = 1;
472 continue;
473 }
dc26bd89
LT
474 if (!strcmp(arg, "--root")) {
475 show_root_diff = 1;
476 continue;
477 }
c5bac17a 478 usage(diff_tree_usage);
73134b6d 479 }
6b5ee137
JH
480 if (diff_options.output_format == DIFF_FORMAT_PATCH)
481 recursive = 1;
73134b6d 482
d288a700
LT
483 paths = get_pathspec(prefix, argv);
484 if (paths) {
c5b42386
LT
485 int i;
486
d288a700 487 nr_paths = count_paths(paths);
812666c8 488 pathlens = xmalloc(nr_paths * sizeof(int));
c5b42386
LT
489 for (i=0; i<nr_paths; i++)
490 pathlens[i] = strlen(paths[i]);
491 }
492
0a8365a1
LT
493 switch (nr_sha1) {
494 case 0:
495 if (!read_stdin)
496 usage(diff_tree_usage);
497 break;
498 case 1:
73848892 499 diff_tree_commit(sha1[0], NULL);
0a8365a1
LT
500 break;
501 case 2:
5c97558c 502 diff_tree_sha1_top(sha1[0], sha1[1], "");
0a8365a1
LT
503 break;
504 }
505
e0965d83 506 if (!read_stdin)
0a8365a1 507 return 0;
e0965d83 508
6b5ee137
JH
509 if (diff_options.detect_rename)
510 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
511 DIFF_SETUP_USE_CACHE);
e0965d83
LT
512 while (fgets(line, sizeof(line), stdin))
513 diff_tree_stdin(line);
514
515 return 0;
9174026c 516}