Merge git://git.kernel.org/pub/scm/gitk/gitk
[alt-git.git] / tree-diff.c
blob701fbba65c2d718a2cdff170a6be6d9993ad27d7
1 /*
2 * Helper functions for tree diff generation
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "tree.h"
8 // What paths are we interested in?
9 static int nr_paths = 0;
10 static const char **paths = NULL;
11 static int *pathlens = NULL;
13 static char *malloc_base(const char *base, const char *path, int pathlen)
15 int baselen = strlen(base);
16 char *newbase = xmalloc(baselen + pathlen + 2);
17 memcpy(newbase, base, baselen);
18 memcpy(newbase + baselen, path, pathlen);
19 memcpy(newbase + baselen + pathlen, "/", 2);
20 return newbase;
23 static int show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base);
25 static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
27 unsigned mode1, mode2;
28 const char *path1, *path2;
29 const unsigned char *sha1, *sha2;
30 int cmp, pathlen1, pathlen2;
32 sha1 = tree_entry_extract(t1, &path1, &mode1);
33 sha2 = tree_entry_extract(t2, &path2, &mode2);
35 pathlen1 = strlen(path1);
36 pathlen2 = strlen(path2);
37 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
38 if (cmp < 0) {
39 show_entry(opt, "-", t1, base);
40 return -1;
42 if (cmp > 0) {
43 show_entry(opt, "+", t2, base);
44 return 1;
46 if (!opt->find_copies_harder &&
47 !memcmp(sha1, sha2, 20) && mode1 == mode2)
48 return 0;
51 * If the filemode has changed to/from a directory from/to a regular
52 * file, we need to consider it a remove and an add.
54 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
55 show_entry(opt, "-", t1, base);
56 show_entry(opt, "+", t2, base);
57 return 0;
60 if (opt->recursive && S_ISDIR(mode1)) {
61 int retval;
62 char *newbase = malloc_base(base, path1, pathlen1);
63 if (opt->tree_in_recursive)
64 opt->change(opt, mode1, mode2,
65 sha1, sha2, base, path1);
66 retval = diff_tree_sha1(sha1, sha2, newbase, opt);
67 free(newbase);
68 return retval;
71 opt->change(opt, mode1, mode2, sha1, sha2, base, path1);
72 return 0;
75 static int interesting(struct tree_desc *desc, const char *base)
77 const char *path;
78 unsigned mode;
79 int i;
80 int baselen, pathlen;
82 if (!nr_paths)
83 return 1;
85 (void)tree_entry_extract(desc, &path, &mode);
87 pathlen = strlen(path);
88 baselen = strlen(base);
90 for (i=0; i < nr_paths; i++) {
91 const char *match = paths[i];
92 int matchlen = pathlens[i];
94 if (baselen >= matchlen) {
95 /* If it doesn't match, move along... */
96 if (strncmp(base, match, matchlen))
97 continue;
99 /* The base is a subdirectory of a path which was specified. */
100 return 1;
103 /* Does the base match? */
104 if (strncmp(base, match, baselen))
105 continue;
107 match += baselen;
108 matchlen -= baselen;
110 if (pathlen > matchlen)
111 continue;
113 if (matchlen > pathlen) {
114 if (match[pathlen] != '/')
115 continue;
116 if (!S_ISDIR(mode))
117 continue;
120 if (strncmp(path, match, pathlen))
121 continue;
123 return 1;
125 return 0; /* No matches */
128 /* A whole sub-tree went away or appeared */
129 static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
131 while (desc->size) {
132 if (interesting(desc, base))
133 show_entry(opt, prefix, desc, base);
134 update_tree_entry(desc);
138 /* A file entry went away or appeared */
139 static int show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
141 unsigned mode;
142 const char *path;
143 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
145 if (opt->recursive && S_ISDIR(mode)) {
146 char type[20];
147 char *newbase = malloc_base(base, path, strlen(path));
148 struct tree_desc inner;
149 void *tree;
151 tree = read_sha1_file(sha1, type, &inner.size);
152 if (!tree || strcmp(type, tree_type))
153 die("corrupt tree sha %s", sha1_to_hex(sha1));
155 inner.buf = tree;
156 show_tree(opt, prefix, &inner, newbase);
158 free(tree);
159 free(newbase);
160 return 0;
163 opt->add_remove(opt, prefix[0], mode, sha1, base, path);
164 return 0;
167 int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
169 while (t1->size | t2->size) {
170 if (nr_paths && t1->size && !interesting(t1, base)) {
171 update_tree_entry(t1);
172 continue;
174 if (nr_paths && t2->size && !interesting(t2, base)) {
175 update_tree_entry(t2);
176 continue;
178 if (!t1->size) {
179 show_entry(opt, "+", t2, base);
180 update_tree_entry(t2);
181 continue;
183 if (!t2->size) {
184 show_entry(opt, "-", t1, base);
185 update_tree_entry(t1);
186 continue;
188 switch (compare_tree_entry(t1, t2, base, opt)) {
189 case -1:
190 update_tree_entry(t1);
191 continue;
192 case 0:
193 update_tree_entry(t1);
194 /* Fallthrough */
195 case 1:
196 update_tree_entry(t2);
197 continue;
199 die("git-diff-tree: internal error");
201 return 0;
204 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
206 void *tree1, *tree2;
207 struct tree_desc t1, t2;
208 int retval;
210 tree1 = read_object_with_reference(old, tree_type, &t1.size, NULL);
211 if (!tree1)
212 die("unable to read source tree (%s)", sha1_to_hex(old));
213 tree2 = read_object_with_reference(new, tree_type, &t2.size, NULL);
214 if (!tree2)
215 die("unable to read destination tree (%s)", sha1_to_hex(new));
216 t1.buf = tree1;
217 t2.buf = tree2;
218 retval = diff_tree(&t1, &t2, base, opt);
219 free(tree1);
220 free(tree2);
221 return retval;
224 static int count_paths(const char **paths)
226 int i = 0;
227 while (*paths++)
228 i++;
229 return i;
232 void diff_tree_setup_paths(const char **p)
234 if (p) {
235 int i;
237 paths = p;
238 nr_paths = count_paths(paths);
239 if (nr_paths == 0) {
240 pathlens = NULL;
241 return;
243 pathlens = xmalloc(nr_paths * sizeof(int));
244 for (i=0; i<nr_paths; i++)
245 pathlens[i] = strlen(paths[i]);