Commit d958e37a by Russell Belfer

Fix issues with git_diff_find_similar

There are a number of bugs in the rename code that only were
obvious when I started testing it against large old repos with
more complex patterns.  (The code to do that testing is not ready
to merge with libgit2, but I do plan to add more thorough tests.)

This contains a significant number of changes and also tweaks the
public API slightly to make emulating core git easier.

Most notably, this separates the GIT_DIFF_FIND_AND_BREAK_REWRITES
flag into FIND_REWRITES (which adds a self-similarity score to
every modified file) and BREAK_REWRITES (which splits the modified
deltas into add/remove pairs in the diff list).  When you do a raw
output of core git, rewrites show up as M090 or such, not at A and
D output, so I wanted to be able to emulate that.

Publicly, this also changes the flags to be uint16_t since we
don't need values out of that range.

Internally, this contains significant changes from a number of
small bug fixes (like using the wrong side of the diff to decide
if the object could be found in the ODB vs the workdir) to larger
issues about which files can and should be compared and how the
various edge cases of similarity scores should be treated.

Honestly, I don't think this is the last update that will have to
be made to this code, but I think this moves us closer to correct
behavior and I tried to document the code so it would be easier
to follow..
parent 0293450e
...@@ -243,6 +243,19 @@ typedef struct { ...@@ -243,6 +243,19 @@ typedef struct {
* `NOT_BINARY` flag set to avoid examining file contents if you do not pass * `NOT_BINARY` flag set to avoid examining file contents if you do not pass
* in hunk and/or line callbacks to the diff foreach iteration function. It * in hunk and/or line callbacks to the diff foreach iteration function. It
* will just use the git attributes for those files. * will just use the git attributes for those files.
*
* The similarity score is zero unless you call `git_diff_find_similar()`
* which does a similarity analysis of files in the diff. Use that
* function to do rename and copy detection, and to split heavily modified
* files in add/delete pairs. After that call, deltas with a status of
* GIT_DELTA_RENAMED or GIT_DELTA_COPIED will have a similarity score
* between 0 and 100 indicating how similar the old and new sides are.
*
* If you ask `git_diff_find_similar` to find heavily modified files to
* break, but to not *actually* break the records, then GIT_DELTA_MODIFIED
* records may have a non-zero similarity score if the self-similarity is
* below the split threshold. To display this value like core Git, invert
* the score (a la `printf("M%03d", 100 - delta->similarity)`).
*/ */
typedef struct { typedef struct {
git_diff_file old_file; git_diff_file old_file;
...@@ -408,18 +421,26 @@ typedef enum { ...@@ -408,18 +421,26 @@ typedef enum {
/** consider unmodified as copy sources? (`--find-copies-harder`) */ /** consider unmodified as copy sources? (`--find-copies-harder`) */
GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED = (1 << 3), GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED = (1 << 3),
/** split large rewrites into delete/add pairs (`--break-rewrites=/M`) */ /** mark large rewrites for split (`--break-rewrites=/M`) */
GIT_DIFF_FIND_AND_BREAK_REWRITES = (1 << 4), GIT_DIFF_FIND_REWRITES = (1 << 4),
/** actually split large rewrites into delete/add pairs */
GIT_DIFF_BREAK_REWRITES = (1 << 5),
/** mark rewrites for split and break into delete/add pairs */
GIT_DIFF_FIND_AND_BREAK_REWRITES =
(GIT_DIFF_FIND_REWRITES | GIT_DIFF_BREAK_REWRITES),
/** consider untracked files as rename/copy targets */
GIT_DIFF_FIND_FROM_UNTRACKED = (1 << 6),
/** turn on all finding features */ /** turn on all finding features */
GIT_DIFF_FIND_ALL = (0x1f), GIT_DIFF_FIND_ALL = (0x0ff),
/** measure similarity ignoring leading whitespace (default) */ /** measure similarity ignoring leading whitespace (default) */
GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE = 0, GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE = 0,
/** measure similarity ignoring all whitespace */ /** measure similarity ignoring all whitespace */
GIT_DIFF_FIND_IGNORE_WHITESPACE = (1 << 6), GIT_DIFF_FIND_IGNORE_WHITESPACE = (1 << 12),
/** measure similarity including all data */ /** measure similarity including all data */
GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE = (1 << 7), GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE = (1 << 13),
} git_diff_find_t; } git_diff_find_t;
/** /**
...@@ -446,7 +467,7 @@ typedef struct { ...@@ -446,7 +467,7 @@ typedef struct {
* - `copy_threshold` is the same as the -C option with a value * - `copy_threshold` is the same as the -C option with a value
* - `rename_from_rewrite_threshold` matches the top of the -B option * - `rename_from_rewrite_threshold` matches the top of the -B option
* - `break_rewrite_threshold` matches the bottom of the -B option * - `break_rewrite_threshold` matches the bottom of the -B option
* - `target_limit` matches the -l option * - `target_limit` matches the -l option (approximately)
* *
* The `metric` option allows you to plug in a custom similarity metric. * The `metric` option allows you to plug in a custom similarity metric.
* Set it to NULL for the default internal metric which is based on sampling * Set it to NULL for the default internal metric which is based on sampling
...@@ -461,18 +482,18 @@ typedef struct { ...@@ -461,18 +482,18 @@ typedef struct {
unsigned int flags; unsigned int flags;
/** Similarity to consider a file renamed (default 50) */ /** Similarity to consider a file renamed (default 50) */
unsigned int rename_threshold; uint16_t rename_threshold;
/** Similarity of modified to be eligible rename source (default 50) */ /** Similarity of modified to be eligible rename source (default 50) */
unsigned int rename_from_rewrite_threshold; uint16_t rename_from_rewrite_threshold;
/** Similarity to consider a file a copy (default 50) */ /** Similarity to consider a file a copy (default 50) */
unsigned int copy_threshold; uint16_t copy_threshold;
/** Similarity to split modify into delete/add pair (default 60) */ /** Similarity to split modify into delete/add pair (default 60) */
unsigned int break_rewrite_threshold; uint16_t break_rewrite_threshold;
/** Maximum similarity sources to examine (a la diff's `-l` option or /** Maximum similarity sources to examine (a la diff's `-l` option or
* the `diff.renameLimit` config) (default 200) * the `diff.renameLimit` config) (default 200)
*/ */
unsigned int target_limit; size_t target_limit;
/** Pluggable similarity metric; pass NULL to use internal metric */ /** Pluggable similarity metric; pass NULL to use internal metric */
git_diff_similarity_metric *metric; git_diff_similarity_metric *metric;
......
...@@ -109,6 +109,13 @@ GIT_INLINE(int) git__is_sizet(git_off_t p) ...@@ -109,6 +109,13 @@ GIT_INLINE(int) git__is_sizet(git_off_t p)
return p == (git_off_t)r; return p == (git_off_t)r;
} }
/** @return true if p fits into the range of a uint32_t */
GIT_INLINE(int) git__is_uint32(size_t p)
{
uint32_t r = (uint32_t)p;
return p == (size_t)r;
}
/* 32-bit cross-platform rotl */ /* 32-bit cross-platform rotl */
#ifdef _MSC_VER /* use built-in method in MSVC */ #ifdef _MSC_VER /* use built-in method in MSVC */
# define git__rotl(v, s) (uint32_t)_rotl(v, s) # define git__rotl(v, s) (uint32_t)_rotl(v, s)
......
...@@ -377,7 +377,8 @@ void test_diff_rename__handles_small_files(void) ...@@ -377,7 +377,8 @@ void test_diff_rename__handles_small_files(void)
*/ */
cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES | GIT_DIFF_FIND_AND_BREAK_REWRITES; opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES |
GIT_DIFF_FIND_AND_BREAK_REWRITES;
cl_git_pass(git_diff_find_similar(diff, &opts)); cl_git_pass(git_diff_find_similar(diff, &opts));
git_diff_list_free(diff); git_diff_list_free(diff);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment