@matheusbsbs / matheus-bagliano / commits / ac1b3d1248
Fork 0

Split up tree diff functions into tree-diff.c library

This makes the tree diff functionality independent of the "git-diff-tree" program, by splitting the core functionality up into a library file. This will be needed for when we teach git-rev-list to only follow a specified set of pathnames, rather than the global revision history. Most of it is a fairly straightforward code move, but it also involves some calling convention cleanup, and moving some of the static variables from diff-tree.c into the options structure. The actual tree change callback routines also become paramterized by the diff_options structure, allowing the library functionality to do something else than just show the diff on stdout. Right now the only user of this functionality remains git-diff-tree itself. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Oct 20, 2005 at 21:05 UTC ac1b3d1248f36b26c2eab55022b9a54bde36b1ee
5 files changed +317 -269
Makefile
+1 -1
@@ -151,7 +151,7 @@ LIB_H = \
151
152 DIFF_OBJS = \
153 diff.o diffcore-break.o diffcore-order.o diffcore-pathspec.o \
154 - diffcore-pickaxe.o diffcore-rename.o
154 + diffcore-pickaxe.o diffcore-rename.o tree-diff.o
155
156 LIB_OBJS = \
157 blob.o commit.o connect.o count-delta.o csum-file.o \
diff-tree.c
+14 -268
@@ -5,256 +5,13 @@
5 static int show_root_diff = 0;
6 static int verbose_header = 0;
7 static int ignore_merges = 1;
8 -static int recursive = 0;
9 -static int show_tree_entry_in_recursive = 0;
8 static int read_stdin = 0;
9
12 -static struct diff_options diff_options;
13 -
10 static const char *header = NULL;
11 static const char *header_prefix = "";
12 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
13
18 -// What paths are we interested in?
19 -static int nr_paths = 0;
20 -static const char **paths = NULL;
21 -static int *pathlens = NULL;
22 -
23 -static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
24 -
25 -static void update_tree_entry(void **bufp, unsigned long *sizep)
26 -{
27 - void *buf = *bufp;
28 - unsigned long size = *sizep;
29 - int len = strlen(buf) + 1 + 20;
30 -
31 - if (size < len)
32 - die("corrupt tree file");
33 - *bufp = buf + len;
34 - *sizep = size - len;
35 -}
36 -
37 -static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
38 -{
39 - int len = strlen(tree)+1;
40 - const unsigned char *sha1 = tree + len;
41 - const char *path = strchr(tree, ' ');
42 - unsigned int mode;
43 -
44 - if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
45 - die("corrupt tree file");
46 - *pathp = path+1;
47 - *modep = DIFF_FILE_CANON_MODE(mode);
48 - return sha1;
49 -}
50 -
51 -static char *malloc_base(const char *base, const char *path, int pathlen)
52 -{
53 - int baselen = strlen(base);
54 - char *newbase = xmalloc(baselen + pathlen + 2);
55 - memcpy(newbase, base, baselen);
56 - memcpy(newbase + baselen, path, pathlen);
57 - memcpy(newbase + baselen + pathlen, "/", 2);
58 - return newbase;
59 -}
60 -
61 -static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
62 -static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
63 -
64 -/* A file entry went away or appeared */
65 -static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
66 -{
67 - unsigned mode;
68 - const char *path;
69 - const unsigned char *sha1 = extract(tree, size, &path, &mode);
70 -
71 - if (recursive && S_ISDIR(mode)) {
72 - char type[20];
73 - unsigned long size;
74 - char *newbase = malloc_base(base, path, strlen(path));
75 - void *tree;
76 -
77 - tree = read_sha1_file(sha1, type, &size);
78 - if (!tree || strcmp(type, "tree"))
79 - die("corrupt tree sha %s", sha1_to_hex(sha1));
80 -
81 - show_tree(prefix, tree, size, newbase);
82 -
83 - free(tree);
84 - free(newbase);
85 - return;
86 - }
87 -
88 - diff_addremove(&diff_options, prefix[0], mode, sha1, base, path);
89 -}
90 -
91 -static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
92 -{
93 - unsigned mode1, mode2;
94 - const char *path1, *path2;
95 - const unsigned char *sha1, *sha2;
96 - int cmp, pathlen1, pathlen2;
97 -
98 - sha1 = extract(tree1, size1, &path1, &mode1);
99 - sha2 = extract(tree2, size2, &path2, &mode2);
100 -
101 - pathlen1 = strlen(path1);
102 - pathlen2 = strlen(path2);
103 - cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
104 - if (cmp < 0) {
105 - show_file("-", tree1, size1, base);
106 - return -1;
107 - }
108 - if (cmp > 0) {
109 - show_file("+", tree2, size2, base);
110 - return 1;
111 - }
112 - if (!diff_options.find_copies_harder &&
113 - !memcmp(sha1, sha2, 20) && mode1 == mode2)
114 - return 0;
115 -
116 - /*
117 - * If the filemode has changed to/from a directory from/to a regular
118 - * file, we need to consider it a remove and an add.
119 - */
120 - if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
121 - show_file("-", tree1, size1, base);
122 - show_file("+", tree2, size2, base);
123 - return 0;
124 - }
125 -
126 - if (recursive && S_ISDIR(mode1)) {
127 - int retval;
128 - char *newbase = malloc_base(base, path1, pathlen1);
129 - if (show_tree_entry_in_recursive)
130 - diff_change(&diff_options, mode1, mode2,
131 - sha1, sha2, base, path1);
132 - retval = diff_tree_sha1(sha1, sha2, newbase);
133 - free(newbase);
134 - return retval;
135 - }
136 -
137 - diff_change(&diff_options, mode1, mode2, sha1, sha2, base, path1);
138 - return 0;
139 -}
140 -
141 -static int interesting(void *tree, unsigned long size, const char *base)
142 -{
143 - const char *path;
144 - unsigned mode;
145 - int i;
146 - int baselen, pathlen;
147 -
148 - if (!nr_paths)
149 - return 1;
150 -
151 - (void)extract(tree, size, &path, &mode);
152 -
153 - pathlen = strlen(path);
154 - baselen = strlen(base);
155 -
156 - for (i=0; i < nr_paths; i++) {
157 - const char *match = paths[i];
158 - int matchlen = pathlens[i];
159 -
160 - if (baselen >= matchlen) {
161 - /* If it doesn't match, move along... */
162 - if (strncmp(base, match, matchlen))
163 - continue;
164 -
165 - /* The base is a subdirectory of a path which was specified. */
166 - return 1;
167 - }
168 -
169 - /* Does the base match? */
170 - if (strncmp(base, match, baselen))
171 - continue;
172 -
173 - match += baselen;
174 - matchlen -= baselen;
175 -
176 - if (pathlen > matchlen)
177 - continue;
178 -
179 - if (matchlen > pathlen) {
180 - if (match[pathlen] != '/')
181 - continue;
182 - if (!S_ISDIR(mode))
183 - continue;
184 - }
185 -
186 - if (strncmp(path, match, pathlen))
187 - continue;
188 -
189 - return 1;
190 - }
191 - return 0; /* No matches */
192 -}
193 -
194 -/* A whole sub-tree went away or appeared */
195 -static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
196 -{
197 - while (size) {
198 - if (interesting(tree, size, base))
199 - show_file(prefix, tree, size, base);
200 - update_tree_entry(&tree, &size);
201 - }
202 -}
203 -
204 -static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
205 -{
206 - while (size1 | size2) {
207 - if (nr_paths && size1 && !interesting(tree1, size1, base)) {
208 - update_tree_entry(&tree1, &size1);
209 - continue;
210 - }
211 - if (nr_paths && size2 && !interesting(tree2, size2, base)) {
212 - update_tree_entry(&tree2, &size2);
213 - continue;
214 - }
215 - if (!size1) {
216 - show_file("+", tree2, size2, base);
217 - update_tree_entry(&tree2, &size2);
218 - continue;
219 - }
220 - if (!size2) {
221 - show_file("-", tree1, size1, base);
222 - update_tree_entry(&tree1, &size1);
223 - continue;
224 - }
225 - switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
226 - case -1:
227 - update_tree_entry(&tree1, &size1);
228 - continue;
229 - case 0:
230 - update_tree_entry(&tree1, &size1);
231 - /* Fallthrough */
232 - case 1:
233 - update_tree_entry(&tree2, &size2);
234 - continue;
235 - }
236 - die("git-diff-tree: internal error");
237 - }
238 - return 0;
239 -}
240 -
241 -static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
242 -{
243 - void *tree1, *tree2;
244 - unsigned long size1, size2;
245 - int retval;
246 -
247 - tree1 = read_object_with_reference(old, "tree", &size1, NULL);
248 - if (!tree1)
249 - die("unable to read source tree (%s)", sha1_to_hex(old));
250 - tree2 = read_object_with_reference(new, "tree", &size2, NULL);
251 - if (!tree2)
252 - die("unable to read destination tree (%s)", sha1_to_hex(new));
253 - retval = diff_tree(tree1, size1, tree2, size2, base);
254 - free(tree1);
255 - free(tree2);
256 - return retval;
257 -}
14 +static struct diff_options diff_options;
15
16 static void call_diff_setup_done(void)
17 {
@@ -285,7 +42,7 @@ static int diff_tree_sha1_top(const unsigned char *old,
42 int ret;
43
44 call_diff_setup_done();
288 - ret = diff_tree_sha1(old, new, base);
45 + ret = diff_tree_sha1(old, new, base, &diff_options);
46 call_diff_flush();
47 return ret;
48 }
@@ -294,13 +51,17 @@ static int diff_root_tree(const unsigned char *new, const char *base)
51 {
52 int retval;
53 void *tree;
297 - unsigned long size;
54 + struct tree_desc empty, real;
55
56 call_diff_setup_done();
300 - tree = read_object_with_reference(new, "tree", &size, NULL);
57 + tree = read_object_with_reference(new, "tree", &real.size, NULL);
58 if (!tree)
59 die("unable to read root tree (%s)", sha1_to_hex(new));
303 - retval = diff_tree("", 0, tree, size, base);
60 + real.buf = tree;
61 +
62 + empty.buf = "";
63 + empty.size = 0;
64 + retval = diff_tree(&empty, &real, base, &diff_options);
65 free(tree);
66 call_diff_flush();
67 return retval;
@@ -387,14 +148,6 @@ static int diff_tree_stdin(char *line)
148 return diff_tree_commit(commit, line);
149 }
150
390 -static int count_paths(const char **paths)
391 -{
392 - int i = 0;
393 - while (*paths++)
394 - i++;
395 - return i;
396 -}
397 -
151 static const char diff_tree_usage[] =
152 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] "
153 "[<common diff options>] <tree-ish> <tree-ish>"
@@ -445,11 +198,12 @@ int main(int argc, const char **argv)
198 break;
199 }
200 if (!strcmp(arg, "-r")) {
448 - recursive = 1;
201 + diff_options.recursive = 1;
202 continue;
203 }
204 if (!strcmp(arg, "-t")) {
452 - recursive = show_tree_entry_in_recursive = 1;
205 + diff_options.recursive = 1;
206 + diff_options.tree_in_recursive = 1;
207 continue;
208 }
209 if (!strcmp(arg, "-m")) {
@@ -478,17 +232,9 @@ int main(int argc, const char **argv)
232 usage(diff_tree_usage);
233 }
234 if (diff_options.output_format == DIFF_FORMAT_PATCH)
481 - recursive = 1;
235 + diff_options.recursive = 1;
236
483 - paths = get_pathspec(prefix, argv);
484 - if (paths) {
485 - int i;
486 -
487 - nr_paths = count_paths(paths);
488 - pathlens = xmalloc(nr_paths * sizeof(int));
489 - for (i=0; i<nr_paths; i++)
490 - pathlens[i] = strlen(paths[i]);
491 - }
237 + diff_tree_setup_paths(get_pathspec(prefix, argv));
238
239 switch (nr_sha1) {
240 case 0:
diff.c
+3
@@ -754,6 +754,9 @@ void diff_setup(struct diff_options *options)
754 options->line_termination = '\n';
755 options->break_opt = -1;
756 options->rename_limit = -1;
757 +
758 + options->change = diff_change;
759 + options->add_remove = diff_addremove;
760 }
761
762 int diff_setup_done(struct diff_options *options)
diff.h
+29
@@ -8,11 +8,31 @@
8 (S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
9 S_ISLNK(mode) ? S_IFLNK : S_IFDIR)
10
11 +struct tree_desc {
12 + void *buf;
13 + unsigned long size;
14 +};
15 +
16 +struct diff_options;
17 +
18 +typedef void (*change_fn_t)(struct diff_options *options,
19 + unsigned old_mode, unsigned new_mode,
20 + const unsigned char *old_sha1,
21 + const unsigned char *new_sha1,
22 + const char *base, const char *path);
23 +
24 +typedef void (*add_remove_fn_t)(struct diff_options *options,
25 + int addremove, unsigned mode,
26 + const unsigned char *sha1,
27 + const char *base, const char *path);
28 +
29 struct diff_options {
30 const char **paths;
31 const char *filter;
32 const char *orderfile;
33 const char *pickaxe;
34 + unsigned recursive:1,
35 + tree_in_recursive:1;
36 int break_opt;
37 int detect_rename;
38 int find_copies_harder;
@@ -23,8 +43,17 @@ struct diff_options {
43 int reverse_diff;
44 int rename_limit;
45 int setup;
46 +
47 + change_fn_t change;
48 + add_remove_fn_t add_remove;
49 };
50
51 +extern void diff_tree_setup_paths(const char **paths);
52 +extern int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
53 + const char *base, struct diff_options *opt);
54 +extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new,
55 + const char *base, struct diff_options *opt);
56 +
57 extern void diff_addremove(struct diff_options *,
58 int addremove,
59 unsigned mode,
tree-diff.c new
+270
@@ -0,0 +1,270 @@
1 +/*
2 + * Helper functions for tree diff generation
3 + */
4 +#include "cache.h"
5 +#include "diff.h"
6 +
7 +// What paths are we interested in?
8 +static int nr_paths = 0;
9 +static const char **paths = NULL;
10 +static int *pathlens = NULL;
11 +
12 +static void update_tree_entry(struct tree_desc *desc)
13 +{
14 + void *buf = desc->buf;
15 + unsigned long size = desc->size;
16 + int len = strlen(buf) + 1 + 20;
17 +
18 + if (size < len)
19 + die("corrupt tree file");
20 + desc->buf = buf + len;
21 + desc->size = size - len;
22 +}
23 +
24 +static const unsigned char *extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
25 +{
26 + void *tree = desc->buf;
27 + unsigned long size = desc->size;
28 + int len = strlen(tree)+1;
29 + const unsigned char *sha1 = tree + len;
30 + const char *path = strchr(tree, ' ');
31 + unsigned int mode;
32 +
33 + if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
34 + die("corrupt tree file");
35 + *pathp = path+1;
36 + *modep = DIFF_FILE_CANON_MODE(mode);
37 + return sha1;
38 +}
39 +
40 +static char *malloc_base(const char *base, const char *path, int pathlen)
41 +{
42 + int baselen = strlen(base);
43 + char *newbase = xmalloc(baselen + pathlen + 2);
44 + memcpy(newbase, base, baselen);
45 + memcpy(newbase + baselen, path, pathlen);
46 + memcpy(newbase + baselen + pathlen, "/", 2);
47 + return newbase;
48 +}
49 +
50 +static int show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base);
51 +
52 +static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
53 +{
54 + unsigned mode1, mode2;
55 + const char *path1, *path2;
56 + const unsigned char *sha1, *sha2;
57 + int cmp, pathlen1, pathlen2;
58 +
59 + sha1 = extract(t1, &path1, &mode1);
60 + sha2 = extract(t2, &path2, &mode2);
61 +
62 + pathlen1 = strlen(path1);
63 + pathlen2 = strlen(path2);
64 + cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
65 + if (cmp < 0) {
66 + show_entry(opt, "-", t1, base);
67 + return -1;
68 + }
69 + if (cmp > 0) {
70 + show_entry(opt, "+", t2, base);
71 + return 1;
72 + }
73 + if (!opt->find_copies_harder &&
74 + !memcmp(sha1, sha2, 20) && mode1 == mode2)
75 + return 0;
76 +
77 + /*
78 + * If the filemode has changed to/from a directory from/to a regular
79 + * file, we need to consider it a remove and an add.
80 + */
81 + if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
82 + show_entry(opt, "-", t1, base);
83 + show_entry(opt, "+", t2, base);
84 + return 0;
85 + }
86 +
87 + if (opt->recursive && S_ISDIR(mode1)) {
88 + int retval;
89 + char *newbase = malloc_base(base, path1, pathlen1);
90 + if (opt->tree_in_recursive)
91 + opt->change(opt, mode1, mode2,
92 + sha1, sha2, base, path1);
93 + retval = diff_tree_sha1(sha1, sha2, newbase, opt);
94 + free(newbase);
95 + return retval;
96 + }
97 +
98 + opt->change(opt, mode1, mode2, sha1, sha2, base, path1);
99 + return 0;
100 +}
101 +
102 +static int interesting(struct tree_desc *desc, const char *base)
103 +{
104 + const char *path;
105 + unsigned mode;
106 + int i;
107 + int baselen, pathlen;
108 +
109 + if (!nr_paths)
110 + return 1;
111 +
112 + (void)extract(desc, &path, &mode);
113 +
114 + pathlen = strlen(path);
115 + baselen = strlen(base);
116 +
117 + for (i=0; i < nr_paths; i++) {
118 + const char *match = paths[i];
119 + int matchlen = pathlens[i];
120 +
121 + if (baselen >= matchlen) {
122 + /* If it doesn't match, move along... */
123 + if (strncmp(base, match, matchlen))
124 + continue;
125 +
126 + /* The base is a subdirectory of a path which was specified. */
127 + return 1;
128 + }
129 +
130 + /* Does the base match? */
131 + if (strncmp(base, match, baselen))
132 + continue;
133 +
134 + match += baselen;
135 + matchlen -= baselen;
136 +
137 + if (pathlen > matchlen)
138 + continue;
139 +
140 + if (matchlen > pathlen) {
141 + if (match[pathlen] != '/')
142 + continue;
143 + if (!S_ISDIR(mode))
144 + continue;
145 + }
146 +
147 + if (strncmp(path, match, pathlen))
148 + continue;
149 +
150 + return 1;
151 + }
152 + return 0; /* No matches */
153 +}
154 +
155 +/* A whole sub-tree went away or appeared */
156 +static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
157 +{
158 + while (desc->size) {
159 + if (interesting(desc, base))
160 + show_entry(opt, prefix, desc, base);
161 + update_tree_entry(desc);
162 + }
163 +}
164 +
165 +/* A file entry went away or appeared */
166 +static int show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
167 +{
168 + unsigned mode;
169 + const char *path;
170 + const unsigned char *sha1 = extract(desc, &path, &mode);
171 +
172 + if (opt->recursive && S_ISDIR(mode)) {
173 + char type[20];
174 + char *newbase = malloc_base(base, path, strlen(path));
175 + struct tree_desc inner;
176 + void *tree;
177 +
178 + tree = read_sha1_file(sha1, type, &inner.size);
179 + if (!tree || strcmp(type, "tree"))
180 + die("corrupt tree sha %s", sha1_to_hex(sha1));
181 +
182 + inner.buf = tree;
183 + show_tree(opt, prefix, &inner, newbase);
184 +
185 + free(tree);
186 + free(newbase);
187 + return 0;
188 + }
189 +
190 + opt->add_remove(opt, prefix[0], mode, sha1, base, path);
191 + return 0;
192 +}
193 +
194 +int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
195 +{
196 + while (t1->size | t2->size) {
197 + if (nr_paths && t1->size && !interesting(t1, base)) {
198 + update_tree_entry(t1);
199 + continue;
200 + }
201 + if (nr_paths && t2->size && !interesting(t2, base)) {
202 + update_tree_entry(t2);
203 + continue;
204 + }
205 + if (!t1->size) {
206 + show_entry(opt, "+", t2, base);
207 + update_tree_entry(t2);
208 + continue;
209 + }
210 + if (!t2->size) {
211 + show_entry(opt, "-", t1, base);
212 + update_tree_entry(t1);
213 + continue;
214 + }
215 + switch (compare_tree_entry(t1, t2, base, opt)) {
216 + case -1:
217 + update_tree_entry(t1);
218 + continue;
219 + case 0:
220 + update_tree_entry(t1);
221 + /* Fallthrough */
222 + case 1:
223 + update_tree_entry(t2);
224 + continue;
225 + }
226 + die("git-diff-tree: internal error");
227 + }
228 + return 0;
229 +}
230 +
231 +int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
232 +{
233 + void *tree1, *tree2;
234 + struct tree_desc t1, t2;
235 + int retval;
236 +
237 + tree1 = read_object_with_reference(old, "tree", &t1.size, NULL);
238 + if (!tree1)
239 + die("unable to read source tree (%s)", sha1_to_hex(old));
240 + tree2 = read_object_with_reference(new, "tree", &t2.size, NULL);
241 + if (!tree2)
242 + die("unable to read destination tree (%s)", sha1_to_hex(new));
243 + t1.buf = tree1;
244 + t2.buf = tree2;
245 + retval = diff_tree(&t1, &t2, base, opt);
246 + free(tree1);
247 + free(tree2);
248 + return retval;
249 +}
250 +
251 +static int count_paths(const char **paths)
252 +{
253 + int i = 0;
254 + while (*paths++)
255 + i++;
256 + return i;
257 +}
258 +
259 +void diff_tree_setup_paths(const char **p)
260 +{
261 + if (p) {
262 + int i;
263 +
264 + paths = p;
265 + nr_paths = count_paths(paths);
266 + pathlens = xmalloc(nr_paths * sizeof(int));
267 + for (i=0; i<nr_paths; i++)
268 + pathlens[i] = strlen(paths[i]);
269 + }
270 +}