summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/diff_tform.c64
-rw-r--r--src/hashsig.c5
-rw-r--r--src/hashsig.h6
3 files changed, 71 insertions, 4 deletions
diff --git a/src/diff_tform.c b/src/diff_tform.c
index 3939230b7..61cf5b392 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 a9cd8fa53..60649fd11 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 70b47f5f3..8c920cbf1 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);
/**