2 * Helper functions for tree diff generation
9 static void show_entry(struct diff_options
*opt
, const char *prefix
,
10 struct tree_desc
*desc
, struct strbuf
*base
);
12 static int compare_tree_entry(struct tree_desc
*t1
, struct tree_desc
*t2
,
13 struct strbuf
*base
, struct diff_options
*opt
)
15 unsigned mode1
, mode2
;
16 const char *path1
, *path2
;
17 const unsigned char *sha1
, *sha2
;
18 int cmp
, pathlen1
, pathlen2
;
19 int old_baselen
= base
->len
;
22 sha1
= tree_entry_extract(t1
, &path1
, &mode1
);
23 sha2
= tree_entry_extract(t2
, &path2
, &mode2
);
25 pathlen1
= tree_entry_len(path1
, sha1
);
26 pathlen2
= tree_entry_len(path2
, sha2
);
27 cmp
= base_name_compare(path1
, pathlen1
, mode1
, path2
, pathlen2
, mode2
);
29 show_entry(opt
, "-", t1
, base
);
33 show_entry(opt
, "+", t2
, base
);
36 if (!DIFF_OPT_TST(opt
, FIND_COPIES_HARDER
) && !hashcmp(sha1
, sha2
) && mode1
== mode2
)
40 * If the filemode has changed to/from a directory from/to a regular
41 * file, we need to consider it a remove and an add.
43 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
44 show_entry(opt
, "-", t1
, base
);
45 show_entry(opt
, "+", t2
, base
);
49 strbuf_add(base
, path1
, pathlen1
);
50 if (DIFF_OPT_TST(opt
, RECURSIVE
) && S_ISDIR(mode1
)) {
51 if (DIFF_OPT_TST(opt
, TREE_IN_RECURSIVE
)) {
52 opt
->change(opt
, mode1
, mode2
,
53 sha1
, sha2
, base
->buf
, 0, 0);
55 strbuf_addch(base
, '/');
56 retval
= diff_tree_sha1(sha1
, sha2
, base
->buf
, opt
);
58 opt
->change(opt
, mode1
, mode2
, sha1
, sha2
, base
->buf
, 0, 0);
60 strbuf_setlen(base
, old_baselen
);
64 /* A whole sub-tree went away or appeared */
65 static void show_tree(struct diff_options
*opt
, const char *prefix
,
66 struct tree_desc
*desc
, struct strbuf
*base
)
69 for (; desc
->size
; update_tree_entry(desc
)) {
71 match
= tree_entry_interesting(&desc
->entry
, base
, 0,
78 show_entry(opt
, prefix
, desc
, base
);
82 /* A file entry went away or appeared */
83 static void show_entry(struct diff_options
*opt
, const char *prefix
,
84 struct tree_desc
*desc
, struct strbuf
*base
)
88 const unsigned char *sha1
= tree_entry_extract(desc
, &path
, &mode
);
89 int pathlen
= tree_entry_len(path
, sha1
);
90 int old_baselen
= base
->len
;
92 strbuf_add(base
, path
, pathlen
);
93 if (DIFF_OPT_TST(opt
, RECURSIVE
) && S_ISDIR(mode
)) {
94 enum object_type type
;
95 struct tree_desc inner
;
99 tree
= read_sha1_file(sha1
, &type
, &size
);
100 if (!tree
|| type
!= OBJ_TREE
)
101 die("corrupt tree sha %s", sha1_to_hex(sha1
));
103 if (DIFF_OPT_TST(opt
, TREE_IN_RECURSIVE
))
104 opt
->add_remove(opt
, *prefix
, mode
, sha1
, base
->buf
, 0);
106 strbuf_addch(base
, '/');
108 init_tree_desc(&inner
, tree
, size
);
109 show_tree(opt
, prefix
, &inner
, base
);
112 opt
->add_remove(opt
, prefix
[0], mode
, sha1
, base
->buf
, 0);
114 strbuf_setlen(base
, old_baselen
);
117 static void skip_uninteresting(struct tree_desc
*t
, struct strbuf
*base
,
118 struct diff_options
*opt
, int *match
)
121 *match
= tree_entry_interesting(&t
->entry
, base
, 0, &opt
->pathspec
);
127 update_tree_entry(t
);
131 int diff_tree(struct tree_desc
*t1
, struct tree_desc
*t2
,
132 const char *base_str
, struct diff_options
*opt
)
135 int baselen
= strlen(base_str
);
136 int t1_match
= 0, t2_match
= 0;
138 /* Enable recursion indefinitely */
139 opt
->pathspec
.recursive
= DIFF_OPT_TST(opt
, RECURSIVE
);
140 opt
->pathspec
.max_depth
= -1;
142 strbuf_init(&base
, PATH_MAX
);
143 strbuf_add(&base
, base_str
, baselen
);
146 if (DIFF_OPT_TST(opt
, QUICK
) &&
147 DIFF_OPT_TST(opt
, HAS_CHANGES
))
149 if (opt
->pathspec
.nr
) {
150 skip_uninteresting(t1
, &base
, opt
, &t1_match
);
151 skip_uninteresting(t2
, &base
, opt
, &t2_match
);
156 show_entry(opt
, "+", t2
, &base
);
157 update_tree_entry(t2
);
161 show_entry(opt
, "-", t1
, &base
);
162 update_tree_entry(t1
);
165 switch (compare_tree_entry(t1
, t2
, &base
, opt
)) {
167 update_tree_entry(t1
);
170 update_tree_entry(t1
);
173 update_tree_entry(t2
);
176 die("git diff-tree: internal error");
179 strbuf_release(&base
);
184 * Does it look like the resulting diff might be due to a rename?
186 * - not a valid previous file
188 static inline int diff_might_be_rename(void)
190 return diff_queued_diff
.nr
== 1 &&
191 !DIFF_FILE_VALID(diff_queued_diff
.queue
[0]->one
);
194 static void try_to_follow_renames(struct tree_desc
*t1
, struct tree_desc
*t2
, const char *base
, struct diff_options
*opt
)
196 struct diff_options diff_opts
;
197 struct diff_queue_struct
*q
= &diff_queued_diff
;
198 struct diff_filepair
*choice
;
199 const char *paths
[1];
202 /* Remove the file creation entry from the diff queue, and remember it */
203 choice
= q
->queue
[0];
206 diff_setup(&diff_opts
);
207 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
208 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
209 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
210 diff_opts
.single_follow
= opt
->pathspec
.raw
[0];
211 diff_opts
.break_opt
= opt
->break_opt
;
213 diff_tree_setup_paths(paths
, &diff_opts
);
214 if (diff_setup_done(&diff_opts
) < 0)
215 die("unable to set up diff options to follow renames");
216 diff_tree(t1
, t2
, base
, &diff_opts
);
217 diffcore_std(&diff_opts
);
218 diff_tree_release_paths(&diff_opts
);
220 /* Go through the new set of filepairing, and see if we find a more interesting one */
221 opt
->found_follow
= 0;
222 for (i
= 0; i
< q
->nr
; i
++) {
223 struct diff_filepair
*p
= q
->queue
[i
];
226 * Found a source? Not only do we use that for the new
227 * diff_queued_diff, we will also use that as the path in
230 if ((p
->status
== 'R' || p
->status
== 'C') &&
231 !strcmp(p
->two
->path
, opt
->pathspec
.raw
[0])) {
232 /* Switch the file-pairs around */
233 q
->queue
[i
] = choice
;
236 /* Update the path we use from now on.. */
237 diff_tree_release_paths(opt
);
238 opt
->pathspec
.raw
[0] = xstrdup(p
->one
->path
);
239 diff_tree_setup_paths(opt
->pathspec
.raw
, opt
);
242 * The caller expects us to return a set of vanilla
243 * filepairs to let a later call to diffcore_std()
244 * it makes to sort the renames out (among other
245 * things), but we already have found renames
246 * ourselves; signal diffcore_std() not to muck with
247 * rename information.
249 opt
->found_follow
= 1;
255 * Then, discard all the non-relevant file pairs...
257 for (i
= 0; i
< q
->nr
; i
++) {
258 struct diff_filepair
*p
= q
->queue
[i
];
259 diff_free_filepair(p
);
263 * .. and re-instate the one we want (which might be either the
264 * original one, or the rename/copy we found)
266 q
->queue
[0] = choice
;
270 int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
, struct diff_options
*opt
)
273 struct tree_desc t1
, t2
;
274 unsigned long size1
, size2
;
277 tree1
= read_object_with_reference(old
, tree_type
, &size1
, NULL
);
279 die("unable to read source tree (%s)", sha1_to_hex(old
));
280 tree2
= read_object_with_reference(new, tree_type
, &size2
, NULL
);
282 die("unable to read destination tree (%s)", sha1_to_hex(new));
283 init_tree_desc(&t1
, tree1
, size1
);
284 init_tree_desc(&t2
, tree2
, size2
);
285 retval
= diff_tree(&t1
, &t2
, base
, opt
);
286 if (!*base
&& DIFF_OPT_TST(opt
, FOLLOW_RENAMES
) && diff_might_be_rename()) {
287 init_tree_desc(&t1
, tree1
, size1
);
288 init_tree_desc(&t2
, tree2
, size2
);
289 try_to_follow_renames(&t1
, &t2
, base
, opt
);
296 int diff_root_tree_sha1(const unsigned char *new, const char *base
, struct diff_options
*opt
)
301 struct tree_desc empty
, real
;
303 tree
= read_object_with_reference(new, tree_type
, &size
, NULL
);
305 die("unable to read root tree (%s)", sha1_to_hex(new));
306 init_tree_desc(&real
, tree
, size
);
308 init_tree_desc(&empty
, "", 0);
309 retval
= diff_tree(&empty
, &real
, base
, opt
);
314 void diff_tree_release_paths(struct diff_options
*opt
)
316 free_pathspec(&opt
->pathspec
);
319 void diff_tree_setup_paths(const char **p
, struct diff_options
*opt
)
321 init_pathspec(&opt
->pathspec
, p
);