2 * Copyright (C) 2005 Junio C Hamano
9 static int matches_pathspec(const char *name, const char **spec, int cnt)
12 int namelen = strlen(name);
13 for (i = 0; i < cnt; i++) {
14 int speclen = strlen(spec[i]);
15 if (! strncmp(spec[i], name, speclen) &&
17 (name[speclen] == 0 ||
18 name[speclen] == '/'))
24 static int parse_oneside_change(const char *cp, struct diff_spec *one,
29 one->file_valid = one->sha1_valid = 1;
31 while ((ch = *cp) && '0' <= ch && ch <= '7') {
32 one->mode = (one->mode << 3) | (ch - '0');
36 if (strncmp(cp, "\tblob\t", 6))
39 if (get_sha1_hex(cp, one->blob_sha1))
48 static int parse_diff_tree_output(const char *buf,
49 const char **spec, int cnt, int reverse)
51 struct diff_spec old, new;
58 if (!cnt || matches_pathspec(cp + 1, spec, cnt))
63 parse_oneside_change(cp, &new, path);
67 parse_oneside_change(cp, &old, path);
70 old.file_valid = old.sha1_valid =
71 new.file_valid = new.sha1_valid = 1;
72 old.mode = new.mode = 0;
73 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
74 old.mode = (old.mode << 3) | (ch - '0');
77 if (strncmp(cp, "->", 2))
80 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
81 new.mode = (new.mode << 3) | (ch - '0');
84 if (strncmp(cp, "\tblob\t", 6))
87 if (get_sha1_hex(cp, old.blob_sha1))
90 if (strncmp(cp, "->", 2))
93 if (get_sha1_hex(cp, new.blob_sha1))
103 if (!cnt || matches_pathspec(path, spec, cnt)) {
105 run_external_diff(path, &new, &old);
107 run_external_diff(path, &old, &new);
112 static const char *diff_tree_helper_usage =
113 "diff-tree-helper [-R] [-z] paths...";
115 int main(int ac, const char **av) {
118 int line_termination = '\n';
122 while (1 < ac && av[1][0] == '-') {
125 else if (av[1][1] == 'z')
126 line_termination = 0;
128 usage(diff_tree_helper_usage);
131 /* the remaining parameters are paths patterns */
135 read_line(&sb, stdin, line_termination);
138 status = parse_diff_tree_output(sb.buf, av+1, ac-1, reverse);
140 fprintf(stderr, "cannot parse %s\n", sb.buf);