From 36fc5497810f60cacdfab249c84583d25032a150 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Latour Date: Tue, 2 Dec 2014 05:11:12 -0800 Subject: Added GIT_HASHSIG_ALLOW_SMALL_FILES to allow computing signatures for small files The implementation of the hashsig API disallows computing a signature on small files containing only a few lines. This new flag disables this behavior. git_diff_find_similar() sets this flag by default which means that rename / copy detection of small files will now work. This in turn affects the behavior of the git_status and git_blame APIs which will now detect rename of small files assuming the right options are passed. --- tests/diff/rename.c | 54 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'tests/diff/rename.c') diff --git a/tests/diff/rename.c b/tests/diff/rename.c index 4bc3eb54c..28e0bf149 100644 --- a/tests/diff/rename.c +++ b/tests/diff/rename.c @@ -381,37 +381,53 @@ void test_diff_rename__not_exact_match(void) git_tree_free(new_tree); } -void test_diff_rename__handles_small_files(void) +void test_diff_rename__test_small_files(void) { - const char *tree_sha = "2bc7f351d20b53f1c72c16c4b036e491c478c49a"; git_index *index; - git_tree *tree; + git_reference *head_reference; + git_commit *head_commit; + git_tree *head_tree; + git_tree *commit_tree; + git_signature *signature; git_diff *diff; - git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; - git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; + git_oid oid; + const git_diff_delta *delta; + git_diff_options diff_options = GIT_DIFF_OPTIONS_INIT; + git_diff_find_options find_options = GIT_DIFF_FIND_OPTIONS_INIT; cl_git_pass(git_repository_index(&index, g_repo)); - tree = resolve_commit_oid_to_tree(g_repo, tree_sha); + cl_git_mkfile("renames/small.txt", "Hello World!\n"); + cl_git_pass(git_index_add_bypath(index, "small.txt")); - cl_git_rewritefile("renames/songof7cities.txt", "single line\n"); - cl_git_pass(git_index_add_bypath(index, "songof7cities.txt")); + cl_git_pass(git_repository_head(&head_reference, g_repo)); + cl_git_pass(git_reference_peel((git_object**)&head_commit, head_reference, GIT_OBJ_COMMIT)); + cl_git_pass(git_commit_tree(&head_tree, head_commit)); + cl_git_pass(git_index_write_tree(&oid, index)); + cl_git_pass(git_tree_lookup(&commit_tree, g_repo, &oid)); + cl_git_pass(git_signature_new(&signature, "Rename", "rename@example.com", 1404157834, 0)); + cl_git_pass(git_commit_create(&oid, g_repo, "HEAD", signature, signature, NULL, "Test commit", commit_tree, 1, (const git_commit**)&head_commit)); - cl_git_rewritefile("renames/untimely.txt", "untimely\n"); - cl_git_pass(git_index_add_bypath(index, "untimely.txt")); + cl_git_mkfile("renames/copy.txt", "Hello World!\n"); + cl_git_rmfile("renames/small.txt"); - /* Tests that we can invoke find_similar on small files - * and that the GIT_EBUFS (too small) error code is not - * propagated to the caller. - */ - cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); + diff_options.flags = GIT_DIFF_INCLUDE_UNTRACKED; + cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, commit_tree, &diff_options)); + find_options.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_FOR_UNTRACKED; + cl_git_pass(git_diff_find_similar(diff, &find_options)); - 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_assert_equal_i(git_diff_num_deltas(diff), 1); + delta = git_diff_get_delta(diff, 0); + cl_assert_equal_i(delta->status, GIT_DELTA_RENAMED); + cl_assert_equal_s(delta->old_file.path, "small.txt"); + cl_assert_equal_s(delta->new_file.path, "copy.txt"); git_diff_free(diff); - git_tree_free(tree); + git_signature_free(signature); + git_tree_free(commit_tree); + git_tree_free(head_tree); + git_commit_free(head_commit); + git_reference_free(head_reference); git_index_free(index); } -- cgit v1.2.3