[PATCH] cleanup of in-code names
[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;
cee99d22 6static int verbose_header = 0;
e0965d83 7static int ignore_merges = 1;
73134b6d 8static int recursive = 0;
e0965d83 9static int read_stdin = 0;
6cbd72f8 10static int line_termination = '\n';
3ebfd4aa 11static int generate_patch = 0;
5c97558c 12static int detect_rename = 0;
f4f21ce3 13static const char *header = NULL;
cee99d22 14static const char *header_prefix = "";
9174026c 15
c5b42386
LT
16// What paths are we interested in?
17static int nr_paths = 0;
18static char **paths = NULL;
19static int *pathlens = NULL;
20
eeb79916 21static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
73134b6d
LT
22
23static void update_tree_entry(void **bufp, unsigned long *sizep)
9174026c
LT
24{
25 void *buf = *bufp;
26 unsigned long size = *sizep;
27 int len = strlen(buf) + 1 + 20;
28
29 if (size < len)
2de381f9 30 die("corrupt tree file");
9174026c
LT
31 *bufp = buf + len;
32 *sizep = size - len;
9174026c
LT
33}
34
35static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
36{
37 int len = strlen(tree)+1;
38 const unsigned char *sha1 = tree + len;
39 const char *path = strchr(tree, ' ');
40
41 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
2de381f9 42 die("corrupt tree file");
9174026c
LT
43 *pathp = path+1;
44 return sha1;
45}
46
262e82b4
LT
47static char *malloc_base(const char *base, const char *path, int pathlen)
48{
49 int baselen = strlen(base);
812666c8 50 char *newbase = xmalloc(baselen + pathlen + 2);
262e82b4
LT
51 memcpy(newbase, base, baselen);
52 memcpy(newbase + baselen, path, pathlen);
53 memcpy(newbase + baselen + pathlen, "/", 2);
54 return newbase;
55}
56
57static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
ed1a368b 58static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
262e82b4
LT
59
60/* A file entry went away or appeared */
73134b6d 61static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
9174026c
LT
62{
63 unsigned mode;
64 const char *path;
65 const unsigned char *sha1 = extract(tree, size, &path, &mode);
262e82b4 66
f4f21ce3
LT
67 if (header) {
68 printf("%s", header);
69 header = NULL;
70 }
71
72 if (silent)
73 return;
74
262e82b4
LT
75 if (recursive && S_ISDIR(mode)) {
76 char type[20];
77 unsigned long size;
78 char *newbase = malloc_base(base, path, strlen(path));
79 void *tree;
80
81 tree = read_sha1_file(sha1, type, &size);
82 if (!tree || strcmp(type, "tree"))
2de381f9 83 die("corrupt tree sha %s", sha1_to_hex(sha1));
262e82b4
LT
84
85 show_tree(prefix, tree, size, newbase);
86
87 free(tree);
88 free(newbase);
89 return;
90 }
91
3ebfd4aa
JH
92 if (generate_patch) {
93 if (!S_ISDIR(mode))
94 diff_addremove(prefix[0], mode, sha1, base, path);
95 }
96 else
97 printf("%s%06o\t%s\t%s\t%s%s%c", prefix, mode,
98 S_ISDIR(mode) ? "tree" : "blob",
99 sha1_to_hex(sha1), base, path,
100 line_termination);
9174026c
LT
101}
102
eeb79916 103static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
9174026c
LT
104{
105 unsigned mode1, mode2;
106 const char *path1, *path2;
107 const unsigned char *sha1, *sha2;
73134b6d 108 int cmp, pathlen1, pathlen2;
eeb79916 109 char old_sha1_hex[50];
9174026c
LT
110
111 sha1 = extract(tree1, size1, &path1, &mode1);
112 sha2 = extract(tree2, size2, &path2, &mode2);
113
73134b6d
LT
114 pathlen1 = strlen(path1);
115 pathlen2 = strlen(path2);
116 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
9174026c 117 if (cmp < 0) {
eeb79916 118 show_file("-", tree1, size1, base);
9174026c
LT
119 return -1;
120 }
121 if (cmp > 0) {
eeb79916 122 show_file("+", tree2, size2, base);
9174026c
LT
123 return 1;
124 }
125 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
126 return 0;
262e82b4
LT
127
128 /*
129 * If the filemode has changed to/from a directory from/to a regular
aebb2679 130 * file, we need to consider it a remove and an add.
262e82b4
LT
131 */
132 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
133 show_file("-", tree1, size1, base);
134 show_file("+", tree2, size2, base);
135 return 0;
136 }
137
138 if (recursive && S_ISDIR(mode1)) {
eeb79916 139 int retval;
262e82b4 140 char *newbase = malloc_base(base, path1, pathlen1);
eeb79916
LT
141 retval = diff_tree_sha1(sha1, sha2, newbase);
142 free(newbase);
143 return retval;
73134b6d
LT
144 }
145
f4f21ce3
LT
146 if (header) {
147 printf("%s", header);
148 header = NULL;
149 }
150 if (silent)
151 return 0;
152
3ebfd4aa
JH
153 if (generate_patch) {
154 if (!S_ISDIR(mode1))
155 diff_change(mode1, mode2, sha1, sha2, base, path1);
156 }
157 else {
158 strcpy(old_sha1_hex, sha1_to_hex(sha1));
159 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
160 S_ISDIR(mode1) ? "tree" : "blob",
161 old_sha1_hex, sha1_to_hex(sha2), base, path1,
162 line_termination);
163 }
9174026c
LT
164 return 0;
165}
166
c5b42386
LT
167static int interesting(void *tree, unsigned long size, const char *base)
168{
169 const char *path;
170 unsigned mode;
171 int i;
172 int baselen, pathlen;
173
174 if (!nr_paths)
175 return 1;
176
177 (void)extract(tree, size, &path, &mode);
178
179 pathlen = strlen(path);
180 baselen = strlen(base);
181
182 for (i=0; i < nr_paths; i++) {
183 const char *match = paths[i];
184 int matchlen = pathlens[i];
185
186 if (baselen >= matchlen) {
187 /* If it doesn't match, move along... */
188 if (strncmp(base, match, matchlen))
189 continue;
190
191 /* The base is a subdirectory of a path which was specified. */
192 return 1;
193 }
194
195 /* Does the base match? */
196 if (strncmp(base, match, baselen))
197 continue;
198
199 match += baselen;
200 matchlen -= baselen;
201
202 if (pathlen > matchlen)
203 continue;
204
cb6c8ed2
LT
205 if (matchlen > pathlen) {
206 if (match[pathlen] != '/')
207 continue;
850e82d8
LT
208 if (!S_ISDIR(mode))
209 continue;
cb6c8ed2
LT
210 }
211
c5b42386
LT
212 if (strncmp(path, match, pathlen))
213 continue;
214
215 return 1;
216 }
217 return 0; /* No matches */
218}
219
ed1a368b
LT
220/* A whole sub-tree went away or appeared */
221static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
222{
223 while (size) {
224 if (interesting(tree, size, base))
225 show_file(prefix, tree, size, base);
226 update_tree_entry(&tree, &size);
227 }
228}
229
eeb79916 230static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
9174026c
LT
231{
232 while (size1 | size2) {
c5b42386
LT
233 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
234 update_tree_entry(&tree1, &size1);
235 continue;
236 }
237 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
238 update_tree_entry(&tree2, &size2);
239 continue;
240 }
9174026c 241 if (!size1) {
eeb79916 242 show_file("+", tree2, size2, base);
9174026c
LT
243 update_tree_entry(&tree2, &size2);
244 continue;
245 }
246 if (!size2) {
eeb79916 247 show_file("-", tree1, size1, base);
9174026c
LT
248 update_tree_entry(&tree1, &size1);
249 continue;
250 }
eeb79916 251 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
9174026c
LT
252 case -1:
253 update_tree_entry(&tree1, &size1);
254 continue;
255 case 0:
256 update_tree_entry(&tree1, &size1);
257 /* Fallthrough */
258 case 1:
259 update_tree_entry(&tree2, &size2);
260 continue;
261 }
667bb59b 262 die("git-diff-tree: internal error");
9174026c
LT
263 }
264 return 0;
265}
266
eeb79916 267static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
9174026c 268{
9174026c
LT
269 void *tree1, *tree2;
270 unsigned long size1, size2;
73134b6d 271 int retval;
9174026c 272
40469ee9 273 tree1 = read_object_with_reference(old, "tree", &size1, 0);
c1fdf2a6 274 if (!tree1)
2de381f9 275 die("unable to read source tree (%s)", sha1_to_hex(old));
40469ee9 276 tree2 = read_object_with_reference(new, "tree", &size2, 0);
c1fdf2a6 277 if (!tree2)
2de381f9 278 die("unable to read destination tree (%s)", sha1_to_hex(new));
eeb79916 279 retval = diff_tree(tree1, size1, tree2, size2, base);
73134b6d
LT
280 free(tree1);
281 free(tree2);
282 return retval;
283}
284
5c97558c
JH
285static int diff_tree_sha1_top(const unsigned char *old,
286 const unsigned char *new, const char *base)
287{
288 int ret;
289 if (generate_patch)
290 diff_setup(detect_rename, 0, 0, 0, 0);
291 ret = diff_tree_sha1(old, new, base);
292 if (generate_patch)
293 diff_flush();
294 return ret;
295}
296
cee99d22
LT
297static int get_one_line(const char *msg, unsigned long len)
298{
299 int ret = 0;
300
301 while (len--) {
302 ret++;
303 if (*msg++ == '\n')
304 break;
305 }
306 return ret;
307}
308
a02ebff6
LT
309static int add_author_info(char *buf, const char *line, int len)
310{
311 char *date;
312 unsigned int namelen;
313 unsigned long time;
314 int tz;
315
316 line += strlen("author ");
317 date = strchr(line, '>');
318 if (!date)
319 return 0;
320 namelen = ++date - line;
321 time = strtoul(date, &date, 10);
322 tz = strtol(date, NULL, 10);
323
324 return sprintf(buf, "Author: %.*s\nDate: %s\n",
325 namelen, line,
326 show_date(time, tz));
327}
328
cee99d22
LT
329static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
330{
331 static char this_header[1000];
332 int offset;
333
334 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
335 if (verbose_header) {
336 int hdr = 1;
337
338 for (;;) {
339 const char *line = msg;
340 int linelen = get_one_line(msg, len);
341
342 if (!linelen)
343 break;
344 if (offset + linelen + 10 > sizeof(this_header))
345 break;
346
347 msg += linelen;
348 len -= linelen;
349 if (linelen == 1)
350 hdr = 0;
a02ebff6
LT
351 if (hdr) {
352 if (!memcmp(line, "author ", 7))
353 offset += add_author_info(this_header + offset, line, linelen);
cee99d22 354 continue;
a02ebff6 355 }
cee99d22
LT
356 memset(this_header + offset, ' ', 4);
357 memcpy(this_header + offset + 4, line, linelen);
358 offset += linelen + 4;
359 }
360 this_header[offset++] = '\n';
361 this_header[offset] = 0;
362 }
363
364 return this_header;
365}
366
b11645be 367static int diff_tree_commit(const unsigned char *commit, const char *name)
e0965d83 368{
e0965d83 369 unsigned long size, offset;
b11645be 370 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
e0965d83 371
e0965d83
LT
372 if (!buf)
373 return -1;
374
375 /* More than one parent? */
376 if (ignore_merges) {
377 if (!memcmp(buf + 46 + 48, "parent ", 7))
378 return 0;
379 }
380
73848892
LT
381 if (!name) {
382 static char commit_name[60];
383 strcpy(commit_name, sha1_to_hex(commit));
384 name = commit_name;
385 }
386
e0965d83
LT
387 offset = 46;
388 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
b11645be 389 unsigned char parent[20];
e0965d83
LT
390 if (get_sha1_hex(buf + offset + 7, parent))
391 return -1;
b11645be 392 header = generate_header(name, sha1_to_hex(parent), buf, size);
5c97558c 393 diff_tree_sha1_top(parent, commit, "");
cee99d22
LT
394 if (!header && verbose_header)
395 header_prefix = "\ndiff-tree ";
e0965d83
LT
396 offset += 48;
397 }
b11645be
LT
398 return 0;
399}
400
401static int diff_tree_stdin(char *line)
402{
403 int len = strlen(line);
404 unsigned char commit[20], parent[20];
405 static char this_header[1000];
406
407 if (!len || line[len-1] != '\n')
408 return -1;
409 line[len-1] = 0;
410 if (get_sha1_hex(line, commit))
411 return -1;
412 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
413 line[40] = 0;
414 line[81] = 0;
415 sprintf(this_header, "%s (from %s)\n", line, line+41);
416 header = this_header;
5c97558c 417 return diff_tree_sha1_top(parent, commit, "");
b11645be
LT
418 }
419 line[40] = 0;
420 return diff_tree_commit(commit, line);
e0965d83
LT
421}
422
5aad72f2 423static char *diff_tree_usage =
667bb59b 424"git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-m] [-s] [-v] <tree-ish> <tree-ish>";
c5bac17a 425
73134b6d
LT
426int main(int argc, char **argv)
427{
0a8365a1 428 int nr_sha1;
e0965d83 429 char line[1000];
0a8365a1 430 unsigned char sha1[2][20];
73134b6d 431
0a8365a1 432 nr_sha1 = 0;
c5b42386 433 for (;;) {
e0965d83 434 char *arg;
c5b42386 435
e0965d83
LT
436 argv++;
437 argc--;
438 arg = *argv;
0a8365a1 439 if (!arg)
c5b42386
LT
440 break;
441
0a8365a1
LT
442 if (*arg != '-') {
443 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
444 nr_sha1++;
445 continue;
446 }
447 break;
448 }
449
450 if (!strcmp(arg, "--")) {
e0965d83
LT
451 argv++;
452 argc--;
453 break;
454 }
bf16c71e 455 if (!strcmp(arg, "-r")) {
73134b6d
LT
456 recursive = 1;
457 continue;
458 }
3ebfd4aa 459 if (!strcmp(arg, "-p")) {
3a663fd9 460 recursive = generate_patch = 1;
3ebfd4aa
JH
461 continue;
462 }
5c97558c
JH
463 if (!strcmp(arg, "-M")) {
464 detect_rename = recursive = generate_patch = 1;
465 continue;
466 }
6cbd72f8
LT
467 if (!strcmp(arg, "-z")) {
468 line_termination = '\0';
469 continue;
470 }
e0965d83
LT
471 if (!strcmp(arg, "-m")) {
472 ignore_merges = 0;
473 continue;
474 }
f4f21ce3
LT
475 if (!strcmp(arg, "-s")) {
476 silent = 1;
477 continue;
478 }
cee99d22
LT
479 if (!strcmp(arg, "-v")) {
480 verbose_header = 1;
481 header_prefix = "diff-tree ";
482 continue;
483 }
e0965d83
LT
484 if (!strcmp(arg, "--stdin")) {
485 read_stdin = 1;
486 continue;
487 }
c5bac17a 488 usage(diff_tree_usage);
73134b6d
LT
489 }
490
e0965d83 491 if (argc > 0) {
c5b42386
LT
492 int i;
493
e0965d83
LT
494 paths = argv;
495 nr_paths = argc;
812666c8 496 pathlens = xmalloc(nr_paths * sizeof(int));
c5b42386
LT
497 for (i=0; i<nr_paths; i++)
498 pathlens[i] = strlen(paths[i]);
499 }
500
0a8365a1
LT
501 switch (nr_sha1) {
502 case 0:
503 if (!read_stdin)
504 usage(diff_tree_usage);
505 break;
506 case 1:
73848892 507 diff_tree_commit(sha1[0], NULL);
0a8365a1
LT
508 break;
509 case 2:
5c97558c 510 diff_tree_sha1_top(sha1[0], sha1[1], "");
0a8365a1
LT
511 break;
512 }
513
e0965d83 514 if (!read_stdin)
0a8365a1 515 return 0;
e0965d83
LT
516
517 while (fgets(line, sizeof(line), stdin))
518 diff_tree_stdin(line);
519
520 return 0;
9174026c 521}