]>
git.ipfire.org Git - thirdparty/git.git/blob - diff-tree.c
6 static int show_root_diff
= 0;
7 static int verbose_header
= 0;
8 static int ignore_merges
= 1;
9 static int recursive
= 0;
10 static int read_stdin
= 0;
11 static int line_termination
= '\n';
12 static int generate_patch
= 0;
13 static int detect_rename
= 0;
14 static const char *header
= NULL
;
15 static const char *header_prefix
= "";
17 // What paths are we interested in?
18 static int nr_paths
= 0;
19 static char **paths
= NULL
;
20 static int *pathlens
= NULL
;
22 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
);
24 static void update_tree_entry(void **bufp
, unsigned long *sizep
)
27 unsigned long size
= *sizep
;
28 int len
= strlen(buf
) + 1 + 20;
31 die("corrupt tree file");
36 static const unsigned char *extract(void *tree
, unsigned long size
, const char **pathp
, unsigned int *modep
)
38 int len
= strlen(tree
)+1;
39 const unsigned char *sha1
= tree
+ len
;
40 const char *path
= strchr(tree
, ' ');
42 if (!path
|| size
< len
+ 20 || sscanf(tree
, "%o", modep
) != 1)
43 die("corrupt tree file");
48 static char *malloc_base(const char *base
, const char *path
, int pathlen
)
50 int baselen
= strlen(base
);
51 char *newbase
= xmalloc(baselen
+ pathlen
+ 2);
52 memcpy(newbase
, base
, baselen
);
53 memcpy(newbase
+ baselen
, path
, pathlen
);
54 memcpy(newbase
+ baselen
+ pathlen
, "/", 2);
58 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
59 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
61 /* A file entry went away or appeared */
62 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
66 const unsigned char *sha1
= extract(tree
, size
, &path
, &mode
);
76 if (recursive
&& S_ISDIR(mode
)) {
79 char *newbase
= malloc_base(base
, path
, strlen(path
));
82 tree
= read_sha1_file(sha1
, type
, &size
);
83 if (!tree
|| strcmp(type
, "tree"))
84 die("corrupt tree sha %s", sha1_to_hex(sha1
));
86 show_tree(prefix
, tree
, size
, newbase
);
95 diff_addremove(prefix
[0], mode
, sha1
, base
, path
);
98 printf("%s%06o\t%s\t%s\t%s%s%c", prefix
, mode
,
99 S_ISDIR(mode
) ? "tree" : "blob",
100 sha1_to_hex(sha1
), base
, path
,
104 static int compare_tree_entry(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
106 unsigned mode1
, mode2
;
107 const char *path1
, *path2
;
108 const unsigned char *sha1
, *sha2
;
109 int cmp
, pathlen1
, pathlen2
;
110 char old_sha1_hex
[50];
112 sha1
= extract(tree1
, size1
, &path1
, &mode1
);
113 sha2
= extract(tree2
, size2
, &path2
, &mode2
);
115 pathlen1
= strlen(path1
);
116 pathlen2
= strlen(path2
);
117 cmp
= cache_name_compare(path1
, pathlen1
, path2
, pathlen2
);
119 show_file("-", tree1
, size1
, base
);
123 show_file("+", tree2
, size2
, base
);
126 if (!memcmp(sha1
, sha2
, 20) && mode1
== mode2
)
130 * If the filemode has changed to/from a directory from/to a regular
131 * file, we need to consider it a remove and an add.
133 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
134 show_file("-", tree1
, size1
, base
);
135 show_file("+", tree2
, size2
, base
);
139 if (recursive
&& S_ISDIR(mode1
)) {
141 char *newbase
= malloc_base(base
, path1
, pathlen1
);
142 retval
= diff_tree_sha1(sha1
, sha2
, newbase
);
148 printf("%s", header
);
154 if (generate_patch
) {
156 diff_change(mode1
, mode2
, sha1
, sha2
, base
, path1
);
159 strcpy(old_sha1_hex
, sha1_to_hex(sha1
));
160 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1
, mode2
,
161 S_ISDIR(mode1
) ? "tree" : "blob",
162 old_sha1_hex
, sha1_to_hex(sha2
), base
, path1
,
168 static int interesting(void *tree
, unsigned long size
, const char *base
)
173 int baselen
, pathlen
;
178 (void)extract(tree
, size
, &path
, &mode
);
180 pathlen
= strlen(path
);
181 baselen
= strlen(base
);
183 for (i
=0; i
< nr_paths
; i
++) {
184 const char *match
= paths
[i
];
185 int matchlen
= pathlens
[i
];
187 if (baselen
>= matchlen
) {
188 /* If it doesn't match, move along... */
189 if (strncmp(base
, match
, matchlen
))
192 /* The base is a subdirectory of a path which was specified. */
196 /* Does the base match? */
197 if (strncmp(base
, match
, baselen
))
203 if (pathlen
> matchlen
)
206 if (matchlen
> pathlen
) {
207 if (match
[pathlen
] != '/')
213 if (strncmp(path
, match
, pathlen
))
218 return 0; /* No matches */
221 /* A whole sub-tree went away or appeared */
222 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
225 if (interesting(tree
, size
, base
))
226 show_file(prefix
, tree
, size
, base
);
227 update_tree_entry(&tree
, &size
);
231 static int diff_tree(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
233 while (size1
| size2
) {
234 if (nr_paths
&& size1
&& !interesting(tree1
, size1
, base
)) {
235 update_tree_entry(&tree1
, &size1
);
238 if (nr_paths
&& size2
&& !interesting(tree2
, size2
, base
)) {
239 update_tree_entry(&tree2
, &size2
);
243 show_file("+", tree2
, size2
, base
);
244 update_tree_entry(&tree2
, &size2
);
248 show_file("-", tree1
, size1
, base
);
249 update_tree_entry(&tree1
, &size1
);
252 switch (compare_tree_entry(tree1
, size1
, tree2
, size2
, base
)) {
254 update_tree_entry(&tree1
, &size1
);
257 update_tree_entry(&tree1
, &size1
);
260 update_tree_entry(&tree2
, &size2
);
263 die("git-diff-tree: internal error");
268 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
)
271 unsigned long size1
, size2
;
274 tree1
= read_object_with_reference(old
, "tree", &size1
, 0);
276 die("unable to read source tree (%s)", sha1_to_hex(old
));
277 tree2
= read_object_with_reference(new, "tree", &size2
, 0);
279 die("unable to read destination tree (%s)", sha1_to_hex(new));
280 retval
= diff_tree(tree1
, size1
, tree2
, size2
, base
);
286 static int diff_tree_sha1_top(const unsigned char *old
,
287 const unsigned char *new, const char *base
)
291 diff_setup(detect_rename
, 0, 0, 0, 0);
292 ret
= diff_tree_sha1(old
, new, base
);
298 static int diff_root_tree(const unsigned char *new, const char *base
)
305 diff_setup(detect_rename
, 0, 0, 0, 0);
306 tree
= read_object_with_reference(new, "tree", &size
, 0);
308 die("unable to read root tree (%s)", sha1_to_hex(new));
309 retval
= diff_tree("", 0, tree
, size
, base
);
316 static int get_one_line(const char *msg
, unsigned long len
)
328 static int add_author_info(char *buf
, const char *line
, int len
)
331 unsigned int namelen
;
335 line
+= strlen("author ");
336 date
= strchr(line
, '>');
339 namelen
= ++date
- line
;
340 time
= strtoul(date
, &date
, 10);
341 tz
= strtol(date
, NULL
, 10);
343 return sprintf(buf
, "Author: %.*s\nDate: %s\n",
345 show_date(time
, tz
));
348 static char *generate_header(const char *commit
, const char *parent
, const char *msg
, unsigned long len
)
350 static char this_header
[1000];
353 offset
= sprintf(this_header
, "%s%s (from %s)\n", header_prefix
, commit
, parent
);
354 if (verbose_header
) {
358 const char *line
= msg
;
359 int linelen
= get_one_line(msg
, len
);
363 if (offset
+ linelen
+ 10 > sizeof(this_header
))
371 if (!memcmp(line
, "author ", 7))
372 offset
+= add_author_info(this_header
+ offset
, line
, linelen
);
375 memset(this_header
+ offset
, ' ', 4);
376 memcpy(this_header
+ offset
+ 4, line
, linelen
);
377 offset
+= linelen
+ 4;
379 this_header
[offset
++] = '\n';
380 this_header
[offset
] = 0;
386 static int diff_tree_commit(const unsigned char *commit
, const char *name
)
388 unsigned long size
, offset
;
389 char *buf
= read_object_with_reference(commit
, "commit", &size
, NULL
);
395 static char commit_name
[60];
396 strcpy(commit_name
, sha1_to_hex(commit
));
401 if (show_root_diff
&& memcmp(buf
+ 46, "parent ", 7)) {
402 header
= generate_header(name
, "root", buf
, size
);
403 diff_root_tree(commit
, "");
406 /* More than one parent? */
408 if (!memcmp(buf
+ 46 + 48, "parent ", 7))
413 while (offset
+ 48 < size
&& !memcmp(buf
+ offset
, "parent ", 7)) {
414 unsigned char parent
[20];
415 if (get_sha1_hex(buf
+ offset
+ 7, parent
))
417 header
= generate_header(name
, sha1_to_hex(parent
), buf
, size
);
418 diff_tree_sha1_top(parent
, commit
, "");
419 if (!header
&& verbose_header
)
420 header_prefix
= "\ndiff-tree ";
426 static int diff_tree_stdin(char *line
)
428 int len
= strlen(line
);
429 unsigned char commit
[20], parent
[20];
430 static char this_header
[1000];
432 if (!len
|| line
[len
-1] != '\n')
435 if (get_sha1_hex(line
, commit
))
437 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
440 sprintf(this_header
, "%s (from %s)\n", line
, line
+41);
441 header
= this_header
;
442 return diff_tree_sha1_top(parent
, commit
, "");
445 return diff_tree_commit(commit
, line
);
448 static char *diff_tree_usage
=
449 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-m] [-s] [-v] <tree-ish> <tree-ish>";
451 int main(int argc
, char **argv
)
455 unsigned char sha1
[2][20];
468 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
475 if (!strcmp(arg
, "--")) {
480 if (!strcmp(arg
, "-r")) {
484 if (!strcmp(arg
, "-p")) {
485 recursive
= generate_patch
= 1;
488 if (!strcmp(arg
, "-M")) {
489 detect_rename
= recursive
= generate_patch
= 1;
492 if (!strcmp(arg
, "-z")) {
493 line_termination
= '\0';
496 if (!strcmp(arg
, "-m")) {
500 if (!strcmp(arg
, "-s")) {
504 if (!strcmp(arg
, "-v")) {
506 header_prefix
= "diff-tree ";
509 if (!strcmp(arg
, "--stdin")) {
513 if (!strcmp(arg
, "--root")) {
517 usage(diff_tree_usage
);
525 pathlens
= xmalloc(nr_paths
* sizeof(int));
526 for (i
=0; i
<nr_paths
; i
++)
527 pathlens
[i
] = strlen(paths
[i
]);
533 usage(diff_tree_usage
);
536 diff_tree_commit(sha1
[0], NULL
);
539 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
546 while (fgets(line
, sizeof(line
), stdin
))
547 diff_tree_stdin(line
);