5
static int show_root_diff = 0;
6
static int verbose_header = 0;
7
static int ignore_merges = 1;
8
-static int recursive = 0;
9
-static int show_tree_entry_in_recursive = 0;
8
static int read_stdin = 0;
9
12
-static struct diff_options diff_options;
13
-
10
static const char *header = NULL;
11
static const char *header_prefix = "";
12
static enum cmit_fmt commit_format = CMIT_FMT_RAW;
13
18
-// What paths are we interested in?
19
-static int nr_paths = 0;
20
-static const char **paths = NULL;
21
-static int *pathlens = NULL;
22
-
23
-static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
24
-
25
-static void update_tree_entry(void **bufp, unsigned long *sizep)
26
-{
27
- void *buf = *bufp;
28
- unsigned long size = *sizep;
29
- int len = strlen(buf) + 1 + 20;
30
-
31
- if (size < len)
32
- die("corrupt tree file");
33
- *bufp = buf + len;
34
- *sizep = size - len;
35
-}
36
-
37
-static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
38
-{
39
- int len = strlen(tree)+1;
40
- const unsigned char *sha1 = tree + len;
41
- const char *path = strchr(tree, ' ');
42
- unsigned int mode;
43
-
44
- if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
45
- die("corrupt tree file");
46
- *pathp = path+1;
47
- *modep = DIFF_FILE_CANON_MODE(mode);
48
- return sha1;
49
-}
50
-
51
-static char *malloc_base(const char *base, const char *path, int pathlen)
52
-{
53
- int baselen = strlen(base);
54
- char *newbase = xmalloc(baselen + pathlen + 2);
55
- memcpy(newbase, base, baselen);
56
- memcpy(newbase + baselen, path, pathlen);
57
- memcpy(newbase + baselen + pathlen, "/", 2);
58
- return newbase;
59
-}
60
-
61
-static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
62
-static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
63
-
64
-/* A file entry went away or appeared */
65
-static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
66
-{
67
- unsigned mode;
68
- const char *path;
69
- const unsigned char *sha1 = extract(tree, size, &path, &mode);
70
-
71
- if (recursive && S_ISDIR(mode)) {
72
- char type[20];
73
- unsigned long size;
74
- char *newbase = malloc_base(base, path, strlen(path));
75
- void *tree;
76
-
77
- tree = read_sha1_file(sha1, type, &size);
78
- if (!tree || strcmp(type, "tree"))
79
- die("corrupt tree sha %s", sha1_to_hex(sha1));
80
-
81
- show_tree(prefix, tree, size, newbase);
82
-
83
- free(tree);
84
- free(newbase);
85
- return;
86
- }
87
-
88
- diff_addremove(&diff_options, prefix[0], mode, sha1, base, path);
89
-}
90
-
91
-static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
92
-{
93
- unsigned mode1, mode2;
94
- const char *path1, *path2;
95
- const unsigned char *sha1, *sha2;
96
- int cmp, pathlen1, pathlen2;
97
-
98
- sha1 = extract(tree1, size1, &path1, &mode1);
99
- sha2 = extract(tree2, size2, &path2, &mode2);
100
-
101
- pathlen1 = strlen(path1);
102
- pathlen2 = strlen(path2);
103
- cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
104
- if (cmp < 0) {
105
- show_file("-", tree1, size1, base);
106
- return -1;
107
- }
108
- if (cmp > 0) {
109
- show_file("+", tree2, size2, base);
110
- return 1;
111
- }
112
- if (!diff_options.find_copies_harder &&
113
- !memcmp(sha1, sha2, 20) && mode1 == mode2)
114
- return 0;
115
-
116
- /*
117
- * If the filemode has changed to/from a directory from/to a regular
118
- * file, we need to consider it a remove and an add.
119
- */
120
- if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
121
- show_file("-", tree1, size1, base);
122
- show_file("+", tree2, size2, base);
123
- return 0;
124
- }
125
-
126
- if (recursive && S_ISDIR(mode1)) {
127
- int retval;
128
- char *newbase = malloc_base(base, path1, pathlen1);
129
- if (show_tree_entry_in_recursive)
130
- diff_change(&diff_options, mode1, mode2,
131
- sha1, sha2, base, path1);
132
- retval = diff_tree_sha1(sha1, sha2, newbase);
133
- free(newbase);
134
- return retval;
135
- }
136
-
137
- diff_change(&diff_options, mode1, mode2, sha1, sha2, base, path1);
138
- return 0;
139
-}
140
-
141
-static int interesting(void *tree, unsigned long size, const char *base)
142
-{
143
- const char *path;
144
- unsigned mode;
145
- int i;
146
- int baselen, pathlen;
147
-
148
- if (!nr_paths)
149
- return 1;
150
-
151
- (void)extract(tree, size, &path, &mode);
152
-
153
- pathlen = strlen(path);
154
- baselen = strlen(base);
155
-
156
- for (i=0; i < nr_paths; i++) {
157
- const char *match = paths[i];
158
- int matchlen = pathlens[i];
159
-
160
- if (baselen >= matchlen) {
161
- /* If it doesn't match, move along... */
162
- if (strncmp(base, match, matchlen))
163
- continue;
164
-
165
- /* The base is a subdirectory of a path which was specified. */
166
- return 1;
167
- }
168
-
169
- /* Does the base match? */
170
- if (strncmp(base, match, baselen))
171
- continue;
172
-
173
- match += baselen;
174
- matchlen -= baselen;
175
-
176
- if (pathlen > matchlen)
177
- continue;
178
-
179
- if (matchlen > pathlen) {
180
- if (match[pathlen] != '/')
181
- continue;
182
- if (!S_ISDIR(mode))
183
- continue;
184
- }
185
-
186
- if (strncmp(path, match, pathlen))
187
- continue;
188
-
189
- return 1;
190
- }
191
- return 0; /* No matches */
192
-}
193
-
194
-/* A whole sub-tree went away or appeared */
195
-static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
196
-{
197
- while (size) {
198
- if (interesting(tree, size, base))
199
- show_file(prefix, tree, size, base);
200
- update_tree_entry(&tree, &size);
201
- }
202
-}
203
-
204
-static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
205
-{
206
- while (size1 | size2) {
207
- if (nr_paths && size1 && !interesting(tree1, size1, base)) {
208
- update_tree_entry(&tree1, &size1);
209
- continue;
210
- }
211
- if (nr_paths && size2 && !interesting(tree2, size2, base)) {
212
- update_tree_entry(&tree2, &size2);
213
- continue;
214
- }
215
- if (!size1) {
216
- show_file("+", tree2, size2, base);
217
- update_tree_entry(&tree2, &size2);
218
- continue;
219
- }
220
- if (!size2) {
221
- show_file("-", tree1, size1, base);
222
- update_tree_entry(&tree1, &size1);
223
- continue;
224
- }
225
- switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
226
- case -1:
227
- update_tree_entry(&tree1, &size1);
228
- continue;
229
- case 0:
230
- update_tree_entry(&tree1, &size1);
231
- /* Fallthrough */
232
- case 1:
233
- update_tree_entry(&tree2, &size2);
234
- continue;
235
- }
236
- die("git-diff-tree: internal error");
237
- }
238
- return 0;
239
-}
240
-
241
-static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
242
-{
243
- void *tree1, *tree2;
244
- unsigned long size1, size2;
245
- int retval;
246
-
247
- tree1 = read_object_with_reference(old, "tree", &size1, NULL);
248
- if (!tree1)
249
- die("unable to read source tree (%s)", sha1_to_hex(old));
250
- tree2 = read_object_with_reference(new, "tree", &size2, NULL);
251
- if (!tree2)
252
- die("unable to read destination tree (%s)", sha1_to_hex(new));
253
- retval = diff_tree(tree1, size1, tree2, size2, base);
254
- free(tree1);
255
- free(tree2);
256
- return retval;
257
-}
14
+static struct diff_options diff_options;
15
16
static void call_diff_setup_done(void)
17
{
42
int ret;
43
44
call_diff_setup_done();
288
- ret = diff_tree_sha1(old, new, base);
45
+ ret = diff_tree_sha1(old, new, base, &diff_options);
46
call_diff_flush();
47
return ret;
48
}
51
{
52
int retval;
53
void *tree;
297
- unsigned long size;
54
+ struct tree_desc empty, real;
55
56
call_diff_setup_done();
300
- tree = read_object_with_reference(new, "tree", &size, NULL);
57
+ tree = read_object_with_reference(new, "tree", &real.size, NULL);
58
if (!tree)
59
die("unable to read root tree (%s)", sha1_to_hex(new));
303
- retval = diff_tree("", 0, tree, size, base);
60
+ real.buf = tree;
61
+
62
+ empty.buf = "";
63
+ empty.size = 0;
64
+ retval = diff_tree(&empty, &real, base, &diff_options);
65
free(tree);
66
call_diff_flush();
67
return retval;
148
return diff_tree_commit(commit, line);
149
}
150
390
-static int count_paths(const char **paths)
391
-{
392
- int i = 0;
393
- while (*paths++)
394
- i++;
395
- return i;
396
-}
397
-
151
static const char diff_tree_usage[] =
152
"git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] "
153
"[<common diff options>] <tree-ish> <tree-ish>"
198
break;
199
}
200
if (!strcmp(arg, "-r")) {
448
- recursive = 1;
201
+ diff_options.recursive = 1;
202
continue;
203
}
204
if (!strcmp(arg, "-t")) {
452
- recursive = show_tree_entry_in_recursive = 1;
205
+ diff_options.recursive = 1;
206
+ diff_options.tree_in_recursive = 1;
207
continue;
208
}
209
if (!strcmp(arg, "-m")) {
232
usage(diff_tree_usage);
233
}
234
if (diff_options.output_format == DIFF_FORMAT_PATCH)
481
- recursive = 1;
235
+ diff_options.recursive = 1;
236
483
- paths = get_pathspec(prefix, argv);
484
- if (paths) {
485
- int i;
486
-
487
- nr_paths = count_paths(paths);
488
- pathlens = xmalloc(nr_paths * sizeof(int));
489
- for (i=0; i<nr_paths; i++)
490
- pathlens[i] = strlen(paths[i]);
491
- }
237
+ diff_tree_setup_paths(get_pathspec(prefix, argv));
238
239
switch (nr_sha1) {
240
case 0: