2 * Helper functions for tree diff generation
9 static char *malloc_base(const char *base
, int baselen
, const char *path
, int pathlen
)
11 char *newbase
= xmalloc(baselen
+ pathlen
+ 2);
12 memcpy(newbase
, base
, baselen
);
13 memcpy(newbase
+ baselen
, path
, pathlen
);
14 memcpy(newbase
+ baselen
+ pathlen
, "/", 2);
18 static char *malloc_fullname(const char *base
, int baselen
, const char *path
, int pathlen
)
20 char *fullname
= xmalloc(baselen
+ pathlen
+ 1);
21 memcpy(fullname
, base
, baselen
);
22 memcpy(fullname
+ baselen
, path
, pathlen
);
23 fullname
[baselen
+ pathlen
] = 0;
27 static void show_entry(struct diff_options
*opt
, const char *prefix
, struct tree_desc
*desc
,
28 const char *base
, int baselen
);
30 static int compare_tree_entry(struct tree_desc
*t1
, struct tree_desc
*t2
, const char *base
, int baselen
, struct diff_options
*opt
)
32 unsigned mode1
, mode2
;
33 const char *path1
, *path2
;
34 const unsigned char *sha1
, *sha2
;
35 int cmp
, pathlen1
, pathlen2
;
38 sha1
= tree_entry_extract(t1
, &path1
, &mode1
);
39 sha2
= tree_entry_extract(t2
, &path2
, &mode2
);
41 pathlen1
= tree_entry_len(path1
, sha1
);
42 pathlen2
= tree_entry_len(path2
, sha2
);
43 cmp
= base_name_compare(path1
, pathlen1
, mode1
, path2
, pathlen2
, mode2
);
45 show_entry(opt
, "-", t1
, base
, baselen
);
49 show_entry(opt
, "+", t2
, base
, baselen
);
52 if (!DIFF_OPT_TST(opt
, FIND_COPIES_HARDER
) && !hashcmp(sha1
, sha2
) && mode1
== mode2
)
56 * If the filemode has changed to/from a directory from/to a regular
57 * file, we need to consider it a remove and an add.
59 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
60 show_entry(opt
, "-", t1
, base
, baselen
);
61 show_entry(opt
, "+", t2
, base
, baselen
);
65 if (DIFF_OPT_TST(opt
, RECURSIVE
) && S_ISDIR(mode1
)) {
67 char *newbase
= malloc_base(base
, baselen
, path1
, pathlen1
);
68 if (DIFF_OPT_TST(opt
, TREE_IN_RECURSIVE
)) {
69 newbase
[baselen
+ pathlen1
] = 0;
70 opt
->change(opt
, mode1
, mode2
,
71 sha1
, sha2
, newbase
, 0, 0);
72 newbase
[baselen
+ pathlen1
] = '/';
74 retval
= diff_tree_sha1(sha1
, sha2
, newbase
, opt
);
79 fullname
= malloc_fullname(base
, baselen
, path1
, pathlen1
);
80 opt
->change(opt
, mode1
, mode2
, sha1
, sha2
, fullname
, 0, 0);
85 /* A whole sub-tree went away or appeared */
86 static void show_tree(struct diff_options
*opt
, const char *prefix
, struct tree_desc
*desc
, const char *base
, int baselen
)
88 int all_interesting
= 0;
95 show
= tree_entry_interesting(&desc
->entry
, base
, baselen
, &opt
->pathspec
);
102 show_entry(opt
, prefix
, desc
, base
, baselen
);
103 update_tree_entry(desc
);
107 /* A file entry went away or appeared */
108 static void show_entry(struct diff_options
*opt
, const char *prefix
, struct tree_desc
*desc
,
109 const char *base
, int baselen
)
113 const unsigned char *sha1
= tree_entry_extract(desc
, &path
, &mode
);
114 int pathlen
= tree_entry_len(path
, sha1
);
116 if (DIFF_OPT_TST(opt
, RECURSIVE
) && S_ISDIR(mode
)) {
117 enum object_type type
;
118 char *newbase
= malloc_base(base
, baselen
, path
, pathlen
);
119 struct tree_desc inner
;
123 tree
= read_sha1_file(sha1
, &type
, &size
);
124 if (!tree
|| type
!= OBJ_TREE
)
125 die("corrupt tree sha %s", sha1_to_hex(sha1
));
127 if (DIFF_OPT_TST(opt
, TREE_IN_RECURSIVE
)) {
128 newbase
[baselen
+ pathlen
] = 0;
129 opt
->add_remove(opt
, *prefix
, mode
, sha1
, newbase
, 0);
130 newbase
[baselen
+ pathlen
] = '/';
133 init_tree_desc(&inner
, tree
, size
);
134 show_tree(opt
, prefix
, &inner
, newbase
, baselen
+ 1 + pathlen
);
139 char *fullname
= malloc_fullname(base
, baselen
, path
, pathlen
);
140 opt
->add_remove(opt
, prefix
[0], mode
, sha1
, fullname
, 0);
145 static void skip_uninteresting(struct tree_desc
*t
, const char *base
, int baselen
, struct diff_options
*opt
, int *all_interesting
)
148 int show
= tree_entry_interesting(&t
->entry
, base
, baselen
, &opt
->pathspec
);
150 *all_interesting
= 1;
152 update_tree_entry(t
);
162 int diff_tree(struct tree_desc
*t1
, struct tree_desc
*t2
, const char *base
, struct diff_options
*opt
)
164 int baselen
= strlen(base
);
165 int all_t1_interesting
= 0;
166 int all_t2_interesting
= 0;
169 if (DIFF_OPT_TST(opt
, QUICK
) &&
170 DIFF_OPT_TST(opt
, HAS_CHANGES
))
172 if (opt
->pathspec
.nr
) {
173 if (!all_t1_interesting
)
174 skip_uninteresting(t1
, base
, baselen
, opt
,
175 &all_t1_interesting
);
176 if (!all_t2_interesting
)
177 skip_uninteresting(t2
, base
, baselen
, opt
,
178 &all_t2_interesting
);
183 show_entry(opt
, "+", t2
, base
, baselen
);
184 update_tree_entry(t2
);
188 show_entry(opt
, "-", t1
, base
, baselen
);
189 update_tree_entry(t1
);
192 switch (compare_tree_entry(t1
, t2
, base
, baselen
, opt
)) {
194 update_tree_entry(t1
);
197 update_tree_entry(t1
);
200 update_tree_entry(t2
);
203 die("git diff-tree: internal error");
209 * Does it look like the resulting diff might be due to a rename?
211 * - not a valid previous file
213 static inline int diff_might_be_rename(void)
215 return diff_queued_diff
.nr
== 1 &&
216 !DIFF_FILE_VALID(diff_queued_diff
.queue
[0]->one
);
219 static void try_to_follow_renames(struct tree_desc
*t1
, struct tree_desc
*t2
, const char *base
, struct diff_options
*opt
)
221 struct diff_options diff_opts
;
222 struct diff_queue_struct
*q
= &diff_queued_diff
;
223 struct diff_filepair
*choice
;
224 const char *paths
[1];
227 /* Remove the file creation entry from the diff queue, and remember it */
228 choice
= q
->queue
[0];
231 diff_setup(&diff_opts
);
232 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
233 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
234 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
235 diff_opts
.single_follow
= opt
->pathspec
.raw
[0];
236 diff_opts
.break_opt
= opt
->break_opt
;
238 diff_tree_setup_paths(paths
, &diff_opts
);
239 if (diff_setup_done(&diff_opts
) < 0)
240 die("unable to set up diff options to follow renames");
241 diff_tree(t1
, t2
, base
, &diff_opts
);
242 diffcore_std(&diff_opts
);
243 diff_tree_release_paths(&diff_opts
);
245 /* Go through the new set of filepairing, and see if we find a more interesting one */
246 opt
->found_follow
= 0;
247 for (i
= 0; i
< q
->nr
; i
++) {
248 struct diff_filepair
*p
= q
->queue
[i
];
251 * Found a source? Not only do we use that for the new
252 * diff_queued_diff, we will also use that as the path in
255 if ((p
->status
== 'R' || p
->status
== 'C') &&
256 !strcmp(p
->two
->path
, opt
->pathspec
.raw
[0])) {
257 /* Switch the file-pairs around */
258 q
->queue
[i
] = choice
;
261 /* Update the path we use from now on.. */
262 diff_tree_release_paths(opt
);
263 opt
->pathspec
.raw
[0] = xstrdup(p
->one
->path
);
264 diff_tree_setup_paths(opt
->pathspec
.raw
, opt
);
267 * The caller expects us to return a set of vanilla
268 * filepairs to let a later call to diffcore_std()
269 * it makes to sort the renames out (among other
270 * things), but we already have found renames
271 * ourselves; signal diffcore_std() not to muck with
272 * rename information.
274 opt
->found_follow
= 1;
280 * Then, discard all the non-relevant file pairs...
282 for (i
= 0; i
< q
->nr
; i
++) {
283 struct diff_filepair
*p
= q
->queue
[i
];
284 diff_free_filepair(p
);
288 * .. and re-instate the one we want (which might be either the
289 * original one, or the rename/copy we found)
291 q
->queue
[0] = choice
;
295 int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
, struct diff_options
*opt
)
298 struct tree_desc t1
, t2
;
299 unsigned long size1
, size2
;
302 tree1
= read_object_with_reference(old
, tree_type
, &size1
, NULL
);
304 die("unable to read source tree (%s)", sha1_to_hex(old
));
305 tree2
= read_object_with_reference(new, tree_type
, &size2
, NULL
);
307 die("unable to read destination tree (%s)", sha1_to_hex(new));
308 init_tree_desc(&t1
, tree1
, size1
);
309 init_tree_desc(&t2
, tree2
, size2
);
310 retval
= diff_tree(&t1
, &t2
, base
, opt
);
311 if (!*base
&& DIFF_OPT_TST(opt
, FOLLOW_RENAMES
) && diff_might_be_rename()) {
312 init_tree_desc(&t1
, tree1
, size1
);
313 init_tree_desc(&t2
, tree2
, size2
);
314 try_to_follow_renames(&t1
, &t2
, base
, opt
);
321 int diff_root_tree_sha1(const unsigned char *new, const char *base
, struct diff_options
*opt
)
326 struct tree_desc empty
, real
;
328 tree
= read_object_with_reference(new, tree_type
, &size
, NULL
);
330 die("unable to read root tree (%s)", sha1_to_hex(new));
331 init_tree_desc(&real
, tree
, size
);
333 init_tree_desc(&empty
, "", 0);
334 retval
= diff_tree(&empty
, &real
, base
, opt
);
339 void diff_tree_release_paths(struct diff_options
*opt
)
341 free_pathspec(&opt
->pathspec
);
344 void diff_tree_setup_paths(const char **p
, struct diff_options
*opt
)
346 init_pathspec(&opt
->pathspec
, p
);