Refine pluggable similarity API This plugs in the three basic similarity strategies for handling whitespace via internal use of the pluggable API. In so doing, I realized that the use of git_buf in the hashsig API was not needed and actually just made it harder to use, so I tweaked that API as well. Note that the similarity metric is still not hooked up in the find_similarity code - this is just setting out the function that will be used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
diff --git a/include/git2/diff.h b/include/git2/diff.h
index d90fedf..c0f4836 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -387,20 +387,46 @@ typedef enum {
/** split large rewrites into delete/add pairs (`--break-rewrites=/M`) */
GIT_DIFF_FIND_AND_BREAK_REWRITES = (1 << 4),
+
+ /** measure similarity ignoring leading whitespace (default) */
+ GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE = 0,
+ /** measure similarity ignoring all whitespace */
+ GIT_DIFF_FIND_IGNORE_WHITESPACE = (1 << 6),
+ /** measure similarity including all data */
+ GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE = (1 << 7),
} git_diff_find_t;
/**
* Pluggable similarity metric
*/
typedef struct {
- int (*calc_signature)(void **out, const git_diff_file *file, void *payload);
+ int (*file_signature)(
+ void **out, const git_diff_file *file,
+ const char *fullpath, void *payload);
+ int (*buffer_signature)(
+ void **out, const git_diff_file *file,
+ const char *buf, size_t buflen, void *payload);
void (*free_signature)(void *sig, void *payload);
- int (*calc_similarity)(int *score, void *siga, void *sigb, void *payload);
+ int (*similarity)(int *score, void *siga, void *sigb, void *payload);
void *payload;
} git_diff_similarity_metric;
/**
* Control behavior of rename and copy detection
+ *
+ * These options mostly mimic parameters that can be passed to git-diff.
+ *
+ * - `rename_threshold` is the same as the -M 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
+ * - `break_rewrite_threshold` matches the bottom of the -B option
+ * - `target_limit` matches the -l option
+ *
+ * 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
+ * hashes of ranges of data in the file. The default metric is a pretty
+ * good similarity approximation that should work fairly well for both text
+ * and binary data, and is pretty fast with fixed memory overhead.
*/
typedef struct {
unsigned int version;
diff --git a/src/diff_tform.c b/src/diff_tform.c
index 3939230..61cf5b3 100644
--- a/src/diff_tform.c
+++ b/src/diff_tform.c
@@ -169,6 +169,60 @@ int git_diff_merge(
return error;
}
+#define FIND_SIMILAR_HASHSIG(NAME,OPT) \
+ static int find_similar__hashsig_for_file ## NAME( \
+ void **out, const git_diff_file *f, const char *path, void *p) { \
+ GIT_UNUSED(f); GIT_UNUSED(p); \
+ return git_hashsig_create_fromfile((git_hashsig **)out, path, OPT); \
+ } \
+ static int find_similar__hashsig_for_buf ## NAME( \
+ void **out, const git_diff_file *f, const char *buf, size_t len, void *p) { \
+ GIT_UNUSED(f); GIT_UNUSED(p); \
+ return git_hashsig_create((git_hashsig **)out, buf, len, OPT); \
+ }
+
+FIND_SIMILAR_HASHSIG(_default, GIT_HASHSIG_SMART_WHITESPACE);
+FIND_SIMILAR_HASHSIG(_ignore_whitespace, GIT_HASHSIG_IGNORE_WHITESPACE);
+FIND_SIMILAR_HASHSIG(_include_whitespace, GIT_HASHSIG_NORMAL);
+
+static void find_similar__hashsig_free(void *sig, void *payload)
+{
+ GIT_UNUSED(payload);
+ git_hashsig_free(sig);
+}
+
+static int find_similar__calc_similarity(
+ int *score, void *siga, void *sigb, void *payload)
+{
+ GIT_UNUSED(payload);
+ *score = git_hashsig_compare(siga, sigb);
+ return 0;
+}
+
+static git_diff_similarity_metric find_similar__internal_metrics[3] = {
+ {
+ find_similar__hashsig_for_file_default,
+ find_similar__hashsig_for_buf_default,
+ find_similar__hashsig_free,
+ find_similar__calc_similarity,
+ NULL
+ },
+ {
+ find_similar__hashsig_for_file_ignore_whitespace,
+ find_similar__hashsig_for_buf_ignore_whitespace,
+ find_similar__hashsig_free,
+ find_similar__calc_similarity,
+ NULL
+ },
+ {
+ find_similar__hashsig_for_file_include_whitespace,
+ find_similar__hashsig_for_buf_include_whitespace,
+ find_similar__hashsig_free,
+ find_similar__calc_similarity,
+ NULL
+ }
+};
+
#define DEFAULT_THRESHOLD 50
#define DEFAULT_BREAK_REWRITE_THRESHOLD 60
#define DEFAULT_TARGET_LIMIT 200
@@ -237,6 +291,16 @@ static int normalize_find_opts(
opts->target_limit = limit;
}
+ /* for now, always assign the same internal metric */
+ if (!opts->metric) {
+ if (opts->flags & GIT_DIFF_FIND_IGNORE_WHITESPACE)
+ opts->metric = &find_similar__internal_metrics[1];
+ else if (opts->flags & GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE)
+ opts->metric = &find_similar__internal_metrics[2];
+ else
+ opts->metric = &find_similar__internal_metrics[0];
+ }
+
return 0;
}
diff --git a/src/hashsig.c b/src/hashsig.c
index a9cd8fa..60649fd 100644
--- a/src/hashsig.c
+++ b/src/hashsig.c
@@ -266,7 +266,8 @@ static git_hashsig *hashsig_alloc(git_hashsig_option_t opts)
int git_hashsig_create(
git_hashsig **out,
- const git_buf *buf,
+ const char *buf,
+ size_t buflen,
git_hashsig_option_t opts)
{
int error;
@@ -274,7 +275,7 @@ int git_hashsig_create(
git_hashsig *sig = hashsig_alloc(opts);
GITERR_CHECK_ALLOC(sig);
- error = hashsig_add_hashes(sig, buf->ptr, buf->size, &prog);
+ error = hashsig_add_hashes(sig, buf, buflen, &prog);
if (!error)
error = hashsig_finalize_hashes(sig);
diff --git a/src/hashsig.h b/src/hashsig.h
index 70b47f5..8c920cb 100644
--- a/src/hashsig.h
+++ b/src/hashsig.h
@@ -7,7 +7,7 @@
#ifndef INCLUDE_hashsig_h__
#define INCLUDE_hashsig_h__
-#include "buffer.h"
+#include "common.h"
/**
* Similarity signature of line hashes for a buffer
@@ -32,11 +32,13 @@ typedef enum {
*
* @param out The array of hashed runs representing the file content
* @param buf The contents of the file to hash
+ * @param buflen The length of the data at `buf`
* @param generate_pairwise_hashes Should pairwise runs be hashed
*/
extern int git_hashsig_create(
git_hashsig **out,
- const git_buf *buf,
+ const char *buf,
+ size_t buflen,
git_hashsig_option_t opts);
/**
diff --git a/tests-clar/core/buffer.c b/tests-clar/core/buffer.c
index 9073381..cb5f016 100644
--- a/tests-clar/core/buffer.c
+++ b/tests-clar/core/buffer.c
@@ -748,8 +748,8 @@ void test_core_buffer__similarity_metric(void)
/* in the first case, we compare data to itself and expect 100% match */
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
- cl_git_pass(git_hashsig_create(&a, &buf, GIT_HASHSIG_NORMAL));
- cl_git_pass(git_hashsig_create(&b, &buf, GIT_HASHSIG_NORMAL));
+ cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
+ cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_assert_equal_i(100, git_hashsig_compare(a, b));
@@ -759,13 +759,13 @@ void test_core_buffer__similarity_metric(void)
/* if we change just a single byte, how much does that change magnify? */
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
- cl_git_pass(git_hashsig_create(&a, &buf, GIT_HASHSIG_NORMAL));
+ cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_buf_sets(&buf,
"Test data\nright here\ninline\ntada\nneeds more data\nlots of data\n"
"is this enough?\nthere has to be enough data to fill the hash array!\n"
"Apparently 191 bytes is the minimum amount of data needed.\nHere goes!\n"
"Let's make sure we've got plenty to go with here.\n smile \n"));
- cl_git_pass(git_hashsig_create(&b, &buf, GIT_HASHSIG_NORMAL));
+ cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
sim = git_hashsig_compare(a, b);
@@ -777,10 +777,10 @@ void test_core_buffer__similarity_metric(void)
/* let's try comparing data to a superset of itself */
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
- cl_git_pass(git_hashsig_create(&a, &buf, GIT_HASHSIG_NORMAL));
+ cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1
"and if I add some more, it should still be pretty similar, yes?\n"));
- cl_git_pass(git_hashsig_create(&b, &buf, GIT_HASHSIG_NORMAL));
+ cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
sim = git_hashsig_compare(a, b);
@@ -792,13 +792,13 @@ void test_core_buffer__similarity_metric(void)
/* what if we keep about half the original data and add half new */
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
- cl_git_pass(git_hashsig_create(&a, &buf, GIT_HASHSIG_NORMAL));
+ cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_buf_sets(&buf,
"test data\nright here\ninline\ntada\nneeds more data\nlots of data\n"
"is this enough?\nthere has to be enough data to fill the hash array!\n"
"okay, that's half the original\nwhat else can we add?\nmore data\n"
"one more line will complete this\nshort\nlines\ndon't\nmatter\n"));
- cl_git_pass(git_hashsig_create(&b, &buf, GIT_HASHSIG_NORMAL));
+ cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
sim = git_hashsig_compare(a, b);
@@ -810,7 +810,7 @@ void test_core_buffer__similarity_metric(void)
/* lastly, let's check that we can hash file content as well */
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
- cl_git_pass(git_hashsig_create(&a, &buf, GIT_HASHSIG_NORMAL));
+ cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_futils_mkdir("scratch", NULL, 0755, GIT_MKDIR_PATH));
cl_git_mkfile("scratch/testdata", SIMILARITY_TEST_DATA_1);
@@ -880,10 +880,10 @@ void test_core_buffer__similarity_metric_whitespace(void)
for (i = 0; i < 3; ++i) {
for (j = 0; j < 3; ++j) {
cl_git_pass(git_buf_sets(&buf, text[i]));
- cl_git_pass(git_hashsig_create(&a, &buf, opt));
+ cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, opt));
cl_git_pass(git_buf_sets(&buf, text[j]));
- cl_git_pass(git_hashsig_create(&b, &buf, opt));
+ cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, opt));
sim = git_hashsig_compare(a, b);