[PATCH] Deltification library work by Nicolas Pitre.
[git/mingw.git] / diff-tree.c
blob59d12f2ba1325bad77dd062936c20cd9cc4d602f
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
5 static int silent = 0;
6 static int verbose_header = 0;
7 static int ignore_merges = 1;
8 static int recursive = 0;
9 static int read_stdin = 0;
10 static int line_termination = '\n';
11 static int generate_patch = 0;
12 static const char *header = NULL;
13 static const char *header_prefix = "";
15 // What paths are we interested in?
16 static int nr_paths = 0;
17 static char **paths = NULL;
18 static int *pathlens = NULL;
20 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
22 static void update_tree_entry(void **bufp, unsigned long *sizep)
24 void *buf = *bufp;
25 unsigned long size = *sizep;
26 int len = strlen(buf) + 1 + 20;
28 if (size < len)
29 die("corrupt tree file");
30 *bufp = buf + len;
31 *sizep = size - len;
34 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
36 int len = strlen(tree)+1;
37 const unsigned char *sha1 = tree + len;
38 const char *path = strchr(tree, ' ');
40 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
41 die("corrupt tree file");
42 *pathp = path+1;
43 return sha1;
46 static char *malloc_base(const char *base, const char *path, int pathlen)
48 int baselen = strlen(base);
49 char *newbase = xmalloc(baselen + pathlen + 2);
50 memcpy(newbase, base, baselen);
51 memcpy(newbase + baselen, path, pathlen);
52 memcpy(newbase + baselen + pathlen, "/", 2);
53 return newbase;
56 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
57 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
59 /* A file entry went away or appeared */
60 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
62 unsigned mode;
63 const char *path;
64 const unsigned char *sha1 = extract(tree, size, &path, &mode);
66 if (header) {
67 printf("%s", header);
68 header = NULL;
71 if (silent)
72 return;
74 if (recursive && S_ISDIR(mode)) {
75 char type[20];
76 unsigned long size;
77 char *newbase = malloc_base(base, path, strlen(path));
78 void *tree;
80 tree = read_sha1_file(sha1, type, &size);
81 if (!tree || strcmp(type, "tree"))
82 die("corrupt tree sha %s", sha1_to_hex(sha1));
84 show_tree(prefix, tree, size, newbase);
86 free(tree);
87 free(newbase);
88 return;
91 if (generate_patch) {
92 if (!S_ISDIR(mode))
93 diff_addremove(prefix[0], mode, sha1, base, path);
95 else
96 printf("%s%06o\t%s\t%s\t%s%s%c", prefix, mode,
97 S_ISDIR(mode) ? "tree" : "blob",
98 sha1_to_hex(sha1), base, path,
99 line_termination);
102 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
104 unsigned mode1, mode2;
105 const char *path1, *path2;
106 const unsigned char *sha1, *sha2;
107 int cmp, pathlen1, pathlen2;
108 char old_sha1_hex[50];
110 sha1 = extract(tree1, size1, &path1, &mode1);
111 sha2 = extract(tree2, size2, &path2, &mode2);
113 pathlen1 = strlen(path1);
114 pathlen2 = strlen(path2);
115 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
116 if (cmp < 0) {
117 show_file("-", tree1, size1, base);
118 return -1;
120 if (cmp > 0) {
121 show_file("+", tree2, size2, base);
122 return 1;
124 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
125 return 0;
128 * If the filemode has changed to/from a directory from/to a regular
129 * file, we need to consider it a remove and an add.
131 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
132 show_file("-", tree1, size1, base);
133 show_file("+", tree2, size2, base);
134 return 0;
137 if (recursive && S_ISDIR(mode1)) {
138 int retval;
139 char *newbase = malloc_base(base, path1, pathlen1);
140 retval = diff_tree_sha1(sha1, sha2, newbase);
141 free(newbase);
142 return retval;
145 if (header) {
146 printf("%s", header);
147 header = NULL;
149 if (silent)
150 return 0;
152 if (generate_patch) {
153 if (!S_ISDIR(mode1))
154 diff_change(mode1, mode2, sha1, sha2, base, path1);
156 else {
157 strcpy(old_sha1_hex, sha1_to_hex(sha1));
158 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
159 S_ISDIR(mode1) ? "tree" : "blob",
160 old_sha1_hex, sha1_to_hex(sha2), base, path1,
161 line_termination);
163 return 0;
166 static int interesting(void *tree, unsigned long size, const char *base)
168 const char *path;
169 unsigned mode;
170 int i;
171 int baselen, pathlen;
173 if (!nr_paths)
174 return 1;
176 (void)extract(tree, size, &path, &mode);
178 pathlen = strlen(path);
179 baselen = strlen(base);
181 for (i=0; i < nr_paths; i++) {
182 const char *match = paths[i];
183 int matchlen = pathlens[i];
185 if (baselen >= matchlen) {
186 /* If it doesn't match, move along... */
187 if (strncmp(base, match, matchlen))
188 continue;
190 /* The base is a subdirectory of a path which was specified. */
191 return 1;
194 /* Does the base match? */
195 if (strncmp(base, match, baselen))
196 continue;
198 match += baselen;
199 matchlen -= baselen;
201 if (pathlen > matchlen)
202 continue;
204 if (matchlen > pathlen) {
205 if (match[pathlen] != '/')
206 continue;
207 if (!S_ISDIR(mode))
208 continue;
211 if (strncmp(path, match, pathlen))
212 continue;
214 return 1;
216 return 0; /* No matches */
219 /* A whole sub-tree went away or appeared */
220 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
222 while (size) {
223 if (interesting(tree, size, base))
224 show_file(prefix, tree, size, base);
225 update_tree_entry(&tree, &size);
229 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
231 while (size1 | size2) {
232 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
233 update_tree_entry(&tree1, &size1);
234 continue;
236 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
237 update_tree_entry(&tree2, &size2);
238 continue;
240 if (!size1) {
241 show_file("+", tree2, size2, base);
242 update_tree_entry(&tree2, &size2);
243 continue;
245 if (!size2) {
246 show_file("-", tree1, size1, base);
247 update_tree_entry(&tree1, &size1);
248 continue;
250 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
251 case -1:
252 update_tree_entry(&tree1, &size1);
253 continue;
254 case 0:
255 update_tree_entry(&tree1, &size1);
256 /* Fallthrough */
257 case 1:
258 update_tree_entry(&tree2, &size2);
259 continue;
261 die("diff-tree: internal error");
263 return 0;
266 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
268 void *tree1, *tree2;
269 unsigned long size1, size2;
270 int retval;
272 tree1 = read_object_with_reference(old, "tree", &size1, 0);
273 if (!tree1)
274 die("unable to read source tree (%s)", sha1_to_hex(old));
275 tree2 = read_object_with_reference(new, "tree", &size2, 0);
276 if (!tree2)
277 die("unable to read destination tree (%s)", sha1_to_hex(new));
278 retval = diff_tree(tree1, size1, tree2, size2, base);
279 free(tree1);
280 free(tree2);
281 return retval;
284 static int get_one_line(const char *msg, unsigned long len)
286 int ret = 0;
288 while (len--) {
289 ret++;
290 if (*msg++ == '\n')
291 break;
293 return ret;
296 static int add_author_info(char *buf, const char *line, int len)
298 char *date;
299 unsigned int namelen;
300 unsigned long time;
301 int tz;
303 line += strlen("author ");
304 date = strchr(line, '>');
305 if (!date)
306 return 0;
307 namelen = ++date - line;
308 time = strtoul(date, &date, 10);
309 tz = strtol(date, NULL, 10);
311 return sprintf(buf, "Author: %.*s\nDate: %s\n",
312 namelen, line,
313 show_date(time, tz));
316 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
318 static char this_header[1000];
319 int offset;
321 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
322 if (verbose_header) {
323 int hdr = 1;
325 for (;;) {
326 const char *line = msg;
327 int linelen = get_one_line(msg, len);
329 if (!linelen)
330 break;
331 if (offset + linelen + 10 > sizeof(this_header))
332 break;
334 msg += linelen;
335 len -= linelen;
336 if (linelen == 1)
337 hdr = 0;
338 if (hdr) {
339 if (!memcmp(line, "author ", 7))
340 offset += add_author_info(this_header + offset, line, linelen);
341 continue;
343 memset(this_header + offset, ' ', 4);
344 memcpy(this_header + offset + 4, line, linelen);
345 offset += linelen + 4;
347 this_header[offset++] = '\n';
348 this_header[offset] = 0;
351 return this_header;
354 static int diff_tree_commit(const unsigned char *commit, const char *name)
356 unsigned long size, offset;
357 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
359 if (!buf)
360 return -1;
362 /* More than one parent? */
363 if (ignore_merges) {
364 if (!memcmp(buf + 46 + 48, "parent ", 7))
365 return 0;
368 if (!name) {
369 static char commit_name[60];
370 strcpy(commit_name, sha1_to_hex(commit));
371 name = commit_name;
374 offset = 46;
375 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
376 unsigned char parent[20];
377 if (get_sha1_hex(buf + offset + 7, parent))
378 return -1;
379 header = generate_header(name, sha1_to_hex(parent), buf, size);
380 diff_tree_sha1(parent, commit, "");
381 if (!header && verbose_header)
382 header_prefix = "\ndiff-tree ";
383 offset += 48;
385 return 0;
388 static int diff_tree_stdin(char *line)
390 int len = strlen(line);
391 unsigned char commit[20], parent[20];
392 static char this_header[1000];
394 if (!len || line[len-1] != '\n')
395 return -1;
396 line[len-1] = 0;
397 if (get_sha1_hex(line, commit))
398 return -1;
399 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
400 line[40] = 0;
401 line[81] = 0;
402 sprintf(this_header, "%s (from %s)\n", line, line+41);
403 header = this_header;
404 return diff_tree_sha1(parent, commit, "");
406 line[40] = 0;
407 return diff_tree_commit(commit, line);
410 static char *diff_tree_usage =
411 "diff-tree [-p] [-r] [-z] [--stdin] [-m] [-s] [-v] <tree sha1> <tree sha1>";
413 int main(int argc, char **argv)
415 int nr_sha1;
416 char line[1000];
417 unsigned char sha1[2][20];
419 nr_sha1 = 0;
420 for (;;) {
421 char *arg;
423 argv++;
424 argc--;
425 arg = *argv;
426 if (!arg)
427 break;
429 if (*arg != '-') {
430 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
431 nr_sha1++;
432 continue;
434 break;
437 if (!strcmp(arg, "--")) {
438 argv++;
439 argc--;
440 break;
442 if (!strcmp(arg, "-r")) {
443 recursive = 1;
444 continue;
446 if (!strcmp(arg, "-p")) {
447 recursive = generate_patch = 1;
448 continue;
450 if (!strcmp(arg, "-z")) {
451 line_termination = '\0';
452 continue;
454 if (!strcmp(arg, "-m")) {
455 ignore_merges = 0;
456 continue;
458 if (!strcmp(arg, "-s")) {
459 silent = 1;
460 continue;
462 if (!strcmp(arg, "-v")) {
463 verbose_header = 1;
464 header_prefix = "diff-tree ";
465 continue;
467 if (!strcmp(arg, "--stdin")) {
468 read_stdin = 1;
469 continue;
471 usage(diff_tree_usage);
474 if (argc > 0) {
475 int i;
477 paths = argv;
478 nr_paths = argc;
479 pathlens = xmalloc(nr_paths * sizeof(int));
480 for (i=0; i<nr_paths; i++)
481 pathlens[i] = strlen(paths[i]);
484 switch (nr_sha1) {
485 case 0:
486 if (!read_stdin)
487 usage(diff_tree_usage);
488 break;
489 case 1:
490 diff_tree_commit(sha1[0], NULL);
491 break;
492 case 2:
493 diff_tree_sha1(sha1[0], sha1[1], "");
494 break;
497 if (!read_stdin)
498 return 0;
500 while (fgets(line, sizeof(line), stdin))
501 diff_tree_stdin(line);
503 return 0;