2 * Helper functions for tree diff generation
9 static void show_path(struct strbuf
*base
, struct diff_options
*opt
,
10 struct tree_desc
*t1
, struct tree_desc
*t2
);
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
;
20 sha1
= tree_entry_extract(t1
, &path1
, &mode1
);
21 sha2
= tree_entry_extract(t2
, &path2
, &mode2
);
23 pathlen1
= tree_entry_len(&t1
->entry
);
24 pathlen2
= tree_entry_len(&t2
->entry
);
27 * NOTE files and directories *always* compare differently,
28 * even when having the same name.
30 cmp
= base_name_compare(path1
, pathlen1
, mode1
, path2
, pathlen2
, mode2
);
32 show_path(base
, opt
, t1
, /*t2=*/NULL
);
36 show_path(base
, opt
, /*t1=*/NULL
, t2
);
39 if (!DIFF_OPT_TST(opt
, FIND_COPIES_HARDER
) && !hashcmp(sha1
, sha2
) && mode1
== mode2
)
42 show_path(base
, opt
, t1
, t2
);
47 /* convert path, t1/t2 -> opt->diff_*() callbacks */
48 static void emit_diff(struct diff_options
*opt
, struct strbuf
*path
,
49 struct tree_desc
*t1
, struct tree_desc
*t2
)
51 unsigned int mode1
= t1
? t1
->entry
.mode
: 0;
52 unsigned int mode2
= t2
? t2
->entry
.mode
: 0;
55 opt
->change(opt
, mode1
, mode2
, t1
->entry
.sha1
, t2
->entry
.sha1
,
56 1, 1, path
->buf
, 0, 0);
59 const unsigned char *sha1
;
65 sha1
= t2
->entry
.sha1
;
69 sha1
= t1
->entry
.sha1
;
73 opt
->add_remove(opt
, addremove
, mode
, sha1
, 1, path
->buf
, 0);
78 /* new path should be added to diff
80 * 3 cases on how/when it should be called and behaves:
82 * !t1, t2 -> path added, parent lacks it
83 * t1, !t2 -> path removed from parent
84 * t1, t2 -> path modified
86 static void show_path(struct strbuf
*base
, struct diff_options
*opt
,
87 struct tree_desc
*t1
, struct tree_desc
*t2
)
92 int old_baselen
= base
->len
;
93 int isdir
, recurse
= 0, emitthis
= 1;
95 /* at least something has to be valid */
99 /* path present in resulting tree */
100 tree_entry_extract(t2
, &path
, &mode
);
101 pathlen
= tree_entry_len(&t2
->entry
);
102 isdir
= S_ISDIR(mode
);
105 * a path was removed - take path from parent. Also take
106 * mode from parent, to decide on recursion.
108 tree_entry_extract(t1
, &path
, &mode
);
109 pathlen
= tree_entry_len(&t1
->entry
);
111 isdir
= S_ISDIR(mode
);
115 if (DIFF_OPT_TST(opt
, RECURSIVE
) && isdir
) {
117 emitthis
= DIFF_OPT_TST(opt
, TREE_IN_RECURSIVE
);
120 strbuf_add(base
, path
, pathlen
);
123 emit_diff(opt
, base
, t1
, t2
);
126 strbuf_addch(base
, '/');
127 diff_tree_sha1(t1
? t1
->entry
.sha1
: NULL
,
128 t2
? t2
->entry
.sha1
: NULL
, base
->buf
, opt
);
131 strbuf_setlen(base
, old_baselen
);
134 static void skip_uninteresting(struct tree_desc
*t
, struct strbuf
*base
,
135 struct diff_options
*opt
)
137 enum interesting match
;
140 match
= tree_entry_interesting(&t
->entry
, base
, 0, &opt
->pathspec
);
142 if (match
== all_entries_not_interesting
)
146 update_tree_entry(t
);
150 int diff_tree(struct tree_desc
*t1
, struct tree_desc
*t2
,
151 const char *base_str
, struct diff_options
*opt
)
154 int baselen
= strlen(base_str
);
156 /* Enable recursion indefinitely */
157 opt
->pathspec
.recursive
= DIFF_OPT_TST(opt
, RECURSIVE
);
159 strbuf_init(&base
, PATH_MAX
);
160 strbuf_add(&base
, base_str
, baselen
);
163 if (diff_can_quit_early(opt
))
165 if (opt
->pathspec
.nr
) {
166 skip_uninteresting(t1
, &base
, opt
);
167 skip_uninteresting(t2
, &base
, opt
);
172 show_path(&base
, opt
, /*t1=*/NULL
, t2
);
173 update_tree_entry(t2
);
177 show_path(&base
, opt
, t1
, /*t2=*/NULL
);
178 update_tree_entry(t1
);
182 cmp
= compare_tree_entry(t1
, t2
, &base
, opt
);
186 update_tree_entry(t1
);
187 update_tree_entry(t2
);
192 update_tree_entry(t1
);
197 update_tree_entry(t2
);
201 strbuf_release(&base
);
206 * Does it look like the resulting diff might be due to a rename?
208 * - not a valid previous file
210 static inline int diff_might_be_rename(void)
212 return diff_queued_diff
.nr
== 1 &&
213 !DIFF_FILE_VALID(diff_queued_diff
.queue
[0]->one
);
216 static void try_to_follow_renames(struct tree_desc
*t1
, struct tree_desc
*t2
, const char *base
, struct diff_options
*opt
)
218 struct diff_options diff_opts
;
219 struct diff_queue_struct
*q
= &diff_queued_diff
;
220 struct diff_filepair
*choice
;
224 * follow-rename code is very specific, we need exactly one
225 * path. Magic that matches more than one path is not
228 GUARD_PATHSPEC(&opt
->pathspec
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
231 * We should reject wildcards as well. Unfortunately we
232 * haven't got a reliable way to detect that 'foo\*bar' in
233 * fact has no wildcards. nowildcard_len is merely a hint for
234 * optimization. Let it slip for now until wildmatch is taught
235 * about dry-run mode and returns wildcard info.
237 if (opt
->pathspec
.has_wildcard
)
238 die("BUG:%s:%d: wildcards are not supported",
242 /* Remove the file creation entry from the diff queue, and remember it */
243 choice
= q
->queue
[0];
246 diff_setup(&diff_opts
);
247 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
248 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
249 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
250 diff_opts
.single_follow
= opt
->pathspec
.items
[0].match
;
251 diff_opts
.break_opt
= opt
->break_opt
;
252 diff_opts
.rename_score
= opt
->rename_score
;
253 diff_setup_done(&diff_opts
);
254 diff_tree(t1
, t2
, base
, &diff_opts
);
255 diffcore_std(&diff_opts
);
256 free_pathspec(&diff_opts
.pathspec
);
258 /* Go through the new set of filepairing, and see if we find a more interesting one */
259 opt
->found_follow
= 0;
260 for (i
= 0; i
< q
->nr
; i
++) {
261 struct diff_filepair
*p
= q
->queue
[i
];
264 * Found a source? Not only do we use that for the new
265 * diff_queued_diff, we will also use that as the path in
268 if ((p
->status
== 'R' || p
->status
== 'C') &&
269 !strcmp(p
->two
->path
, opt
->pathspec
.items
[0].match
)) {
272 /* Switch the file-pairs around */
273 q
->queue
[i
] = choice
;
276 /* Update the path we use from now on.. */
277 path
[0] = p
->one
->path
;
279 free_pathspec(&opt
->pathspec
);
280 parse_pathspec(&opt
->pathspec
,
281 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
282 PATHSPEC_LITERAL_PATH
, "", path
);
285 * The caller expects us to return a set of vanilla
286 * filepairs to let a later call to diffcore_std()
287 * it makes to sort the renames out (among other
288 * things), but we already have found renames
289 * ourselves; signal diffcore_std() not to muck with
290 * rename information.
292 opt
->found_follow
= 1;
298 * Then, discard all the non-relevant file pairs...
300 for (i
= 0; i
< q
->nr
; i
++) {
301 struct diff_filepair
*p
= q
->queue
[i
];
302 diff_free_filepair(p
);
306 * .. and re-instate the one we want (which might be either the
307 * original one, or the rename/copy we found)
309 q
->queue
[0] = choice
;
313 int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
, struct diff_options
*opt
)
316 struct tree_desc t1
, t2
;
317 unsigned long size1
, size2
;
320 tree1
= fill_tree_descriptor(&t1
, old
);
321 tree2
= fill_tree_descriptor(&t2
, new);
324 retval
= diff_tree(&t1
, &t2
, base
, opt
);
325 if (!*base
&& DIFF_OPT_TST(opt
, FOLLOW_RENAMES
) && diff_might_be_rename()) {
326 init_tree_desc(&t1
, tree1
, size1
);
327 init_tree_desc(&t2
, tree2
, size2
);
328 try_to_follow_renames(&t1
, &t2
, base
, opt
);
335 int diff_root_tree_sha1(const unsigned char *new, const char *base
, struct diff_options
*opt
)
337 return diff_tree_sha1(NULL
, new, base
, opt
);