[PATCH] ssh-protocol version, command types, response code
[git/mingw.git] / diff-tree.c
blob3881c23ca37a86c41ec12b2d5c4713ab01cc9d82
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
4 #include "commit.h"
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 show_tree_entry_in_recursive = 0;
11 static int read_stdin = 0;
12 static int diff_output_format = DIFF_FORMAT_HUMAN;
13 static int detect_rename = 0;
14 static int diff_setup_opt = 0;
15 static int diff_score_opt = 0;
16 static const char *pickaxe = NULL;
17 static int pickaxe_opts = 0;
18 static int diff_break_opt = -1;
19 static const char *orderfile = NULL;
20 static const char *header = NULL;
21 static const char *header_prefix = "";
23 // What paths are we interested in?
24 static int nr_paths = 0;
25 static const char **paths = NULL;
26 static int *pathlens = NULL;
28 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
30 static void update_tree_entry(void **bufp, unsigned long *sizep)
32 void *buf = *bufp;
33 unsigned long size = *sizep;
34 int len = strlen(buf) + 1 + 20;
36 if (size < len)
37 die("corrupt tree file");
38 *bufp = buf + len;
39 *sizep = size - len;
42 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
44 int len = strlen(tree)+1;
45 const unsigned char *sha1 = tree + len;
46 const char *path = strchr(tree, ' ');
47 unsigned int mode;
49 if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
50 die("corrupt tree file");
51 *pathp = path+1;
52 *modep = DIFF_FILE_CANON_MODE(mode);
53 return sha1;
56 static char *malloc_base(const char *base, const char *path, int pathlen)
58 int baselen = strlen(base);
59 char *newbase = xmalloc(baselen + pathlen + 2);
60 memcpy(newbase, base, baselen);
61 memcpy(newbase + baselen, path, pathlen);
62 memcpy(newbase + baselen + pathlen, "/", 2);
63 return newbase;
66 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
67 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
69 /* A file entry went away or appeared */
70 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
72 unsigned mode;
73 const char *path;
74 const unsigned char *sha1 = extract(tree, size, &path, &mode);
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;
82 tree = read_sha1_file(sha1, type, &size);
83 if (!tree || strcmp(type, "tree"))
84 die("corrupt tree sha %s", sha1_to_hex(sha1));
86 show_tree(prefix, tree, size, newbase);
88 free(tree);
89 free(newbase);
90 return;
93 diff_addremove(prefix[0], mode, sha1, base, path);
96 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
98 unsigned mode1, mode2;
99 const char *path1, *path2;
100 const unsigned char *sha1, *sha2;
101 int cmp, pathlen1, pathlen2;
103 sha1 = extract(tree1, size1, &path1, &mode1);
104 sha2 = extract(tree2, size2, &path2, &mode2);
106 pathlen1 = strlen(path1);
107 pathlen2 = strlen(path2);
108 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
109 if (cmp < 0) {
110 show_file("-", tree1, size1, base);
111 return -1;
113 if (cmp > 0) {
114 show_file("+", tree2, size2, base);
115 return 1;
117 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
118 return 0;
121 * If the filemode has changed to/from a directory from/to a regular
122 * file, we need to consider it a remove and an add.
124 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
125 show_file("-", tree1, size1, base);
126 show_file("+", tree2, size2, base);
127 return 0;
130 if (recursive && S_ISDIR(mode1)) {
131 int retval;
132 char *newbase = malloc_base(base, path1, pathlen1);
133 if (show_tree_entry_in_recursive)
134 diff_change(mode1, mode2, sha1, sha2, base, path1);
135 retval = diff_tree_sha1(sha1, sha2, newbase);
136 free(newbase);
137 return retval;
140 diff_change(mode1, mode2, sha1, sha2, base, path1);
141 return 0;
144 static int interesting(void *tree, unsigned long size, const char *base)
146 const char *path;
147 unsigned mode;
148 int i;
149 int baselen, pathlen;
151 if (!nr_paths)
152 return 1;
154 (void)extract(tree, size, &path, &mode);
156 pathlen = strlen(path);
157 baselen = strlen(base);
159 for (i=0; i < nr_paths; i++) {
160 const char *match = paths[i];
161 int matchlen = pathlens[i];
163 if (baselen >= matchlen) {
164 /* If it doesn't match, move along... */
165 if (strncmp(base, match, matchlen))
166 continue;
168 /* The base is a subdirectory of a path which was specified. */
169 return 1;
172 /* Does the base match? */
173 if (strncmp(base, match, baselen))
174 continue;
176 match += baselen;
177 matchlen -= baselen;
179 if (pathlen > matchlen)
180 continue;
182 if (matchlen > pathlen) {
183 if (match[pathlen] != '/')
184 continue;
185 if (!S_ISDIR(mode))
186 continue;
189 if (strncmp(path, match, pathlen))
190 continue;
192 return 1;
194 return 0; /* No matches */
197 /* A whole sub-tree went away or appeared */
198 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
200 while (size) {
201 if (interesting(tree, size, base))
202 show_file(prefix, tree, size, base);
203 update_tree_entry(&tree, &size);
207 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
209 while (size1 | size2) {
210 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
211 update_tree_entry(&tree1, &size1);
212 continue;
214 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
215 update_tree_entry(&tree2, &size2);
216 continue;
218 if (!size1) {
219 show_file("+", tree2, size2, base);
220 update_tree_entry(&tree2, &size2);
221 continue;
223 if (!size2) {
224 show_file("-", tree1, size1, base);
225 update_tree_entry(&tree1, &size1);
226 continue;
228 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
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;
239 die("git-diff-tree: internal error");
241 return 0;
244 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
246 void *tree1, *tree2;
247 unsigned long size1, size2;
248 int retval;
250 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
251 if (!tree1)
252 die("unable to read source tree (%s)", sha1_to_hex(old));
253 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
254 if (!tree2)
255 die("unable to read destination tree (%s)", sha1_to_hex(new));
256 retval = diff_tree(tree1, size1, tree2, size2, base);
257 free(tree1);
258 free(tree2);
259 return retval;
262 static void call_diff_setup(void)
264 diff_setup(diff_setup_opt);
267 static int call_diff_flush(void)
269 diffcore_std(0,
270 detect_rename, diff_score_opt,
271 pickaxe, pickaxe_opts,
272 diff_break_opt,
273 orderfile);
274 if (diff_queue_is_empty()) {
275 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
276 return 0;
278 if (header) {
279 const char *fmt = "%s";
280 if (diff_output_format == DIFF_FORMAT_MACHINE)
281 fmt = "%s%c";
283 printf(fmt, header, 0);
284 header = NULL;
286 diff_flush(diff_output_format, 1);
287 return 1;
290 static int diff_tree_sha1_top(const unsigned char *old,
291 const unsigned char *new, const char *base)
293 int ret;
295 call_diff_setup();
296 ret = diff_tree_sha1(old, new, base);
297 call_diff_flush();
298 return ret;
301 static int diff_root_tree(const unsigned char *new, const char *base)
303 int retval;
304 void *tree;
305 unsigned long size;
307 call_diff_setup();
308 tree = read_object_with_reference(new, "tree", &size, NULL);
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);
313 call_diff_flush();
314 return retval;
317 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
319 static char this_header[16384];
320 int offset;
322 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
323 if (verbose_header) {
324 offset += pretty_print_commit(msg, len, this_header + offset, sizeof(this_header) - offset);
325 this_header[offset++] = '\n';
326 this_header[offset++] = 0;
329 return this_header;
332 static int diff_tree_commit(const unsigned char *commit, const char *name)
334 unsigned long size, offset;
335 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
337 if (!buf)
338 return -1;
340 if (!name) {
341 static char commit_name[60];
342 strcpy(commit_name, sha1_to_hex(commit));
343 name = commit_name;
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, "");
352 /* More than one parent? */
353 if (ignore_merges) {
354 if (!memcmp(buf + 46 + 48, "parent ", 7))
355 return 0;
358 offset = 46;
359 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
360 unsigned char parent[20];
361 if (get_sha1_hex(buf + offset + 7, parent))
362 return -1;
363 header = generate_header(name, sha1_to_hex(parent), buf, size);
364 diff_tree_sha1_top(parent, commit, "");
365 if (!header && verbose_header) {
366 header_prefix = "\ndiff-tree ";
368 * Don't print multiple merge entries if we
369 * don't print the diffs.
372 offset += 48;
374 return 0;
377 static int diff_tree_stdin(char *line)
379 int len = strlen(line);
380 unsigned char commit[20], parent[20];
381 static char this_header[1000];
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;
393 return diff_tree_sha1_top(parent, commit, "");
395 line[40] = 0;
396 return diff_tree_commit(commit, line);
399 static char *diff_tree_usage =
400 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
402 int main(int argc, const char **argv)
404 int nr_sha1;
405 char line[1000];
406 unsigned char sha1[2][20];
408 nr_sha1 = 0;
409 for (;;) {
410 const char *arg;
412 argv++;
413 argc--;
414 arg = *argv;
415 if (!arg)
416 break;
418 if (*arg != '-') {
419 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
420 nr_sha1++;
421 continue;
423 break;
426 if (!strcmp(arg, "--")) {
427 argv++;
428 argc--;
429 break;
431 if (!strcmp(arg, "-r")) {
432 recursive = 1;
433 continue;
435 if (!strcmp(arg, "-t")) {
436 recursive = show_tree_entry_in_recursive = 1;
437 continue;
439 if (!strcmp(arg, "-R")) {
440 diff_setup_opt |= DIFF_SETUP_REVERSE;
441 continue;
443 if (!strcmp(arg, "-p")) {
444 diff_output_format = DIFF_FORMAT_PATCH;
445 recursive = 1;
446 continue;
448 if (!strncmp(arg, "-S", 2)) {
449 pickaxe = arg + 2;
450 continue;
452 if (!strncmp(arg, "-O", 2)) {
453 orderfile = arg + 2;
454 continue;
456 if (!strcmp(arg, "--pickaxe-all")) {
457 pickaxe_opts = DIFF_PICKAXE_ALL;
458 continue;
460 if (!strncmp(arg, "-M", 2)) {
461 detect_rename = DIFF_DETECT_RENAME;
462 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
463 usage(diff_tree_usage);
464 continue;
466 if (!strncmp(arg, "-C", 2)) {
467 detect_rename = DIFF_DETECT_COPY;
468 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
469 usage(diff_tree_usage);
470 continue;
472 if (!strncmp(arg, "-B", 2)) {
473 if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
474 usage(diff_tree_usage);
475 continue;
477 if (!strcmp(arg, "-z")) {
478 diff_output_format = DIFF_FORMAT_MACHINE;
479 continue;
481 if (!strcmp(arg, "-m")) {
482 ignore_merges = 0;
483 continue;
485 if (!strcmp(arg, "-s")) {
486 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
487 continue;
489 if (!strcmp(arg, "-v")) {
490 verbose_header = 1;
491 header_prefix = "diff-tree ";
492 continue;
494 if (!strcmp(arg, "--stdin")) {
495 read_stdin = 1;
496 continue;
498 if (!strcmp(arg, "--root")) {
499 show_root_diff = 1;
500 continue;
502 usage(diff_tree_usage);
505 if (argc > 0) {
506 int i;
508 paths = argv;
509 nr_paths = argc;
510 pathlens = xmalloc(nr_paths * sizeof(int));
511 for (i=0; i<nr_paths; i++)
512 pathlens[i] = strlen(paths[i]);
515 switch (nr_sha1) {
516 case 0:
517 if (!read_stdin)
518 usage(diff_tree_usage);
519 break;
520 case 1:
521 diff_tree_commit(sha1[0], NULL);
522 break;
523 case 2:
524 diff_tree_sha1_top(sha1[0], sha1[1], "");
525 break;
528 if (!read_stdin)
529 return 0;
531 if (detect_rename)
532 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
533 DIFF_SETUP_USE_CACHE);
534 while (fgets(line, sizeof(line), stdin))
535 diff_tree_stdin(line);
537 return 0;