Code

[PATCH] fix usage string for renamed git commands
[git.git] / diff-tree.c
1 #include "cache.h"
2 #include "diff.h"
4 static int recursive = 0;
5 static int line_termination = '\n';
6 static int generate_patch = 0;
8 // What paths are we interested in?
9 static int nr_paths = 0;
10 static char **paths = NULL;
11 static int *pathlens = NULL;
13 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
15 static void update_tree_entry(void **bufp, unsigned long *sizep)
16 {
17         void *buf = *bufp;
18         unsigned long size = *sizep;
19         int len = strlen(buf) + 1 + 20;
21         if (size < len)
22                 die("corrupt tree file");
23         *bufp = buf + len;
24         *sizep = size - len;
25 }
27 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
28 {
29         int len = strlen(tree)+1;
30         const unsigned char *sha1 = tree + len;
31         const char *path = strchr(tree, ' ');
33         if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
34                 die("corrupt tree file");
35         *pathp = path+1;
36         return sha1;
37 }
39 static char *malloc_base(const char *base, const char *path, int pathlen)
40 {
41         int baselen = strlen(base);
42         char *newbase = xmalloc(baselen + pathlen + 2);
43         memcpy(newbase, base, baselen);
44         memcpy(newbase + baselen, path, pathlen);
45         memcpy(newbase + baselen + pathlen, "/", 2);
46         return newbase;
47 }
49 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
51 /* A whole sub-tree went away or appeared */
52 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
53 {
54         while (size) {
55                 show_file(prefix, tree, size, base);
56                 update_tree_entry(&tree, &size);
57         }
58 }
60 /* A file entry went away or appeared */
61 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
62 {
63         unsigned mode;
64         const char *path;
65         const unsigned char *sha1 = extract(tree, size, &path, &mode);
67         if (recursive && S_ISDIR(mode)) {
68                 char type[20];
69                 unsigned long size;
70                 char *newbase = malloc_base(base, path, strlen(path));
71                 void *tree;
73                 tree = read_sha1_file(sha1, type, &size);
74                 if (!tree || strcmp(type, "tree"))
75                         die("corrupt tree sha %s", sha1_to_hex(sha1));
77                 show_tree(prefix, tree, size, newbase);
78                 
79                 free(tree);
80                 free(newbase);
81                 return;
82         }
84         if (generate_patch) {
85                 if (!S_ISDIR(mode))
86                         diff_addremove(prefix[0], mode, sha1, base, path);
87         }
88         else
89                 printf("%s%06o\t%s\t%s\t%s%s%c", prefix, mode,
90                        S_ISDIR(mode) ? "tree" : "blob",
91                        sha1_to_hex(sha1), base, path,
92                        line_termination);
93 }
95 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
96 {
97         unsigned mode1, mode2;
98         const char *path1, *path2;
99         const unsigned char *sha1, *sha2;
100         int cmp, pathlen1, pathlen2;
101         char old_sha1_hex[50];
103         sha1 = extract(tree1, size1, &path1, &mode1);
104         sha2 = extract(tree2, size2, &path2, &mode2);
106         pathlen1 = strlen(path1);
107         pathlen2 = strlen(path2);
108         cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
109         if (cmp < 0) {
110                 show_file("-", tree1, size1, base);
111                 return -1;
112         }
113         if (cmp > 0) {
114                 show_file("+", tree2, size2, base);
115                 return 1;
116         }
117         if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
118                 return 0;
120         /*
121          * If the filemode has changed to/from a directory from/to a regular
122          * file, we need to consider it a remove and an add.
123          */
124         if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
125                 show_file("-", tree1, size1, base);
126                 show_file("+", tree2, size2, base);
127                 return 0;
128         }
130         if (recursive && S_ISDIR(mode1)) {
131                 int retval;
132                 char *newbase = malloc_base(base, path1, pathlen1);
133                 retval = diff_tree_sha1(sha1, sha2, newbase);
134                 free(newbase);
135                 return retval;
136         }
138         if (generate_patch) {
139                 if (!S_ISDIR(mode1))
140                         diff_change(mode1, mode2, sha1, sha2, base, path1);
141         }
142         else {
143                 strcpy(old_sha1_hex, sha1_to_hex(sha1));
144                 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
145                        S_ISDIR(mode1) ? "tree" : "blob",
146                        old_sha1_hex, sha1_to_hex(sha2), base, path1,
147                        line_termination);
148         }
149         return 0;
152 static int interesting(void *tree, unsigned long size, const char *base)
154         const char *path;
155         unsigned mode;
156         int i;
157         int baselen, pathlen;
159         if (!nr_paths)
160                 return 1;
162         (void)extract(tree, size, &path, &mode);
164         pathlen = strlen(path);
165         baselen = strlen(base);
167         for (i=0; i < nr_paths; i++) {
168                 const char *match = paths[i];
169                 int matchlen = pathlens[i];
171                 if (baselen >= matchlen) {
172                         /* If it doesn't match, move along... */
173                         if (strncmp(base, match, matchlen))
174                                 continue;
176                         /* The base is a subdirectory of a path which was specified. */
177                         return 1;
178                 }
180                 /* Does the base match? */
181                 if (strncmp(base, match, baselen))
182                         continue;
184                 match += baselen;
185                 matchlen -= baselen;
187                 if (pathlen > matchlen)
188                         continue;
190                 if (strncmp(path, match, pathlen))
191                         continue;
193                 return 1;
194         }
195         return 0; /* No matches */
198 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
200         while (size1 | size2) {
201                 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
202                         update_tree_entry(&tree1, &size1);
203                         continue;
204                 }
205                 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
206                         update_tree_entry(&tree2, &size2);
207                         continue;
208                 }
209                 if (!size1) {
210                         show_file("+", tree2, size2, base);
211                         update_tree_entry(&tree2, &size2);
212                         continue;
213                 }
214                 if (!size2) {
215                         show_file("-", tree1, size1, base);
216                         update_tree_entry(&tree1, &size1);
217                         continue;
218                 }
219                 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
220                 case -1:
221                         update_tree_entry(&tree1, &size1);
222                         continue;
223                 case 0:
224                         update_tree_entry(&tree1, &size1);
225                         /* Fallthrough */
226                 case 1:
227                         update_tree_entry(&tree2, &size2);
228                         continue;
229                 }
230                 die("diff-tree: internal error");
231         }
232         return 0;
235 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
237         void *tree1, *tree2;
238         unsigned long size1, size2;
239         int retval;
241         tree1 = read_object_with_reference(old, "tree", &size1, 0);
242         if (!tree1)
243                 die("unable to read source tree (%s)", sha1_to_hex(old));
244         tree2 = read_object_with_reference(new, "tree", &size2, 0);
245         if (!tree2)
246                 die("unable to read destination tree (%s)", sha1_to_hex(new));
247         retval = diff_tree(tree1, size1, tree2, size2, base);
248         free(tree1);
249         free(tree2);
250         return retval;
253 static char *diff_tree_usage = "diff-tree [-r] [-z] <tree sha1> <tree sha1>";
255 int main(int argc, char **argv)
257         unsigned char old[20], new[20];
259         for (;;) {
260                 char *arg = argv[1];
262                 if (!arg || *arg != '-')
263                         break;
265                 argv++;
266                 argc--;
267                 if (!strcmp(arg, "-r")) {
268                         recursive = 1;
269                         continue;
270                 }
271                 if (!strcmp(arg, "-p")) {
272                         recursive = generate_patch = 1;
273                         continue;
274                 }
275                 if (!strcmp(arg, "-z")) {
276                         line_termination = '\0';
277                         continue;
278                 }
279                 usage(diff_tree_usage);
280         }
282         if (argc < 3 || get_sha1_hex(argv[1], old) || get_sha1_hex(argv[2], new))
283                 usage(diff_tree_usage);
285         if (argc > 3) {
286                 int i;
288                 paths = &argv[3];
289                 nr_paths = argc - 3;
290                 pathlens = xmalloc(nr_paths * sizeof(int));
291                 for (i=0; i<nr_paths; i++)
292                         pathlens[i] = strlen(paths[i]);
293         }
295         return diff_tree_sha1(old, new, "");