2 * Copyright (C) 2005 Junio C Hamano
8 static int matches_pathspec(const char *name, const char **spec, int cnt)
11 int namelen = strlen(name);
12 for (i = 0; i < cnt; i++) {
13 int speclen = strlen(spec[i]);
14 if (! strncmp(spec[i], name, speclen) &&
16 (name[speclen] == 0 ||
17 name[speclen] == '/'))
23 static int parse_oneside_change(const char *cp, struct diff_spec *one,
28 one->file_valid = one->sha1_valid = 1;
30 while ((ch = *cp) && '0' <= ch && ch <= '7') {
31 one->mode = (one->mode << 3) | (ch - '0');
35 if (strncmp(cp, "\tblob\t", 6))
38 if (get_sha1_hex(cp, one->blob_sha1))
47 static int parse_diff_tree_output(const char *buf,
48 const char **spec, int cnt, int reverse)
50 struct diff_spec old, new;
57 if (!cnt || matches_pathspec(cp + 1, spec, cnt))
62 parse_oneside_change(cp, &new, path);
66 parse_oneside_change(cp, &old, path);
69 old.file_valid = old.sha1_valid =
70 new.file_valid = new.sha1_valid = 1;
71 old.mode = new.mode = 0;
72 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
73 old.mode = (old.mode << 3) | (ch - '0');
76 if (strncmp(cp, "->", 2))
79 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
80 new.mode = (new.mode << 3) | (ch - '0');
83 if (strncmp(cp, "\tblob\t", 6))
86 if (get_sha1_hex(cp, old.blob_sha1))
89 if (strncmp(cp, "->", 2))
92 if (get_sha1_hex(cp, new.blob_sha1))
102 if (!cnt || matches_pathspec(path, spec, cnt)) {
104 run_external_diff(path, &new, &old);
106 run_external_diff(path, &old, &new);
111 static const char *diff_tree_helper_usage =
112 "diff-tree-helper [-R] [-z] paths...";
114 int main(int ac, const char **av) {
117 int line_termination = '\n';
121 while (1 < ac && av[1][0] == '-') {
124 else if (av[1][1] == 'z')
125 line_termination = 0;
127 usage(diff_tree_helper_usage);
130 /* the remaining parameters are paths patterns */
134 read_line(&sb, stdin, line_termination);
137 status = parse_diff_tree_output(sb.buf, av+1, ac-1, reverse);
139 fprintf(stderr, "cannot parse %s\n", sb.buf);