2 * Helper functions for tree diff generation
7 // What paths are we interested in?
8 static int nr_paths = 0;
9 static const char **paths = NULL;
10 static int *pathlens = NULL;
12 static char *malloc_base(const char *base, const char *path, int pathlen)
14 int baselen = strlen(base);
15 char *newbase = xmalloc(baselen + pathlen + 2);
16 memcpy(newbase, base, baselen);
17 memcpy(newbase + baselen, path, pathlen);
18 memcpy(newbase + baselen + pathlen, "/", 2);
22 static int show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base);
24 static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
26 unsigned mode1, mode2;
27 const char *path1, *path2;
28 const unsigned char *sha1, *sha2;
29 int cmp, pathlen1, pathlen2;
31 sha1 = tree_entry_extract(t1, &path1, &mode1);
32 sha2 = tree_entry_extract(t2, &path2, &mode2);
34 pathlen1 = strlen(path1);
35 pathlen2 = strlen(path2);
36 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
38 show_entry(opt, "-", t1, base);
42 show_entry(opt, "+", t2, base);
45 if (!opt->find_copies_harder &&
46 !memcmp(sha1, sha2, 20) && mode1 == mode2)
50 * If the filemode has changed to/from a directory from/to a regular
51 * file, we need to consider it a remove and an add.
53 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
54 show_entry(opt, "-", t1, base);
55 show_entry(opt, "+", t2, base);
59 if (opt->recursive && S_ISDIR(mode1)) {
61 char *newbase = malloc_base(base, path1, pathlen1);
62 if (opt->tree_in_recursive)
63 opt->change(opt, mode1, mode2,
64 sha1, sha2, base, path1);
65 retval = diff_tree_sha1(sha1, sha2, newbase, opt);
70 opt->change(opt, mode1, mode2, sha1, sha2, base, path1);
74 static int interesting(struct tree_desc *desc, const char *base)
84 (void)tree_entry_extract(desc, &path, &mode);
86 pathlen = strlen(path);
87 baselen = strlen(base);
89 for (i=0; i < nr_paths; i++) {
90 const char *match = paths[i];
91 int matchlen = pathlens[i];
93 if (baselen >= matchlen) {
94 /* If it doesn't match, move along... */
95 if (strncmp(base, match, matchlen))
98 /* The base is a subdirectory of a path which was specified. */
102 /* Does the base match? */
103 if (strncmp(base, match, baselen))
109 if (pathlen > matchlen)
112 if (matchlen > pathlen) {
113 if (match[pathlen] != '/')
119 if (strncmp(path, match, pathlen))
124 return 0; /* No matches */
127 /* A whole sub-tree went away or appeared */
128 static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
131 if (interesting(desc, base))
132 show_entry(opt, prefix, desc, base);
133 update_tree_entry(desc);
137 /* A file entry went away or appeared */
138 static int show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
142 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
144 if (opt->recursive && S_ISDIR(mode)) {
146 char *newbase = malloc_base(base, path, strlen(path));
147 struct tree_desc inner;
150 tree = read_sha1_file(sha1, type, &inner.size);
151 if (!tree || strcmp(type, "tree"))
152 die("corrupt tree sha %s", sha1_to_hex(sha1));
155 show_tree(opt, prefix, &inner, newbase);
162 opt->add_remove(opt, prefix[0], mode, sha1, base, path);
166 int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
168 while (t1->size | t2->size) {
169 if (nr_paths && t1->size && !interesting(t1, base)) {
170 update_tree_entry(t1);
173 if (nr_paths && t2->size && !interesting(t2, base)) {
174 update_tree_entry(t2);
178 show_entry(opt, "+", t2, base);
179 update_tree_entry(t2);
183 show_entry(opt, "-", t1, base);
184 update_tree_entry(t1);
187 switch (compare_tree_entry(t1, t2, base, opt)) {
189 update_tree_entry(t1);
192 update_tree_entry(t1);
195 update_tree_entry(t2);
198 die("git-diff-tree: internal error");
203 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
206 struct tree_desc t1, t2;
209 tree1 = read_object_with_reference(old, "tree", &t1.size, NULL);
211 die("unable to read source tree (%s)", sha1_to_hex(old));
212 tree2 = read_object_with_reference(new, "tree", &t2.size, NULL);
214 die("unable to read destination tree (%s)", sha1_to_hex(new));
217 retval = diff_tree(&t1, &t2, base, opt);
223 static int count_paths(const char **paths)
231 void diff_tree_setup_paths(const char **p)
237 nr_paths = count_paths(paths);
242 pathlens = xmalloc(nr_paths * sizeof(int));
243 for (i=0; i<nr_paths; i++)
244 pathlens[i] = strlen(paths[i]);