diff.c: don't add extra '/' to pathname
[git/git.git] / diff-tree-helper.c
CommitLineData
be3cfa85
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
d1df5743
JH
4#include "cache.h"
5#include "strbuf.h"
6#include "diff.h"
7
8static int matches_pathspec(const char *name, char **spec, int cnt)
9{
10 int i;
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) &&
15 speclen <= namelen &&
16 (name[speclen] == 0 ||
17 name[speclen] == '/'))
18 return 1;
19 }
20 return 0;
21}
22
be3cfa85
JH
23static int parse_oneside_change(const char *cp, struct diff_spec *one,
24 char *path)
25{
d1df5743 26 int ch;
be3cfa85
JH
27
28 one->file_valid = one->sha1_valid = 1;
29 one->mode = 0;
30 while ((ch = *cp) && '0' <= ch && ch <= '7') {
31 one->mode = (one->mode << 3) | (ch - '0');
32 cp++;
33 }
34
d1df5743
JH
35 if (strncmp(cp, "\tblob\t", 6))
36 return -1;
37 cp += 6;
be3cfa85 38 if (get_sha1_hex(cp, one->u.sha1))
d1df5743
JH
39 return -1;
40 cp += 40;
41 if (*cp++ != '\t')
42 return -1;
43 strcpy(path, cp);
44 return 0;
45}
46
0b32ff0d
JH
47#define PLEASE_WARN -1
48#define WARNED_OURSELVES -2
49
d1df5743 50static int parse_diff_tree_output(const char *buf,
be3cfa85
JH
51 struct diff_spec *old,
52 struct diff_spec *new,
d1df5743
JH
53 char *path) {
54 const char *cp = buf;
55 int ch;
d1df5743
JH
56
57 switch (*cp++) {
0b32ff0d 58 case 'U':
91f192a9 59 diff_unmerge(cp + 1);
0b32ff0d 60 return WARNED_OURSELVES;
d1df5743 61 case '+':
be3cfa85
JH
62 old->file_valid = 0;
63 return parse_oneside_change(cp, new, path);
d1df5743 64 case '-':
be3cfa85
JH
65 new->file_valid = 0;
66 return parse_oneside_change(cp, old, path);
d1df5743
JH
67 case '*':
68 break;
69 default:
0b32ff0d 70 return PLEASE_WARN;
d1df5743
JH
71 }
72
73 /* This is for '*' entries */
be3cfa85
JH
74 old->file_valid = old->sha1_valid = 1;
75 new->file_valid = new->sha1_valid = 1;
76
77 old->mode = new->mode = 0;
78 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
79 old->mode = (old->mode << 3) | (ch - '0');
80 cp++;
81 }
d1df5743 82 if (strncmp(cp, "->", 2))
0b32ff0d 83 return PLEASE_WARN;
d1df5743 84 cp += 2;
be3cfa85
JH
85 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
86 new->mode = (new->mode << 3) | (ch - '0');
87 cp++;
88 }
d1df5743 89 if (strncmp(cp, "\tblob\t", 6))
0b32ff0d 90 return PLEASE_WARN;
d1df5743 91 cp += 6;
be3cfa85 92 if (get_sha1_hex(cp, old->u.sha1))
0b32ff0d 93 return PLEASE_WARN;
d1df5743
JH
94 cp += 40;
95 if (strncmp(cp, "->", 2))
0b32ff0d 96 return PLEASE_WARN;
d1df5743 97 cp += 2;
be3cfa85 98 if (get_sha1_hex(cp, new->u.sha1))
0b32ff0d 99 return PLEASE_WARN;
d1df5743
JH
100 cp += 40;
101 if (*cp++ != '\t')
0b32ff0d 102 return PLEASE_WARN;
d1df5743 103 strcpy(path, cp);
d1df5743
JH
104 return 0;
105}
106
107static const char *diff_tree_helper_usage =
108"diff-tree-helper [-R] [-z] paths...";
109
110int main(int ac, char **av) {
111 struct strbuf sb;
112 int reverse_diff = 0;
113 int line_termination = '\n';
114
115 strbuf_init(&sb);
116
117 while (1 < ac && av[1][0] == '-') {
118 if (av[1][1] == 'R')
119 reverse_diff = 1;
120 else if (av[1][1] == 'z')
121 line_termination = 0;
122 else
123 usage(diff_tree_helper_usage);
124 ac--; av++;
125 }
126 /* the remaining parameters are paths patterns */
127
d1df5743 128 while (1) {
0b32ff0d 129 int status;
be3cfa85 130 struct diff_spec old, new;
d1df5743
JH
131 char path[PATH_MAX];
132 read_line(&sb, stdin, line_termination);
133 if (sb.eof)
134 break;
0b32ff0d
JH
135 status = parse_diff_tree_output(sb.buf, &old, &new, path);
136 if (status) {
137 if (status == PLEASE_WARN)
138 fprintf(stderr, "cannot parse %s\n", sb.buf);
d1df5743
JH
139 continue;
140 }
be3cfa85 141 if (1 < ac && !matches_pathspec(path, av+1, ac-1))
d1df5743
JH
142 continue;
143
be3cfa85 144 run_external_diff(path, &old, &new);
d1df5743
JH
145 }
146 return 0;
147}