diff options
Diffstat (limited to 'tests')
50 files changed, 1627 insertions, 345 deletions
diff --git a/tests/blame/buffer.c b/tests/blame/buffer.c index 912ee9846..340b1dced 100644 --- a/tests/blame/buffer.c +++ b/tests/blame/buffer.c @@ -1,7 +1,7 @@ #include "blame_helpers.h" -git_repository *g_repo; -git_blame *g_fileblame, *g_bufferblame; +static git_repository *g_repo; +static git_blame *g_fileblame, *g_bufferblame; void test_blame_buffer__initialize(void) { diff --git a/tests/blame/simple.c b/tests/blame/simple.c index 79bd56b83..11ff4cd19 100644 --- a/tests/blame/simple.c +++ b/tests/blame/simple.c @@ -303,3 +303,18 @@ void test_blame_simple__can_restrict_to_newish_commits(void) check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, 1, "be3563a", "branch_file.txt"); check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, 0, "a65fedf", "branch_file.txt"); } + +void test_blame_simple__can_restrict_to_first_parent_commits(void) +{ + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + opts.flags |= GIT_BLAME_FIRST_PARENT; + + cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git"))); + + cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts)); + cl_assert_equal_i(4, git_blame_get_hunk_count(g_blame)); + check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, 0, "da237394", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 2, 6, 5, 0, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 3, 11, 5, 0, "bc7c5ac2", "b.txt"); +} diff --git a/tests/checkout/conflict.c b/tests/checkout/conflict.c index a8b93b28d..2fea511da 100644 --- a/tests/checkout/conflict.c +++ b/tests/checkout/conflict.c @@ -61,12 +61,19 @@ struct checkout_name_entry { void test_checkout_conflict__initialize(void) { + git_config *cfg; + g_repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&g_index, g_repo); cl_git_rewritefile( TEST_REPO_PATH "/.gitattributes", "* text eol=lf\n"); + + /* Ensure that the user's merge.conflictstyle doesn't interfere */ + cl_git_pass(git_repository_config(&cfg, g_repo)); + cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge")); + git_config_free(cfg); } void test_checkout_conflict__cleanup(void) diff --git a/tests/checkout/crlf.c b/tests/checkout/crlf.c index cba79432f..0d78ee039 100644 --- a/tests/checkout/crlf.c +++ b/tests/checkout/crlf.c @@ -18,19 +18,6 @@ void test_checkout_crlf__cleanup(void) cl_git_sandbox_cleanup(); } -void test_checkout_crlf__detect_crlf_autocrlf_false(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - cl_repo_set_bool(g_repo, "core.autocrlf", false); - - git_checkout_head(g_repo, &opts); - - check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); - check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); -} - void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void) { git_index *index; @@ -100,18 +87,6 @@ void test_checkout_crlf__more_crlf_autocrlf_true(void) check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_AS_CRLF); } -void test_checkout_crlf__all_crlf_autocrlf_true(void) -{ - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; - - cl_repo_set_bool(g_repo, "core.autocrlf", true); - - git_checkout_head(g_repo, &opts); - - check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); -} - void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void) { git_index *index; @@ -229,3 +204,97 @@ void test_checkout_crlf__with_ident(void) git_index_free(index); } + +void test_checkout_crlf__autocrlf_false_no_attrs(void) +{ + git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; + opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; + + cl_repo_set_bool(g_repo, "core.autocrlf", false); + + git_checkout_head(g_repo, &opts); + + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); +} + +void test_checkout_crlf__autocrlf_true_no_attrs(void) +{ + git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; + opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; + + cl_repo_set_bool(g_repo, "core.autocrlf", true); + + git_checkout_head(g_repo, &opts); + + if (GIT_EOL_NATIVE == GIT_EOL_CRLF) { + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF); + } else { + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); + } +} + +void test_checkout_crlf__autocrlf_input_no_attrs(void) +{ + git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; + opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; + + cl_repo_set_string(g_repo, "core.autocrlf", "input"); + + git_checkout_head(g_repo, &opts); + + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); +} + +void test_checkout_crlf__autocrlf_false_text_auto_attr(void) +{ + git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; + opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; + + cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n"); + + cl_repo_set_bool(g_repo, "core.autocrlf", false); + + git_checkout_head(g_repo, &opts); + + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); +} + +void test_checkout_crlf__autocrlf_true_text_auto_attr(void) +{ + git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; + opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; + + cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n"); + + cl_repo_set_bool(g_repo, "core.autocrlf", true); + + git_checkout_head(g_repo, &opts); + + if (GIT_EOL_NATIVE == GIT_EOL_CRLF) { + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF); + } else { + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); + } +} + +void test_checkout_crlf__autocrlf_input_text_auto_attr(void) +{ + git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; + opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; + + cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n"); + + cl_repo_set_string(g_repo, "core.autocrlf", "input"); + + git_checkout_head(g_repo, &opts); + + check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW); + check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW); +} diff --git a/tests/checkout/tree.c b/tests/checkout/tree.c index 407908ad3..f433b2698 100644 --- a/tests/checkout/tree.c +++ b/tests/checkout/tree.c @@ -571,7 +571,7 @@ void test_checkout_tree__donot_update_deleted_file_by_default(void) cl_git_pass(git_oid_fromstr(&old_id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); cl_git_pass(git_commit_lookup(&old_commit, g_repo, &old_id)); - cl_git_pass(git_reset(g_repo, (git_object *)old_commit, GIT_RESET_HARD)); + cl_git_pass(git_reset(g_repo, (git_object *)old_commit, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(p_unlink("testrepo/branch_file.txt")); cl_git_pass(git_index_remove_bypath(index ,"branch_file.txt")); diff --git a/tests/clar_libgit2.c b/tests/clar_libgit2.c index 50762cdb8..9b062ef78 100644 --- a/tests/clar_libgit2.c +++ b/tests/clar_libgit2.c @@ -420,6 +420,14 @@ int cl_repo_get_bool(git_repository *repo, const char *cfg) return val; } +void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value) +{ + git_config *config; + cl_git_pass(git_repository_config(&config, repo)); + cl_git_pass(git_config_set_string(config, cfg, value)); + git_config_free(config); +} + /* this is essentially the code from git__unescape modified slightly */ static size_t strip_cr_from_buf(char *start, size_t len) { @@ -468,6 +476,7 @@ void clar__assert_equal_file( p_snprintf( buf, sizeof(buf), "file content mismatch at byte %d", (int)(total_bytes + pos)); + p_close(fd); clar__fail(file, line, buf, path, 1); } diff --git a/tests/clar_libgit2.h b/tests/clar_libgit2.h index b9ef5627e..915111244 100644 --- a/tests/clar_libgit2.h +++ b/tests/clar_libgit2.h @@ -116,4 +116,6 @@ void cl_repo_commit_from_index( void cl_repo_set_bool(git_repository *repo, const char *cfg, int value); int cl_repo_get_bool(git_repository *repo, const char *cfg); +void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value); + #endif diff --git a/tests/commit/write.c b/tests/commit/write.c index 8e5b67f2f..b1cdf4485 100644 --- a/tests/commit/write.c +++ b/tests/commit/write.c @@ -7,6 +7,8 @@ static const char *commit_message = "This commit has been created in memory\n\ static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd"; static const char *root_commit_message = "This is a root commit\n\ This is a root commit and should be the only one in this branch\n"; +static const char *root_reflog_message = "commit (initial): This is a root commit \ + This is a root commit and should be the only one in this branch"; static char *head_old; static git_reference *head, *branch; static git_commit *commit; @@ -101,6 +103,8 @@ void test_commit_write__root(void) git_signature *author, *committer; const char *branch_name = "refs/heads/root-commit-branch"; git_tree *tree; + git_reflog *log; + const git_reflog_entry *entry; git_oid_fromstr(&tree_id, tree_oid); cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); @@ -130,7 +134,6 @@ void test_commit_write__root(void) 0)); git_object_free((git_object *)tree); - git_signature_free(committer); git_signature_free(author); /* @@ -144,4 +147,14 @@ void test_commit_write__root(void) branch_oid = git_reference_target(branch); cl_git_pass(git_oid_cmp(branch_oid, &commit_id)); cl_assert_equal_s(root_commit_message, git_commit_message(commit)); + + cl_git_pass(git_reflog_read(&log, g_repo, branch_name)); + cl_assert_equal_i(1, git_reflog_entrycount(log)); + entry = git_reflog_entry_byindex(log, 0); + cl_assert_equal_s(committer->email, git_reflog_entry_committer(entry)->email); + cl_assert_equal_s(committer->name, git_reflog_entry_committer(entry)->name); + cl_assert_equal_s(root_reflog_message, git_reflog_entry_message(entry)); + + git_signature_free(committer); + git_reflog_free(log); } diff --git a/tests/core/env.c b/tests/core/env.c index 0fa6472d7..a32f5ed3e 100644 --- a/tests/core/env.c +++ b/tests/core/env.c @@ -1,5 +1,6 @@ #include "clar_libgit2.h" #include "fileops.h" +#include "sysdir.h" #include "path.h" #ifdef GIT_WIN32 @@ -41,12 +42,12 @@ void test_core_env__initialize(void) static void reset_global_search_path(void) { - cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL)); + cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL, NULL)); } static void reset_system_search_path(void) { - cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL)); + cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, NULL)); } void test_core_env__cleanup(void) @@ -120,18 +121,18 @@ void test_core_env__0(void) git_buf_rtruncate_at_char(&path, '/'); cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile)); setenv_and_check("HOME", path.ptr); reset_global_search_path(); - cl_git_pass(git_futils_find_global_file(&found, testfile)); + cl_git_pass(git_sysdir_find_global_file(&found, testfile)); cl_setenv("HOME", env_save[0]); reset_global_search_path(); cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile)); #ifdef GIT_WIN32 setenv_and_check("HOMEDRIVE", NULL); @@ -139,7 +140,7 @@ void test_core_env__0(void) setenv_and_check("USERPROFILE", path.ptr); reset_global_search_path(); - cl_git_pass(git_futils_find_global_file(&found, testfile)); + cl_git_pass(git_sysdir_find_global_file(&found, testfile)); { int root = git_path_root(path.ptr); @@ -150,7 +151,7 @@ void test_core_env__0(void) reset_global_search_path(); cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile)); old = path.ptr[root]; path.ptr[root] = '\0'; @@ -159,7 +160,7 @@ void test_core_env__0(void) setenv_and_check("HOMEPATH", &path.ptr[root]); reset_global_search_path(); - cl_git_pass(git_futils_find_global_file(&found, testfile)); + cl_git_pass(git_sysdir_find_global_file(&found, testfile)); } } #endif @@ -177,7 +178,7 @@ void test_core_env__1(void) git_buf path = GIT_BUF_INIT; cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); + GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile")); cl_git_pass(cl_setenv("HOME", "doesnotexist")); #ifdef GIT_WIN32 @@ -187,7 +188,7 @@ void test_core_env__1(void) reset_global_search_path(); cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); + GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile")); cl_git_pass(cl_setenv("HOME", NULL)); #ifdef GIT_WIN32 @@ -198,17 +199,17 @@ void test_core_env__1(void) reset_system_search_path(); cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); + GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile")); cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile")); + GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile")); #ifdef GIT_WIN32 cl_git_pass(cl_setenv("PROGRAMFILES", NULL)); reset_system_search_path(); cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile")); + GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile")); #endif git_buf_free(&path); @@ -242,13 +243,13 @@ static void check_global_searchpath( cl_assert_equal_s(out, path); /* find file using new path */ - cl_git_pass(git_futils_find_global_file(temp, file)); + cl_git_pass(git_sysdir_find_global_file(temp, file)); /* reset path and confirm file not found */ cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(temp, file)); + GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file)); } void test_core_env__2(void) @@ -285,7 +286,7 @@ void test_core_env__2(void) /* default should be NOTFOUND */ cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile)); /* try plain, append $PATH, and prepend $PATH */ check_global_searchpath(path.ptr, 0, testfile, &found); diff --git a/tests/core/pqueue.c b/tests/core/pqueue.c new file mode 100644 index 000000000..d91dbb0cd --- /dev/null +++ b/tests/core/pqueue.c @@ -0,0 +1,97 @@ +#include "clar_libgit2.h" +#include "pqueue.h" + +static int cmp_ints(const void *v1, const void *v2) +{ + int i1 = *(int *)v1, i2 = *(int *)v2; + return (i1 < i2) ? -1 : (i1 > i2) ? 1 : 0; +} + +void test_core_pqueue__items_are_put_in_order(void) +{ + git_pqueue pq; + int i, vals[20]; + + cl_git_pass(git_pqueue_init(&pq, 0, 20, cmp_ints)); + + for (i = 0; i < 20; ++i) { + if (i < 10) + vals[i] = 10 - i; /* 10 down to 1 */ + else + vals[i] = i + 1; /* 11 up to 20 */ + + cl_git_pass(git_pqueue_insert(&pq, &vals[i])); + } + + cl_assert_equal_i(20, git_pqueue_size(&pq)); + + for (i = 1; i <= 20; ++i) { + void *p = git_pqueue_pop(&pq); + cl_assert(p); + cl_assert_equal_i(i, *(int *)p); + } + + cl_assert_equal_i(0, git_pqueue_size(&pq)); + + git_pqueue_free(&pq); +} + +void test_core_pqueue__interleave_inserts_and_pops(void) +{ + git_pqueue pq; + int chunk, v, i, vals[200]; + + cl_git_pass(git_pqueue_init(&pq, 0, 20, cmp_ints)); + + for (v = 0, chunk = 20; chunk <= 200; chunk += 20) { + /* push the next 20 */ + for (; v < chunk; ++v) { + vals[v] = (v & 1) ? 200 - v : v; + cl_git_pass(git_pqueue_insert(&pq, &vals[v])); + } + + /* pop the lowest 10 */ + for (i = 0; i < 10; ++i) + (void)git_pqueue_pop(&pq); + } + + cl_assert_equal_i(100, git_pqueue_size(&pq)); + + /* at this point, we've popped 0-99 */ + + for (v = 100; v < 200; ++v) { + void *p = git_pqueue_pop(&pq); + cl_assert(p); + cl_assert_equal_i(v, *(int *)p); + } + + cl_assert_equal_i(0, git_pqueue_size(&pq)); + + git_pqueue_free(&pq); +} + +void test_core_pqueue__max_heap_size(void) +{ + git_pqueue pq; + int i, vals[100]; + + cl_git_pass(git_pqueue_init(&pq, GIT_PQUEUE_FIXED_SIZE, 50, cmp_ints)); + + for (i = 0; i < 100; ++i) { + vals[i] = (i & 1) ? 100 - i : i; + cl_git_pass(git_pqueue_insert(&pq, &vals[i])); + } + + cl_assert_equal_i(50, git_pqueue_size(&pq)); + + for (i = 50; i < 100; ++i) { + void *p = git_pqueue_pop(&pq); + cl_assert(p); + cl_assert_equal_i(i, *(int *)p); + } + + cl_assert_equal_i(0, git_pqueue_size(&pq)); + + git_pqueue_free(&pq); + +} diff --git a/tests/diff/blob.c b/tests/diff/blob.c index 74df3cf85..d1fff9c5b 100644 --- a/tests/diff/blob.c +++ b/tests/diff/blob.c @@ -51,6 +51,20 @@ void test_diff_blob__cleanup(void) cl_git_sandbox_cleanup(); } +static void assert_one_modified( + int hunks, int lines, int ctxt, int adds, int dels, diff_expects *exp) +{ + cl_assert_equal_i(1, exp->files); + cl_assert_equal_i(1, exp->file_status[GIT_DELTA_MODIFIED]); + cl_assert_equal_i(0, exp->files_binary); + + cl_assert_equal_i(hunks, exp->hunks); + cl_assert_equal_i(lines, exp->lines); + cl_assert_equal_i(ctxt, exp->line_ctxt); + cl_assert_equal_i(adds, exp->line_adds); + cl_assert_equal_i(dels, exp->line_dels); +} + void test_diff_blob__can_compare_text_blobs(void) { git_blob *a, *b, *c; @@ -71,79 +85,81 @@ void test_diff_blob__can_compare_text_blobs(void) /* Doing the equivalent of a `git diff -U1` on these files */ /* diff on tests/resources/attr/root_test1 */ + memset(&expected, 0, sizeof(expected)); cl_git_pass(git_diff_blobs( a, NULL, b, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); + assert_one_modified(1, 6, 1, 5, 0, &expected); - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(6, expected.lines); - cl_assert_equal_i(1, expected.line_ctxt); - cl_assert_equal_i(5, expected.line_adds); - cl_assert_equal_i(0, expected.line_dels); + /* same diff but use direct buffers */ + memset(&expected, 0, sizeof(expected)); + cl_git_pass(git_diff_buffers( + git_blob_rawcontent(a), (size_t)git_blob_rawsize(a), NULL, + git_blob_rawcontent(b), (size_t)git_blob_rawsize(b), NULL, &opts, + diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); + assert_one_modified(1, 6, 1, 5, 0, &expected); /* diff on tests/resources/attr/root_test2 */ memset(&expected, 0, sizeof(expected)); cl_git_pass(git_diff_blobs( b, NULL, c, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(15, expected.lines); - cl_assert_equal_i(3, expected.line_ctxt); - cl_assert_equal_i(9, expected.line_adds); - cl_assert_equal_i(3, expected.line_dels); + assert_one_modified(1, 15, 3, 9, 3, &expected); /* diff on tests/resources/attr/root_test3 */ memset(&expected, 0, sizeof(expected)); cl_git_pass(git_diff_blobs( a, NULL, c, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(13, expected.lines); - cl_assert_equal_i(0, expected.line_ctxt); - cl_assert_equal_i(12, expected.line_adds); - cl_assert_equal_i(1, expected.line_dels); + assert_one_modified(1, 13, 0, 12, 1, &expected); memset(&expected, 0, sizeof(expected)); cl_git_pass(git_diff_blobs( c, NULL, d, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); - - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - - cl_assert_equal_i(2, expected.hunks); - cl_assert_equal_i(14, expected.lines); - cl_assert_equal_i(4, expected.line_ctxt); - cl_assert_equal_i(6, expected.line_adds); - cl_assert_equal_i(4, expected.line_dels); + assert_one_modified(2, 14, 4, 6, 4, &expected); git_blob_free(a); git_blob_free(b); git_blob_free(c); } +static void assert_patch_matches_blobs( + git_patch *p, git_blob *a, git_blob *b, + int hunks, int l0, int l1, int ctxt, int adds, int dels) +{ + const git_diff_delta *delta; + size_t tc, ta, td; + + cl_assert(p != NULL); + + delta = git_patch_get_delta(p); + cl_assert(delta != NULL); + + cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); + cl_assert(git_oid_equal(git_blob_id(a), &delta->old_file.id)); + cl_assert_equal_sz(git_blob_rawsize(a), delta->old_file.size); + cl_assert(git_oid_equal(git_blob_id(b), &delta->new_file.id)); + cl_assert_equal_sz(git_blob_rawsize(b), delta->new_file.size); + + cl_assert_equal_i(hunks, (int)git_patch_num_hunks(p)); + + if (hunks > 0) + cl_assert_equal_i(l0, git_patch_num_lines_in_hunk(p, 0)); + if (hunks > 1) + cl_assert_equal_i(l1, git_patch_num_lines_in_hunk(p, 1)); + + cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); + cl_assert_equal_i(ctxt, (int)tc); + cl_assert_equal_i(adds, (int)ta); + cl_assert_equal_i(dels, (int)td); +} + void test_diff_blob__can_compare_text_blobs_with_patch(void) { git_blob *a, *b, *c; git_oid a_oid, b_oid, c_oid; git_patch *p; - const git_diff_delta *delta; - size_t tc, ta, td; /* tests/resources/attr/root_test1 */ cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); @@ -161,92 +177,22 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) /* diff on tests/resources/attr/root_test1 */ cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, &opts)); - - cl_assert(p != NULL); - - delta = git_patch_get_delta(p); - cl_assert(delta != NULL); - cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); - cl_assert(git_oid_equal(git_blob_id(a), &delta->old_file.id)); - cl_assert_equal_sz(git_blob_rawsize(a), delta->old_file.size); - cl_assert(git_oid_equal(git_blob_id(b), &delta->new_file.id)); - cl_assert_equal_sz(git_blob_rawsize(b), delta->new_file.size); - - cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); - cl_assert_equal_i(6, git_patch_num_lines_in_hunk(p, 0)); - - cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); - cl_assert_equal_i(1, (int)tc); - cl_assert_equal_i(5, (int)ta); - cl_assert_equal_i(0, (int)td); - + assert_patch_matches_blobs(p, a, b, 1, 6, 0, 1, 5, 0); git_patch_free(p); /* diff on tests/resources/attr/root_test2 */ cl_git_pass(git_patch_from_blobs(&p, b, NULL, c, NULL, &opts)); - - cl_assert(p != NULL); - - delta = git_patch_get_delta(p); - cl_assert(delta != NULL); - cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); - cl_assert(git_oid_equal(git_blob_id(b), &delta->old_file.id)); - cl_assert_equal_sz(git_blob_rawsize(b), delta->old_file.size); - cl_assert(git_oid_equal(git_blob_id(c), &delta->new_file.id)); - cl_assert_equal_sz(git_blob_rawsize(c), delta->new_file.size); - - cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); - cl_assert_equal_i(15, git_patch_num_lines_in_hunk(p, 0)); - - cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); - cl_assert_equal_i(3, (int)tc); - cl_assert_equal_i(9, (int)ta); - cl_assert_equal_i(3, (int)td); - + assert_patch_matches_blobs(p, b, c, 1, 15, 0, 3, 9, 3); git_patch_free(p); /* diff on tests/resources/attr/root_test3 */ cl_git_pass(git_patch_from_blobs(&p, a, NULL, c, NULL, &opts)); - - cl_assert(p != NULL); - - delta = git_patch_get_delta(p); - cl_assert(delta != NULL); - cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); - cl_assert(git_oid_equal(git_blob_id(a), &delta->old_file.id)); - cl_assert_equal_sz(git_blob_rawsize(a), delta->old_file.size); - cl_assert(git_oid_equal(git_blob_id(c), &delta->new_file.id)); - cl_assert_equal_sz(git_blob_rawsize(c), delta->new_file.size); - - cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); - cl_assert_equal_i(0, (int)tc); - cl_assert_equal_i(12, (int)ta); - cl_assert_equal_i(1, (int)td); - + assert_patch_matches_blobs(p, a, c, 1, 13, 0, 0, 12, 1); git_patch_free(p); /* one more */ cl_git_pass(git_patch_from_blobs(&p, c, NULL, d, NULL, &opts)); - - cl_assert(p != NULL); - - delta = git_patch_get_delta(p); - cl_assert(delta != NULL); - cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); - cl_assert(git_oid_equal(git_blob_id(c), &delta->old_file.id)); - cl_assert_equal_sz(git_blob_rawsize(c), delta->old_file.size); - cl_assert(git_oid_equal(git_blob_id(d), &delta->new_file.id)); - cl_assert_equal_sz(git_blob_rawsize(d), delta->new_file.size); - - cl_assert_equal_i(2, (int)git_patch_num_hunks(p)); - cl_assert_equal_i(5, git_patch_num_lines_in_hunk(p, 0)); - cl_assert_equal_i(9, git_patch_num_lines_in_hunk(p, 1)); - - cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); - cl_assert_equal_i(4, (int)tc); - cl_assert_equal_i(6, (int)ta); - cl_assert_equal_i(4, (int)td); - + assert_patch_matches_blobs(p, c, d, 2, 5, 9, 4, 6, 4); git_patch_free(p); git_blob_free(a); @@ -656,14 +602,7 @@ void test_diff_blob__can_compare_blob_to_buffer(void) /* diff from blob a to content of b */ quick_diff_blob_to_str(a, NULL, b_content, 0, NULL); - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(6, expected.lines); - cl_assert_equal_i(1, expected.line_ctxt); - cl_assert_equal_i(5, expected.line_adds); - cl_assert_equal_i(0, expected.line_dels); + assert_one_modified(1, 6, 1, 5, 0, &expected); /* diff from blob a to content of a */ opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; @@ -910,14 +849,7 @@ void test_diff_blob__using_path_and_attributes(void) changed = "Hello from the root\nMore lines\nAnd more\nGo here\n"; quick_diff_blob_to_str(nonbin, NULL, changed, 0, NULL); - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(3, expected.lines); - cl_assert_equal_i(0, expected.line_ctxt); - cl_assert_equal_i(3, expected.line_adds); - cl_assert_equal_i(0, expected.line_dels); + assert_one_modified(1, 3, 0, 3, 0, &expected); quick_diff_blob_to_str(nonbin, "foo/bar.binary", changed, 0, NULL); cl_assert_equal_i(1, expected.files); @@ -925,29 +857,12 @@ void test_diff_blob__using_path_and_attributes(void) cl_assert_equal_i(1, expected.files_binary); cl_assert_equal_i(0, expected.hunks); cl_assert_equal_i(0, expected.lines); - cl_assert_equal_i(0, expected.line_ctxt); - cl_assert_equal_i(0, expected.line_adds); - cl_assert_equal_i(0, expected.line_dels); quick_diff_blob_to_str(nonbin, "foo/bar.textary", changed, 0, NULL); - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(3, expected.lines); - cl_assert_equal_i(0, expected.line_ctxt); - cl_assert_equal_i(3, expected.line_adds); - cl_assert_equal_i(0, expected.line_dels); + assert_one_modified(1, 3, 0, 3, 0, &expected); quick_diff_blob_to_str(nonbin, "foo/bar.alphary", changed, 0, NULL); - cl_assert_equal_i(1, expected.files); - cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); - cl_assert_equal_i(0, expected.files_binary); - cl_assert_equal_i(1, expected.hunks); - cl_assert_equal_i(3, expected.lines); - cl_assert_equal_i(0, expected.line_ctxt); - cl_assert_equal_i(3, expected.line_adds); - cl_assert_equal_i(0, expected.line_dels); + assert_one_modified(1, 3, 0, 3, 0, &expected); cl_git_pass(git_patch_from_blob_and_buffer( &p, nonbin, "zzz.normal", changed, strlen(changed), NULL, &opts)); @@ -1066,3 +981,28 @@ void test_diff_blob__using_path_and_attributes(void) git_blob_free(nonbin); git_blob_free(bin); } + +void test_diff_blob__can_compare_buffer_to_buffer(void) +{ + const char *a = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n"; + const char *b = "a\nB\nc\nd\nE\nF\nh\nj\nk\n"; + + opts.interhunk_lines = 0; + opts.context_lines = 0; + + memset(&expected, 0, sizeof(expected)); + + cl_git_pass(git_diff_buffers( + a, strlen(a), NULL, b, strlen(b), NULL, + &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); + assert_one_modified(4, 9, 0, 4, 5, &expected); + + opts.flags ^= GIT_DIFF_REVERSE; + + memset(&expected, 0, sizeof(expected)); + + cl_git_pass(git_diff_buffers( + a, strlen(a), NULL, b, strlen(b), NULL, + &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); + assert_one_modified(4, 9, 0, 5, 4, &expected); +} diff --git a/tests/filter/blob.c b/tests/filter/blob.c index 8dce6470a..48edb8405 100644 --- a/tests/filter/blob.c +++ b/tests/filter/blob.c @@ -41,7 +41,8 @@ void test_filter_blob__all_crlf(void) cl_git_pass(git_blob_filtered_content(&buf, blob, "file.lf", 1)); - cl_assert_equal_s(ALL_CRLF_TEXT_AS_LF, buf.ptr); + /* we never convert CRLF -> LF on platforms that have LF */ + cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr); git_buf_free(&buf); git_blob_free(blob); diff --git a/tests/index/addall.c b/tests/index/addall.c index 452733710..a7e2583b2 100644 --- a/tests/index/addall.c +++ b/tests/index/addall.c @@ -3,7 +3,7 @@ #include "posix.h" #include "fileops.h" -git_repository *g_repo = NULL; +static git_repository *g_repo = NULL; #define TEST_DIR "addall" void test_index_addall__initialize(void) diff --git a/tests/index/crlf.c b/tests/index/crlf.c new file mode 100644 index 000000000..cf69c6226 --- /dev/null +++ b/tests/index/crlf.c @@ -0,0 +1,136 @@ +#include "clar_libgit2.h" +#include "../filter/crlf.h" + +#include "git2/checkout.h" +#include "repository.h" +#include "posix.h" + +#define FILE_CONTENTS_LF "one\ntwo\nthree\nfour\n" +#define FILE_CONTENTS_CRLF "one\r\ntwo\r\nthree\r\nfour\r\n" + +#define FILE_OID_LF "f384549cbeb481e437091320de6d1f2e15e11b4a" +#define FILE_OID_CRLF "7fbf4d847b191141d80f30c8ab03d2ad4cd543a9" + +static git_repository *g_repo; +static git_index *g_index; + +void test_index_crlf__initialize(void) +{ + g_repo = cl_git_sandbox_init("crlf"); + cl_git_pass(git_repository_index(&g_index, g_repo)); +} + +void test_index_crlf__cleanup(void) +{ + git_index_free(g_index); + cl_git_sandbox_cleanup(); +} + +void test_index_crlf__autocrlf_false_no_attrs(void) +{ + const git_index_entry *entry; + git_oid oid; + + cl_repo_set_bool(g_repo, "core.autocrlf", false); + + cl_git_mkfile("./crlf/newfile.txt", + (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF); + + cl_git_pass(git_index_add_bypath(g_index, "newfile.txt")); + entry = git_index_get_bypath(g_index, "newfile.txt", 0); + + cl_git_pass(git_oid_fromstr(&oid, + (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_OID_CRLF : FILE_OID_LF)); + cl_assert(git_oid_cmp(&oid, &entry->id) == 0); +} + +void test_index_crlf__autocrlf_true_no_attrs(void) +{ + const git_index_entry *entry; + git_oid oid; + + cl_repo_set_bool(g_repo, "core.autocrlf", true); + + cl_git_mkfile("./crlf/newfile.txt", + (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF); + + cl_git_pass(git_index_add_bypath(g_index, "newfile.txt")); + entry = git_index_get_bypath(g_index, "newfile.txt", 0); + + cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF)); + cl_assert(git_oid_cmp(&oid, &entry->id) == 0); +} + +void test_index_crlf__autocrlf_input_no_attrs(void) +{ + const git_index_entry *entry; + git_oid oid; + + cl_repo_set_string(g_repo, "core.autocrlf", "input"); + + cl_git_mkfile("./crlf/newfile.txt", + (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF); + + cl_git_pass(git_index_add_bypath(g_index, "newfile.txt")); + entry = git_index_get_bypath(g_index, "newfile.txt", 0); + + cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF)); + cl_assert(git_oid_cmp(&oid, &entry->id) == 0); +} + +void test_index_crlf__autocrlf_false_text_auto_attr(void) +{ + const git_index_entry *entry; + git_oid oid; + + cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n"); + + cl_repo_set_bool(g_repo, "core.autocrlf", false); + + cl_git_mkfile("./crlf/newfile.txt", + (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF); + + cl_git_pass(git_index_add_bypath(g_index, "newfile.txt")); + entry = git_index_get_bypath(g_index, "newfile.txt", 0); + + cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF)); + cl_assert(git_oid_cmp(&oid, &entry->id) == 0); +} + +void test_index_crlf__autocrlf_true_text_auto_attr(void) +{ + const git_index_entry *entry; + git_oid oid; + + cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n"); + + cl_repo_set_bool(g_repo, "core.autocrlf", false); + + cl_git_mkfile("./crlf/newfile.txt", + (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF); + + cl_git_pass(git_index_add_bypath(g_index, "newfile.txt")); + entry = git_index_get_bypath(g_index, "newfile.txt", 0); + + cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF)); + cl_assert(git_oid_cmp(&oid, &entry->id) == 0); +} + +void test_index_crlf__autocrlf_input_text_auto_attr(void) +{ + const git_index_entry *entry; + git_oid oid; + + cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n"); + + cl_repo_set_string(g_repo, "core.autocrlf", "input"); + + cl_git_mkfile("./crlf/newfile.txt", + (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF); + + cl_git_pass(git_index_add_bypath(g_index, "newfile.txt")); + entry = git_index_get_bypath(g_index, "newfile.txt", 0); + + cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF)); + cl_assert(git_oid_cmp(&oid, &entry->id) == 0); +} diff --git a/tests/index/names.c b/tests/index/names.c index 9007b1b15..4723449d9 100644 --- a/tests/index/names.c +++ b/tests/index/names.c @@ -86,10 +86,10 @@ void test_index_names__cleaned_on_reset_hard(void) { git_object *target; - retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876"); + cl_git_pass(git_revparse_single(&target, repo, "3a34580")); test_index_names__add(); - cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL)); cl_assert(git_index_name_entrycount(repo_index) == 0); git_object_free(target); @@ -99,10 +99,10 @@ void test_index_names__cleaned_on_reset_mixed(void) { git_object *target; - retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876"); + cl_git_pass(git_revparse_single(&target, repo, "3a34580")); test_index_names__add(); - cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED)); + cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL)); cl_assert(git_index_name_entrycount(repo_index) == 0); git_object_free(target); diff --git a/tests/index/reuc.c b/tests/index/reuc.c index a18d5602e..bf051c827 100644 --- a/tests/index/reuc.c +++ b/tests/index/reuc.c @@ -295,10 +295,10 @@ void test_index_reuc__cleaned_on_reset_hard(void) { git_object *target; - retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876"); + cl_git_pass(git_revparse_single(&target, repo, "3a34580")); test_index_reuc__add(); - cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL)); cl_assert(reuc_entry_exists() == false); git_object_free(target); @@ -308,10 +308,10 @@ void test_index_reuc__cleaned_on_reset_mixed(void) { git_object *target; - retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876"); + cl_git_pass(git_revparse_single(&target, repo, "3a34580")); test_index_reuc__add(); - cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED)); + cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL)); cl_assert(reuc_entry_exists() == false); git_object_free(target); @@ -321,12 +321,12 @@ void test_index_reuc__retained_on_reset_soft(void) { git_object *target; - retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876"); + cl_git_pass(git_revparse_single(&target, repo, "3a34580")); - git_reset(repo, target, GIT_RESET_HARD); + git_reset(repo, target, GIT_RESET_HARD, NULL, NULL); test_index_reuc__add(); - cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); cl_assert(reuc_entry_exists() == true); git_object_free(target); diff --git a/tests/index/tests.c b/tests/index/tests.c index bd90bc557..6e28af1f7 100644 --- a/tests/index/tests.c +++ b/tests/index/tests.c @@ -80,7 +80,7 @@ void test_index_tests__empty_index(void) cl_assert(index->on_disk == 0); cl_assert(git_index_entrycount(index) == 0); - cl_assert(index->entries.sorted); + cl_assert(git_vector_is_sorted(&index->entries)); git_index_free(index); } @@ -95,7 +95,7 @@ void test_index_tests__default_test_index(void) cl_assert(index->on_disk); cl_assert(git_index_entrycount(index) == index_entry_count); - cl_assert(index->entries.sorted); + cl_assert(git_vector_is_sorted(&index->entries)); entries = (git_index_entry **)index->entries.contents; @@ -118,7 +118,7 @@ void test_index_tests__gitgit_index(void) cl_assert(index->on_disk); cl_assert(git_index_entrycount(index) == index_entry_count_2); - cl_assert(index->entries.sorted); + cl_assert(git_vector_is_sorted(&index->entries)); cl_assert(index->tree != NULL); git_index_free(index); @@ -195,7 +195,7 @@ void test_index_tests__sort1(void) cl_git_pass(git_index_open(&index, "fake-index")); /* FIXME: this test is slightly dumb */ - cl_assert(index->entries.sorted); + cl_assert(git_vector_is_sorted(&index->entries)); git_index_free(index); } @@ -543,3 +543,37 @@ void test_index_tests__corrupted_extension(void) cl_git_fail_with(git_index_open(&index, TEST_INDEXBAD_PATH), GIT_ERROR); } + +static void assert_index_is_sorted(git_index *index) +{ + git_vector *entries = &index->entries; + size_t i; + + cl_assert(git_vector_is_sorted(entries)); + + for (i = 1; i < git_vector_length(entries); ++i) { + git_index_entry *prev = git_vector_get(entries, i - 1); + git_index_entry *curr = git_vector_get(entries, i); + cl_assert(index->entries._cmp(prev, curr) <= 0); + } +} + +void test_index_tests__reload_while_ignoring_case(void) +{ + git_index *index; + unsigned int caps; + + cl_git_pass(git_index_open(&index, TEST_INDEX_PATH)); + assert_index_is_sorted(index); + + caps = git_index_caps(index); + cl_git_pass(git_index_set_caps(index, caps &= ~GIT_INDEXCAP_IGNORE_CASE)); + cl_git_pass(git_index_read(index, true)); + assert_index_is_sorted(index); + + cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE)); + cl_git_pass(git_index_read(index, true)); + assert_index_is_sorted(index); + + git_index_free(index); +} diff --git a/tests/merge/workdir/dirty.c b/tests/merge/workdir/dirty.c new file mode 100644 index 000000000..a77f9b205 --- /dev/null +++ b/tests/merge/workdir/dirty.c @@ -0,0 +1,322 @@ +#include "clar_libgit2.h" +#include "git2/merge.h" +#include "buffer.h" +#include "merge.h" +#include "../merge_helpers.h" +#include "posix.h" + +#define TEST_REPO_PATH "merge-resolve" +#define MERGE_BRANCH_OID "7cb63eed597130ba4abb87b3e544b85021905520" + +#define AUTOMERGEABLE_MERGED_FILE \ + "this file is changed in master\n" \ + "this file is automergeable\n" \ + "this file is automergeable\n" \ + "this file is automergeable\n" \ + "this file is automergeable\n" \ + "this file is automergeable\n" \ + "this file is automergeable\n" \ + "this file is automergeable\n" \ + "this file is changed in branch\n" + +#define CHANGED_IN_BRANCH_FILE \ + "changed in branch\n" + +static git_repository *repo; +static git_index *repo_index; + +static char *unaffected[][4] = { + { "added-in-master.txt", NULL }, + { "changed-in-master.txt", NULL }, + { "unchanged.txt", NULL }, + { "added-in-master.txt", "changed-in-master.txt", NULL }, + { "added-in-master.txt", "unchanged.txt", NULL }, + { "changed-in-master.txt", "unchanged.txt", NULL }, + { "added-in-master.txt", "changed-in-master.txt", "unchanged.txt", NULL }, + { "new_file.txt", NULL }, + { "new_file.txt", "unchanged.txt", NULL }, + { NULL }, +}; + +static char *affected[][5] = { + { "automergeable.txt", NULL }, + { "changed-in-branch.txt", NULL }, + { "conflicting.txt", NULL }, + { "removed-in-branch.txt", NULL }, + { "automergeable.txt", "changed-in-branch.txt", NULL }, + { "automergeable.txt", "conflicting.txt", NULL }, + { "automergeable.txt", "removed-in-branch.txt", NULL }, + { "changed-in-branch.txt", "conflicting.txt", NULL }, + { "changed-in-branch.txt", "removed-in-branch.txt", NULL }, + { "conflicting.txt", "removed-in-branch.txt", NULL }, + { "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", NULL }, + { "automergeable.txt", "changed-in-branch.txt", "removed-in-branch.txt", NULL }, + { "automergeable.txt", "conflicting.txt", "removed-in-branch.txt", NULL }, + { "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL }, + { "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL }, + { NULL }, +}; + +static char *result_contents[4][6] = { + { "automergeable.txt", AUTOMERGEABLE_MERGED_FILE, NULL, NULL }, + { "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE, NULL, NULL }, + { "automergeable.txt", AUTOMERGEABLE_MERGED_FILE, "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE, NULL, NULL }, + { NULL } +}; + +void test_merge_workdir_dirty__initialize(void) +{ + repo = cl_git_sandbox_init(TEST_REPO_PATH); + git_repository_index(&repo_index, repo); +} + +void test_merge_workdir_dirty__cleanup(void) +{ + git_index_free(repo_index); + cl_git_sandbox_cleanup(); +} + +static void set_core_autocrlf_to(git_repository *repo, bool value) +{ + git_config *cfg; + + cl_git_pass(git_repository_config(&cfg, repo)); + cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", value)); + + git_config_free(cfg); +} + +static int merge_branch(git_merge_result **result, int merge_file_favor, int checkout_strategy) +{ + git_oid their_oids[1]; + git_merge_head *their_heads[1]; + git_merge_opts opts = GIT_MERGE_OPTS_INIT; + int error; + + cl_git_pass(git_oid_fromstr(&their_oids[0], MERGE_BRANCH_OID)); + cl_git_pass(git_merge_head_from_id(&their_heads[0], repo, &their_oids[0])); + + opts.merge_tree_opts.file_favor = merge_file_favor; + opts.checkout_opts.checkout_strategy = checkout_strategy; + error = git_merge(result, repo, (const git_merge_head **)their_heads, 1, &opts); + + git_merge_head_free(their_heads[0]); + + return error; +} + +static void write_files(char *files[]) +{ + char *filename; + git_buf path = GIT_BUF_INIT, content = GIT_BUF_INIT; + size_t i; + + for (i = 0, filename = files[i]; filename; filename = files[++i]) { + git_buf_clear(&path); + git_buf_clear(&content); + + git_buf_printf(&path, "%s/%s", TEST_REPO_PATH, filename); + git_buf_printf(&content, "This is a dirty file in the working directory!\n\n" + "It will not be staged! Its filename is %s.\n", filename); + + cl_git_mkfile(path.ptr, content.ptr); + } + + git_buf_free(&path); + git_buf_free(&content); +} + +static void hack_index(char *files[]) +{ + char *filename; + struct stat statbuf; + git_buf path = GIT_BUF_INIT; + git_index_entry *entry; + size_t i; + + /* Update the index to suggest that checkout placed these files on + * disk, keeping the object id but updating the cache, which will + * emulate a Git implementation's different filter. + */ + for (i = 0, filename = files[i]; filename; filename = files[++i]) { + git_buf_clear(&path); + + cl_assert(entry = (git_index_entry *) + git_index_get_bypath(repo_index, filename, 0)); + + cl_git_pass(git_buf_printf(&path, "%s/%s", TEST_REPO_PATH, filename)); + cl_git_pass(p_stat(path.ptr, &statbuf)); + + entry->ctime.seconds = (git_time_t)statbuf.st_ctime; + entry->ctime.nanoseconds = 0; + entry->mtime.seconds = (git_time_t)statbuf.st_mtime; + entry->mtime.nanoseconds = 0; + entry->dev = statbuf.st_dev; + entry->ino = statbuf.st_ino; + entry->uid = statbuf.st_uid; + entry->gid = statbuf.st_gid; + entry->file_size = statbuf.st_size; + } + + git_buf_free(&path); +} + +static void stage_random_files(char *files[]) +{ + char *filename; + size_t i; + + write_files(files); + + for (i = 0, filename = files[i]; filename; filename = files[++i]) + cl_git_pass(git_index_add_bypath(repo_index, filename)); +} + +static void stage_content(char *content[]) +{ + git_reference *head; + git_object *head_object; + git_merge_result *result = NULL; + git_buf path = GIT_BUF_INIT; + char *filename, *text; + size_t i; + + cl_git_pass(git_repository_head(&head, repo)); + cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJ_COMMIT)); + cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL, NULL)); + + for (i = 0, filename = content[i], text = content[++i]; + filename && text; + filename = content[++i], text = content[++i]) { + + git_buf_clear(&path); + + cl_git_pass(git_buf_printf(&path, "%s/%s", TEST_REPO_PATH, filename)); + + cl_git_mkfile(path.ptr, text); + cl_git_pass(git_index_add_bypath(repo_index, filename)); + } + + git_merge_result_free(result); + git_object_free(head_object); + git_reference_free(head); + git_buf_free(&path); +} + +static int merge_dirty_files(char *dirty_files[]) +{ + git_reference *head; + git_object *head_object; + git_merge_result *result = NULL; + int error; + + cl_git_pass(git_repository_head(&head, repo)); + cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJ_COMMIT)); + cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL, NULL)); + + write_files(dirty_files); + + error = merge_branch(&result, 0, 0); + + git_merge_result_free(result); + git_object_free(head_object); + git_reference_free(head); + + return error; +} + +static int merge_differently_filtered_files(char *files[]) +{ + git_reference *head; + git_object *head_object; + git_merge_result *result = NULL; + int error; + + cl_git_pass(git_repository_head(&head, repo)); + cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJ_COMMIT)); + cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL, NULL)); + + write_files(files); + hack_index(files); + + cl_git_pass(git_index_write(repo_index)); + + error = merge_branch(&result, 0, 0); + + git_merge_result_free(result); + git_object_free(head_object); + git_reference_free(head); + + return error; +} + +static int merge_staged_files(char *staged_files[]) +{ + git_merge_result *result = NULL; + int error; + + stage_random_files(staged_files); + + error = merge_branch(&result, 0, 0); + + git_merge_result_free(result); + + return error; +} + +void test_merge_workdir_dirty__unaffected_dirty_files_allowed(void) +{ + char **files; + size_t i; + + for (i = 0, files = unaffected[i]; files[0]; files = unaffected[++i]) + cl_git_pass(merge_dirty_files(files)); +} + +void test_merge_workdir_dirty__affected_dirty_files_disallowed(void) +{ + char **files; + size_t i; + + for (i = 0, files = affected[i]; files[0]; files = affected[++i]) + cl_git_fail(merge_dirty_files(files)); +} + +void test_merge_workdir_dirty__staged_files_in_index_disallowed(void) +{ + char **files; + size_t i; + + for (i = 0, files = unaffected[i]; files[0]; files = unaffected[++i]) + cl_git_fail(merge_staged_files(files)); + + for (i = 0, files = affected[i]; files[0]; files = affected[++i]) + cl_git_fail(merge_staged_files(files)); +} + +void test_merge_workdir_dirty__identical_staged_files_allowed(void) +{ + git_merge_result *result; + char **content; + size_t i; + + set_core_autocrlf_to(repo, false); + + for (i = 0, content = result_contents[i]; content[0]; content = result_contents[++i]) { + stage_content(content); + + git_index_write(repo_index); + cl_git_pass(merge_branch(&result, 0, 0)); + + git_merge_result_free(result); + } +} + +void test_merge_workdir_dirty__honors_cache(void) +{ + char **files; + size_t i; + + for (i = 0, files = affected[i]; files[0]; files = affected[++i]) + cl_git_pass(merge_differently_filtered_files(files)); +} diff --git a/tests/merge/workdir/renames.c b/tests/merge/workdir/renames.c index d38397983..27747720e 100644 --- a/tests/merge/workdir/renames.c +++ b/tests/merge/workdir/renames.c @@ -17,7 +17,14 @@ static git_repository *repo; // Fixture setup and teardown void test_merge_workdir_renames__initialize(void) { + git_config *cfg; + repo = cl_git_sandbox_init(TEST_REPO_PATH); + + /* Ensure that the user's merge.conflictstyle doesn't interfere */ + cl_git_pass(git_repository_config(&cfg, repo)); + cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge")); + git_config_free(cfg); } void test_merge_workdir_renames__cleanup(void) diff --git a/tests/merge/workdir/simple.c b/tests/merge/workdir/simple.c index 4ff761cf8..a9a63651c 100644 --- a/tests/merge/workdir/simple.c +++ b/tests/merge/workdir/simple.c @@ -116,8 +116,15 @@ static git_index *repo_index; // Fixture setup and teardown void test_merge_workdir_simple__initialize(void) { + git_config *cfg; + repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&repo_index, repo); + + /* Ensure that the user's merge.conflictstyle doesn't interfere */ + cl_git_pass(git_repository_config(&cfg, repo)); + cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge")); + git_config_free(cfg); } void test_merge_workdir_simple__cleanup(void) @@ -585,7 +592,7 @@ void test_merge_workdir_simple__directory_file(void) cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_DIR OURS_DIRECTORY_FILE, 1, NULL, NULL)); cl_git_pass(git_reference_name_to_id(&head_commit_id, repo, GIT_HEAD_FILE)); cl_git_pass(git_commit_lookup(&head_commit, repo, &head_commit_id)); - cl_git_pass(git_reset(repo, (git_object *)head_commit, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head_commit, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_oid_fromstr(&their_oids[0], THEIRS_DIRECTORY_FILE)); cl_git_pass(git_merge_head_from_id(&their_heads[0], repo, &their_oids[0])); @@ -684,7 +691,7 @@ void test_merge_workdir_simple__binary(void) cl_git_pass(git_oid_fromstr(&their_oid, "ad01aebfdf2ac13145efafe3f9fcf798882f1730")); cl_git_pass(git_commit_lookup(&our_commit, repo, &our_oid)); - cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_merge_head_from_id(&their_head, repo, &their_oid)); diff --git a/tests/merge/workdir/submodules.c b/tests/merge/workdir/submodules.c index f01faac43..42451bde7 100644 --- a/tests/merge/workdir/submodules.c +++ b/tests/merge/workdir/submodules.c @@ -46,7 +46,7 @@ void test_merge_workdir_submodules__automerge(void) cl_git_pass(git_reference_lookup(&our_ref, repo, "refs/heads/" SUBMODULE_MAIN_BRANCH)); cl_git_pass(git_commit_lookup(&our_commit, repo, git_reference_target(our_ref))); - cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_reference_lookup(&their_ref, repo, "refs/heads/" SUBMODULE_OTHER_BRANCH)); cl_git_pass(git_merge_head_from_ref(&their_head, repo, their_ref)); @@ -82,7 +82,7 @@ void test_merge_workdir_submodules__take_changed(void) cl_git_pass(git_reference_lookup(&our_ref, repo, "refs/heads/" SUBMODULE_MAIN_BRANCH)); cl_git_pass(git_commit_lookup(&our_commit, repo, git_reference_target(our_ref))); - cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_reference_lookup(&their_ref, repo, "refs/heads/" SUBMODULE_OTHER2_BRANCH)); cl_git_pass(git_merge_head_from_ref(&their_head, repo, their_ref)); diff --git a/tests/network/fetchlocal.c b/tests/network/fetchlocal.c index 28c7115bf..4c39394bb 100644 --- a/tests/network/fetchlocal.c +++ b/tests/network/fetchlocal.c @@ -37,7 +37,7 @@ void test_network_fetchlocal__complete(void) git_remote_set_callbacks(origin, &callbacks); cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_download(origin)); - cl_git_pass(git_remote_update_tips(origin)); + cl_git_pass(git_remote_update_tips(origin, NULL, NULL)); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(19, (int)refnames.count); @@ -75,7 +75,7 @@ void test_network_fetchlocal__partial(void) git_remote_set_callbacks(origin, &callbacks); cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_download(origin)); - cl_git_pass(git_remote_update_tips(origin)); + cl_git_pass(git_remote_update_tips(origin, NULL, NULL)); git_strarray_free(&refnames); diff --git a/tests/network/remote/local.c b/tests/network/remote/local.c index 9b9f716b9..589e6ac9b 100644 --- a/tests/network/remote/local.c +++ b/tests/network/remote/local.c @@ -115,7 +115,7 @@ void test_network_remote_local__shorthand_fetch_refspec0(void) cl_git_pass(git_remote_add_fetch(remote, refspec2)); cl_git_pass(git_remote_download(remote)); - cl_git_pass(git_remote_update_tips(remote)); + cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master")); git_reference_free(ref); @@ -137,7 +137,7 @@ void test_network_remote_local__shorthand_fetch_refspec1(void) cl_git_pass(git_remote_add_fetch(remote, refspec2)); cl_git_pass(git_remote_download(remote)); - cl_git_pass(git_remote_update_tips(remote)); + cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_fail(git_reference_lookup(&ref, repo, "refs/remotes/master")); @@ -152,7 +152,7 @@ void test_network_remote_local__tagopt(void) git_remote_set_autotag(remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL); cl_git_pass(git_remote_download(remote)); - cl_git_pass(git_remote_update_tips(remote)); + cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_fail(git_reference_lookup(&ref, repo, "refs/remotes/master")); @@ -171,7 +171,7 @@ void test_network_remote_local__push_to_bare_remote(void) connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_add_fetch(remote, "master:master")); cl_git_pass(git_remote_download(remote)); - cl_git_pass(git_remote_update_tips(remote)); + cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); git_remote_disconnect(remote); /* Set up an empty bare repo to push into */ @@ -208,7 +208,7 @@ void test_network_remote_local__push_to_bare_remote_with_file_url(void) connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_add_fetch(remote, "master:master")); cl_git_pass(git_remote_download(remote)); - cl_git_pass(git_remote_update_tips(remote)); + cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); git_remote_disconnect(remote); /* Set up an empty bare repo to push into */ @@ -248,7 +248,7 @@ void test_network_remote_local__push_to_non_bare_remote(void) connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_add_fetch(remote, "master:master")); cl_git_pass(git_remote_download(remote)); - cl_git_pass(git_remote_update_tips(remote)); + cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); git_remote_disconnect(remote); /* Set up an empty non-bare repo to push into */ @@ -273,3 +273,86 @@ void test_network_remote_local__push_to_non_bare_remote(void) git_remote_free(localremote); cl_fixture_cleanup("localbare.git"); } + +void test_network_remote_local__fetch(void) +{ + const char *refspec = "master:remotes/sloppy/master"; + + git_reflog *log; + const git_reflog_entry *entry; + git_signature *sig; + git_reference *ref; + + cl_git_pass(git_signature_now(&sig, "Foo Bar", "foo@example.com")); + + connect_to_local_repository(cl_fixture("testrepo.git")); + cl_git_pass(git_remote_add_fetch(remote, refspec)); + + cl_git_pass(git_remote_fetch(remote, sig, "UPDAAAAAATE!!")); + + cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master")); + git_reference_free(ref); + + cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master")); + cl_assert_equal_i(1, git_reflog_entrycount(log)); + entry = git_reflog_entry_byindex(log, 0); + cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email); + cl_assert_equal_s("UPDAAAAAATE!!", git_reflog_entry_message(entry)); + + git_reflog_free(log); + git_signature_free(sig); +} + +void test_network_remote_local__reflog(void) +{ + const char *refspec = "master:remotes/sloppy/master"; + + git_reflog *log; + const git_reflog_entry *entry; + git_signature *sig; + + cl_git_pass(git_signature_now(&sig, "Foo Bar", "foo@example.com")); + + connect_to_local_repository(cl_fixture("testrepo.git")); + cl_git_pass(git_remote_add_fetch(remote, refspec)); + + cl_git_pass(git_remote_download(remote)); + cl_git_pass(git_remote_update_tips(remote, sig, "UPDAAAAAATE!!")); + + cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master")); + cl_assert_equal_i(1, git_reflog_entrycount(log)); + entry = git_reflog_entry_byindex(log, 0); + cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email); + cl_assert_equal_s("UPDAAAAAATE!!", git_reflog_entry_message(entry)); + + git_reflog_free(log); + git_signature_free(sig); +} + +void test_network_remote_local__fetch_default_reflog_message(void) +{ + const char *refspec = "master:remotes/sloppy/master"; + + git_reflog *log; + const git_reflog_entry *entry; + git_signature *sig; + char expected_reflog_msg[1024]; + + cl_git_pass(git_signature_now(&sig, "Foo Bar", "foo@example.com")); + + connect_to_local_repository(cl_fixture("testrepo.git")); + cl_git_pass(git_remote_add_fetch(remote, refspec)); + + cl_git_pass(git_remote_fetch(remote, sig, NULL)); + + cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master")); + cl_assert_equal_i(1, git_reflog_entrycount(log)); + entry = git_reflog_entry_byindex(log, 0); + cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email); + + sprintf(expected_reflog_msg, "fetch %s", git_remote_url(remote)); + cl_assert_equal_s(expected_reflog_msg, git_reflog_entry_message(entry)); + + git_reflog_free(log); + git_signature_free(sig); +} diff --git a/tests/object/commit/commitstagedfile.c b/tests/object/commit/commitstagedfile.c index fbeeccbbd..3e7b3c02c 100644 --- a/tests/object/commit/commitstagedfile.c +++ b/tests/object/commit/commitstagedfile.c @@ -132,3 +132,79 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) git_tree_free(tree); git_index_free(index); } + +static void assert_commit_tree_has_n_entries(git_commit *c, int count) +{ + git_tree *tree; + cl_git_pass(git_commit_tree(&tree, c)); + cl_assert_equal_i(count, git_tree_entrycount(tree)); + git_tree_free(tree); +} + +static void assert_commit_is_head_(git_commit *c, const char *file, int line) +{ + git_commit *head; + cl_git_pass(git_revparse_single((git_object **)&head, repo, "HEAD")); + clar__assert(git_oid_equal(git_commit_id(c), git_commit_id(head)), file, line, "Commit is not the HEAD", NULL, 1); + git_commit_free(head); +} +#define assert_commit_is_head(C) assert_commit_is_head_((C),__FILE__,__LINE__) + +void test_object_commit_commitstagedfile__amend_commit(void) +{ + git_index *index; + git_oid old_oid, new_oid, tree_oid; + git_commit *old_commit, *new_commit; + git_tree *tree; + + /* make a commit */ + + cl_git_mkfile("treebuilder/myfile", "This is a file\n"); + cl_git_pass(git_repository_index(&index, repo)); + cl_git_pass(git_index_add_bypath(index, "myfile")); + cl_repo_commit_from_index(&old_oid, repo, NULL, 0, "first commit"); + + cl_git_pass(git_commit_lookup(&old_commit, repo, &old_oid)); + + cl_assert_equal_i(0, git_commit_parentcount(old_commit)); + assert_commit_tree_has_n_entries(old_commit, 1); + assert_commit_is_head(old_commit); + + /* let's amend the message of the HEAD commit */ + + cl_git_pass(git_commit_amend( + &new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", NULL)); + + cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid)); + + cl_assert_equal_i(0, git_commit_parentcount(new_commit)); + assert_commit_tree_has_n_entries(new_commit, 1); + assert_commit_is_head(new_commit); + + git_commit_free(old_commit); + old_commit = new_commit; + + /* let's amend the tree of that last commit */ + + cl_git_mkfile("treebuilder/anotherfile", "This is another file\n"); + cl_git_pass(git_index_add_bypath(index, "anotherfile")); + cl_git_pass(git_index_write_tree(&tree_oid, index)); + cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); + cl_assert_equal_i(2, git_tree_entrycount(tree)); + + cl_git_pass(git_commit_amend( + &new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", tree)); + git_tree_free(tree); + + cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid)); + + cl_assert_equal_i(0, git_commit_parentcount(new_commit)); + assert_commit_tree_has_n_entries(new_commit, 2); + assert_commit_is_head(new_commit); + + /* cleanup */ + + git_commit_free(old_commit); + git_commit_free(new_commit); + git_index_free(index); +} diff --git a/tests/object/tree/write.c b/tests/object/tree/write.c index 3bea0ed4d..45356e807 100644 --- a/tests/object/tree/write.c +++ b/tests/object/tree/write.c @@ -9,7 +9,7 @@ static const char *third_tree = "eb86d8b81d6adbd5290a935d6c9976882de98488"; static git_repository *g_repo; -// Fixture setup and teardown +/* Fixture setup and teardown */ void test_object_tree_write__initialize(void) { g_repo = cl_git_sandbox_init("testrepo"); @@ -22,7 +22,7 @@ void test_object_tree_write__cleanup(void) void test_object_tree_write__from_memory(void) { - // write a tree from a memory + /* write a tree from a memory */ git_treebuilder *builder; git_tree *tree; git_oid id, bid, rid, id2; @@ -31,7 +31,9 @@ void test_object_tree_write__from_memory(void) git_oid_fromstr(&id2, second_tree); git_oid_fromstr(&bid, blob_oid); - //create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER. + /* create a second tree from first tree using `git_treebuilder_insert` + * on REPOSITORY_FOLDER. + */ cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); cl_git_pass(git_treebuilder_create(&builder, tree)); @@ -61,7 +63,7 @@ void test_object_tree_write__from_memory(void) void test_object_tree_write__subtree(void) { - // write a hierarchical tree from a memory + /* write a hierarchical tree from a memory */ git_treebuilder *builder; git_tree *tree; git_oid id, bid, subtree_id, id2, id3; @@ -72,25 +74,25 @@ void test_object_tree_write__subtree(void) git_oid_fromstr(&id3, third_tree); git_oid_fromstr(&bid, blob_oid); - //create subtree + /* create subtree */ cl_git_pass(git_treebuilder_create(&builder, NULL)); cl_git_pass(git_treebuilder_insert( - NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB)); //-V536 + NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB)); /* -V536 */ cl_git_pass(git_treebuilder_write(&subtree_id, g_repo, builder)); git_treebuilder_free(builder); - // create parent tree + /* create parent tree */ cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); cl_git_pass(git_treebuilder_create(&builder, tree)); cl_git_pass(git_treebuilder_insert( - NULL, builder, "new", &subtree_id, GIT_FILEMODE_TREE)); //-V536 + NULL, builder, "new", &subtree_id, GIT_FILEMODE_TREE)); /* -V536 */ cl_git_pass(git_treebuilder_write(&id_hiearar, g_repo, builder)); git_treebuilder_free(builder); git_tree_free(tree); cl_assert(git_oid_cmp(&id_hiearar, &id3) == 0); - // check data is correct + /* check data is correct */ cl_git_pass(git_tree_lookup(&tree, g_repo, &id_hiearar)); cl_assert(2 == git_tree_entrycount(tree)); git_tree_free(tree); @@ -314,3 +316,83 @@ void test_object_tree_write__filtering(void) git_tree_free(tree); } + +void test_object_tree_write__cruel_paths(void) +{ + static const char *the_paths[] = { + "C:\\", + " : * ? \" \n < > |", + "a\\b", + "\\\\b\a", + ":\\", + "COM1", + "foo.aux", + REP1024("1234"), /* 4096 char string */ + REP1024("12345678"), /* 8192 char string */ + "\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD", /* Ūnĭcōde̽ */ + NULL + }; + git_treebuilder *builder; + git_tree *tree; + git_oid id, bid, subid; + const char **scan; + int count = 0, i, j; + git_tree_entry *te; + + git_oid_fromstr(&bid, blob_oid); + + /* create tree */ + cl_git_pass(git_treebuilder_create(&builder, NULL)); + for (scan = the_paths; *scan; ++scan) { + cl_git_pass(git_treebuilder_insert( + NULL, builder, *scan, &bid, GIT_FILEMODE_BLOB)); + count++; + } + cl_git_pass(git_treebuilder_write(&id, g_repo, builder)); + git_treebuilder_free(builder); + + /* check data is correct */ + cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); + + cl_assert_equal_i(count, git_tree_entrycount(tree)); + + for (scan = the_paths; *scan; ++scan) { + const git_tree_entry *cte = git_tree_entry_byname(tree, *scan); + cl_assert(cte != NULL); + cl_assert_equal_s(*scan, git_tree_entry_name(cte)); + } + for (scan = the_paths; *scan; ++scan) { + cl_git_pass(git_tree_entry_bypath(&te, tree, *scan)); + cl_assert_equal_s(*scan, git_tree_entry_name(te)); + git_tree_entry_free(te); + } + + git_tree_free(tree); + + /* let's try longer paths */ + cl_git_pass(git_treebuilder_create(&builder, NULL)); + for (scan = the_paths; *scan; ++scan) { + cl_git_pass(git_treebuilder_insert( + NULL, builder, *scan, &id, GIT_FILEMODE_TREE)); + } + cl_git_pass(git_treebuilder_write(&subid, g_repo, builder)); + git_treebuilder_free(builder); + + /* check data is correct */ + cl_git_pass(git_tree_lookup(&tree, g_repo, &subid)); + + cl_assert_equal_i(count, git_tree_entrycount(tree)); + + for (i = 0; i < count; ++i) { + for (j = 0; j < count; ++j) { + git_buf b = GIT_BUF_INIT; + cl_git_pass(git_buf_joinpath(&b, the_paths[i], the_paths[j])); + cl_git_pass(git_tree_entry_bypath(&te, tree, b.ptr)); + cl_assert_equal_s(the_paths[j], git_tree_entry_name(te)); + git_tree_entry_free(te); + git_buf_free(&b); + } + } + + git_tree_free(tree); +} diff --git a/tests/online/clone.c b/tests/online/clone.c index 1222d174d..fa2408a75 100644 --- a/tests/online/clone.c +++ b/tests/online/clone.c @@ -192,30 +192,28 @@ static int cred_failure_cb( { GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(allowed_types); GIT_UNUSED(data); - return -1; + return -172; } -void test_online_clone__cred_callback_failure_is_euser(void) +void test_online_clone__cred_callback_failure_return_code_is_tunnelled(void) { const char *remote_url = cl_getenv("GITTEST_REMOTE_URL"); const char *remote_user = cl_getenv("GITTEST_REMOTE_USER"); - const char *remote_default = cl_getenv("GITTEST_REMOTE_DEFAULT"); - int error; if (!remote_url) { printf("GITTEST_REMOTE_URL unset; skipping clone test\n"); return; } - if (!remote_user && !remote_default) { - printf("GITTEST_REMOTE_USER and GITTEST_REMOTE_DEFAULT unset; skipping clone test\n"); + if (!remote_user) { + printf("GITTEST_REMOTE_USER unset; skipping clone test\n"); return; } g_options.remote_callbacks.credentials = cred_failure_cb; - cl_git_fail(error = git_clone(&g_repo, remote_url, "./foo", &g_options)); - cl_assert_equal_i(error, GIT_EUSER); + /* TODO: this should expect -172. */ + cl_git_fail_with(git_clone(&g_repo, remote_url, "./foo", &g_options), -1); } void test_online_clone__credentials(void) diff --git a/tests/online/fetch.c b/tests/online/fetch.c index 8f71cf3f5..cb84e846c 100644 --- a/tests/online/fetch.c +++ b/tests/online/fetch.c @@ -48,7 +48,7 @@ static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n) git_remote_set_autotag(remote, flag); cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_download(remote)); - cl_git_pass(git_remote_update_tips(remote)); + cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); git_remote_disconnect(remote); cl_assert_equal_i(counter, n); cl_assert(bytes_received > 0); @@ -117,7 +117,7 @@ void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date cl_assert_equal_i(false, invoked); - cl_git_pass(git_remote_update_tips(remote)); + cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); git_remote_disconnect(remote); git_remote_free(remote); diff --git a/tests/online/fetchhead.c b/tests/online/fetchhead.c index 57b183f88..0b3f20db1 100644 --- a/tests/online/fetchhead.c +++ b/tests/online/fetchhead.c @@ -51,7 +51,7 @@ static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fet cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_download(remote)); - cl_git_pass(git_remote_update_tips(remote)); + cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); git_remote_disconnect(remote); git_remote_free(remote); diff --git a/tests/online/push.c b/tests/online/push.c index 8efe21e0e..55b97b282 100644 --- a/tests/online/push.c +++ b/tests/online/push.c @@ -351,7 +351,7 @@ void test_online_push__initialize(void) /* Now that we've deleted everything, fetch from the remote */ cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_download(_remote)); - cl_git_pass(git_remote_update_tips(_remote)); + cl_git_pass(git_remote_update_tips(_remote, NULL, NULL)); git_remote_disconnect(_remote); } else printf("GITTEST_REMOTE_URL unset; skipping push test\n"); @@ -414,11 +414,13 @@ static void do_push( git_push_options opts = GIT_PUSH_OPTIONS_INIT; size_t i; int pack_progress_calls = 0, transfer_progress_calls = 0; + git_signature *pusher; if (_remote) { /* Auto-detect the number of threads to use */ opts.pb_parallelism = 0; + cl_git_pass(git_signature_now(&pusher, "Foo Bar", "foo@example.com")); cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH)); cl_git_pass(git_push_new(&push, _remote)); @@ -455,13 +457,15 @@ static void do_push( verify_refs(_remote, expected_refs, expected_refs_len); - cl_git_pass(git_push_update_tips(push)); + cl_git_pass(git_push_update_tips(push, pusher, "test push")); verify_tracking_branches(_remote, expected_refs, expected_refs_len); git_push_free(push); git_remote_disconnect(_remote); + git_signature_free(pusher); } + } /* Call push_finish() without ever calling git_push_add_refspec() */ @@ -528,6 +532,9 @@ void test_online_push__b5_cancel(void) void test_online_push__multi(void) { + git_reflog *log; + const git_reflog_entry *entry; + const char *specs[] = { "refs/heads/b1:refs/heads/b1", "refs/heads/b2:refs/heads/b2", @@ -552,6 +559,15 @@ void test_online_push__multi(void) do_push(specs, ARRAY_SIZE(specs), exp_stats, ARRAY_SIZE(exp_stats), exp_refs, ARRAY_SIZE(exp_refs), 0, 1); + + cl_git_pass(git_reflog_read(&log, _repo, "refs/remotes/test/b1")); + entry = git_reflog_entry_byindex(log, 0); + if (entry) { + cl_assert_equal_s("test push", git_reflog_entry_message(entry)); + cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email); + } + + git_reflog_free(log); } void test_online_push__implicit_tgt(void) diff --git a/tests/refs/branches/create.c b/tests/refs/branches/create.c index d4b0763da..b91eed6e8 100644 --- a/tests/refs/branches/create.c +++ b/tests/refs/branches/create.c @@ -29,15 +29,16 @@ void test_refs_branches_create__cleanup(void) static void retrieve_target_from_oid(git_commit **out, git_repository *repo, const char *sha) { - git_oid oid; + git_object *obj; - cl_git_pass(git_oid_fromstr(&oid, sha)); - cl_git_pass(git_commit_lookup(out, repo, &oid)); + cl_git_pass(git_revparse_single(&obj, repo, sha)); + cl_git_pass(git_commit_lookup(out, repo, git_object_id(obj))); + git_object_free(obj); } static void retrieve_known_commit(git_commit **commit, git_repository *repo) { - retrieve_target_from_oid(commit, repo, "e90810b8df3e80c413d903f631643c716887138d"); + retrieve_target_from_oid(commit, repo, "e90810b8df3"); } #define NEW_BRANCH_NAME "new-branch-on-the-block" diff --git a/tests/refs/branches/delete.c b/tests/refs/branches/delete.c index 7d1d400c8..ed5f1627b 100644 --- a/tests/refs/branches/delete.c +++ b/tests/refs/branches/delete.c @@ -78,7 +78,7 @@ void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD( git_reference_free(head); /* Detach HEAD and make it target the commit that "master" points to */ - git_repository_detach_head(repo); + git_repository_detach_head(repo, NULL, NULL); cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL)); cl_git_pass(git_branch_delete(branch)); diff --git a/tests/refs/delete.c b/tests/refs/delete.c index 5e4afb138..9d1c3fd79 100644 --- a/tests/refs/delete.c +++ b/tests/refs/delete.c @@ -91,3 +91,17 @@ void test_refs_delete__packed_only(void) git_reference_free(ref); git_refdb_free(refdb); } + +void test_refs_delete__remove(void) +{ + git_reference *ref; + + /* Check that passing no old values lets us delete */ + + cl_git_pass(git_reference_lookup(&ref, g_repo, packed_test_head_name)); + git_reference_free(ref); + + cl_git_pass(git_reference_remove(g_repo, packed_test_head_name)); + + cl_git_fail(git_reference_lookup(&ref, g_repo, packed_test_head_name)); +} diff --git a/tests/refs/races.c b/tests/refs/races.c new file mode 100644 index 000000000..643290a8e --- /dev/null +++ b/tests/refs/races.c @@ -0,0 +1,152 @@ +#include "clar_libgit2.h" + +#include "repository.h" +#include "git2/reflog.h" +#include "reflog.h" +#include "ref_helpers.h" + +static const char *commit_id = "099fabac3a9ea935598528c27f866e34089c2eff"; +static const char *refname = "refs/heads/master"; +static const char *other_refname = "refs/heads/foo"; +static const char *other_commit_id = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"; + +static git_repository *g_repo; + +void test_refs_races__initialize(void) +{ + g_repo = cl_git_sandbox_init("testrepo"); +} + +void test_refs_races__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +void test_refs_races__create_matching(void) +{ + git_reference *ref, *ref2, *ref3; + git_oid id, other_id; + + git_oid_fromstr(&id, commit_id); + git_oid_fromstr(&other_id, other_commit_id); + + cl_git_fail_with(GIT_EMODIFIED, git_reference_create_matching(&ref, g_repo, refname, &other_id, 1, &other_id, NULL, NULL)); + + cl_git_pass(git_reference_lookup(&ref, g_repo, refname)); + cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL, NULL)); + cl_git_fail_with(GIT_EMODIFIED, git_reference_set_target(&ref3, ref, &other_id, NULL, NULL)); + + git_reference_free(ref); + git_reference_free(ref2); + git_reference_free(ref3); +} + +void test_refs_races__symbolic_create_matching(void) +{ + git_reference *ref, *ref2, *ref3; + git_oid id, other_id; + + git_oid_fromstr(&id, commit_id); + git_oid_fromstr(&other_id, other_commit_id); + + cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_create_matching(&ref, g_repo, "HEAD", other_refname, 1, other_refname, NULL, NULL)); + + cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD")); + cl_git_pass(git_reference_symbolic_create_matching(&ref2, g_repo, "HEAD", other_refname, 1, NULL, NULL, refname)); + cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL, NULL)); + + git_reference_free(ref); + git_reference_free(ref2); + git_reference_free(ref3); +} + +void test_refs_races__delete(void) +{ + git_reference *ref, *ref2; + git_oid id, other_id; + + git_oid_fromstr(&id, commit_id); + git_oid_fromstr(&other_id, other_commit_id); + + /* We can delete a value that matches */ + cl_git_pass(git_reference_lookup(&ref, g_repo, refname)); + cl_git_pass(git_reference_delete(ref)); + git_reference_free(ref); + + /* We cannot delete a symbolic value that doesn't match */ + cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD")); + cl_git_pass(git_reference_symbolic_create_matching(&ref2, g_repo, "HEAD", other_refname, 1, NULL, NULL, refname)); + cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref)); + + git_reference_free(ref); + git_reference_free(ref2); + + cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL, NULL)); + git_reference_free(ref); + + /* We cannot delete an oid value that doesn't match */ + cl_git_pass(git_reference_lookup(&ref, g_repo, refname)); + cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL, NULL)); + cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref)); + + git_reference_free(ref); + git_reference_free(ref2); +} + +void test_refs_races__switch_oid_to_symbolic(void) +{ + git_reference *ref, *ref2, *ref3; + git_oid id, other_id; + + git_oid_fromstr(&id, commit_id); + git_oid_fromstr(&other_id, other_commit_id); + + /* Removing a direct ref when it's currently symbolic should fail */ + cl_git_pass(git_reference_lookup(&ref, g_repo, refname)); + cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL, NULL)); + cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref)); + + git_reference_free(ref); + git_reference_free(ref2); + + cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL, NULL)); + git_reference_free(ref); + + /* Updating a direct ref when it's currently symbolic should fail */ + cl_git_pass(git_reference_lookup(&ref, g_repo, refname)); + cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL, NULL)); + cl_git_fail_with(GIT_EMODIFIED, git_reference_set_target(&ref3, ref, &other_id, NULL, NULL)); + + git_reference_free(ref); + git_reference_free(ref2); + git_reference_free(ref3); +} + +void test_refs_races__switch_symbolic_to_oid(void) +{ + git_reference *ref, *ref2, *ref3; + git_oid id, other_id; + + git_oid_fromstr(&id, commit_id); + git_oid_fromstr(&other_id, other_commit_id); + + /* Removing a symbolic ref when it's currently direct should fail */ + cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD")); + cl_git_pass(git_reference_create(&ref2, g_repo, "HEAD", &id, 1, NULL, NULL)); + cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref)); + + git_reference_free(ref); + git_reference_free(ref2); + + cl_git_pass(git_reference_symbolic_create(&ref, g_repo, "HEAD", refname, 1, NULL, NULL)); + git_reference_free(ref); + + /* Updating a symbolic ref when it's currently direct should fail */ + cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD")); + cl_git_pass(git_reference_create(&ref2, g_repo, "HEAD", &id, 1, NULL, NULL)); + cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL, NULL)); + + git_reference_free(ref); + git_reference_free(ref2); + git_reference_free(ref3); +} diff --git a/tests/refs/read.c b/tests/refs/read.c index 35cf17e9e..52c307eb0 100644 --- a/tests/refs/read.c +++ b/tests/refs/read.c @@ -271,6 +271,21 @@ void test_refs_read__can_determine_if_a_reference_is_a_tag(void) assert_is_tag("refs/remotes/test/master", false); } +static void assert_is_note(const char *name, bool expected_noteness) +{ + git_reference *reference; + cl_git_pass(git_reference_lookup(&reference, g_repo, name)); + cl_assert_equal_i(expected_noteness, git_reference_is_note(reference)); + git_reference_free(reference); +} + +void test_refs_read__can_determine_if_a_reference_is_a_note(void) +{ + assert_is_note("refs/notes/fanout", true); + assert_is_note("refs/heads/packed", false); + assert_is_note("refs/remotes/test/master", false); +} + void test_refs_read__invalid_name_returns_EINVALIDSPEC(void) { git_reference *reference; diff --git a/tests/repo/config.c b/tests/repo/config.c index e77acc8c5..2e7be37aa 100644 --- a/tests/repo/config.c +++ b/tests/repo/config.c @@ -1,4 +1,5 @@ #include "clar_libgit2.h" +#include "sysdir.h" #include "fileops.h" #include <ctype.h> @@ -47,7 +48,7 @@ void test_repo_config__open_missing_global(void) git_config_free(config); git_repository_free(repo); - git_futils_dirs_global_shutdown(); + git_sysdir_global_shutdown(); } void test_repo_config__open_missing_global_with_separators(void) @@ -76,7 +77,7 @@ void test_repo_config__open_missing_global_with_separators(void) git_config_free(config); git_repository_free(repo); - git_futils_dirs_global_shutdown(); + git_sysdir_global_shutdown(); } #include "repository.h" @@ -105,7 +106,7 @@ void test_repo_config__read_no_configs(void) cl_assert_equal_i(GIT_ABBREV_DEFAULT, val); git_repository_free(repo); - git_futils_dirs_global_shutdown(); + git_sysdir_global_shutdown(); /* with just system */ @@ -204,5 +205,5 @@ void test_repo_config__read_no_configs(void) cl_assert(!git_path_exists("empty_standard_repo/.git/config")); cl_assert(!git_path_exists("alternate/3/.gitconfig")); - git_futils_dirs_global_shutdown(); + git_sysdir_global_shutdown(); } diff --git a/tests/repo/head.c b/tests/repo/head.c index 71cfc3c33..c5965fac6 100644 --- a/tests/repo/head.c +++ b/tests/repo/head.c @@ -15,21 +15,42 @@ void test_repo_head__cleanup(void) cl_git_sandbox_cleanup(); } +static void check_last_reflog_entry(const char *email, const char *message) +{ + git_reflog *log; + const git_reflog_entry *entry; + + cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE)); + cl_assert(git_reflog_entrycount(log) > 0); + entry = git_reflog_entry_byindex(log, 0); + if (email) + cl_assert_equal_s(email, git_reflog_entry_committer(entry)->email); + if (message) + cl_assert_equal_s(message, git_reflog_entry_message(entry)); + git_reflog_free(log); +} + void test_repo_head__head_detached(void) { git_reference *ref; + git_signature *sig; - cl_git_pass(git_repository_head_detached(repo)); + cl_git_pass(git_signature_now(&sig, "Foo Bar", "foo@example.com")); - cl_git_pass(git_repository_detach_head(repo)); + cl_assert_equal_i(false, git_repository_head_detached(repo)); + cl_git_pass(git_repository_detach_head(repo, sig, "CABLE DETACHED")); + check_last_reflog_entry(sig->email, "CABLE DETACHED"); cl_assert_equal_i(true, git_repository_head_detached(repo)); - /* take the reop back to it's original state */ - cl_git_pass(git_reference_symbolic_create(&ref, repo, "HEAD", "refs/heads/master", 1, NULL, NULL)); + /* take the repo back to it's original state */ + cl_git_pass(git_reference_symbolic_create(&ref, repo, "HEAD", "refs/heads/master", + true, sig, "REATTACH")); git_reference_free(ref); + check_last_reflog_entry(sig->email, "REATTACH"); cl_assert_equal_i(false, git_repository_head_detached(repo)); + git_signature_free(sig); } void test_repo_head__unborn_head(void) @@ -147,7 +168,7 @@ void test_repo_head__detach_head_Detaches_HEAD_and_make_it_point_to_the_peeled_c { cl_assert_equal_i(false, git_repository_head_detached(repo)); - cl_git_pass(git_repository_detach_head(repo)); + cl_git_pass(git_repository_detach_head(repo, NULL, NULL)); assert_head_is_correctly_detached(); } @@ -158,7 +179,7 @@ void test_repo_head__detach_head_Fails_if_HEAD_and_point_to_a_non_commitish(void cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, "refs/tags/point_to_blob", 1, NULL, NULL)); - cl_git_fail(git_repository_detach_head(repo)); + cl_git_fail(git_repository_detach_head(repo, NULL, NULL)); git_reference_free(head); } @@ -167,7 +188,7 @@ void test_repo_head__detaching_an_unborn_branch_returns_GIT_EUNBORNBRANCH(void) { make_head_unborn(repo, NON_EXISTING_HEAD); - cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_detach_head(repo)); + cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_detach_head(repo, NULL, NULL)); } void test_repo_head__retrieving_an_unborn_branch_returns_GIT_EUNBORNBRANCH(void) @@ -195,10 +216,41 @@ void test_repo_head__can_tell_if_an_unborn_head_is_detached(void) cl_assert_equal_i(false, git_repository_head_detached(repo)); } -void test_repo_head__setting_head_updates_reflog(void) +static void test_reflog(git_repository *repo, size_t idx, + const char *old_spec, const char *new_spec, + const char *email, const char *message) { git_reflog *log; - const git_reflog_entry *entry1, *entry2, *entry3; + const git_reflog_entry *entry; + + cl_git_pass(git_reflog_read(&log, repo, "HEAD")); + entry = git_reflog_entry_byindex(log, idx); + + if (old_spec) { + git_object *obj; + cl_git_pass(git_revparse_single(&obj, repo, old_spec)); + cl_assert_equal_i(0, git_oid_cmp(git_object_id(obj), git_reflog_entry_id_old(entry))); + git_object_free(obj); + } + if (new_spec) { + git_object *obj; + cl_git_pass(git_revparse_single(&obj, repo, new_spec)); + cl_assert_equal_i(0, git_oid_cmp(git_object_id(obj), git_reflog_entry_id_new(entry))); + git_object_free(obj); + } + + if (email) { + cl_assert_equal_s(email, git_reflog_entry_committer(entry)->email); + } + if (message) { + cl_assert_equal_s(message, git_reflog_entry_message(entry)); + } + + git_reflog_free(log); +} + +void test_repo_head__setting_head_updates_reflog(void) +{ git_object *tag; git_signature *sig; @@ -208,19 +260,13 @@ void test_repo_head__setting_head_updates_reflog(void) cl_git_pass(git_repository_set_head(repo, "refs/heads/unborn", sig, "message2")); cl_git_pass(git_revparse_single(&tag, repo, "tags/test")); cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag), sig, "message3")); + cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked", sig, "message4")); - cl_git_pass(git_reflog_read(&log, repo, "HEAD")); - entry1 = git_reflog_entry_byindex(log, 2); - entry2 = git_reflog_entry_byindex(log, 1); - entry3 = git_reflog_entry_byindex(log, 0); - cl_assert_equal_s("message1", git_reflog_entry_message(entry1)); - cl_assert_equal_s("message2", git_reflog_entry_message(entry2)); - cl_assert_equal_s("message3", git_reflog_entry_message(entry3)); - cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry1)->email); - cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry2)->email); - cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry3)->email); + test_reflog(repo, 3, NULL, "refs/heads/haacked", "foo@example.com", "message1"); + test_reflog(repo, 2, "refs/heads/haacked", NULL, "foo@example.com", "message2"); + test_reflog(repo, 1, NULL, "tags/test^{commit}", "foo@example.com", "message3"); + test_reflog(repo, 0, "tags/test^{commit}", "refs/heads/haacked", "foo@example.com", "message4"); - git_reflog_free(log); git_object_free(tag); git_signature_free(sig); } diff --git a/tests/repo/headtree.c b/tests/repo/headtree.c index e899ac399..79d88c0a7 100644 --- a/tests/repo/headtree.c +++ b/tests/repo/headtree.c @@ -20,7 +20,7 @@ void test_repo_headtree__cleanup(void) void test_repo_headtree__can_retrieve_the_root_tree_from_a_detached_head(void) { - cl_git_pass(git_repository_detach_head(repo)); + cl_git_pass(git_repository_detach_head(repo, NULL, NULL)); cl_git_pass(git_repository_head_tree(&tree, repo)); diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index 56b51852c..fb70a9ea0 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -156,7 +156,7 @@ void test_repo_iterator__index_icase(void) { git_iterator *i; git_index *index; - unsigned int caps; + int caps; g_repo = cl_git_sandbox_init("icase"); diff --git a/tests/repo/message.c b/tests/repo/message.c index 57e8e5f4d..87574590b 100644 --- a/tests/repo/message.c +++ b/tests/repo/message.c @@ -4,38 +4,36 @@ #include "posix.h" static git_repository *_repo; -static git_buf _path; -static git_buf _actual; void test_repo_message__initialize(void) { - _repo = cl_git_sandbox_init("testrepo.git"); - git_buf_init(&_actual, 0); + _repo = cl_git_sandbox_init("testrepo.git"); } void test_repo_message__cleanup(void) { - cl_git_sandbox_cleanup(); - git_buf_free(&_path); - git_buf_free(&_actual); + cl_git_sandbox_cleanup(); } void test_repo_message__none(void) { - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&_actual, _repo)); + git_buf actual = GIT_BUF_INIT; + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&actual, _repo)); } void test_repo_message__message(void) { + git_buf path = GIT_BUF_INIT, actual = GIT_BUF_INIT; const char expected[] = "Test\n\nThis is a test of the emergency broadcast system\n"; - cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), "MERGE_MSG")); - cl_git_mkfile(git_buf_cstr(&_path), expected); + cl_git_pass(git_buf_joinpath(&path, git_repository_path(_repo), "MERGE_MSG")); + cl_git_mkfile(git_buf_cstr(&path), expected); - cl_git_pass(git_repository_message(&_actual, _repo)); - cl_assert_equal_s(expected, _actual); - git_buf_free(&_actual); + cl_git_pass(git_repository_message(&actual, _repo)); + cl_assert_equal_s(expected, git_buf_cstr(&actual)); + git_buf_free(&actual); - cl_git_pass(p_unlink(git_buf_cstr(&_path))); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&_actual, _repo)); + cl_git_pass(p_unlink(git_buf_cstr(&path))); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&actual, _repo)); + git_buf_free(&path); } diff --git a/tests/repo/open.c b/tests/repo/open.c index 7cfe041c2..f7420bd3a 100644 --- a/tests/repo/open.c +++ b/tests/repo/open.c @@ -1,5 +1,6 @@ #include "clar_libgit2.h" #include "fileops.h" +#include "sysdir.h" #include <ctype.h> void test_repo_open__cleanup(void) @@ -323,7 +324,7 @@ void test_repo_open__no_config(void) git_repository_free(repo); cl_fixture_cleanup("empty_standard_repo"); - git_futils_dirs_global_shutdown(); + git_sysdir_global_shutdown(); } void test_repo_open__force_bare(void) diff --git a/tests/repo/shallow.c b/tests/repo/shallow.c index 1cc66ae40..5aeaf2def 100644 --- a/tests/repo/shallow.c +++ b/tests/repo/shallow.c @@ -31,3 +31,9 @@ void test_repo_shallow__shallow_repo(void) cl_assert_equal_i(1, git_repository_is_shallow(g_repo)); } +void test_repo_shallow__clears_errors(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); + cl_assert_equal_p(NULL, giterr_last()); +} diff --git a/tests/repo/state.c b/tests/repo/state.c index 5a0a5f360..5e7227205 100644 --- a/tests/repo/state.c +++ b/tests/repo/state.c @@ -37,7 +37,7 @@ void test_repo_state__none_with_HEAD_attached(void) void test_repo_state__none_with_HEAD_detached(void) { - cl_git_pass(git_repository_detach_head(_repo)); + cl_git_pass(git_repository_detach_head(_repo, NULL, NULL)); assert_repo_state(GIT_REPOSITORY_STATE_NONE); } diff --git a/tests/reset/hard.c b/tests/reset/hard.c index 0f80d32df..d4c7db45f 100644 --- a/tests/reset/hard.c +++ b/tests/reset/hard.c @@ -69,10 +69,9 @@ void test_reset_hard__resetting_reverts_modified_files(void) cl_assert_equal_s(before[i], content.ptr); } - retrieve_target_from_oid( - &target, repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f"); + cl_git_pass(git_revparse_single(&target, repo, "26a125e")); - cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL)); for (i = 0; i < 4; ++i) { cl_git_pass(git_buf_joinpath(&path, wd, files[i])); @@ -95,9 +94,9 @@ void test_reset_hard__cannot_reset_in_a_bare_repository(void) cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git"))); cl_assert(git_repository_is_bare(bare) == true); - retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO); + cl_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO)); - cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD)); + cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD, NULL, NULL)); git_repository_free(bare); } @@ -152,8 +151,8 @@ void test_reset_hard__resetting_reverts_unmerged(void) unmerged_index_init(index, entries); cl_git_pass(git_index_write(index)); - retrieve_target_from_oid(&target, repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f"); - cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); + cl_git_pass(git_revparse_single(&target, repo, "26a125e")); + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL)); cl_assert(git_path_exists("status/conflicting_file") == 0); @@ -183,8 +182,8 @@ void test_reset_hard__cleans_up_merge(void) cl_git_pass(git_buf_joinpath(&orig_head_path, git_repository_path(repo), "ORIG_HEAD")); cl_git_mkfile(git_buf_cstr(&orig_head_path), "0017bd4ab1ec30440b17bae1680cff124ab5f1f6"); - retrieve_target_from_oid(&target, repo, "0017bd4ab1ec30440b17bae1680cff124ab5f1f6"); - cl_git_pass(git_reset(repo, target, GIT_RESET_HARD)); + cl_git_pass(git_revparse_single(&target, repo, "0017bd4")); + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL)); cl_assert(!git_path_exists(git_buf_cstr(&merge_head_path))); cl_assert(!git_path_exists(git_buf_cstr(&merge_msg_path))); @@ -198,3 +197,30 @@ void test_reset_hard__cleans_up_merge(void) git_buf_free(&merge_mode_path); git_buf_free(&orig_head_path); } + +void test_reset_hard__reflog_is_correct(void) +{ + const char *exp_msg = "commit: Add a file which name should appear before the " + "\"subdir/\" folder while being dealt with by the treewalker"; + + reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg); + + /* Branch not moving, no reflog entry */ + cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}")); + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL)); + reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg); + + /* Moved branch, expect default message */ + cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}")); + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL)); + reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 4, NULL, "reset: moving"); + + /* Moved branch, expect custom message */ + cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}")); + cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, "message1")); + reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 5, NULL, "message1"); +} diff --git a/tests/reset/mixed.c b/tests/reset/mixed.c index 7b90c23f1..25272a75c 100644 --- a/tests/reset/mixed.c +++ b/tests/reset/mixed.c @@ -27,9 +27,9 @@ void test_reset_mixed__cannot_reset_in_a_bare_repository(void) cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git"))); cl_assert(git_repository_is_bare(bare) == true); - retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO); + cl_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO)); - cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED)); + cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED, NULL, NULL)); git_repository_free(bare); } @@ -40,10 +40,36 @@ void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void) cl_git_pass(git_status_file(&status, repo, "macro_bad")); cl_assert(status == GIT_STATUS_CURRENT); - retrieve_target_from_oid(&target, repo, "605812ab7fe421fdd325a935d35cb06a9234a7d7"); + cl_git_pass(git_revparse_single(&target, repo, "605812a")); - cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED)); + cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL)); cl_git_pass(git_status_file(&status, repo, "macro_bad")); cl_assert(status == GIT_STATUS_WT_NEW); } + +void test_reset_mixed__reflog_is_correct(void) +{ + const char *exp_msg = "commit: Updating test data so we can test inter-hunk-context"; + + reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg); + + /* Branch not moving, no reflog entry */ + cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}")); + cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL)); + reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg); + + /* Moved branch, expect default message */ + cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}")); + cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL)); + reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 10, NULL, "reset: moving"); + + /* Moved branch, expect custom message */ + cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}")); + cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, "message1")); + reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 11, NULL, "message1"); +} diff --git a/tests/reset/reset_helpers.c b/tests/reset/reset_helpers.c index 17edca4e9..7a335a600 100644 --- a/tests/reset/reset_helpers.c +++ b/tests/reset/reset_helpers.c @@ -1,10 +1,20 @@ #include "clar_libgit2.h" #include "reset_helpers.h" -void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha) +void reflog_check(git_repository *repo, const char *refname, + size_t exp_count, const char *exp_email, const char *exp_msg) { - git_oid oid; + git_reflog *log; + const git_reflog_entry *entry; - cl_git_pass(git_oid_fromstr(&oid, sha)); - cl_git_pass(git_object_lookup(object_out, repo, &oid, GIT_OBJ_ANY)); + cl_git_pass(git_reflog_read(&log, repo, refname)); + cl_assert_equal_i(exp_count, git_reflog_entrycount(log)); + entry = git_reflog_entry_byindex(log, 0); + + if (exp_email) + cl_assert_equal_s(exp_email, git_reflog_entry_committer(entry)->email); + if (exp_msg) + cl_assert_equal_s(exp_msg, git_reflog_entry_message(entry)); + + git_reflog_free(log); } diff --git a/tests/reset/reset_helpers.h b/tests/reset/reset_helpers.h index 5dbe9d2c7..e7e048514 100644 --- a/tests/reset/reset_helpers.h +++ b/tests/reset/reset_helpers.h @@ -3,4 +3,5 @@ #define KNOWN_COMMIT_IN_BARE_REPO "e90810b8df3e80c413d903f631643c716887138d" #define KNOWN_COMMIT_IN_ATTR_REPO "217878ab49e1314388ea2e32dc6fdb58a1b969e0" -extern void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha); +void reflog_check(git_repository *repo, const char *refname, + size_t exp_count, const char *exp_email, const char *exp_msg); diff --git a/tests/reset/soft.c b/tests/reset/soft.c index bd6fcc205..c889c0355 100644 --- a/tests/reset/soft.c +++ b/tests/reset/soft.c @@ -26,12 +26,11 @@ static void assert_reset_soft(bool should_be_detached) cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); - - retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); + cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO)); cl_assert(git_repository_head_detached(repo) == should_be_detached); - cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); cl_assert(git_repository_head_detached(repo) == should_be_detached); @@ -46,7 +45,7 @@ void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(vo void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void) { - git_repository_detach_head(repo); + git_repository_detach_head(repo, NULL, NULL); assert_reset_soft(true); } @@ -60,9 +59,9 @@ void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_ch git_oid_fmt(raw_head_oid, &oid); raw_head_oid[GIT_OID_HEXSZ] = '\0'; - retrieve_target_from_oid(&target, repo, raw_head_oid); + cl_git_pass(git_revparse_single(&target, repo, raw_head_oid)); - cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); cl_git_pass(git_oid_streq(&oid, raw_head_oid)); @@ -73,9 +72,9 @@ void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void git_oid oid; /* b25fa35 is a tag, pointing to another tag which points to commit e90810b */ - retrieve_target_from_oid(&target, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); + cl_git_pass(git_revparse_single(&target, repo, "b25fa35")); - cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); cl_assert(git_repository_head_detached(repo) == false); cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); @@ -85,27 +84,27 @@ void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void) { /* 53fc32d is the tree of commit e90810b */ - retrieve_target_from_oid(&target, repo, "53fc32d17276939fc79ed05badaef2db09990016"); + cl_git_pass(git_revparse_single(&target, repo, "53fc32d")); - cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT)); + cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); git_object_free(target); /* 521d87c is an annotated tag pointing to a blob */ - retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91"); - cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT)); + cl_git_pass(git_revparse_single(&target, repo, "521d87c")); + cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); } void test_reset_soft__resetting_against_an_unborn_head_repo_makes_the_head_no_longer_unborn(void) { git_reference *head; - retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); + cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO)); make_head_unborn(repo, NON_EXISTING_HEAD); cl_assert_equal_i(true, git_repository_head_unborn(repo)); - cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); cl_assert_equal_i(false, git_repository_head_unborn(repo)); @@ -119,13 +118,13 @@ void test_reset_soft__fails_when_merging(void) { git_buf merge_head_path = GIT_BUF_INIT; - cl_git_pass(git_repository_detach_head(repo)); + cl_git_pass(git_repository_detach_head(repo, NULL, NULL)); cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n"); - retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); + cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO)); - cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT)); + cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path))); git_buf_free(&merge_head_path); @@ -153,5 +152,31 @@ void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE cl_git_pass(git_reference_peel(&target, head, GIT_OBJ_COMMIT)); git_reference_free(head); - cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT)); + cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); +} + +void test_reset_soft_reflog_is_correct(void) +{ + const char *exp_msg = "commit: Updating test data so we can test inter-hunk-context"; + + reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg); + + /* Branch not moving, no reflog entry */ + cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}")); + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); + reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg); + + /* Moved branch, expect default message */ + cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}")); + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL)); + reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 10, NULL, "reset: moving"); + + /* Moved branch, expect custom message */ + cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}")); + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, "message1")); + reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg); + reflog_check(repo, "refs/heads/master", 11, NULL, "message1"); } diff --git a/tests/revert/workdir.c b/tests/revert/workdir.c index 5be397c93..47abeb8f0 100644 --- a/tests/revert/workdir.c +++ b/tests/revert/workdir.c @@ -15,8 +15,15 @@ static git_index *repo_index; // Fixture setup and teardown void test_revert_workdir__initialize(void) { + git_config *cfg; + repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&repo_index, repo); + + /* Ensure that the user's merge.conflictstyle doesn't interfere */ + cl_git_pass(git_repository_config(&cfg, repo)); + cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge")); + git_config_free(cfg); } void test_revert_workdir__cleanup(void) @@ -41,7 +48,7 @@ void test_revert_workdir__automerge(void) git_oid_fromstr(&head_oid, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); - cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL)); git_oid_fromstr(&revert_oid, "d1d403d22cbe24592d725f442835cf46fe60c8ac"); cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid)); @@ -74,7 +81,7 @@ void test_revert_workdir__conflicts(void) cl_git_pass(git_repository_head(&head_ref, repo)); cl_git_pass(git_reference_peel((git_object **)&head, head_ref, GIT_OBJ_COMMIT)); - cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid)); cl_git_pass(git_revert(repo, commit, NULL)); @@ -125,7 +132,7 @@ void test_revert_workdir__orphan(void) git_oid_fromstr(&head_oid, "39467716290f6df775a91cdb9a4eb39295018145"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); - cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL)); git_oid_fromstr(&revert_oid, "ebb03002cee5d66c7732dd06241119fe72ab96a5"); cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid)); @@ -160,7 +167,7 @@ void test_revert_workdir__again(void) cl_git_pass(git_repository_head(&head_ref, repo)); cl_git_pass(git_reference_peel((git_object **)&orig_head, head_ref, GIT_OBJ_COMMIT)); - cl_git_pass(git_reset(repo, (git_object *)orig_head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)orig_head, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_revert(repo, orig_head, NULL)); @@ -208,7 +215,7 @@ void test_revert_workdir__again_after_automerge(void) git_oid_fromstr(&head_oid, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); - cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL)); git_oid_fromstr(&revert_oid, "d1d403d22cbe24592d725f442835cf46fe60c8ac"); cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid)); @@ -256,7 +263,7 @@ void test_revert_workdir__again_after_edit(void) cl_git_pass(git_oid_fromstr(&orig_head_oid, "399fb3aba3d9d13f7d40a9254ce4402067ef3149")); cl_git_pass(git_commit_lookup(&orig_head, repo, &orig_head_oid)); - cl_git_pass(git_reset(repo, (git_object *)orig_head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)orig_head, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_oid_fromstr(&revert_oid, "2d440f2b3147d3dc7ad1085813478d6d869d5a4d")); cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid)); @@ -307,7 +314,7 @@ void test_revert_workdir__again_after_edit_two(void) cl_git_pass(git_oid_fromstr(&head_commit_oid, "e34ef1afe54eb526fd92eec66084125f340f1d65")); cl_git_pass(git_commit_lookup(&head_commit, repo, &head_commit_oid)); - cl_git_pass(git_reset(repo, (git_object *)head_commit, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head_commit, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_oid_fromstr(&revert_commit_oid, "71eb9c2b53dbbf3c45fb28b27c850db4b7fb8011")); cl_git_pass(git_commit_lookup(&revert_commit, repo, &revert_commit_oid)); @@ -360,7 +367,7 @@ void test_revert_workdir__conflict_use_ours(void) git_oid_fromstr(&head_oid, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); - cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL)); git_oid_fromstr(&revert_oid, "d1d403d22cbe24592d725f442835cf46fe60c8ac"); cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid)); @@ -396,7 +403,7 @@ void test_revert_workdir__rename_1_of_2(void) git_oid_fromstr(&head_oid, "cef56612d71a6af8d8015691e4865f7fece905b5"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); - cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL)); git_oid_fromstr(&revert_oid, "55568c8de5322ff9a95d72747a239cdb64a19965"); cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid)); @@ -430,7 +437,7 @@ void test_revert_workdir__rename(void) git_oid_fromstr(&head_oid, "55568c8de5322ff9a95d72747a239cdb64a19965"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); - cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL)); git_oid_fromstr(&revert_oid, "0aa8c7e40d342fff78d60b29a4ba8e993ed79c51"); cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid)); @@ -459,7 +466,7 @@ void test_revert_workdir__head(void) /* HEAD is 2d440f2b3147d3dc7ad1085813478d6d869d5a4d */ cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_reference_peel((git_object **)&commit, head, GIT_OBJ_COMMIT)); - cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_revert(repo, commit, NULL)); cl_assert(merge_test_index(repo_index, merge_index_entries, 4)); @@ -496,7 +503,7 @@ void test_revert_workdir__merge_fails_without_mainline_specified(void) git_oid_fromstr(&head_oid, "5acdc74af27172ec491d213ee36cea7eb9ef2579"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); - cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL)); cl_must_fail(git_revert(repo, head, NULL)); cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG")); @@ -523,7 +530,7 @@ void test_revert_workdir__merge_first_parent(void) git_oid_fromstr(&head_oid, "5acdc74af27172ec491d213ee36cea7eb9ef2579"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); - cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_revert(repo, head, &opts)); @@ -548,7 +555,7 @@ void test_revert_workdir__merge_second_parent(void) git_oid_fromstr(&head_oid, "5acdc74af27172ec491d213ee36cea7eb9ef2579"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); - cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD)); + cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL)); cl_git_pass(git_revert(repo, head, &opts)); diff --git a/tests/revwalk/basic.c b/tests/revwalk/basic.c index 6d55aed54..9fe8a350b 100644 --- a/tests/revwalk/basic.c +++ b/tests/revwalk/basic.c @@ -252,3 +252,41 @@ void test_revwalk_basic__push_range(void) cl_git_pass(git_revwalk_push_range(_walk, "9fd738e~2..9fd738e")); cl_git_pass(test_walk_only(_walk, commit_sorting_segment, 1)); } + +void test_revwalk_basic__push_mixed(void) +{ + git_oid oid; + int i = 0; + + revwalk_basic_setup_walk(NULL); + + git_revwalk_reset(_walk); + git_revwalk_sorting(_walk, 0); + cl_git_pass(git_revwalk_push_glob(_walk, "tags")); + + while (git_revwalk_next(&oid, _walk) == 0) { + i++; + } + + /* git rev-list --count --glob=tags #=> 9 */ + cl_assert_equal_i(9, i); +} + +void test_revwalk_basic__push_all(void) +{ + git_oid oid; + int i = 0; + + revwalk_basic_setup_walk(NULL); + + git_revwalk_reset(_walk); + git_revwalk_sorting(_walk, 0); + cl_git_pass(git_revwalk_push_glob(_walk, "*")); + + while (git_revwalk_next(&oid, _walk) == 0) { + i++; + } + + /* git rev-list --count --all #=> 15 */ + cl_assert_equal_i(15, i); +} diff --git a/tests/stash/save.c b/tests/stash/save.c index 293a89a97..b5a793eef 100644 --- a/tests/stash/save.c +++ b/tests/stash/save.c @@ -196,7 +196,7 @@ void test_stash_save__cannot_stash_against_a_bare_repository(void) void test_stash_save__can_stash_against_a_detached_head(void) { - git_repository_detach_head(repo); + git_repository_detach_head(repo, NULL, NULL); cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); |
