Initialize tree descriptors with a helper function rather than by hand.
[git/git-svn.git] / tree-diff.c
blob36788058aefb567eee042ea3e44baca144302835
1 /*
2 * Helper functions for tree diff generation
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "tree.h"
8 static char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
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;
17 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
18 const char *base, int baselen);
20 static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
22 unsigned mode1, mode2;
23 const char *path1, *path2;
24 const unsigned char *sha1, *sha2;
25 int cmp, pathlen1, pathlen2;
27 sha1 = tree_entry_extract(t1, &path1, &mode1);
28 sha2 = tree_entry_extract(t2, &path2, &mode2);
30 pathlen1 = tree_entry_len(path1, sha1);
31 pathlen2 = tree_entry_len(path2, sha2);
32 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
33 if (cmp < 0) {
34 show_entry(opt, "-", t1, base, baselen);
35 return -1;
37 if (cmp > 0) {
38 show_entry(opt, "+", t2, base, baselen);
39 return 1;
41 if (!opt->find_copies_harder && !hashcmp(sha1, sha2) && mode1 == mode2)
42 return 0;
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.
48 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
49 show_entry(opt, "-", t1, base, baselen);
50 show_entry(opt, "+", t2, base, baselen);
51 return 0;
54 if (opt->recursive && S_ISDIR(mode1)) {
55 int retval;
56 char *newbase = malloc_base(base, baselen, path1, pathlen1);
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;
65 opt->change(opt, mode1, mode2, sha1, sha2, base, path1);
66 return 0;
70 * Is a tree entry interesting given the pathspec we have?
72 * Return:
73 * - positive for yes
74 * - zero for no
75 * - negative for "no, and no subsequent entries will be either"
77 static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
79 const char *path;
80 const unsigned char *sha1;
81 unsigned mode;
82 int i;
83 int pathlen;
85 if (!opt->nr_paths)
86 return 1;
88 sha1 = tree_entry_extract(desc, &path, &mode);
90 pathlen = tree_entry_len(path, sha1);
92 for (i=0; i < opt->nr_paths; i++) {
93 const char *match = opt->paths[i];
94 int matchlen = opt->pathlens[i];
96 if (baselen >= matchlen) {
97 /* If it doesn't match, move along... */
98 if (strncmp(base, match, matchlen))
99 continue;
101 /* The base is a subdirectory of a path which was specified. */
102 return 1;
105 /* Does the base match? */
106 if (strncmp(base, match, baselen))
107 continue;
109 match += baselen;
110 matchlen -= baselen;
112 if (pathlen > matchlen)
113 continue;
115 if (matchlen > pathlen) {
116 if (match[pathlen] != '/')
117 continue;
118 if (!S_ISDIR(mode))
119 continue;
122 if (strncmp(path, match, pathlen))
123 continue;
125 return 1;
127 return 0; /* No matches */
130 /* A whole sub-tree went away or appeared */
131 static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
133 while (desc->size) {
134 int show = tree_entry_interesting(desc, base, baselen, opt);
135 if (show < 0)
136 break;
137 if (show)
138 show_entry(opt, prefix, desc, base, baselen);
139 update_tree_entry(desc);
143 /* A file entry went away or appeared */
144 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
145 const char *base, int baselen)
147 unsigned mode;
148 const char *path;
149 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
151 if (opt->recursive && S_ISDIR(mode)) {
152 enum object_type type;
153 int pathlen = tree_entry_len(path, sha1);
154 char *newbase = malloc_base(base, baselen, path, pathlen);
155 struct tree_desc inner;
156 void *tree;
157 unsigned long size;
159 tree = read_sha1_file(sha1, &type, &size);
160 if (!tree || type != OBJ_TREE)
161 die("corrupt tree sha %s", sha1_to_hex(sha1));
163 init_tree_desc(&inner, tree, size);
164 show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
166 free(tree);
167 free(newbase);
168 } else {
169 opt->add_remove(opt, prefix[0], mode, sha1, base, path);
173 static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt)
175 while (t->size) {
176 int show = tree_entry_interesting(t, base, baselen, opt);
177 if (!show) {
178 update_tree_entry(t);
179 continue;
181 /* Skip it all? */
182 if (show < 0)
183 t->size = 0;
184 return;
188 int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
190 int baselen = strlen(base);
192 for (;;) {
193 if (opt->quiet && opt->has_changes)
194 break;
195 if (opt->nr_paths) {
196 skip_uninteresting(t1, base, baselen, opt);
197 skip_uninteresting(t2, base, baselen, opt);
199 if (!t1->size) {
200 if (!t2->size)
201 break;
202 show_entry(opt, "+", t2, base, baselen);
203 update_tree_entry(t2);
204 continue;
206 if (!t2->size) {
207 show_entry(opt, "-", t1, base, baselen);
208 update_tree_entry(t1);
209 continue;
211 switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
212 case -1:
213 update_tree_entry(t1);
214 continue;
215 case 0:
216 update_tree_entry(t1);
217 /* Fallthrough */
218 case 1:
219 update_tree_entry(t2);
220 continue;
222 die("git-diff-tree: internal error");
224 return 0;
227 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
229 void *tree1, *tree2;
230 struct tree_desc t1, t2;
231 unsigned long size1, size2;
232 int retval;
234 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
235 if (!tree1)
236 die("unable to read source tree (%s)", sha1_to_hex(old));
237 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
238 if (!tree2)
239 die("unable to read destination tree (%s)", sha1_to_hex(new));
240 init_tree_desc(&t1, tree1, size1);
241 init_tree_desc(&t2, tree2, size2);
242 retval = diff_tree(&t1, &t2, base, opt);
243 free(tree1);
244 free(tree2);
245 return retval;
248 int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
250 int retval;
251 void *tree;
252 unsigned long size;
253 struct tree_desc empty, real;
255 tree = read_object_with_reference(new, tree_type, &size, NULL);
256 if (!tree)
257 die("unable to read root tree (%s)", sha1_to_hex(new));
258 init_tree_desc(&real, tree, size);
260 init_tree_desc(&empty, "", 0);
261 retval = diff_tree(&empty, &real, base, opt);
262 free(tree);
263 return retval;
266 static int count_paths(const char **paths)
268 int i = 0;
269 while (*paths++)
270 i++;
271 return i;
274 void diff_tree_release_paths(struct diff_options *opt)
276 free(opt->pathlens);
279 void diff_tree_setup_paths(const char **p, struct diff_options *opt)
281 opt->nr_paths = 0;
282 opt->pathlens = NULL;
283 opt->paths = NULL;
285 if (p) {
286 int i;
288 opt->paths = p;
289 opt->nr_paths = count_paths(p);
290 if (opt->nr_paths == 0) {
291 opt->pathlens = NULL;
292 return;
294 opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
295 for (i=0; i < opt->nr_paths; i++)
296 opt->pathlens[i] = strlen(p[i]);