Commit | Line | Data |
---|---|---|
ac1b3d12 LT |
1 | /* |
2 | * Helper functions for tree diff generation | |
3 | */ | |
4 | #include "cache.h" | |
5 | #include "diff.h" | |
8e440259 | 6 | #include "tree.h" |
ac1b3d12 | 7 | |
304de2d2 | 8 | static char *malloc_base(const char *base, int baselen, const char *path, int pathlen) |
ac1b3d12 | 9 | { |
ac1b3d12 LT |
10 | char *newbase = xmalloc(baselen + pathlen + 2); |
11 | memcpy(newbase, base, baselen); | |
12 | memcpy(newbase + baselen, path, pathlen); | |
13 | memcpy(newbase + baselen + pathlen, "/", 2); | |
14 | return newbase; | |
15 | } | |
16 | ||
cf995ede | 17 | static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, |
304de2d2 | 18 | const char *base, int baselen); |
ac1b3d12 | 19 | |
304de2d2 | 20 | static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt) |
ac1b3d12 LT |
21 | { |
22 | unsigned mode1, mode2; | |
23 | const char *path1, *path2; | |
24 | const unsigned char *sha1, *sha2; | |
25 | int cmp, pathlen1, pathlen2; | |
26 | ||
50f9a858 LT |
27 | sha1 = tree_entry_extract(t1, &path1, &mode1); |
28 | sha2 = tree_entry_extract(t2, &path2, &mode2); | |
ac1b3d12 | 29 | |
304de2d2 LT |
30 | pathlen1 = tree_entry_len(path1, sha1); |
31 | pathlen2 = tree_entry_len(path2, sha2); | |
ac1b3d12 LT |
32 | cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2); |
33 | if (cmp < 0) { | |
304de2d2 | 34 | show_entry(opt, "-", t1, base, baselen); |
ac1b3d12 LT |
35 | return -1; |
36 | } | |
37 | if (cmp > 0) { | |
304de2d2 | 38 | show_entry(opt, "+", t2, base, baselen); |
ac1b3d12 LT |
39 | return 1; |
40 | } | |
a89fccd2 | 41 | if (!opt->find_copies_harder && !hashcmp(sha1, sha2) && mode1 == mode2) |
ac1b3d12 LT |
42 | return 0; |
43 | ||
44 | /* | |
45 | * If the filemode has changed to/from a directory from/to a regular | |
46 | * file, we need to consider it a remove and an add. | |
47 | */ | |
48 | if (S_ISDIR(mode1) != S_ISDIR(mode2)) { | |
304de2d2 LT |
49 | show_entry(opt, "-", t1, base, baselen); |
50 | show_entry(opt, "+", t2, base, baselen); | |
ac1b3d12 LT |
51 | return 0; |
52 | } | |
53 | ||
54 | if (opt->recursive && S_ISDIR(mode1)) { | |
55 | int retval; | |
304de2d2 | 56 | char *newbase = malloc_base(base, baselen, path1, pathlen1); |
ac1b3d12 LT |
57 | if (opt->tree_in_recursive) |
58 | opt->change(opt, mode1, mode2, | |
59 | sha1, sha2, base, path1); | |
60 | retval = diff_tree_sha1(sha1, sha2, newbase, opt); | |
61 | free(newbase); | |
62 | return retval; | |
63 | } | |
64 | ||
65 | opt->change(opt, mode1, mode2, sha1, sha2, base, path1); | |
66 | return 0; | |
67 | } | |
68 | ||
304de2d2 | 69 | static int interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt) |
ac1b3d12 LT |
70 | { |
71 | const char *path; | |
304de2d2 | 72 | const unsigned char *sha1; |
ac1b3d12 LT |
73 | unsigned mode; |
74 | int i; | |
304de2d2 | 75 | int pathlen; |
ac1b3d12 | 76 | |
a8baa7b9 | 77 | if (!opt->nr_paths) |
ac1b3d12 LT |
78 | return 1; |
79 | ||
304de2d2 | 80 | sha1 = tree_entry_extract(desc, &path, &mode); |
ac1b3d12 | 81 | |
304de2d2 | 82 | pathlen = tree_entry_len(path, sha1); |
ac1b3d12 | 83 | |
a8baa7b9 JH |
84 | for (i=0; i < opt->nr_paths; i++) { |
85 | const char *match = opt->paths[i]; | |
86 | int matchlen = opt->pathlens[i]; | |
ac1b3d12 LT |
87 | |
88 | if (baselen >= matchlen) { | |
89 | /* If it doesn't match, move along... */ | |
90 | if (strncmp(base, match, matchlen)) | |
91 | continue; | |
92 | ||
93 | /* The base is a subdirectory of a path which was specified. */ | |
94 | return 1; | |
95 | } | |
96 | ||
97 | /* Does the base match? */ | |
98 | if (strncmp(base, match, baselen)) | |
99 | continue; | |
100 | ||
101 | match += baselen; | |
102 | matchlen -= baselen; | |
103 | ||
104 | if (pathlen > matchlen) | |
105 | continue; | |
106 | ||
107 | if (matchlen > pathlen) { | |
108 | if (match[pathlen] != '/') | |
109 | continue; | |
110 | if (!S_ISDIR(mode)) | |
111 | continue; | |
112 | } | |
113 | ||
114 | if (strncmp(path, match, pathlen)) | |
115 | continue; | |
116 | ||
117 | return 1; | |
118 | } | |
119 | return 0; /* No matches */ | |
120 | } | |
121 | ||
122 | /* A whole sub-tree went away or appeared */ | |
304de2d2 | 123 | static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen) |
ac1b3d12 LT |
124 | { |
125 | while (desc->size) { | |
304de2d2 LT |
126 | if (interesting(desc, base, baselen, opt)) |
127 | show_entry(opt, prefix, desc, base, baselen); | |
ac1b3d12 LT |
128 | update_tree_entry(desc); |
129 | } | |
130 | } | |
131 | ||
132 | /* A file entry went away or appeared */ | |
cf995ede | 133 | static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, |
304de2d2 | 134 | const char *base, int baselen) |
ac1b3d12 LT |
135 | { |
136 | unsigned mode; | |
137 | const char *path; | |
50f9a858 | 138 | const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode); |
ac1b3d12 LT |
139 | |
140 | if (opt->recursive && S_ISDIR(mode)) { | |
21666f1a | 141 | enum object_type type; |
304de2d2 LT |
142 | int pathlen = tree_entry_len(path, sha1); |
143 | char *newbase = malloc_base(base, baselen, path, pathlen); | |
ac1b3d12 LT |
144 | struct tree_desc inner; |
145 | void *tree; | |
146 | ||
21666f1a NP |
147 | tree = read_sha1_file(sha1, &type, &inner.size); |
148 | if (!tree || type != OBJ_TREE) | |
ac1b3d12 LT |
149 | die("corrupt tree sha %s", sha1_to_hex(sha1)); |
150 | ||
151 | inner.buf = tree; | |
304de2d2 | 152 | show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen); |
ac1b3d12 LT |
153 | |
154 | free(tree); | |
155 | free(newbase); | |
cf995ede DR |
156 | } else { |
157 | opt->add_remove(opt, prefix[0], mode, sha1, base, path); | |
ac1b3d12 | 158 | } |
ac1b3d12 LT |
159 | } |
160 | ||
161 | int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt) | |
162 | { | |
304de2d2 LT |
163 | int baselen = strlen(base); |
164 | ||
ac1b3d12 | 165 | while (t1->size | t2->size) { |
822cac01 JH |
166 | if (opt->quiet && opt->has_changes) |
167 | break; | |
304de2d2 | 168 | if (opt->nr_paths && t1->size && !interesting(t1, base, baselen, opt)) { |
ac1b3d12 LT |
169 | update_tree_entry(t1); |
170 | continue; | |
171 | } | |
304de2d2 | 172 | if (opt->nr_paths && t2->size && !interesting(t2, base, baselen, opt)) { |
ac1b3d12 LT |
173 | update_tree_entry(t2); |
174 | continue; | |
175 | } | |
176 | if (!t1->size) { | |
304de2d2 | 177 | show_entry(opt, "+", t2, base, baselen); |
ac1b3d12 LT |
178 | update_tree_entry(t2); |
179 | continue; | |
180 | } | |
181 | if (!t2->size) { | |
304de2d2 | 182 | show_entry(opt, "-", t1, base, baselen); |
ac1b3d12 LT |
183 | update_tree_entry(t1); |
184 | continue; | |
185 | } | |
304de2d2 | 186 | switch (compare_tree_entry(t1, t2, base, baselen, opt)) { |
ac1b3d12 LT |
187 | case -1: |
188 | update_tree_entry(t1); | |
189 | continue; | |
190 | case 0: | |
191 | update_tree_entry(t1); | |
192 | /* Fallthrough */ | |
193 | case 1: | |
194 | update_tree_entry(t2); | |
195 | continue; | |
196 | } | |
197 | die("git-diff-tree: internal error"); | |
198 | } | |
199 | return 0; | |
200 | } | |
201 | ||
202 | int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt) | |
203 | { | |
204 | void *tree1, *tree2; | |
205 | struct tree_desc t1, t2; | |
206 | int retval; | |
207 | ||
8e440259 | 208 | tree1 = read_object_with_reference(old, tree_type, &t1.size, NULL); |
ac1b3d12 LT |
209 | if (!tree1) |
210 | die("unable to read source tree (%s)", sha1_to_hex(old)); | |
8e440259 | 211 | tree2 = read_object_with_reference(new, tree_type, &t2.size, NULL); |
ac1b3d12 LT |
212 | if (!tree2) |
213 | die("unable to read destination tree (%s)", sha1_to_hex(new)); | |
214 | t1.buf = tree1; | |
215 | t2.buf = tree2; | |
216 | retval = diff_tree(&t1, &t2, base, opt); | |
217 | free(tree1); | |
218 | free(tree2); | |
219 | return retval; | |
220 | } | |
221 | ||
2b60356d RS |
222 | int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt) |
223 | { | |
224 | int retval; | |
225 | void *tree; | |
226 | struct tree_desc empty, real; | |
227 | ||
228 | tree = read_object_with_reference(new, tree_type, &real.size, NULL); | |
229 | if (!tree) | |
230 | die("unable to read root tree (%s)", sha1_to_hex(new)); | |
231 | real.buf = tree; | |
232 | ||
233 | empty.size = 0; | |
234 | empty.buf = ""; | |
235 | retval = diff_tree(&empty, &real, base, opt); | |
236 | free(tree); | |
237 | return retval; | |
238 | } | |
239 | ||
ac1b3d12 LT |
240 | static int count_paths(const char **paths) |
241 | { | |
242 | int i = 0; | |
243 | while (*paths++) | |
244 | i++; | |
245 | return i; | |
246 | } | |
247 | ||
a8baa7b9 | 248 | void diff_tree_release_paths(struct diff_options *opt) |
ac1b3d12 | 249 | { |
a8baa7b9 JH |
250 | free(opt->pathlens); |
251 | } | |
252 | ||
253 | void diff_tree_setup_paths(const char **p, struct diff_options *opt) | |
254 | { | |
255 | opt->nr_paths = 0; | |
256 | opt->pathlens = NULL; | |
257 | opt->paths = NULL; | |
258 | ||
ac1b3d12 LT |
259 | if (p) { |
260 | int i; | |
261 | ||
a8baa7b9 JH |
262 | opt->paths = p; |
263 | opt->nr_paths = count_paths(p); | |
264 | if (opt->nr_paths == 0) { | |
265 | opt->pathlens = NULL; | |
7e4a2a84 JH |
266 | return; |
267 | } | |
a8baa7b9 JH |
268 | opt->pathlens = xmalloc(opt->nr_paths * sizeof(int)); |
269 | for (i=0; i < opt->nr_paths; i++) | |
270 | opt->pathlens[i] = strlen(p[i]); | |
ac1b3d12 LT |
271 | } |
272 | } |