gitweb: Do not use esc_html in esc_path
[git/kusma.git] / tree-diff.c
blob37d235e06e2cbfbd761fd02d7e73648a14a60daf
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, const char *path, int pathlen)
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;
18 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
19 const char *base);
21 static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
23 unsigned mode1, mode2;
24 const char *path1, *path2;
25 const unsigned char *sha1, *sha2;
26 int cmp, pathlen1, pathlen2;
28 sha1 = tree_entry_extract(t1, &path1, &mode1);
29 sha2 = tree_entry_extract(t2, &path2, &mode2);
31 pathlen1 = strlen(path1);
32 pathlen2 = strlen(path2);
33 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
34 if (cmp < 0) {
35 show_entry(opt, "-", t1, base);
36 return -1;
38 if (cmp > 0) {
39 show_entry(opt, "+", t2, base);
40 return 1;
42 if (!opt->find_copies_harder && !hashcmp(sha1, sha2) && mode1 == mode2)
43 return 0;
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.
49 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
50 show_entry(opt, "-", t1, base);
51 show_entry(opt, "+", t2, base);
52 return 0;
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;
66 opt->change(opt, mode1, mode2, sha1, sha2, base, path1);
67 return 0;
70 static int interesting(struct tree_desc *desc, const char *base, struct diff_options *opt)
72 const char *path;
73 unsigned mode;
74 int i;
75 int baselen, pathlen;
77 if (!opt->nr_paths)
78 return 1;
80 (void)tree_entry_extract(desc, &path, &mode);
82 pathlen = strlen(path);
83 baselen = strlen(base);
85 for (i=0; i < opt->nr_paths; i++) {
86 const char *match = opt->paths[i];
87 int matchlen = opt->pathlens[i];
89 if (baselen >= matchlen) {
90 /* If it doesn't match, move along... */
91 if (strncmp(base, match, matchlen))
92 continue;
94 /* The base is a subdirectory of a path which was specified. */
95 return 1;
98 /* Does the base match? */
99 if (strncmp(base, match, baselen))
100 continue;
102 match += baselen;
103 matchlen -= baselen;
105 if (pathlen > matchlen)
106 continue;
108 if (matchlen > pathlen) {
109 if (match[pathlen] != '/')
110 continue;
111 if (!S_ISDIR(mode))
112 continue;
115 if (strncmp(path, match, pathlen))
116 continue;
118 return 1;
120 return 0; /* No matches */
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)
126 while (desc->size) {
127 if (interesting(desc, base, opt))
128 show_entry(opt, prefix, desc, base);
129 update_tree_entry(desc);
133 /* A file entry went away or appeared */
134 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
135 const char *base)
137 unsigned mode;
138 const char *path;
139 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
141 if (opt->recursive && S_ISDIR(mode)) {
142 char type[20];
143 char *newbase = malloc_base(base, path, strlen(path));
144 struct tree_desc inner;
145 void *tree;
147 tree = read_sha1_file(sha1, type, &inner.size);
148 if (!tree || strcmp(type, tree_type))
149 die("corrupt tree sha %s", sha1_to_hex(sha1));
151 inner.buf = tree;
152 show_tree(opt, prefix, &inner, newbase);
154 free(tree);
155 free(newbase);
156 } else {
157 opt->add_remove(opt, prefix[0], mode, sha1, base, path);
161 int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
163 while (t1->size | t2->size) {
164 if (opt->nr_paths && t1->size && !interesting(t1, base, opt)) {
165 update_tree_entry(t1);
166 continue;
168 if (opt->nr_paths && t2->size && !interesting(t2, base, opt)) {
169 update_tree_entry(t2);
170 continue;
172 if (!t1->size) {
173 show_entry(opt, "+", t2, base);
174 update_tree_entry(t2);
175 continue;
177 if (!t2->size) {
178 show_entry(opt, "-", t1, base);
179 update_tree_entry(t1);
180 continue;
182 switch (compare_tree_entry(t1, t2, base, opt)) {
183 case -1:
184 update_tree_entry(t1);
185 continue;
186 case 0:
187 update_tree_entry(t1);
188 /* Fallthrough */
189 case 1:
190 update_tree_entry(t2);
191 continue;
193 die("git-diff-tree: internal error");
195 return 0;
198 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
200 void *tree1, *tree2;
201 struct tree_desc t1, t2;
202 int retval;
204 tree1 = read_object_with_reference(old, tree_type, &t1.size, NULL);
205 if (!tree1)
206 die("unable to read source tree (%s)", sha1_to_hex(old));
207 tree2 = read_object_with_reference(new, tree_type, &t2.size, NULL);
208 if (!tree2)
209 die("unable to read destination tree (%s)", sha1_to_hex(new));
210 t1.buf = tree1;
211 t2.buf = tree2;
212 retval = diff_tree(&t1, &t2, base, opt);
213 free(tree1);
214 free(tree2);
215 return retval;
218 int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
220 int retval;
221 void *tree;
222 struct tree_desc empty, real;
224 tree = read_object_with_reference(new, tree_type, &real.size, NULL);
225 if (!tree)
226 die("unable to read root tree (%s)", sha1_to_hex(new));
227 real.buf = tree;
229 empty.size = 0;
230 empty.buf = "";
231 retval = diff_tree(&empty, &real, base, opt);
232 free(tree);
233 return retval;
236 static int count_paths(const char **paths)
238 int i = 0;
239 while (*paths++)
240 i++;
241 return i;
244 void diff_tree_release_paths(struct diff_options *opt)
246 free(opt->pathlens);
249 void diff_tree_setup_paths(const char **p, struct diff_options *opt)
251 opt->nr_paths = 0;
252 opt->pathlens = NULL;
253 opt->paths = NULL;
255 if (p) {
256 int i;
258 opt->paths = p;
259 opt->nr_paths = count_paths(p);
260 if (opt->nr_paths == 0) {
261 opt->pathlens = NULL;
262 return;
264 opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
265 for (i=0; i < opt->nr_paths; i++)
266 opt->pathlens[i] = strlen(p[i]);