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