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
,
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
);
86 * Is a tree entry interesting given the pathspec we have?
89 * - 2 for "yes, and all subsequent entries will be"
92 * - negative for "no, and no subsequent entries will be either"
94 static int tree_entry_interesting(struct tree_desc
*desc
, const char *base
, int baselen
, struct diff_options
*opt
)
97 const unsigned char *sha1
;
101 int never_interesting
= -1;
106 sha1
= tree_entry_extract(desc
, &path
, &mode
);
108 pathlen
= tree_entry_len(path
, sha1
);
110 for (i
= 0; i
< opt
->nr_paths
; i
++) {
111 const char *match
= opt
->paths
[i
];
112 int matchlen
= opt
->pathlens
[i
];
113 int m
= -1; /* signals that we haven't called strncmp() */
115 if (baselen
>= matchlen
) {
116 /* If it doesn't match, move along... */
117 if (strncmp(base
, match
, matchlen
))
121 * If the base is a subdirectory of a path which
122 * was specified, all of them are interesting.
125 base
[matchlen
] == '/' ||
126 match
[matchlen
- 1] == '/')
129 /* Just a random prefix match */
133 /* Does the base match? */
134 if (strncmp(base
, match
, baselen
))
140 if (never_interesting
) {
142 * We have not seen any match that sorts later
143 * than the current path.
147 * Does match sort strictly earlier than path
148 * with their common parts?
150 m
= strncmp(match
, path
,
151 (matchlen
< pathlen
) ? matchlen
: pathlen
);
156 * If we come here even once, that means there is at
157 * least one pathspec that would sort equal to or
158 * later than the path we are currently looking at.
159 * In other words, if we have never reached this point
160 * after iterating all pathspecs, it means all
161 * pathspecs are either outside of base, or inside the
162 * base but sorts strictly earlier than the current
163 * one. In either case, they will never match the
164 * subsequent entries. In such a case, we initialized
165 * the variable to -1 and that is what will be
166 * returned, allowing the caller to terminate early.
168 never_interesting
= 0;
171 if (pathlen
> matchlen
)
174 if (matchlen
> pathlen
) {
175 if (match
[pathlen
] != '/')
183 * we cheated and did not do strncmp(), so we do
186 m
= strncmp(match
, path
, pathlen
);
189 * If common part matched earlier then it is a hit,
190 * because we rejected the case where path is not a
191 * leading directory and is shorter than match.
196 return never_interesting
; /* No matches */
199 /* A whole sub-tree went away or appeared */
200 static void show_tree(struct diff_options
*opt
, const char *prefix
, struct tree_desc
*desc
, const char *base
, int baselen
)
202 int all_interesting
= 0;
209 show
= tree_entry_interesting(desc
, base
, baselen
,
217 show_entry(opt
, prefix
, desc
, base
, baselen
);
218 update_tree_entry(desc
);
222 /* A file entry went away or appeared */
223 static void show_entry(struct diff_options
*opt
, const char *prefix
, struct tree_desc
*desc
,
224 const char *base
, int baselen
)
228 const unsigned char *sha1
= tree_entry_extract(desc
, &path
, &mode
);
229 int pathlen
= tree_entry_len(path
, sha1
);
231 if (DIFF_OPT_TST(opt
, RECURSIVE
) && S_ISDIR(mode
)) {
232 enum object_type type
;
233 char *newbase
= malloc_base(base
, baselen
, path
, pathlen
);
234 struct tree_desc inner
;
238 tree
= read_sha1_file(sha1
, &type
, &size
);
239 if (!tree
|| type
!= OBJ_TREE
)
240 die("corrupt tree sha %s", sha1_to_hex(sha1
));
242 if (DIFF_OPT_TST(opt
, TREE_IN_RECURSIVE
)) {
243 newbase
[baselen
+ pathlen
] = 0;
244 opt
->add_remove(opt
, *prefix
, mode
, sha1
, newbase
);
245 newbase
[baselen
+ pathlen
] = '/';
248 init_tree_desc(&inner
, tree
, size
);
249 show_tree(opt
, prefix
, &inner
, newbase
, baselen
+ 1 + pathlen
);
254 char *fullname
= malloc_fullname(base
, baselen
, path
, pathlen
);
255 opt
->add_remove(opt
, prefix
[0], mode
, sha1
, fullname
);
260 static void skip_uninteresting(struct tree_desc
*t
, const char *base
, int baselen
, struct diff_options
*opt
)
262 int all_interesting
= 0;
269 show
= tree_entry_interesting(t
, base
, baselen
, opt
);
274 update_tree_entry(t
);
284 int diff_tree(struct tree_desc
*t1
, struct tree_desc
*t2
, const char *base
, struct diff_options
*opt
)
286 int baselen
= strlen(base
);
289 if (DIFF_OPT_TST(opt
, QUIET
) && DIFF_OPT_TST(opt
, HAS_CHANGES
))
292 skip_uninteresting(t1
, base
, baselen
, opt
);
293 skip_uninteresting(t2
, base
, baselen
, opt
);
298 show_entry(opt
, "+", t2
, base
, baselen
);
299 update_tree_entry(t2
);
303 show_entry(opt
, "-", t1
, base
, baselen
);
304 update_tree_entry(t1
);
307 switch (compare_tree_entry(t1
, t2
, base
, baselen
, opt
)) {
309 update_tree_entry(t1
);
312 update_tree_entry(t1
);
315 update_tree_entry(t2
);
318 die("git diff-tree: internal error");
324 * Does it look like the resulting diff might be due to a rename?
326 * - not a valid previous file
328 static inline int diff_might_be_rename(void)
330 return diff_queued_diff
.nr
== 1 &&
331 !DIFF_FILE_VALID(diff_queued_diff
.queue
[0]->one
);
334 static void try_to_follow_renames(struct tree_desc
*t1
, struct tree_desc
*t2
, const char *base
, struct diff_options
*opt
)
336 struct diff_options diff_opts
;
337 struct diff_queue_struct
*q
= &diff_queued_diff
;
338 struct diff_filepair
*choice
;
339 const char *paths
[1];
342 /* Remove the file creation entry from the diff queue, and remember it */
343 choice
= q
->queue
[0];
346 diff_setup(&diff_opts
);
347 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
348 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
349 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
350 diff_opts
.single_follow
= opt
->paths
[0];
351 diff_opts
.break_opt
= opt
->break_opt
;
353 diff_tree_setup_paths(paths
, &diff_opts
);
354 if (diff_setup_done(&diff_opts
) < 0)
355 die("unable to set up diff options to follow renames");
356 diff_tree(t1
, t2
, base
, &diff_opts
);
357 diffcore_std(&diff_opts
);
358 diff_tree_release_paths(&diff_opts
);
360 /* Go through the new set of filepairing, and see if we find a more interesting one */
361 for (i
= 0; i
< q
->nr
; i
++) {
362 struct diff_filepair
*p
= q
->queue
[i
];
365 * Found a source? Not only do we use that for the new
366 * diff_queued_diff, we will also use that as the path in
369 if ((p
->status
== 'R' || p
->status
== 'C') && !strcmp(p
->two
->path
, opt
->paths
[0])) {
370 /* Switch the file-pairs around */
371 q
->queue
[i
] = choice
;
374 /* Update the path we use from now on.. */
375 diff_tree_release_paths(opt
);
376 opt
->paths
[0] = xstrdup(p
->one
->path
);
377 diff_tree_setup_paths(opt
->paths
, opt
);
383 * Then, discard all the non-relevant file pairs...
385 for (i
= 0; i
< q
->nr
; i
++) {
386 struct diff_filepair
*p
= q
->queue
[i
];
387 diff_free_filepair(p
);
391 * .. and re-instate the one we want (which might be either the
392 * original one, or the rename/copy we found)
394 q
->queue
[0] = choice
;
398 int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
, struct diff_options
*opt
)
401 struct tree_desc t1
, t2
;
402 unsigned long size1
, size2
;
405 tree1
= read_object_with_reference(old
, tree_type
, &size1
, NULL
);
407 die("unable to read source tree (%s)", sha1_to_hex(old
));
408 tree2
= read_object_with_reference(new, tree_type
, &size2
, NULL
);
410 die("unable to read destination tree (%s)", sha1_to_hex(new));
411 init_tree_desc(&t1
, tree1
, size1
);
412 init_tree_desc(&t2
, tree2
, size2
);
413 retval
= diff_tree(&t1
, &t2
, base
, opt
);
414 if (DIFF_OPT_TST(opt
, FOLLOW_RENAMES
) && diff_might_be_rename()) {
415 init_tree_desc(&t1
, tree1
, size1
);
416 init_tree_desc(&t2
, tree2
, size2
);
417 try_to_follow_renames(&t1
, &t2
, base
, opt
);
424 int diff_root_tree_sha1(const unsigned char *new, const char *base
, struct diff_options
*opt
)
429 struct tree_desc empty
, real
;
431 tree
= read_object_with_reference(new, tree_type
, &size
, NULL
);
433 die("unable to read root tree (%s)", sha1_to_hex(new));
434 init_tree_desc(&real
, tree
, size
);
436 init_tree_desc(&empty
, "", 0);
437 retval
= diff_tree(&empty
, &real
, base
, opt
);
442 static int count_paths(const char **paths
)
450 void diff_tree_release_paths(struct diff_options
*opt
)
455 void diff_tree_setup_paths(const char **p
, struct diff_options
*opt
)
458 opt
->pathlens
= NULL
;
465 opt
->nr_paths
= count_paths(p
);
466 if (opt
->nr_paths
== 0) {
467 opt
->pathlens
= NULL
;
470 opt
->pathlens
= xmalloc(opt
->nr_paths
* sizeof(int));
471 for (i
=0; i
< opt
->nr_paths
; i
++)
472 opt
->pathlens
[i
] = strlen(p
[i
]);