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