Commit bf145a6a by Ben Straub

Merge pull request #1746 from libgit2/rename-detection-performance

Rename detection slow
parents 46c37911 e8242022
...@@ -361,7 +361,7 @@ IF (BUILD_CLAR) ...@@ -361,7 +361,7 @@ IF (BUILD_CLAR)
ADD_CUSTOM_COMMAND( ADD_CUSTOM_COMMAND(
OUTPUT ${CLAR_PATH}/clar.suite OUTPUT ${CLAR_PATH}/clar.suite
COMMAND ${PYTHON_EXECUTABLE} generate.py -f -xonline . COMMAND ${PYTHON_EXECUTABLE} generate.py -f -xonline -xstress .
DEPENDS ${SRC_TEST} DEPENDS ${SRC_TEST}
WORKING_DIRECTORY ${CLAR_PATH} WORKING_DIRECTORY ${CLAR_PATH}
) )
......
...@@ -39,25 +39,26 @@ ...@@ -39,25 +39,26 @@
#define GITERR_CHECK_ARRAY(a) GITERR_CHECK_ALLOC((a).ptr) #define GITERR_CHECK_ARRAY(a) GITERR_CHECK_ALLOC((a).ptr)
typedef git_array_t(void) git_array_generic_t; typedef git_array_t(char) git_array_generic_t;
/* use a generic array for growth so this can return the new item */ /* use a generic array for growth so this can return the new item */
GIT_INLINE(void *) git_array_grow(git_array_generic_t *a, size_t item_size) GIT_INLINE(void *) git_array_grow(void *_a, size_t item_size)
{ {
git_array_generic_t *a = _a;
uint32_t new_size = (a->size < 8) ? 8 : a->asize * 3 / 2; uint32_t new_size = (a->size < 8) ? 8 : a->asize * 3 / 2;
void *new_array = git__realloc(a->ptr, new_size * item_size); char *new_array = git__realloc(a->ptr, new_size * item_size);
if (!new_array) { if (!new_array) {
git_array_clear(*a); git_array_clear(*a);
return NULL; return NULL;
} else { } else {
a->ptr = new_array; a->asize = new_size; a->size++; a->ptr = new_array; a->asize = new_size; a->size++;
return (((char *)a->ptr) + (a->size - 1) * item_size); return a->ptr + (a->size - 1) * item_size;
} }
} }
#define git_array_alloc(a) \ #define git_array_alloc(a) \
((a).size >= (a).asize) ? \ ((a).size >= (a).asize) ? \
git_array_grow((git_array_generic_t *)&(a), sizeof(*(a).ptr)) : \ git_array_grow(&(a), sizeof(*(a).ptr)) : \
(a).ptr ? &(a).ptr[(a).size++] : NULL (a).ptr ? &(a).ptr[(a).size++] : NULL
#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : NULL) #define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : NULL)
......
...@@ -105,6 +105,7 @@ static int write_file_stream( ...@@ -105,6 +105,7 @@ static int write_file_stream(
static int write_file_filtered( static int write_file_filtered(
git_oid *oid, git_oid *oid,
git_off_t *size,
git_odb *odb, git_odb *odb,
const char *full_path, const char *full_path,
git_vector *filters) git_vector *filters)
...@@ -123,8 +124,11 @@ static int write_file_filtered( ...@@ -123,8 +124,11 @@ static int write_file_filtered(
git_buf_free(&source); git_buf_free(&source);
/* Write the file to disk if it was properly filtered */ /* Write the file to disk if it was properly filtered */
if (!error) if (!error) {
*size = dest.size;
error = git_odb_write(oid, odb, dest.ptr, dest.size, GIT_OBJ_BLOB); error = git_odb_write(oid, odb, dest.ptr, dest.size, GIT_OBJ_BLOB);
}
git_buf_free(&dest); git_buf_free(&dest);
return error; return error;
...@@ -152,21 +156,46 @@ static int write_symlink( ...@@ -152,21 +156,46 @@ static int write_symlink(
return error; return error;
} }
static int blob_create_internal(git_oid *oid, git_repository *repo, const char *content_path, const char *hint_path, bool try_load_filters) int git_blob__create_from_paths(
git_oid *oid,
struct stat *out_st,
git_repository *repo,
const char *content_path,
const char *hint_path,
mode_t hint_mode,
bool try_load_filters)
{ {
int error; int error;
struct stat st; struct stat st;
git_odb *odb = NULL; git_odb *odb = NULL;
git_off_t size; git_off_t size;
mode_t mode;
git_buf path = GIT_BUF_INIT;
assert(hint_path || !try_load_filters); assert(hint_path || !try_load_filters);
if ((error = git_path_lstat(content_path, &st)) < 0 || (error = git_repository_odb__weakptr(&odb, repo)) < 0) if (!content_path) {
return error; if (git_repository__ensure_not_bare(repo, "create blob from file") < 0)
return GIT_EBAREREPO;
if (git_buf_joinpath(
&path, git_repository_workdir(repo), hint_path) < 0)
return -1;
content_path = path.ptr;
}
if ((error = git_path_lstat(content_path, &st)) < 0 ||
(error = git_repository_odb(&odb, repo)) < 0)
goto done;
if (out_st)
memcpy(out_st, &st, sizeof(st));
size = st.st_size; size = st.st_size;
mode = hint_mode ? hint_mode : st.st_mode;
if (S_ISLNK(st.st_mode)) { if (S_ISLNK(mode)) {
error = write_symlink(oid, odb, content_path, (size_t)size); error = write_symlink(oid, odb, content_path, (size_t)size);
} else { } else {
git_vector write_filters = GIT_VECTOR_INIT; git_vector write_filters = GIT_VECTOR_INIT;
...@@ -187,7 +216,8 @@ static int blob_create_internal(git_oid *oid, git_repository *repo, const char * ...@@ -187,7 +216,8 @@ static int blob_create_internal(git_oid *oid, git_repository *repo, const char *
error = write_file_stream(oid, odb, content_path, size); error = write_file_stream(oid, odb, content_path, size);
} else { } else {
/* We need to apply one or more filters */ /* We need to apply one or more filters */
error = write_file_filtered(oid, odb, content_path, &write_filters); error = write_file_filtered(
oid, &size, odb, content_path, &write_filters);
} }
git_filters_free(&write_filters); git_filters_free(&write_filters);
...@@ -207,34 +237,21 @@ static int blob_create_internal(git_oid *oid, git_repository *repo, const char * ...@@ -207,34 +237,21 @@ static int blob_create_internal(git_oid *oid, git_repository *repo, const char *
*/ */
} }
done:
git_odb_free(odb);
git_buf_free(&path);
return error; return error;
} }
int git_blob_create_fromworkdir(git_oid *oid, git_repository *repo, const char *path) int git_blob_create_fromworkdir(
git_oid *oid, git_repository *repo, const char *path)
{ {
git_buf full_path = GIT_BUF_INIT; return git_blob__create_from_paths(oid, NULL, repo, NULL, path, 0, true);
const char *workdir;
int error;
if ((error = git_repository__ensure_not_bare(repo, "create blob from file")) < 0)
return error;
workdir = git_repository_workdir(repo);
if (git_buf_joinpath(&full_path, workdir, path) < 0) {
git_buf_free(&full_path);
return -1;
}
error = blob_create_internal(
oid, repo, git_buf_cstr(&full_path),
git_buf_cstr(&full_path) + strlen(workdir), true);
git_buf_free(&full_path);
return error;
} }
int git_blob_create_fromdisk(git_oid *oid, git_repository *repo, const char *path) int git_blob_create_fromdisk(
git_oid *oid, git_repository *repo, const char *path)
{ {
int error; int error;
git_buf full_path = GIT_BUF_INIT; git_buf full_path = GIT_BUF_INIT;
...@@ -251,8 +268,8 @@ int git_blob_create_fromdisk(git_oid *oid, git_repository *repo, const char *pat ...@@ -251,8 +268,8 @@ int git_blob_create_fromdisk(git_oid *oid, git_repository *repo, const char *pat
if (workdir && !git__prefixcmp(hintpath, workdir)) if (workdir && !git__prefixcmp(hintpath, workdir))
hintpath += strlen(workdir); hintpath += strlen(workdir);
error = blob_create_internal( error = git_blob__create_from_paths(
oid, repo, git_buf_cstr(&full_path), hintpath, true); oid, NULL, repo, git_buf_cstr(&full_path), hintpath, 0, true);
git_buf_free(&full_path); git_buf_free(&full_path);
return error; return error;
...@@ -272,11 +289,8 @@ int git_blob_create_fromchunks( ...@@ -272,11 +289,8 @@ int git_blob_create_fromchunks(
git_filebuf file = GIT_FILEBUF_INIT; git_filebuf file = GIT_FILEBUF_INIT;
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
if (git_buf_join_n( if (git_buf_joinpath(
&path, '/', 3, &path, git_repository_path(repo), GIT_OBJECTS_DIR "streamed") < 0)
git_repository_path(repo),
GIT_OBJECTS_DIR,
"streamed") < 0)
goto cleanup; goto cleanup;
content = git__malloc(BUFFER_SIZE); content = git__malloc(BUFFER_SIZE);
...@@ -303,7 +317,8 @@ int git_blob_create_fromchunks( ...@@ -303,7 +317,8 @@ int git_blob_create_fromchunks(
if (git_filebuf_flush(&file) < 0) if (git_filebuf_flush(&file) < 0)
goto cleanup; goto cleanup;
error = blob_create_internal(oid, repo, file.path_lock, hintpath, hintpath != NULL); error = git_blob__create_from_paths(
oid, NULL, repo, file.path_lock, hintpath, 0, hintpath != NULL);
cleanup: cleanup:
git_buf_free(&path); git_buf_free(&path);
......
...@@ -21,4 +21,13 @@ void git_blob__free(void *blob); ...@@ -21,4 +21,13 @@ void git_blob__free(void *blob);
int git_blob__parse(void *blob, git_odb_object *obj); int git_blob__parse(void *blob, git_odb_object *obj);
int git_blob__getbuf(git_buf *buffer, git_blob *blob); int git_blob__getbuf(git_buf *buffer, git_blob *blob);
extern int git_blob__create_from_paths(
git_oid *out_oid,
struct stat *out_st,
git_repository *repo,
const char *full_path,
const char *hint_path,
mode_t hint_mode,
bool apply_filters);
#endif #endif
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "iterator.h" #include "iterator.h"
#include "repository.h" #include "repository.h"
#include "pool.h" #include "pool.h"
#include "odb.h"
#define DIFF_OLD_PREFIX_DEFAULT "a/" #define DIFF_OLD_PREFIX_DEFAULT "a/"
#define DIFF_NEW_PREFIX_DEFAULT "b/" #define DIFF_NEW_PREFIX_DEFAULT "b/"
...@@ -108,5 +109,33 @@ extern void git_diff_find_similar__hashsig_free(void *sig, void *payload); ...@@ -108,5 +109,33 @@ extern void git_diff_find_similar__hashsig_free(void *sig, void *payload);
extern int git_diff_find_similar__calc_similarity( extern int git_diff_find_similar__calc_similarity(
int *score, void *siga, void *sigb, void *payload); int *score, void *siga, void *sigb, void *payload);
/*
* Sometimes a git_diff_file will have a zero size; this attempts to
* fill in the size without loading the blob if possible. If that is
* not possible, then it will return the git_odb_object that had to be
* loaded and the caller can use it or dispose of it as needed.
*/
GIT_INLINE(int) git_diff_file__resolve_zero_size(
git_diff_file *file, git_odb_object **odb_obj, git_repository *repo)
{
int error;
git_odb *odb;
size_t len;
git_otype type;
if ((error = git_repository_odb(&odb, repo)) < 0)
return error;
error = git_odb__read_header_or_object(
odb_obj, &len, &type, odb, &file->oid);
git_odb_free(odb);
if (!error)
file->size = (git_off_t)len;
return error;
}
#endif #endif
...@@ -241,19 +241,9 @@ static int diff_file_content_load_blob(git_diff_file_content *fc) ...@@ -241,19 +241,9 @@ static int diff_file_content_load_blob(git_diff_file_content *fc)
/* if we don't know size, try to peek at object header first */ /* if we don't know size, try to peek at object header first */
if (!fc->file->size) { if (!fc->file->size) {
git_odb *odb; if ((error = git_diff_file__resolve_zero_size(
size_t len; fc->file, &odb_obj, fc->repo)) < 0)
git_otype type;
if (!(error = git_repository_odb__weakptr(&odb, fc->repo))) {
error = git_odb__read_header_or_object(
&odb_obj, &len, &type, odb, &fc->file->oid);
git_odb_free(odb);
}
if (error)
return error; return error;
fc->file->size = len;
} }
if (diff_file_content_binary_by_size(fc)) if (diff_file_content_binary_by_size(fc))
...@@ -417,6 +407,9 @@ int git_diff_file_content__load(git_diff_file_content *fc) ...@@ -417,6 +407,9 @@ int git_diff_file_content__load(git_diff_file_content *fc)
void git_diff_file_content__unload(git_diff_file_content *fc) void git_diff_file_content__unload(git_diff_file_content *fc)
{ {
if ((fc->flags & GIT_DIFF_FLAG__LOADED) == 0)
return;
if (fc->flags & GIT_DIFF_FLAG__FREE_DATA) { if (fc->flags & GIT_DIFF_FLAG__FREE_DATA) {
git__free(fc->map.data); git__free(fc->map.data);
fc->map.data = ""; fc->map.data = "";
......
...@@ -230,6 +230,10 @@ static int diff_patch_generate(git_diff_patch *patch, git_diff_output *output) ...@@ -230,6 +230,10 @@ static int diff_patch_generate(git_diff_patch *patch, git_diff_output *output)
if ((patch->flags & GIT_DIFF_PATCH_DIFFED) != 0) if ((patch->flags & GIT_DIFF_PATCH_DIFFED) != 0)
return 0; return 0;
/* if we are not looking at the hunks and lines, don't do the diff */
if (!output->hunk_cb && !output->data_cb)
return 0;
if ((patch->flags & GIT_DIFF_PATCH_LOADED) == 0 && if ((patch->flags & GIT_DIFF_PATCH_LOADED) == 0 &&
(error = diff_patch_load(patch, output)) < 0) (error = diff_patch_load(patch, output)) < 0)
return error; return error;
...@@ -284,8 +288,8 @@ int git_diff_foreach( ...@@ -284,8 +288,8 @@ int git_diff_foreach(
if (diff_required(diff, "git_diff_foreach") < 0) if (diff_required(diff, "git_diff_foreach") < 0)
return -1; return -1;
diff_output_init((git_diff_output *)&xo, diff_output_init(
&diff->opts, file_cb, hunk_cb, data_cb, payload); &xo.output, &diff->opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, &diff->opts); git_xdiff_init(&xo, &diff->opts);
git_vector_foreach(&diff->deltas, idx, patch.delta) { git_vector_foreach(&diff->deltas, idx, patch.delta) {
...@@ -296,10 +300,10 @@ int git_diff_foreach( ...@@ -296,10 +300,10 @@ int git_diff_foreach(
if (!(error = diff_patch_init_from_diff(&patch, diff, idx))) { if (!(error = diff_patch_init_from_diff(&patch, diff, idx))) {
error = diff_patch_file_callback(&patch, (git_diff_output *)&xo); error = diff_patch_file_callback(&patch, &xo.output);
if (!error) if (!error)
error = diff_patch_generate(&patch, (git_diff_output *)&xo); error = diff_patch_generate(&patch, &xo.output);
git_diff_patch_free(&patch); git_diff_patch_free(&patch);
} }
...@@ -437,7 +441,7 @@ int git_diff_blobs( ...@@ -437,7 +441,7 @@ int git_diff_blobs(
memset(&xo, 0, sizeof(xo)); memset(&xo, 0, sizeof(xo));
diff_output_init( diff_output_init(
(git_diff_output *)&xo, opts, file_cb, hunk_cb, data_cb, payload); &xo.output, opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, opts); git_xdiff_init(&xo, opts);
if (!old_path && new_path) if (!old_path && new_path)
...@@ -448,7 +452,7 @@ int git_diff_blobs( ...@@ -448,7 +452,7 @@ int git_diff_blobs(
error = diff_patch_from_blobs( error = diff_patch_from_blobs(
&pd, &xo, old_blob, old_path, new_blob, new_path, opts); &pd, &xo, old_blob, old_path, new_blob, new_path, opts);
git_diff_patch_free((git_diff_patch *)&pd); git_diff_patch_free(&pd.patch);
return error; return error;
} }
...@@ -473,7 +477,7 @@ int git_diff_patch_from_blobs( ...@@ -473,7 +477,7 @@ int git_diff_patch_from_blobs(
memset(&xo, 0, sizeof(xo)); memset(&xo, 0, sizeof(xo));
diff_output_to_patch((git_diff_output *)&xo, &pd->patch); diff_output_to_patch(&xo.output, &pd->patch);
git_xdiff_init(&xo, opts); git_xdiff_init(&xo, opts);
error = diff_patch_from_blobs( error = diff_patch_from_blobs(
...@@ -549,7 +553,7 @@ int git_diff_blob_to_buffer( ...@@ -549,7 +553,7 @@ int git_diff_blob_to_buffer(
memset(&xo, 0, sizeof(xo)); memset(&xo, 0, sizeof(xo));
diff_output_init( diff_output_init(
(git_diff_output *)&xo, opts, file_cb, hunk_cb, data_cb, payload); &xo.output, opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, opts); git_xdiff_init(&xo, opts);
if (!old_path && buf_path) if (!old_path && buf_path)
...@@ -560,7 +564,7 @@ int git_diff_blob_to_buffer( ...@@ -560,7 +564,7 @@ int git_diff_blob_to_buffer(
error = diff_patch_from_blob_and_buffer( error = diff_patch_from_blob_and_buffer(
&pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts); &pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts);
git_diff_patch_free((git_diff_patch *)&pd); git_diff_patch_free(&pd.patch);
return error; return error;
} }
...@@ -586,7 +590,7 @@ int git_diff_patch_from_blob_and_buffer( ...@@ -586,7 +590,7 @@ int git_diff_patch_from_blob_and_buffer(
memset(&xo, 0, sizeof(xo)); memset(&xo, 0, sizeof(xo));
diff_output_to_patch((git_diff_output *)&xo, &pd->patch); diff_output_to_patch(&xo.output, &pd->patch);
git_xdiff_init(&xo, opts); git_xdiff_init(&xo, opts);
error = diff_patch_from_blob_and_buffer( error = diff_patch_from_blob_and_buffer(
...@@ -638,13 +642,13 @@ int git_diff_get_patch( ...@@ -638,13 +642,13 @@ int git_diff_get_patch(
if ((error = diff_patch_alloc_from_diff(&patch, diff, idx)) < 0) if ((error = diff_patch_alloc_from_diff(&patch, diff, idx)) < 0)
return error; return error;
diff_output_to_patch((git_diff_output *)&xo, patch); diff_output_to_patch(&xo.output, patch);
git_xdiff_init(&xo, &diff->opts); git_xdiff_init(&xo, &diff->opts);
error = diff_patch_file_callback(patch, (git_diff_output *)&xo); error = diff_patch_file_callback(patch, &xo.output);
if (!error) if (!error)
error = diff_patch_generate(patch, (git_diff_output *)&xo); error = diff_patch_generate(patch, &xo.output);
if (!error) { if (!error) {
/* if cumulative diff size is < 0.5 total size, flatten the patch */ /* if cumulative diff size is < 0.5 total size, flatten the patch */
......
...@@ -13,12 +13,15 @@ typedef uint64_t hashsig_state; ...@@ -13,12 +13,15 @@ typedef uint64_t hashsig_state;
#define HASHSIG_SCALE 100 #define HASHSIG_SCALE 100
#define HASHSIG_HASH_WINDOW 32 #define HASHSIG_MAX_RUN 80
#define HASHSIG_HASH_START 0 #define HASHSIG_HASH_START 0x012345678ABCDEF0LL
#define HASHSIG_HASH_SHIFT 5 #define HASHSIG_HASH_SHIFT 5
#define HASHSIG_HASH_MASK 0x7FFFFFFF
#define HASHSIG_HASH_MIX(S,CH) \
(S) = ((S) << HASHSIG_HASH_SHIFT) - (S) + (hashsig_state)(CH)
#define HASHSIG_HEAP_SIZE ((1 << 7) - 1) #define HASHSIG_HEAP_SIZE ((1 << 7) - 1)
#define HASHSIG_HEAP_MIN_SIZE 4
typedef int (*hashsig_cmp)(const void *a, const void *b, void *); typedef int (*hashsig_cmp)(const void *a, const void *b, void *);
...@@ -28,14 +31,6 @@ typedef struct { ...@@ -28,14 +31,6 @@ typedef struct {
hashsig_t values[HASHSIG_HEAP_SIZE]; hashsig_t values[HASHSIG_HEAP_SIZE];
} hashsig_heap; } hashsig_heap;
typedef struct {
hashsig_state state, shift_n;
char window[HASHSIG_HASH_WINDOW];
int win_len, win_pos, saw_lf;
} hashsig_in_progress;
#define HASHSIG_IN_PROGRESS_INIT { HASHSIG_HASH_START, 1, {0}, 0, 0, 1 }
struct git_hashsig { struct git_hashsig {
hashsig_heap mins; hashsig_heap mins;
hashsig_heap maxs; hashsig_heap maxs;
...@@ -43,8 +38,8 @@ struct git_hashsig { ...@@ -43,8 +38,8 @@ struct git_hashsig {
int considered; int considered;
}; };
#define HEAP_LCHILD_OF(I) (((I)*2)+1) #define HEAP_LCHILD_OF(I) (((I)<<1)+1)
#define HEAP_RCHILD_OF(I) (((I)*2)+2) #define HEAP_RCHILD_OF(I) (((I)<<1)+2)
#define HEAP_PARENT_OF(I) (((I)-1)>>1) #define HEAP_PARENT_OF(I) (((I)-1)>>1)
static void hashsig_heap_init(hashsig_heap *h, hashsig_cmp cmp) static void hashsig_heap_init(hashsig_heap *h, hashsig_cmp cmp)
...@@ -115,134 +110,109 @@ static void hashsig_heap_sort(hashsig_heap *h) ...@@ -115,134 +110,109 @@ static void hashsig_heap_sort(hashsig_heap *h)
static void hashsig_heap_insert(hashsig_heap *h, hashsig_t val) static void hashsig_heap_insert(hashsig_heap *h, hashsig_t val)
{ {
/* if heap is full, pop top if new element should replace it */
if (h->size == h->asize && h->cmp(&val, &h->values[0], NULL) > 0) {
h->size--;
h->values[0] = h->values[h->size];
hashsig_heap_down(h, 0);
}
/* if heap is not full, insert new element */ /* if heap is not full, insert new element */
if (h->size < h->asize) { if (h->size < h->asize) {
h->values[h->size++] = val; h->values[h->size++] = val;
hashsig_heap_up(h, h->size - 1); hashsig_heap_up(h, h->size - 1);
} }
}
GIT_INLINE(bool) hashsig_include_char(
char ch, git_hashsig_option_t opt, int *saw_lf)
{
if ((opt & GIT_HASHSIG_IGNORE_WHITESPACE) && git__isspace(ch))
return false;
if (opt & GIT_HASHSIG_SMART_WHITESPACE) { /* if heap is full, pop top if new element should replace it */
if (ch == '\r' || (*saw_lf && git__isspace(ch))) else if (h->cmp(&val, &h->values[0], NULL) > 0) {
return false; h->size--;
h->values[0] = h->values[h->size];
*saw_lf = (ch == '\n'); hashsig_heap_down(h, 0);
} }
return true;
} }
static void hashsig_initial_window( typedef struct {
git_hashsig *sig, int use_ignores;
const char **data, uint8_t ignore_ch[256];
size_t size, } hashsig_in_progress;
hashsig_in_progress *prog)
{
hashsig_state state, shift_n;
int win_len;
const char *scan, *end;
/* init until we have processed at least HASHSIG_HASH_WINDOW data */
if (prog->win_len >= HASHSIG_HASH_WINDOW)
return;
state = prog->state;
win_len = prog->win_len;
shift_n = prog->shift_n;
scan = *data;
end = scan + size;
while (scan < end && win_len < HASHSIG_HASH_WINDOW) {
char ch = *scan++;
if (!hashsig_include_char(ch, sig->opt, &prog->saw_lf))
continue;
state = (state * HASHSIG_HASH_SHIFT + ch) & HASHSIG_HASH_MASK;
if (!win_len)
shift_n = 1;
else
shift_n = (shift_n * HASHSIG_HASH_SHIFT) & HASHSIG_HASH_MASK;
prog->window[win_len++] = ch;
}
/* insert initial hash if we just finished */ static void hashsig_in_progress_init(
hashsig_in_progress *prog, git_hashsig *sig)
{
int i;
if (win_len == HASHSIG_HASH_WINDOW) { switch (sig->opt) {
hashsig_heap_insert(&sig->mins, (hashsig_t)state); case GIT_HASHSIG_IGNORE_WHITESPACE:
hashsig_heap_insert(&sig->maxs, (hashsig_t)state); for (i = 0; i < 256; ++i)
sig->considered = 1; prog->ignore_ch[i] = git__isspace_nonlf(i);
prog->use_ignores = 1;
break;
case GIT_HASHSIG_SMART_WHITESPACE:
for (i = 0; i < 256; ++i)
prog->ignore_ch[i] = git__isspace(i);
prog->use_ignores = 1;
break;
default:
memset(prog, 0, sizeof(*prog));
break;
} }
prog->state = state;
prog->win_len = win_len;
prog->shift_n = shift_n;
*data = scan;
} }
#define HASHSIG_IN_PROGRESS_INIT { 1 }
static int hashsig_add_hashes( static int hashsig_add_hashes(
git_hashsig *sig, git_hashsig *sig,
const char *data, const uint8_t *data,
size_t size, size_t size,
hashsig_in_progress *prog) hashsig_in_progress *prog)
{ {
const char *scan = data, *end = data + size; const uint8_t *scan = data, *end = data + size;
hashsig_state state, shift_n, rmv; hashsig_state state = HASHSIG_HASH_START;
int use_ignores = prog->use_ignores, len;
uint8_t ch;
if (prog->win_len < HASHSIG_HASH_WINDOW) while (scan < end) {
hashsig_initial_window(sig, &scan, size, prog); state = HASHSIG_HASH_START;
state = prog->state; for (len = 0; scan < end && len < HASHSIG_MAX_RUN; ) {
shift_n = prog->shift_n; ch = *scan;
/* advance window, adding new chars and removing old */ if (use_ignores)
for (; scan < end && git__isspace_nonlf(ch); ch = *scan)
++scan;
else if (sig->opt != GIT_HASHSIG_NORMAL)
for (; scan < end && ch == '\r'; ch = *scan)
++scan;
for (; scan < end; ++scan) { /* peek at next character to decide what to do next */
char ch = *scan; if (sig->opt == GIT_HASHSIG_SMART_WHITESPACE)
use_ignores = (ch == '\n');
if (!hashsig_include_char(ch, sig->opt, &prog->saw_lf)) if (scan >= end)
continue; break;
++scan;
rmv = shift_n * prog->window[prog->win_pos]; /* check run terminator */
if (ch == '\n' || ch == '\0')
break;
state = (state - rmv) & HASHSIG_HASH_MASK; ++len;
state = (state * HASHSIG_HASH_SHIFT) & HASHSIG_HASH_MASK; HASHSIG_HASH_MIX(state, ch);
state = (state + ch) & HASHSIG_HASH_MASK; }
if (len > 0) {
hashsig_heap_insert(&sig->mins, (hashsig_t)state); hashsig_heap_insert(&sig->mins, (hashsig_t)state);
hashsig_heap_insert(&sig->maxs, (hashsig_t)state); hashsig_heap_insert(&sig->maxs, (hashsig_t)state);
sig->considered++; sig->considered++;
prog->window[prog->win_pos] = ch; while (scan < end && (*scan == '\n' || !*scan))
prog->win_pos = (prog->win_pos + 1) % HASHSIG_HASH_WINDOW; ++scan;
}
} }
prog->state = state; prog->use_ignores = use_ignores;
return 0; return 0;
} }
static int hashsig_finalize_hashes(git_hashsig *sig) static int hashsig_finalize_hashes(git_hashsig *sig)
{ {
if (sig->mins.size < HASHSIG_HEAP_SIZE) { if (sig->mins.size < HASHSIG_HEAP_MIN_SIZE) {
giterr_set(GITERR_INVALID, giterr_set(GITERR_INVALID,
"File too small for similarity signature calculation"); "File too small for similarity signature calculation");
return GIT_EBUFS; return GIT_EBUFS;
...@@ -274,11 +244,13 @@ int git_hashsig_create( ...@@ -274,11 +244,13 @@ int git_hashsig_create(
git_hashsig_option_t opts) git_hashsig_option_t opts)
{ {
int error; int error;
hashsig_in_progress prog = HASHSIG_IN_PROGRESS_INIT; hashsig_in_progress prog;
git_hashsig *sig = hashsig_alloc(opts); git_hashsig *sig = hashsig_alloc(opts);
GITERR_CHECK_ALLOC(sig); GITERR_CHECK_ALLOC(sig);
error = hashsig_add_hashes(sig, buf, buflen, &prog); hashsig_in_progress_init(&prog, sig);
error = hashsig_add_hashes(sig, (const uint8_t *)buf, buflen, &prog);
if (!error) if (!error)
error = hashsig_finalize_hashes(sig); error = hashsig_finalize_hashes(sig);
...@@ -296,10 +268,10 @@ int git_hashsig_create_fromfile( ...@@ -296,10 +268,10 @@ int git_hashsig_create_fromfile(
const char *path, const char *path,
git_hashsig_option_t opts) git_hashsig_option_t opts)
{ {
char buf[4096]; uint8_t buf[0x1000];
ssize_t buflen = 0; ssize_t buflen = 0;
int error = 0, fd; int error = 0, fd;
hashsig_in_progress prog = HASHSIG_IN_PROGRESS_INIT; hashsig_in_progress prog;
git_hashsig *sig = hashsig_alloc(opts); git_hashsig *sig = hashsig_alloc(opts);
GITERR_CHECK_ALLOC(sig); GITERR_CHECK_ALLOC(sig);
...@@ -308,6 +280,8 @@ int git_hashsig_create_fromfile( ...@@ -308,6 +280,8 @@ int git_hashsig_create_fromfile(
return fd; return fd;
} }
hashsig_in_progress_init(&prog, sig);
while (!error) { while (!error) {
if ((buflen = p_read(fd, buf, sizeof(buf))) <= 0) { if ((buflen = p_read(fd, buf, sizeof(buf))) <= 0) {
if ((error = (int)buflen) < 0) if ((error = (int)buflen) < 0)
...@@ -362,6 +336,12 @@ static int hashsig_heap_compare(const hashsig_heap *a, const hashsig_heap *b) ...@@ -362,6 +336,12 @@ static int hashsig_heap_compare(const hashsig_heap *a, const hashsig_heap *b)
int git_hashsig_compare(const git_hashsig *a, const git_hashsig *b) int git_hashsig_compare(const git_hashsig *a, const git_hashsig *b)
{ {
/* if we have fewer than the maximum number of elements, then just use
* one array since the two arrays will be the same
*/
if (a->mins.size < HASHSIG_HEAP_SIZE)
return hashsig_heap_compare(&a->mins, &b->mins);
else
return (hashsig_heap_compare(&a->mins, &b->mins) + return (hashsig_heap_compare(&a->mins, &b->mins) +
hashsig_heap_compare(&a->maxs, &b->maxs)) / 2; hashsig_heap_compare(&a->maxs, &b->maxs)) / 2;
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "iterator.h" #include "iterator.h"
#include "pathspec.h" #include "pathspec.h"
#include "ignore.h" #include "ignore.h"
#include "blob.h"
#include "git2/odb.h" #include "git2/odb.h"
#include "git2/oid.h" #include "git2/oid.h"
...@@ -604,42 +605,23 @@ int git_index_entry__cmp_icase(const void *a, const void *b) ...@@ -604,42 +605,23 @@ int git_index_entry__cmp_icase(const void *a, const void *b)
return strcasecmp(entry_a->path, entry_b->path); return strcasecmp(entry_a->path, entry_b->path);
} }
static int index_entry_init(git_index_entry **entry_out, git_index *index, const char *rel_path) static int index_entry_init(
git_index_entry **entry_out, git_index *index, const char *rel_path)
{ {
int error = 0;
git_index_entry *entry = NULL; git_index_entry *entry = NULL;
struct stat st; struct stat st;
git_oid oid; git_oid oid;
const char *workdir;
git_buf full_path = GIT_BUF_INIT;
int error;
if (INDEX_OWNER(index) == NULL) if (INDEX_OWNER(index) == NULL)
return create_index_error(-1, return create_index_error(-1,
"Could not initialize index entry. " "Could not initialize index entry. "
"Index is not backed up by an existing repository."); "Index is not backed up by an existing repository.");
workdir = git_repository_workdir(INDEX_OWNER(index)); /* write the blob to disk and get the oid and stat info */
error = git_blob__create_from_paths(
if (!workdir) &oid, &st, INDEX_OWNER(index), NULL, rel_path, 0, true);
return create_index_error(GIT_EBAREREPO, if (error < 0)
"Could not initialize index entry. Repository is bare");
if ((error = git_buf_joinpath(&full_path, workdir, rel_path)) < 0)
return error;
if ((error = git_path_lstat(full_path.ptr, &st)) < 0) {
git_buf_free(&full_path);
return error;
}
git_buf_free(&full_path); /* done with full path */
/* There is no need to validate the rel_path here, since it will be
* immediately validated by the call to git_blob_create_fromfile.
*/
/* write the blob to disk and get the oid */
if ((error = git_blob_create_fromworkdir(&oid, INDEX_OWNER(index), rel_path)) < 0)
return error; return error;
entry = git__calloc(1, sizeof(git_index_entry)); entry = git__calloc(1, sizeof(git_index_entry));
......
...@@ -294,6 +294,11 @@ GIT_INLINE(bool) git__isspace(int c) ...@@ -294,6 +294,11 @@ GIT_INLINE(bool) git__isspace(int c)
return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v' || c == 0x85 /* Unicode CR+LF */); return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v' || c == 0x85 /* Unicode CR+LF */);
} }
GIT_INLINE(bool) git__isspace_nonlf(int c)
{
return (c == ' ' || c == '\t' || c == '\f' || c == '\r' || c == '\v' || c == 0x85 /* Unicode CR+LF */);
}
GIT_INLINE(bool) git__iswildcard(int c) GIT_INLINE(bool) git__iswildcard(int c)
{ {
return (c == '*' || c == '?' || c == '['); return (c == '*' || c == '?' || c == '[');
......
...@@ -34,6 +34,20 @@ void cl_git_report_failure(int, const char *, int, const char *); ...@@ -34,6 +34,20 @@ void cl_git_report_failure(int, const char *, int, const char *);
#define cl_assert_equal_sz(sz1,sz2) cl_assert_equal_i((int)sz1, (int)(sz2)) #define cl_assert_equal_sz(sz1,sz2) cl_assert_equal_i((int)sz1, (int)(sz2))
GIT_INLINE(void) clar__assert_in_range(
int lo, int val, int hi,
const char *file, int line, const char *err, int should_abort)
{
if (lo > val || hi < val) {
char buf[128];
snprintf(buf, sizeof(buf), "%d not in [%d,%d]", val, lo, hi);
clar__fail(file, line, err, buf, should_abort);
}
}
#define cl_assert_in_range(L,V,H) \
clar__assert_in_range((L),(V),(H),__FILE__,__LINE__,"Range check: " #V " in [" #L "," #H "]", 1)
/* /*
* Some utility macros for building long strings * Some utility macros for building long strings
*/ */
......
...@@ -734,10 +734,11 @@ void test_core_buffer__classify_with_utf8(void) ...@@ -734,10 +734,11 @@ void test_core_buffer__classify_with_utf8(void)
} }
#define SIMILARITY_TEST_DATA_1 \ #define SIMILARITY_TEST_DATA_1 \
"test data\nright here\ninline\ntada\nneeds more data\nlots of data\n" \ "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
"is this enough?\nthere has to be enough data to fill the hash array!\n" \ "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
"Apparently 191 bytes is the minimum amount of data needed.\nHere goes!\n" \ "020\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \
"Let's make sure we've got plenty to go with here.\n smile \n" "030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \
"040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n"
void test_core_buffer__similarity_metric(void) void test_core_buffer__similarity_metric(void)
{ {
...@@ -761,15 +762,17 @@ void test_core_buffer__similarity_metric(void) ...@@ -761,15 +762,17 @@ void test_core_buffer__similarity_metric(void)
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_buf_sets(&buf, cl_git_pass(git_buf_sets(&buf,
"Test data\nright here\ninline\ntada\nneeds more data\nlots of data\n" "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
"is this enough?\nthere has to be enough data to fill the hash array!\n" "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
"Apparently 191 bytes is the minimum amount of data needed.\nHere goes!\n" "x020x\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \
"Let's make sure we've got plenty to go with here.\n smile \n")); "030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \
"040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n"
));
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
sim = git_hashsig_compare(a, b); sim = git_hashsig_compare(a, b);
cl_assert(95 < sim && sim < 100); /* expect >95% similarity */ cl_assert_in_range(95, sim, 100); /* expect >95% similarity */
git_hashsig_free(a); git_hashsig_free(a);
git_hashsig_free(b); git_hashsig_free(b);
...@@ -779,12 +782,13 @@ void test_core_buffer__similarity_metric(void) ...@@ -779,12 +782,13 @@ void test_core_buffer__similarity_metric(void)
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, 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 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")); "050\n051\n052\n053\n054\n055\n056\n057\n058\n059\n"));
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
sim = git_hashsig_compare(a, b); sim = git_hashsig_compare(a, b);
/* 20% lines added ~= 10% lines changed */
cl_assert(70 < sim && sim < 80); /* expect in the 70-80% similarity range */ cl_assert_in_range(85, sim, 95); /* expect similarity around 90% */
git_hashsig_free(a); git_hashsig_free(a);
git_hashsig_free(b); git_hashsig_free(b);
...@@ -794,15 +798,19 @@ void test_core_buffer__similarity_metric(void) ...@@ -794,15 +798,19 @@ void test_core_buffer__similarity_metric(void)
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_buf_sets(&buf, cl_git_pass(git_buf_sets(&buf,
"test data\nright here\ninline\ntada\nneeds more data\nlots of data\n" "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
"is this enough?\nthere has to be enough data to fill the hash array!\n" "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
"okay, that's half the original\nwhat else can we add?\nmore data\n" "020x\n021\n022\n023\n024\n" \
"one more line will complete this\nshort\nlines\ndon't\nmatter\n")); "x25\nx26\nx27\nx28\nx29\n" \
"x30\nx31\nx32\nx33\nx34\nx35\nx36\nx37\nx38\nx39\n" \
"x40\nx41\nx42\nx43\nx44\nx45\nx46\nx47\nx48\nx49\n"
));
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
sim = git_hashsig_compare(a, b); sim = git_hashsig_compare(a, b);
/* 50% lines changed */
cl_assert(40 < sim && sim < 60); /* expect in the 40-60% similarity range */ cl_assert_in_range(40, sim, 60); /* expect in the 40-60% similarity range */
git_hashsig_free(a); git_hashsig_free(a);
git_hashsig_free(b); git_hashsig_free(b);
...@@ -891,7 +899,7 @@ void test_core_buffer__similarity_metric_whitespace(void) ...@@ -891,7 +899,7 @@ void test_core_buffer__similarity_metric_whitespace(void)
if (i == j) if (i == j)
cl_assert_equal_i(100, sim); cl_assert_equal_i(100, sim);
else else
cl_assert(sim < 30); /* expect pretty different */ cl_assert_in_range(0, sim, 30); /* pretty different */
} else { } else {
cl_assert_equal_i(100, sim); cl_assert_equal_i(100, sim);
} }
......
...@@ -236,6 +236,8 @@ void test_diff_rename__not_exact_match(void) ...@@ -236,6 +236,8 @@ void test_diff_rename__not_exact_match(void)
&diff, g_repo, old_tree, new_tree, &diffopts)); &diff, g_repo, old_tree, new_tree, &diffopts));
opts.flags = GIT_DIFF_FIND_ALL; opts.flags = GIT_DIFF_FIND_ALL;
opts.break_rewrite_threshold = 70;
cl_git_pass(git_diff_find_similar(diff, &opts)); cl_git_pass(git_diff_find_similar(diff, &opts));
memset(&exp, 0, sizeof(exp)); memset(&exp, 0, sizeof(exp));
...@@ -312,8 +314,8 @@ void test_diff_rename__not_exact_match(void) ...@@ -312,8 +314,8 @@ void test_diff_rename__not_exact_match(void)
/* the default match algorithm is going to find the internal /* the default match algorithm is going to find the internal
* whitespace differences in the lines of sixserving.txt to be * whitespace differences in the lines of sixserving.txt to be
* significant enough that this will decide to split it into * significant enough that this will decide to split it into an ADD
* an ADD and a DELETE * and a DELETE
*/ */
memset(&exp, 0, sizeof(exp)); memset(&exp, 0, sizeof(exp));
...@@ -480,6 +482,7 @@ void test_diff_rename__working_directory_changes(void) ...@@ -480,6 +482,7 @@ void test_diff_rename__working_directory_changes(void)
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts)); cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts));
opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE; opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE;
opts.rename_threshold = 70;
cl_git_pass(git_diff_find_similar(diff, &opts)); cl_git_pass(git_diff_find_similar(diff, &opts));
memset(&exp, 0, sizeof(exp)); memset(&exp, 0, sizeof(exp));
...@@ -868,6 +871,8 @@ void test_diff_rename__from_deleted_to_split(void) ...@@ -868,6 +871,8 @@ void test_diff_rename__from_deleted_to_split(void)
struct rename_expected struct rename_expected
{ {
size_t len; size_t len;
unsigned int *status;
const char **sources; const char **sources;
const char **targets; const char **targets;
...@@ -882,7 +887,7 @@ int test_names_expected(const git_diff_delta *delta, float progress, void *p) ...@@ -882,7 +887,7 @@ int test_names_expected(const git_diff_delta *delta, float progress, void *p)
cl_assert(expected->idx < expected->len); cl_assert(expected->idx < expected->len);
cl_assert_equal_i(delta->status, GIT_DELTA_RENAMED); cl_assert_equal_i(delta->status, expected->status[expected->idx]);
cl_assert(git__strcmp(expected->sources[expected->idx], cl_assert(git__strcmp(expected->sources[expected->idx],
delta->old_file.path) == 0); delta->old_file.path) == 0);
...@@ -904,9 +909,10 @@ void test_diff_rename__rejected_match_can_match_others(void) ...@@ -904,9 +909,10 @@ void test_diff_rename__rejected_match_can_match_others(void)
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
git_buf one = GIT_BUF_INIT, two = GIT_BUF_INIT; git_buf one = GIT_BUF_INIT, two = GIT_BUF_INIT;
unsigned int status[] = { GIT_DELTA_RENAMED, GIT_DELTA_RENAMED };
const char *sources[] = { "Class1.cs", "Class2.cs" }; const char *sources[] = { "Class1.cs", "Class2.cs" };
const char *targets[] = { "ClassA.cs", "ClassB.cs" }; const char *targets[] = { "ClassA.cs", "ClassB.cs" };
struct rename_expected expect = { 2, sources, targets }; struct rename_expected expect = { 2, status, sources, targets };
char *ptr; char *ptr;
opts.checkout_strategy = GIT_CHECKOUT_FORCE; opts.checkout_strategy = GIT_CHECKOUT_FORCE;
...@@ -988,9 +994,10 @@ void test_diff_rename__rejected_match_can_match_others_two(void) ...@@ -988,9 +994,10 @@ void test_diff_rename__rejected_match_can_match_others_two(void)
git_diff_list *diff; git_diff_list *diff;
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
unsigned int status[] = { GIT_DELTA_RENAMED, GIT_DELTA_RENAMED };
const char *sources[] = { "a.txt", "b.txt" }; const char *sources[] = { "a.txt", "b.txt" };
const char *targets[] = { "c.txt", "d.txt" }; const char *targets[] = { "c.txt", "d.txt" };
struct rename_expected expect = { 2, sources, targets }; struct rename_expected expect = { 2, status, sources, targets };
opts.checkout_strategy = GIT_CHECKOUT_FORCE; opts.checkout_strategy = GIT_CHECKOUT_FORCE;
...@@ -1033,6 +1040,112 @@ void test_diff_rename__rejected_match_can_match_others_two(void) ...@@ -1033,6 +1040,112 @@ void test_diff_rename__rejected_match_can_match_others_two(void)
git_reference_free(selfsimilar); git_reference_free(selfsimilar);
} }
void test_diff_rename__rejected_match_can_match_others_three(void)
{
git_reference *head, *selfsimilar;
git_index *index;
git_tree *tree;
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
git_diff_list *diff;
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
/* Both cannot be renames from a.txt */
unsigned int status[] = { GIT_DELTA_ADDED, GIT_DELTA_RENAMED };
const char *sources[] = { "0001.txt", "a.txt" };
const char *targets[] = { "0001.txt", "0002.txt" };
struct rename_expected expect = { 2, status, sources, targets };
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
cl_git_pass(git_reference_symbolic_set_target(
&selfsimilar, head, "refs/heads/renames_similar_two"));
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(p_unlink("renames/a.txt"));
cl_git_pass(git_index_remove_bypath(index, "a.txt"));
write_similarity_file_two("renames/0001.txt", 7);
write_similarity_file_two("renames/0002.txt", 0);
cl_git_pass(git_index_add_bypath(index, "0001.txt"));
cl_git_pass(git_index_add_bypath(index, "0002.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(
git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));
cl_git_pass(
git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
cl_git_pass(git_diff_find_similar(diff, &findopts));
cl_git_pass(
git_diff_foreach(diff, test_names_expected, NULL, NULL, &expect));
cl_assert(expect.idx == expect.len);
git_diff_list_free(diff);
git_tree_free(tree);
git_index_free(index);
git_reference_free(head);
git_reference_free(selfsimilar);
}
void test_diff_rename__can_rename_from_rewrite(void)
{
git_index *index;
git_tree *tree;
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
git_diff_list *diff;
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
unsigned int status[] = { GIT_DELTA_RENAMED, GIT_DELTA_RENAMED };
const char *sources[] = { "ikeepsix.txt", "songof7cities.txt" };
const char *targets[] = { "songof7cities.txt", "this-is-a-rename.txt" };
struct rename_expected expect = { 2, status, sources, targets };
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(p_rename("renames/songof7cities.txt", "renames/this-is-a-rename.txt"));
cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/songof7cities.txt"));
cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_add_bypath(index, "songof7cities.txt"));
cl_git_pass(git_index_add_bypath(index, "this-is-a-rename.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(
git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));
cl_git_pass(
git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
findopts.flags |= GIT_DIFF_FIND_AND_BREAK_REWRITES |
GIT_DIFF_FIND_REWRITES |
GIT_DIFF_FIND_RENAMES_FROM_REWRITES;
cl_git_pass(git_diff_find_similar(diff, &findopts));
cl_git_pass(
git_diff_foreach(diff, test_names_expected, NULL, NULL, &expect));
cl_assert(expect.idx == expect.len);
git_diff_list_free(diff);
git_tree_free(tree);
git_index_free(index);
}
void test_diff_rename__case_changes_are_split(void) void test_diff_rename__case_changes_are_split(void)
{ {
git_index *index; git_index *index;
......
#include "clar_libgit2.h"
#include "../diff/diff_helpers.h"
static git_repository *g_repo = NULL;
void test_stress_diff__initialize(void)
{
}
void test_stress_diff__cleanup(void)
{
cl_git_sandbox_cleanup();
}
#define ANOTHER_POEM \
"OH, glorious are the guarded heights\nWhere guardian souls abide—\nSelf-exiled from our gross delights—\nAbove, beyond, outside:\nAn ampler arc their spirit swings—\nCommands a juster view—\nWe have their word for all these things,\nNo doubt their words are true.\n\nYet we, the bond slaves of our day,\nWhom dirt and danger press—\nCo-heirs of insolence, delay,\nAnd leagued unfaithfulness—\nSuch is our need must seek indeed\nAnd, having found, engage\nThe men who merely do the work\nFor which they draw the wage.\n\nFrom forge and farm and mine and bench,\nDeck, altar, outpost lone—\nMill, school, battalion, counter, trench,\nRail, senate, sheepfold, throne—\nCreation's cry goes up on high\nFrom age to cheated age:\n\"Send us the men who do the work\n\"For which they draw the wage!\"\n"
static void test_with_many(size_t expected_new)
{
git_index *index;
git_tree *tree, *new_tree;
git_diff_list *diff = NULL;
diff_expects exp;
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(
git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));
cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/ikeepsix2.txt"));
cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_add_bypath(index, "ikeepsix2.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, NULL, NULL, &exp));
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(expected_new + 1, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(expected_new + 2, exp.files);
opts.flags = GIT_DIFF_FIND_ALL;
cl_git_pass(git_diff_find_similar(diff, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, NULL, NULL, &exp));
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
cl_assert_equal_i(expected_new, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(expected_new + 1, exp.files);
git_diff_list_free(diff);
{
git_object *parent;
git_signature *sig;
git_oid tree_id, commit_id;
git_reference *ref;
cl_git_pass(git_index_write_tree(&tree_id, index));
cl_git_pass(git_tree_lookup(&new_tree, g_repo, &tree_id));
cl_git_pass(git_revparse_ext(&parent, &ref, g_repo, "HEAD"));
cl_git_pass(git_signature_new(
&sig, "Sm Test", "sm@tester.test", 1372350000, 480));
cl_git_pass(git_commit_create_v(
&commit_id, g_repo, git_reference_name(ref), sig, sig,
NULL, "yoyoyo", new_tree, 1, parent));
git_object_free(parent);
git_reference_free(ref);
git_signature_free(sig);
}
cl_git_pass(git_diff_tree_to_tree(
&diff, g_repo, tree, new_tree, &diffopts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, NULL, NULL, &exp));
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(expected_new + 1, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(expected_new + 2, exp.files);
opts.flags = GIT_DIFF_FIND_ALL;
cl_git_pass(git_diff_find_similar(diff, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, NULL, NULL, &exp));
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
cl_assert_equal_i(expected_new, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(expected_new + 1, exp.files);
git_diff_list_free(diff);
git_tree_free(new_tree);
git_tree_free(tree);
git_index_free(index);
}
void test_stress_diff__rename_big_files(void)
{
git_index *index;
char tmp[64];
int i, j;
git_buf b = GIT_BUF_INIT;
g_repo = cl_git_sandbox_init("renames");
cl_git_pass(git_repository_index(&index, g_repo));
for (i = 0; i < 100; i += 1) {
snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
for (j = i * 256; j > 0; --j)
git_buf_printf(&b, "more content %d\n", i);
cl_git_mkfile(tmp, b.ptr);
}
for (i = 0; i < 100; i += 1) {
snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
cl_git_pass(git_index_add_bypath(index, tmp + strlen("renames/")));
}
git_buf_free(&b);
git_index_free(index);
test_with_many(100);
}
void test_stress_diff__rename_many_files(void)
{
git_index *index;
char tmp[64];
int i;
git_buf b = GIT_BUF_INIT;
g_repo = cl_git_sandbox_init("renames");
cl_git_pass(git_repository_index(&index, g_repo));
git_buf_printf(&b, "%08d\n" ANOTHER_POEM "%08d\n" ANOTHER_POEM ANOTHER_POEM, 0, 0);
for (i = 0; i < 2500; i += 1) {
snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
snprintf(b.ptr, 9, "%08d", i);
b.ptr[8] = '\n';
cl_git_mkfile(tmp, b.ptr);
}
git_buf_free(&b);
for (i = 0; i < 2500; i += 1) {
snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
cl_git_pass(git_index_add_bypath(index, tmp + strlen("renames/")));
}
git_index_free(index);
test_with_many(2500);
}
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