git-diff-tree: clean up output
[git/git.git] / diff-tree.c
CommitLineData
e0965d83 1#include <ctype.h>
9174026c 2#include "cache.h"
3ebfd4aa 3#include "diff.h"
9174026c 4
f4f21ce3 5static int silent = 0;
e0965d83 6static int ignore_merges = 1;
73134b6d 7static int recursive = 0;
e0965d83 8static int read_stdin = 0;
6cbd72f8 9static int line_termination = '\n';
3ebfd4aa 10static int generate_patch = 0;
f4f21ce3 11static const char *header = NULL;
9174026c 12
c5b42386
LT
13// What paths are we interested in?
14static int nr_paths = 0;
15static char **paths = NULL;
16static int *pathlens = NULL;
17
eeb79916 18static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
73134b6d
LT
19
20static void update_tree_entry(void **bufp, unsigned long *sizep)
9174026c
LT
21{
22 void *buf = *bufp;
23 unsigned long size = *sizep;
24 int len = strlen(buf) + 1 + 20;
25
26 if (size < len)
2de381f9 27 die("corrupt tree file");
9174026c
LT
28 *bufp = buf + len;
29 *sizep = size - len;
9174026c
LT
30}
31
32static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
33{
34 int len = strlen(tree)+1;
35 const unsigned char *sha1 = tree + len;
36 const char *path = strchr(tree, ' ');
37
38 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
2de381f9 39 die("corrupt tree file");
9174026c
LT
40 *pathp = path+1;
41 return sha1;
42}
43
262e82b4
LT
44static char *malloc_base(const char *base, const char *path, int pathlen)
45{
46 int baselen = strlen(base);
812666c8 47 char *newbase = xmalloc(baselen + pathlen + 2);
262e82b4
LT
48 memcpy(newbase, base, baselen);
49 memcpy(newbase + baselen, path, pathlen);
50 memcpy(newbase + baselen + pathlen, "/", 2);
51 return newbase;
52}
53
54static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
55
56/* A whole sub-tree went away or appeared */
57static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
58{
59 while (size) {
60 show_file(prefix, tree, size, base);
61 update_tree_entry(&tree, &size);
62 }
63}
64
65/* A file entry went away or appeared */
73134b6d 66static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
9174026c
LT
67{
68 unsigned mode;
69 const char *path;
70 const unsigned char *sha1 = extract(tree, size, &path, &mode);
262e82b4 71
f4f21ce3
LT
72 if (header) {
73 printf("%s", header);
74 header = NULL;
75 }
76
77 if (silent)
78 return;
79
262e82b4
LT
80 if (recursive && S_ISDIR(mode)) {
81 char type[20];
82 unsigned long size;
83 char *newbase = malloc_base(base, path, strlen(path));
84 void *tree;
85
86 tree = read_sha1_file(sha1, type, &size);
87 if (!tree || strcmp(type, "tree"))
2de381f9 88 die("corrupt tree sha %s", sha1_to_hex(sha1));
262e82b4
LT
89
90 show_tree(prefix, tree, size, newbase);
91
92 free(tree);
93 free(newbase);
94 return;
95 }
96
3ebfd4aa
JH
97 if (generate_patch) {
98 if (!S_ISDIR(mode))
99 diff_addremove(prefix[0], mode, sha1, base, path);
100 }
101 else
102 printf("%s%06o\t%s\t%s\t%s%s%c", prefix, mode,
103 S_ISDIR(mode) ? "tree" : "blob",
104 sha1_to_hex(sha1), base, path,
105 line_termination);
9174026c
LT
106}
107
eeb79916 108static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
9174026c
LT
109{
110 unsigned mode1, mode2;
111 const char *path1, *path2;
112 const unsigned char *sha1, *sha2;
73134b6d 113 int cmp, pathlen1, pathlen2;
eeb79916 114 char old_sha1_hex[50];
9174026c
LT
115
116 sha1 = extract(tree1, size1, &path1, &mode1);
117 sha2 = extract(tree2, size2, &path2, &mode2);
118
73134b6d
LT
119 pathlen1 = strlen(path1);
120 pathlen2 = strlen(path2);
121 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
9174026c 122 if (cmp < 0) {
eeb79916 123 show_file("-", tree1, size1, base);
9174026c
LT
124 return -1;
125 }
126 if (cmp > 0) {
eeb79916 127 show_file("+", tree2, size2, base);
9174026c
LT
128 return 1;
129 }
130 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
131 return 0;
262e82b4
LT
132
133 /*
134 * If the filemode has changed to/from a directory from/to a regular
aebb2679 135 * file, we need to consider it a remove and an add.
262e82b4
LT
136 */
137 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
138 show_file("-", tree1, size1, base);
139 show_file("+", tree2, size2, base);
140 return 0;
141 }
142
143 if (recursive && S_ISDIR(mode1)) {
eeb79916 144 int retval;
262e82b4 145 char *newbase = malloc_base(base, path1, pathlen1);
eeb79916
LT
146 retval = diff_tree_sha1(sha1, sha2, newbase);
147 free(newbase);
148 return retval;
73134b6d
LT
149 }
150
f4f21ce3
LT
151 if (header) {
152 printf("%s", header);
153 header = NULL;
154 }
155 if (silent)
156 return 0;
157
3ebfd4aa
JH
158 if (generate_patch) {
159 if (!S_ISDIR(mode1))
160 diff_change(mode1, mode2, sha1, sha2, base, path1);
161 }
162 else {
163 strcpy(old_sha1_hex, sha1_to_hex(sha1));
164 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
165 S_ISDIR(mode1) ? "tree" : "blob",
166 old_sha1_hex, sha1_to_hex(sha2), base, path1,
167 line_termination);
168 }
9174026c
LT
169 return 0;
170}
171
c5b42386
LT
172static int interesting(void *tree, unsigned long size, const char *base)
173{
174 const char *path;
175 unsigned mode;
176 int i;
177 int baselen, pathlen;
178
179 if (!nr_paths)
180 return 1;
181
182 (void)extract(tree, size, &path, &mode);
183
184 pathlen = strlen(path);
185 baselen = strlen(base);
186
187 for (i=0; i < nr_paths; i++) {
188 const char *match = paths[i];
189 int matchlen = pathlens[i];
190
191 if (baselen >= matchlen) {
192 /* If it doesn't match, move along... */
193 if (strncmp(base, match, matchlen))
194 continue;
195
196 /* The base is a subdirectory of a path which was specified. */
197 return 1;
198 }
199
200 /* Does the base match? */
201 if (strncmp(base, match, baselen))
202 continue;
203
204 match += baselen;
205 matchlen -= baselen;
206
207 if (pathlen > matchlen)
208 continue;
209
210 if (strncmp(path, match, pathlen))
211 continue;
212
213 return 1;
214 }
215 return 0; /* No matches */
216}
217
eeb79916 218static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
9174026c
LT
219{
220 while (size1 | size2) {
c5b42386
LT
221 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
222 update_tree_entry(&tree1, &size1);
223 continue;
224 }
225 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
226 update_tree_entry(&tree2, &size2);
227 continue;
228 }
9174026c 229 if (!size1) {
eeb79916 230 show_file("+", tree2, size2, base);
9174026c
LT
231 update_tree_entry(&tree2, &size2);
232 continue;
233 }
234 if (!size2) {
eeb79916 235 show_file("-", tree1, size1, base);
9174026c
LT
236 update_tree_entry(&tree1, &size1);
237 continue;
238 }
eeb79916 239 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
9174026c
LT
240 case -1:
241 update_tree_entry(&tree1, &size1);
242 continue;
243 case 0:
244 update_tree_entry(&tree1, &size1);
245 /* Fallthrough */
246 case 1:
247 update_tree_entry(&tree2, &size2);
248 continue;
249 }
2de381f9 250 die("diff-tree: internal error");
9174026c
LT
251 }
252 return 0;
253}
254
eeb79916 255static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
9174026c 256{
9174026c
LT
257 void *tree1, *tree2;
258 unsigned long size1, size2;
73134b6d 259 int retval;
9174026c 260
40469ee9 261 tree1 = read_object_with_reference(old, "tree", &size1, 0);
c1fdf2a6 262 if (!tree1)
2de381f9 263 die("unable to read source tree (%s)", sha1_to_hex(old));
40469ee9 264 tree2 = read_object_with_reference(new, "tree", &size2, 0);
c1fdf2a6 265 if (!tree2)
2de381f9 266 die("unable to read destination tree (%s)", sha1_to_hex(new));
eeb79916 267 retval = diff_tree(tree1, size1, tree2, size2, base);
73134b6d
LT
268 free(tree1);
269 free(tree2);
270 return retval;
271}
272
e0965d83
LT
273static int diff_tree_stdin(char *line)
274{
275 int len = strlen(line);
276 unsigned char commit[20], parent[20];
277 unsigned long size, offset;
f4f21ce3 278 static char this_header[100];
e0965d83
LT
279 char *buf;
280
281 if (!len || line[len-1] != '\n')
282 return -1;
283 line[len-1] = 0;
284 if (get_sha1_hex(line, commit))
285 return -1;
286 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
f4f21ce3 287 line[40] = 0;
e0965d83 288 line[81] = 0;
f4f21ce3
LT
289 sprintf(this_header, "%s (from %s)\n", line, line+41);
290 header = this_header;
e0965d83
LT
291 return diff_tree_sha1(parent, commit, "");
292 }
293 buf = read_object_with_reference(commit, "commit", &size, NULL);
294 if (!buf)
295 return -1;
296
297 /* More than one parent? */
298 if (ignore_merges) {
299 if (!memcmp(buf + 46 + 48, "parent ", 7))
300 return 0;
301 }
302
303 line[40] = 0;
304 offset = 46;
305 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
306 if (get_sha1_hex(buf + offset + 7, parent))
307 return -1;
f4f21ce3
LT
308 sprintf(this_header, "%s (from %s)\n", line, sha1_to_hex(parent));
309 header = this_header;
e0965d83
LT
310 diff_tree_sha1(parent, commit, "");
311 offset += 48;
312 }
313 return -1;
314}
315
ed4eeaf2 316static char *diff_tree_usage = "diff-tree [-p] [-r] [-z] <tree sha1> <tree sha1>";
c5bac17a 317
73134b6d
LT
318int main(int argc, char **argv)
319{
e0965d83 320 char line[1000];
73134b6d
LT
321 unsigned char old[20], new[20];
322
c5b42386 323 for (;;) {
e0965d83 324 char *arg;
c5b42386 325
e0965d83
LT
326 argv++;
327 argc--;
328 arg = *argv;
c5b42386
LT
329 if (!arg || *arg != '-')
330 break;
331
e0965d83
LT
332 if (!strcmp(arg, "-")) {
333 argv++;
334 argc--;
335 break;
336 }
bf16c71e 337 if (!strcmp(arg, "-r")) {
73134b6d
LT
338 recursive = 1;
339 continue;
340 }
3ebfd4aa 341 if (!strcmp(arg, "-p")) {
3a663fd9 342 recursive = generate_patch = 1;
3ebfd4aa
JH
343 continue;
344 }
6cbd72f8
LT
345 if (!strcmp(arg, "-z")) {
346 line_termination = '\0';
347 continue;
348 }
e0965d83
LT
349 if (!strcmp(arg, "-m")) {
350 ignore_merges = 0;
351 continue;
352 }
f4f21ce3
LT
353 if (!strcmp(arg, "-s")) {
354 silent = 1;
355 continue;
356 }
e0965d83
LT
357 if (!strcmp(arg, "--stdin")) {
358 read_stdin = 1;
359 continue;
360 }
c5bac17a 361 usage(diff_tree_usage);
73134b6d
LT
362 }
363
e0965d83
LT
364 if (!read_stdin) {
365 if (argc < 2 || get_sha1(argv[0], old) || get_sha1(argv[1], new))
366 usage(diff_tree_usage);
367 argv += 2;
368 argc -= 2;
369 }
c5b42386 370
e0965d83 371 if (argc > 0) {
c5b42386
LT
372 int i;
373
e0965d83
LT
374 paths = argv;
375 nr_paths = argc;
812666c8 376 pathlens = xmalloc(nr_paths * sizeof(int));
c5b42386
LT
377 for (i=0; i<nr_paths; i++)
378 pathlens[i] = strlen(paths[i]);
379 }
380
e0965d83
LT
381 if (!read_stdin)
382 return diff_tree_sha1(old, new, "");
383
384 while (fgets(line, sizeof(line), stdin))
385 diff_tree_stdin(line);
386
387 return 0;
9174026c 388}