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
->u
.sha1
))
47 static int parse_diff_tree_output(const char *buf
, const char **spec
, int cnt
)
49 struct diff_spec old
, new;
56 if (!cnt
|| matches_pathspec(cp
+ 1, spec
, cnt
))
61 parse_oneside_change(cp
, &new, path
);
65 parse_oneside_change(cp
, &old
, path
);
68 old
.file_valid
= old
.sha1_valid
=
69 new.file_valid
= new.sha1_valid
= 1;
70 old
.mode
= new.mode
= 0;
71 while ((ch
= *cp
) && ('0' <= ch
&& ch
<= '7')) {
72 old
.mode
= (old
.mode
<< 3) | (ch
- '0');
75 if (strncmp(cp
, "->", 2))
78 while ((ch
= *cp
) && ('0' <= ch
&& ch
<= '7')) {
79 new.mode
= (new.mode
<< 3) | (ch
- '0');
82 if (strncmp(cp
, "\tblob\t", 6))
85 if (get_sha1_hex(cp
, old
.u
.sha1
))
88 if (strncmp(cp
, "->", 2))
91 if (get_sha1_hex(cp
, new.u
.sha1
))
101 if (!cnt
|| matches_pathspec(path
, spec
, cnt
))
102 run_external_diff(path
, &old
, &new);
106 static const char *diff_tree_helper_usage
=
107 "diff-tree-helper [-R] [-z] paths...";
109 int main(int ac
, const char **av
) {
111 int reverse_diff
= 0;
112 int line_termination
= '\n';
116 while (1 < ac
&& av
[1][0] == '-') {
119 else if (av
[1][1] == 'z')
120 line_termination
= 0;
122 usage(diff_tree_helper_usage
);
125 /* the remaining parameters are paths patterns */
129 read_line(&sb
, stdin
, line_termination
);
132 status
= parse_diff_tree_output(sb
.buf
, av
+1, ac
-1);
134 fprintf(stderr
, "cannot parse %s\n", sb
.buf
);