2 * Helper functions for tree diff generation
10 * Compare two tree entries, taking into account only path/S_ISDIR(mode),
11 * but not their sha1's.
13 * NOTE files and directories *always* compare differently, even when having
14 * the same name - thanks to base_name_compare().
16 static int tree_entry_pathcmp(struct tree_desc
*t1
, struct tree_desc
*t2
)
18 struct name_entry
*e1
, *e2
;
23 cmp
= base_name_compare(e1
->path
, tree_entry_len(e1
), e1
->mode
,
24 e2
->path
, tree_entry_len(e2
), e2
->mode
);
29 /* convert path, t1/t2 -> opt->diff_*() callbacks */
30 static void emit_diff(struct diff_options
*opt
, struct strbuf
*path
,
31 struct tree_desc
*t1
, struct tree_desc
*t2
)
33 unsigned int mode1
= t1
? t1
->entry
.mode
: 0;
34 unsigned int mode2
= t2
? t2
->entry
.mode
: 0;
37 opt
->change(opt
, mode1
, mode2
, t1
->entry
.sha1
, t2
->entry
.sha1
,
38 1, 1, path
->buf
, 0, 0);
41 const unsigned char *sha1
;
47 sha1
= t2
->entry
.sha1
;
51 sha1
= t1
->entry
.sha1
;
55 opt
->add_remove(opt
, addremove
, mode
, sha1
, 1, path
->buf
, 0);
60 /* new path should be added to diff
62 * 3 cases on how/when it should be called and behaves:
64 * !t1, t2 -> path added, parent lacks it
65 * t1, !t2 -> path removed from parent
66 * t1, t2 -> path modified
68 static void show_path(struct strbuf
*base
, struct diff_options
*opt
,
69 struct tree_desc
*t1
, struct tree_desc
*t2
)
74 int old_baselen
= base
->len
;
75 int isdir
, recurse
= 0, emitthis
= 1;
77 /* at least something has to be valid */
81 /* path present in resulting tree */
82 tree_entry_extract(t2
, &path
, &mode
);
83 pathlen
= tree_entry_len(&t2
->entry
);
84 isdir
= S_ISDIR(mode
);
87 * a path was removed - take path from parent. Also take
88 * mode from parent, to decide on recursion.
90 tree_entry_extract(t1
, &path
, &mode
);
91 pathlen
= tree_entry_len(&t1
->entry
);
93 isdir
= S_ISDIR(mode
);
97 if (DIFF_OPT_TST(opt
, RECURSIVE
) && isdir
) {
99 emitthis
= DIFF_OPT_TST(opt
, TREE_IN_RECURSIVE
);
102 strbuf_add(base
, path
, pathlen
);
105 emit_diff(opt
, base
, t1
, t2
);
108 strbuf_addch(base
, '/');
109 diff_tree_sha1(t1
? t1
->entry
.sha1
: NULL
,
110 t2
? t2
->entry
.sha1
: NULL
, base
->buf
, opt
);
113 strbuf_setlen(base
, old_baselen
);
116 static void skip_uninteresting(struct tree_desc
*t
, struct strbuf
*base
,
117 struct diff_options
*opt
)
119 enum interesting match
;
122 match
= tree_entry_interesting(&t
->entry
, base
, 0, &opt
->pathspec
);
124 if (match
== all_entries_not_interesting
)
128 update_tree_entry(t
);
132 int diff_tree(struct tree_desc
*t1
, struct tree_desc
*t2
,
133 const char *base_str
, struct diff_options
*opt
)
136 int baselen
= strlen(base_str
);
138 /* Enable recursion indefinitely */
139 opt
->pathspec
.recursive
= DIFF_OPT_TST(opt
, RECURSIVE
);
141 strbuf_init(&base
, PATH_MAX
);
142 strbuf_add(&base
, base_str
, baselen
);
147 if (diff_can_quit_early(opt
))
149 if (opt
->pathspec
.nr
) {
150 skip_uninteresting(t1
, &base
, opt
);
151 skip_uninteresting(t2
, &base
, opt
);
156 show_path(&base
, opt
, /*t1=*/NULL
, t2
);
157 update_tree_entry(t2
);
161 show_path(&base
, opt
, t1
, /*t2=*/NULL
);
162 update_tree_entry(t1
);
166 cmp
= tree_entry_pathcmp(t1
, t2
);
170 if (DIFF_OPT_TST(opt
, FIND_COPIES_HARDER
) ||
171 hashcmp(t1
->entry
.sha1
, t2
->entry
.sha1
) ||
172 (t1
->entry
.mode
!= t2
->entry
.mode
))
173 show_path(&base
, opt
, t1
, t2
);
175 update_tree_entry(t1
);
176 update_tree_entry(t2
);
181 show_path(&base
, opt
, t1
, /*t2=*/NULL
);
182 update_tree_entry(t1
);
187 show_path(&base
, opt
, /*t1=*/NULL
, t2
);
188 update_tree_entry(t2
);
192 strbuf_release(&base
);
197 * Does it look like the resulting diff might be due to a rename?
199 * - not a valid previous file
201 static inline int diff_might_be_rename(void)
203 return diff_queued_diff
.nr
== 1 &&
204 !DIFF_FILE_VALID(diff_queued_diff
.queue
[0]->one
);
207 static void try_to_follow_renames(struct tree_desc
*t1
, struct tree_desc
*t2
, const char *base
, struct diff_options
*opt
)
209 struct diff_options diff_opts
;
210 struct diff_queue_struct
*q
= &diff_queued_diff
;
211 struct diff_filepair
*choice
;
215 * follow-rename code is very specific, we need exactly one
216 * path. Magic that matches more than one path is not
219 GUARD_PATHSPEC(&opt
->pathspec
, PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
);
222 * We should reject wildcards as well. Unfortunately we
223 * haven't got a reliable way to detect that 'foo\*bar' in
224 * fact has no wildcards. nowildcard_len is merely a hint for
225 * optimization. Let it slip for now until wildmatch is taught
226 * about dry-run mode and returns wildcard info.
228 if (opt
->pathspec
.has_wildcard
)
229 die("BUG:%s:%d: wildcards are not supported",
233 /* Remove the file creation entry from the diff queue, and remember it */
234 choice
= q
->queue
[0];
237 diff_setup(&diff_opts
);
238 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
239 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
240 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
241 diff_opts
.single_follow
= opt
->pathspec
.items
[0].match
;
242 diff_opts
.break_opt
= opt
->break_opt
;
243 diff_opts
.rename_score
= opt
->rename_score
;
244 diff_setup_done(&diff_opts
);
245 diff_tree(t1
, t2
, base
, &diff_opts
);
246 diffcore_std(&diff_opts
);
247 free_pathspec(&diff_opts
.pathspec
);
249 /* Go through the new set of filepairing, and see if we find a more interesting one */
250 opt
->found_follow
= 0;
251 for (i
= 0; i
< q
->nr
; i
++) {
252 struct diff_filepair
*p
= q
->queue
[i
];
255 * Found a source? Not only do we use that for the new
256 * diff_queued_diff, we will also use that as the path in
259 if ((p
->status
== 'R' || p
->status
== 'C') &&
260 !strcmp(p
->two
->path
, opt
->pathspec
.items
[0].match
)) {
263 /* Switch the file-pairs around */
264 q
->queue
[i
] = choice
;
267 /* Update the path we use from now on.. */
268 path
[0] = p
->one
->path
;
270 free_pathspec(&opt
->pathspec
);
271 parse_pathspec(&opt
->pathspec
,
272 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
273 PATHSPEC_LITERAL_PATH
, "", path
);
276 * The caller expects us to return a set of vanilla
277 * filepairs to let a later call to diffcore_std()
278 * it makes to sort the renames out (among other
279 * things), but we already have found renames
280 * ourselves; signal diffcore_std() not to muck with
281 * rename information.
283 opt
->found_follow
= 1;
289 * Then, discard all the non-relevant file pairs...
291 for (i
= 0; i
< q
->nr
; i
++) {
292 struct diff_filepair
*p
= q
->queue
[i
];
293 diff_free_filepair(p
);
297 * .. and re-instate the one we want (which might be either the
298 * original one, or the rename/copy we found)
300 q
->queue
[0] = choice
;
304 int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
, struct diff_options
*opt
)
307 struct tree_desc t1
, t2
;
308 unsigned long size1
, size2
;
311 tree1
= fill_tree_descriptor(&t1
, old
);
312 tree2
= fill_tree_descriptor(&t2
, new);
315 retval
= diff_tree(&t1
, &t2
, base
, opt
);
316 if (!*base
&& DIFF_OPT_TST(opt
, FOLLOW_RENAMES
) && diff_might_be_rename()) {
317 init_tree_desc(&t1
, tree1
, size1
);
318 init_tree_desc(&t2
, tree2
, size2
);
319 try_to_follow_renames(&t1
, &t2
, base
, opt
);
326 int diff_root_tree_sha1(const unsigned char *new, const char *base
, struct diff_options
*opt
)
328 return diff_tree_sha1(NULL
, new, base
, opt
);