diff options
Diffstat (limited to 'tests/repo')
-rw-r--r-- | tests/repo/config.c | 208 | ||||
-rw-r--r-- | tests/repo/discover.c | 142 | ||||
-rw-r--r-- | tests/repo/getters.c | 40 | ||||
-rw-r--r-- | tests/repo/hashfile.c | 85 | ||||
-rw-r--r-- | tests/repo/head.c | 196 | ||||
-rw-r--r-- | tests/repo/headtree.c | 53 | ||||
-rw-r--r-- | tests/repo/init.c | 606 | ||||
-rw-r--r-- | tests/repo/iterator.c | 962 | ||||
-rw-r--r-- | tests/repo/message.c | 52 | ||||
-rw-r--r-- | tests/repo/open.c | 376 | ||||
-rw-r--r-- | tests/repo/pathspec.c | 385 | ||||
-rw-r--r-- | tests/repo/repo_helpers.c | 22 | ||||
-rw-r--r-- | tests/repo/repo_helpers.h | 6 | ||||
-rw-r--r-- | tests/repo/setters.c | 107 | ||||
-rw-r--r-- | tests/repo/shallow.c | 33 | ||||
-rw-r--r-- | tests/repo/state.c | 96 |
16 files changed, 3369 insertions, 0 deletions
diff --git a/tests/repo/config.c b/tests/repo/config.c new file mode 100644 index 000000000..e77acc8c5 --- /dev/null +++ b/tests/repo/config.c @@ -0,0 +1,208 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include <ctype.h> + +static git_buf path = GIT_BUF_INIT; + +void test_repo_config__initialize(void) +{ + cl_fixture_sandbox("empty_standard_repo"); + cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git")); + + git_buf_clear(&path); + + cl_must_pass(p_mkdir("alternate", 0777)); + cl_git_pass(git_path_prettify(&path, "alternate", NULL)); +} + +void test_repo_config__cleanup(void) +{ + cl_git_pass(git_path_prettify(&path, "alternate", NULL)); + cl_git_pass(git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)); + git_buf_free(&path); + cl_assert(!git_path_isdir("alternate")); + + cl_fixture_cleanup("empty_standard_repo"); +} + +void test_repo_config__open_missing_global(void) +{ + git_repository *repo; + git_config *config, *global; + + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); + + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); + cl_git_pass(git_repository_config(&config, repo)); + cl_git_pass(git_config_open_level(&global, config, GIT_CONFIG_LEVEL_GLOBAL)); + + cl_git_pass(git_config_set_string(global, "test.set", "42")); + + git_config_free(global); + git_config_free(config); + git_repository_free(repo); + + git_futils_dirs_global_shutdown(); +} + +void test_repo_config__open_missing_global_with_separators(void) +{ + git_repository *repo; + git_config *config, *global; + + cl_git_pass(git_buf_printf(&path, "%c%s", GIT_PATH_LIST_SEPARATOR, "dummy")); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); + + git_buf_free(&path); + + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); + cl_git_pass(git_repository_config(&config, repo)); + cl_git_pass(git_config_open_level(&global, config, GIT_CONFIG_LEVEL_GLOBAL)); + + cl_git_pass(git_config_set_string(global, "test.set", "42")); + + git_config_free(global); + git_config_free(config); + git_repository_free(repo); + + git_futils_dirs_global_shutdown(); +} + +#include "repository.h" + +void test_repo_config__read_no_configs(void) +{ + git_repository *repo; + int val; + + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); + + /* with none */ + + cl_must_pass(p_unlink("empty_standard_repo/.git/config")); + cl_assert(!git_path_isfile("empty_standard_repo/.git/config")); + + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); + git_repository__cvar_cache_clear(repo); + val = -1; + cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV)); + cl_assert_equal_i(GIT_ABBREV_DEFAULT, val); + git_repository_free(repo); + + git_futils_dirs_global_shutdown(); + + /* with just system */ + + cl_must_pass(p_mkdir("alternate/1", 0777)); + cl_git_pass(git_buf_joinpath(&path, path.ptr, "1")); + cl_git_rewritefile("alternate/1/gitconfig", "[core]\n\tabbrev = 10\n"); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); + + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); + git_repository__cvar_cache_clear(repo); + val = -1; + cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV)); + cl_assert_equal_i(10, val); + git_repository_free(repo); + + /* with xdg + system */ + + cl_must_pass(p_mkdir("alternate/2", 0777)); + path.ptr[path.size - 1] = '2'; + cl_git_rewritefile("alternate/2/config", "[core]\n\tabbrev = 20\n"); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); + + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); + git_repository__cvar_cache_clear(repo); + val = -1; + cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV)); + cl_assert_equal_i(20, val); + git_repository_free(repo); + + /* with global + xdg + system */ + + cl_must_pass(p_mkdir("alternate/3", 0777)); + path.ptr[path.size - 1] = '3'; + cl_git_rewritefile("alternate/3/.gitconfig", "[core]\n\tabbrev = 30\n"); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); + + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); + git_repository__cvar_cache_clear(repo); + val = -1; + cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV)); + cl_assert_equal_i(30, val); + git_repository_free(repo); + + /* with all configs */ + + cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n"); + + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); + git_repository__cvar_cache_clear(repo); + val = -1; + cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV)); + cl_assert_equal_i(40, val); + git_repository_free(repo); + + /* with all configs but delete the files ? */ + + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); + git_repository__cvar_cache_clear(repo); + val = -1; + cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV)); + cl_assert_equal_i(40, val); + + cl_must_pass(p_unlink("empty_standard_repo/.git/config")); + cl_assert(!git_path_isfile("empty_standard_repo/.git/config")); + + cl_must_pass(p_unlink("alternate/1/gitconfig")); + cl_assert(!git_path_isfile("alternate/1/gitconfig")); + + cl_must_pass(p_unlink("alternate/2/config")); + cl_assert(!git_path_isfile("alternate/2/config")); + + cl_must_pass(p_unlink("alternate/3/.gitconfig")); + cl_assert(!git_path_isfile("alternate/3/.gitconfig")); + + git_repository__cvar_cache_clear(repo); + val = -1; + cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV)); + cl_assert_equal_i(40, val); + git_repository_free(repo); + + /* reopen */ + + cl_assert(!git_path_isfile("empty_standard_repo/.git/config")); + cl_assert(!git_path_isfile("alternate/3/.gitconfig")); + + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); + git_repository__cvar_cache_clear(repo); + val = -1; + cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV)); + cl_assert_equal_i(7, val); + git_repository_free(repo); + + cl_assert(!git_path_exists("empty_standard_repo/.git/config")); + cl_assert(!git_path_exists("alternate/3/.gitconfig")); + + git_futils_dirs_global_shutdown(); +} diff --git a/tests/repo/discover.c b/tests/repo/discover.c new file mode 100644 index 000000000..f93ff2462 --- /dev/null +++ b/tests/repo/discover.c @@ -0,0 +1,142 @@ +#include "clar_libgit2.h" + +#include "odb.h" +#include "fileops.h" +#include "repository.h" + +#define TEMP_REPO_FOLDER "temprepo/" +#define DISCOVER_FOLDER TEMP_REPO_FOLDER "discover.git" + +#define SUB_REPOSITORY_FOLDER_NAME "sub_repo" +#define SUB_REPOSITORY_FOLDER DISCOVER_FOLDER "/" SUB_REPOSITORY_FOLDER_NAME +#define SUB_REPOSITORY_FOLDER_SUB SUB_REPOSITORY_FOLDER "/sub" +#define SUB_REPOSITORY_FOLDER_SUB_SUB SUB_REPOSITORY_FOLDER_SUB "/subsub" +#define SUB_REPOSITORY_FOLDER_SUB_SUB_SUB SUB_REPOSITORY_FOLDER_SUB_SUB "/subsubsub" + +#define REPOSITORY_ALTERNATE_FOLDER DISCOVER_FOLDER "/alternate_sub_repo" +#define REPOSITORY_ALTERNATE_FOLDER_SUB REPOSITORY_ALTERNATE_FOLDER "/sub" +#define REPOSITORY_ALTERNATE_FOLDER_SUB_SUB REPOSITORY_ALTERNATE_FOLDER_SUB "/subsub" +#define REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/subsubsub" + +#define ALTERNATE_MALFORMED_FOLDER1 DISCOVER_FOLDER "/alternate_malformed_repo1" +#define ALTERNATE_MALFORMED_FOLDER2 DISCOVER_FOLDER "/alternate_malformed_repo2" +#define ALTERNATE_MALFORMED_FOLDER3 DISCOVER_FOLDER "/alternate_malformed_repo3" +#define ALTERNATE_NOT_FOUND_FOLDER DISCOVER_FOLDER "/alternate_not_found_repo" + +static void ensure_repository_discover(const char *start_path, + const char *ceiling_dirs, + const char *expected_path) +{ + char found_path[GIT_PATH_MAX]; + cl_git_pass(git_repository_discover(found_path, sizeof(found_path), start_path, 0, ceiling_dirs)); + //across_fs is always 0 as we can't automate the filesystem change tests + cl_assert_equal_s(found_path, expected_path); +} + +static void write_file(const char *path, const char *content) +{ + git_file file; + int error; + + if (git_path_exists(path)) { + cl_git_pass(p_unlink(path)); + } + + file = git_futils_creat_withpath(path, 0777, 0666); + cl_assert(file >= 0); + + error = p_write(file, content, strlen(content) * sizeof(char)); + p_close(file); + cl_git_pass(error); +} + +//no check is performed on ceiling_dirs length, so be sure it's long enough +static void append_ceiling_dir(git_buf *ceiling_dirs, const char *path) +{ + git_buf pretty_path = GIT_BUF_INIT; + char ceiling_separator[2] = { GIT_PATH_LIST_SEPARATOR, '\0' }; + + cl_git_pass(git_path_prettify_dir(&pretty_path, path, NULL)); + + if (ceiling_dirs->size > 0) + git_buf_puts(ceiling_dirs, ceiling_separator); + + git_buf_puts(ceiling_dirs, pretty_path.ptr); + + git_buf_free(&pretty_path); + cl_assert(git_buf_oom(ceiling_dirs) == 0); +} + +void test_repo_discover__0(void) +{ + // test discover + git_repository *repo; + git_buf ceiling_dirs_buf = GIT_BUF_INIT; + const char *ceiling_dirs; + char repository_path[GIT_PATH_MAX]; + char sub_repository_path[GIT_PATH_MAX]; + char found_path[GIT_PATH_MAX]; + const mode_t mode = 0777; + + git_futils_mkdir_r(DISCOVER_FOLDER, NULL, mode); + append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER); + ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); + + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs)); + + cl_git_pass(git_repository_init(&repo, DISCOVER_FOLDER, 1)); + cl_git_pass(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs)); + git_repository_free(repo); + + cl_git_pass(git_repository_init(&repo, SUB_REPOSITORY_FOLDER, 0)); + cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode)); + cl_git_pass(git_repository_discover(sub_repository_path, sizeof(sub_repository_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); + + cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode)); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, sub_repository_path); + + cl_git_pass(git_futils_mkdir_r(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, NULL, mode)); + write_file(REPOSITORY_ALTERNATE_FOLDER "/" DOT_GIT, "gitdir: ../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT); + write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/" DOT_GIT, "gitdir: ../../../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT); + write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB "/" DOT_GIT, "gitdir: ../../../../"); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path); + + cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER1, NULL, mode)); + write_file(ALTERNATE_MALFORMED_FOLDER1 "/" DOT_GIT, "Anything but not gitdir:"); + cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER2, NULL, mode)); + write_file(ALTERNATE_MALFORMED_FOLDER2 "/" DOT_GIT, "gitdir:"); + cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER3, NULL, mode)); + write_file(ALTERNATE_MALFORMED_FOLDER3 "/" DOT_GIT, "gitdir: \n\n\n"); + cl_git_pass(git_futils_mkdir_r(ALTERNATE_NOT_FOUND_FOLDER, NULL, mode)); + write_file(ALTERNATE_NOT_FOUND_FOLDER "/" DOT_GIT, "gitdir: a_repository_that_surely_does_not_exist"); + cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs)); + cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs)); + cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs)); + + append_ceiling_dir(&ceiling_dirs_buf, SUB_REPOSITORY_FOLDER); + ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); + + //this must pass as ceiling_directories cannot predent the current + //working directory to be checked + cl_git_pass(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs)); + + //.gitfile redirection should not be affected by ceiling directories + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path); + + cl_git_pass(git_futils_rmdir_r(TEMP_REPO_FOLDER, NULL, GIT_RMDIR_REMOVE_FILES)); + git_repository_free(repo); + git_buf_free(&ceiling_dirs_buf); +} + diff --git a/tests/repo/getters.c b/tests/repo/getters.c new file mode 100644 index 000000000..b8ede126c --- /dev/null +++ b/tests/repo/getters.c @@ -0,0 +1,40 @@ +#include "clar_libgit2.h" + +void test_repo_getters__is_empty_correctly_deals_with_pristine_looking_repos(void) +{ + git_repository *repo; + + repo = cl_git_sandbox_init("empty_bare.git"); + cl_git_remove_placeholders(git_repository_path(repo), "dummy-marker.txt"); + + cl_assert_equal_i(true, git_repository_is_empty(repo)); + + cl_git_sandbox_cleanup(); +} + +void test_repo_getters__is_empty_can_detect_used_repositories(void) +{ + git_repository *repo; + + cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); + + cl_assert_equal_i(false, git_repository_is_empty(repo)); + + git_repository_free(repo); +} + +void test_repo_getters__retrieving_the_odb_honors_the_refcount(void) +{ + git_odb *odb; + git_repository *repo; + + cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); + + cl_git_pass(git_repository_odb(&odb, repo)); + cl_assert(((git_refcount *)odb)->refcount.val == 2); + + git_repository_free(repo); + cl_assert(((git_refcount *)odb)->refcount.val == 1); + + git_odb_free(odb); +} diff --git a/tests/repo/hashfile.c b/tests/repo/hashfile.c new file mode 100644 index 000000000..4cc9f18b4 --- /dev/null +++ b/tests/repo/hashfile.c @@ -0,0 +1,85 @@ +#include "clar_libgit2.h" +#include "buffer.h" + +static git_repository *_repo; + +void test_repo_hashfile__initialize(void) +{ + _repo = cl_git_sandbox_init("status"); +} + +void test_repo_hashfile__cleanup(void) +{ + cl_git_sandbox_cleanup(); + _repo = NULL; +} + +void test_repo_hashfile__simple(void) +{ + git_oid a, b; + git_buf full = GIT_BUF_INIT; + + /* hash with repo relative path */ + cl_git_pass(git_odb_hashfile(&a, "status/current_file", GIT_OBJ_BLOB)); + cl_git_pass(git_repository_hashfile(&b, _repo, "current_file", GIT_OBJ_BLOB, NULL)); + cl_assert(git_oid_equal(&a, &b)); + + cl_git_pass(git_buf_joinpath(&full, git_repository_workdir(_repo), "current_file")); + + /* hash with full path */ + cl_git_pass(git_odb_hashfile(&a, full.ptr, GIT_OBJ_BLOB)); + cl_git_pass(git_repository_hashfile(&b, _repo, full.ptr, GIT_OBJ_BLOB, NULL)); + cl_assert(git_oid_equal(&a, &b)); + + /* hash with invalid type */ + cl_git_fail(git_odb_hashfile(&a, full.ptr, GIT_OBJ_ANY)); + cl_git_fail(git_repository_hashfile(&b, _repo, full.ptr, GIT_OBJ_OFS_DELTA, NULL)); + + git_buf_free(&full); +} + +void test_repo_hashfile__filtered(void) +{ + git_oid a, b; + + cl_repo_set_bool(_repo, "core.autocrlf", true); + + cl_git_append2file("status/.gitattributes", "*.txt text\n*.bin binary\n\n"); + + /* create some sample content with CRLF in it */ + cl_git_mkfile("status/testfile.txt", "content\r\n"); + cl_git_mkfile("status/testfile.bin", "other\r\nstuff\r\n"); + + /* not equal hashes because of filtering */ + cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJ_BLOB)); + cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJ_BLOB, NULL)); + cl_assert(git_oid_cmp(&a, &b)); + + /* equal hashes because filter is binary */ + cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJ_BLOB)); + cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJ_BLOB, NULL)); + cl_assert(git_oid_equal(&a, &b)); + + /* equal hashes when 'as_file' points to binary filtering */ + cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJ_BLOB)); + cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJ_BLOB, "foo.bin")); + cl_assert(git_oid_equal(&a, &b)); + + /* not equal hashes when 'as_file' points to text filtering */ + cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJ_BLOB)); + cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJ_BLOB, "foo.txt")); + cl_assert(git_oid_cmp(&a, &b)); + + /* equal hashes when 'as_file' is empty and turns off filtering */ + cl_git_pass(git_odb_hashfile(&a, "status/testfile.txt", GIT_OBJ_BLOB)); + cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJ_BLOB, "")); + cl_assert(git_oid_equal(&a, &b)); + + cl_git_pass(git_odb_hashfile(&a, "status/testfile.bin", GIT_OBJ_BLOB)); + cl_git_pass(git_repository_hashfile(&b, _repo, "testfile.bin", GIT_OBJ_BLOB, "")); + cl_assert(git_oid_equal(&a, &b)); + + /* some hash type failures */ + cl_git_fail(git_odb_hashfile(&a, "status/testfile.txt", 0)); + cl_git_fail(git_repository_hashfile(&b, _repo, "testfile.txt", GIT_OBJ_ANY, NULL)); +} diff --git a/tests/repo/head.c b/tests/repo/head.c new file mode 100644 index 000000000..5a55984bd --- /dev/null +++ b/tests/repo/head.c @@ -0,0 +1,196 @@ +#include "clar_libgit2.h" +#include "refs.h" +#include "repo_helpers.h" +#include "posix.h" + +static git_repository *repo; + +void test_repo_head__initialize(void) +{ + repo = cl_git_sandbox_init("testrepo.git"); +} + +void test_repo_head__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +void test_repo_head__head_detached(void) +{ + git_reference *ref; + + cl_git_pass(git_repository_head_detached(repo)); + + cl_git_pass(git_repository_detach_head(repo)); + + 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)); + git_reference_free(ref); + + cl_assert_equal_i(false, git_repository_head_detached(repo)); +} + +void test_repo_head__unborn_head(void) +{ + git_reference *ref; + + cl_git_pass(git_repository_head_detached(repo)); + + make_head_unborn(repo, NON_EXISTING_HEAD); + + cl_assert(git_repository_head_unborn(repo) == 1); + + + /* take the repo back to it's original state */ + cl_git_pass(git_reference_symbolic_create(&ref, repo, "HEAD", "refs/heads/master", 1)); + cl_assert(git_repository_head_unborn(repo) == 0); + + git_reference_free(ref); +} + +void test_repo_head__set_head_Attaches_HEAD_to_un_unborn_branch_when_the_branch_doesnt_exist(void) +{ + git_reference *head; + + cl_git_pass(git_repository_set_head(repo, "refs/heads/doesnt/exist/yet")); + + cl_assert_equal_i(false, git_repository_head_detached(repo)); + + cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_head(&head, repo)); +} + +void test_repo_head__set_head_Returns_ENOTFOUND_when_the_reference_doesnt_exist(void) +{ + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head(repo, "refs/tags/doesnt/exist/yet")); +} + +void test_repo_head__set_head_Fails_when_the_reference_points_to_a_non_commitish(void) +{ + cl_git_fail(git_repository_set_head(repo, "refs/tags/point_to_blob")); +} + +void test_repo_head__set_head_Attaches_HEAD_when_the_reference_points_to_a_branch(void) +{ + git_reference *head; + + cl_git_pass(git_repository_set_head(repo, "refs/heads/br2")); + + cl_assert_equal_i(false, git_repository_head_detached(repo)); + + cl_git_pass(git_repository_head(&head, repo)); + cl_assert_equal_s("refs/heads/br2", git_reference_name(head)); + + git_reference_free(head); +} + +static void assert_head_is_correctly_detached(void) +{ + git_reference *head; + git_object *commit; + + cl_assert_equal_i(true, git_repository_head_detached(repo)); + + cl_git_pass(git_repository_head(&head, repo)); + + cl_git_pass(git_object_lookup(&commit, repo, git_reference_target(head), GIT_OBJ_COMMIT)); + + git_object_free(commit); + git_reference_free(head); +} + +void test_repo_head__set_head_Detaches_HEAD_when_the_reference_doesnt_point_to_a_branch(void) +{ + cl_git_pass(git_repository_set_head(repo, "refs/tags/test")); + + cl_assert_equal_i(true, git_repository_head_detached(repo)); + + assert_head_is_correctly_detached(); +} + +void test_repo_head__set_head_detached_Return_ENOTFOUND_when_the_object_doesnt_exist(void) +{ + git_oid oid; + + cl_git_pass(git_oid_fromstr(&oid, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef")); + + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head_detached(repo, &oid)); +} + +void test_repo_head__set_head_detached_Fails_when_the_object_isnt_a_commitish(void) +{ + git_object *blob; + + cl_git_pass(git_revparse_single(&blob, repo, "point_to_blob")); + + cl_git_fail(git_repository_set_head_detached(repo, git_object_id(blob))); + + git_object_free(blob); +} + +void test_repo_head__set_head_detached_Detaches_HEAD_and_make_it_point_to_the_peeled_commit(void) +{ + git_object *tag; + + cl_git_pass(git_revparse_single(&tag, repo, "tags/test")); + cl_assert_equal_i(GIT_OBJ_TAG, git_object_type(tag)); + + cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag))); + + assert_head_is_correctly_detached(); + + git_object_free(tag); +} + +void test_repo_head__detach_head_Detaches_HEAD_and_make_it_point_to_the_peeled_commit(void) +{ + cl_assert_equal_i(false, git_repository_head_detached(repo)); + + cl_git_pass(git_repository_detach_head(repo)); + + assert_head_is_correctly_detached(); +} + +void test_repo_head__detach_head_Fails_if_HEAD_and_point_to_a_non_commitish(void) +{ + git_reference *head; + + cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, "refs/tags/point_to_blob", 1)); + + cl_git_fail(git_repository_detach_head(repo)); + + git_reference_free(head); +} + +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)); +} + +void test_repo_head__retrieving_an_unborn_branch_returns_GIT_EUNBORNBRANCH(void) +{ + git_reference *head; + + make_head_unborn(repo, NON_EXISTING_HEAD); + + cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_head(&head, repo)); +} + +void test_repo_head__retrieving_a_missing_head_returns_GIT_ENOTFOUND(void) +{ + git_reference *head; + + delete_head(repo); + + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_head(&head, repo)); +} + +void test_repo_head__can_tell_if_an_unborn_head_is_detached(void) +{ + make_head_unborn(repo, NON_EXISTING_HEAD); + + cl_assert_equal_i(false, git_repository_head_detached(repo)); +} diff --git a/tests/repo/headtree.c b/tests/repo/headtree.c new file mode 100644 index 000000000..e899ac399 --- /dev/null +++ b/tests/repo/headtree.c @@ -0,0 +1,53 @@ +#include "clar_libgit2.h" +#include "repository.h" +#include "repo_helpers.h" +#include "posix.h" + +static git_repository *repo; +static git_tree *tree; + +void test_repo_headtree__initialize(void) +{ + repo = cl_git_sandbox_init("testrepo.git"); + tree = NULL; +} + +void test_repo_headtree__cleanup(void) +{ + git_tree_free(tree); + cl_git_sandbox_cleanup(); +} + +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_head_tree(&tree, repo)); + + cl_assert(git_oid_streq(git_tree_id(tree), "az")); +} + +void test_repo_headtree__can_retrieve_the_root_tree_from_a_non_detached_head(void) +{ + cl_assert_equal_i(false, git_repository_head_detached(repo)); + + cl_git_pass(git_repository_head_tree(&tree, repo)); + + cl_assert(git_oid_streq(git_tree_id(tree), "az")); +} + +void test_repo_headtree__when_head_is_unborn_returns_EUNBORNBRANCH(void) +{ + make_head_unborn(repo, NON_EXISTING_HEAD); + + cl_assert_equal_i(true, git_repository_head_unborn(repo)); + + cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_head_tree(&tree, repo)); +} + +void test_repo_headtree__when_head_is_missing_returns_ENOTFOUND(void) +{ + delete_head(repo); + + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_head_tree(&tree, repo)); +} diff --git a/tests/repo/init.c b/tests/repo/init.c new file mode 100644 index 000000000..aea383c64 --- /dev/null +++ b/tests/repo/init.c @@ -0,0 +1,606 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include "repository.h" +#include "config.h" +#include "path.h" + +enum repo_mode { + STANDARD_REPOSITORY = 0, + BARE_REPOSITORY = 1 +}; + +static git_repository *_repo = NULL; +static mode_t g_umask = 0; + +void test_repo_init__initialize(void) +{ + _repo = NULL; + + /* load umask if not already loaded */ + if (!g_umask) { + g_umask = p_umask(022); + (void)p_umask(g_umask); + } +} + +static void cleanup_repository(void *path) +{ + git_repository_free(_repo); + _repo = NULL; + + cl_fixture_cleanup((const char *)path); +} + +static void ensure_repository_init( + const char *working_directory, + int is_bare, + const char *expected_path_repository, + const char *expected_working_directory) +{ + const char *workdir; + + cl_assert(!git_path_isdir(working_directory)); + + cl_git_pass(git_repository_init(&_repo, working_directory, is_bare)); + + workdir = git_repository_workdir(_repo); + if (workdir != NULL || expected_working_directory != NULL) { + cl_assert( + git__suffixcmp(workdir, expected_working_directory) == 0 + ); + } + + cl_assert( + git__suffixcmp(git_repository_path(_repo), expected_path_repository) == 0 + ); + + cl_assert(git_repository_is_bare(_repo) == is_bare); + +#ifdef GIT_WIN32 + if (!is_bare) { + DWORD fattrs = GetFileAttributes(git_repository_path(_repo)); + cl_assert((fattrs & FILE_ATTRIBUTE_HIDDEN) != 0); + } +#endif + + cl_assert(git_repository_is_empty(_repo)); +} + +void test_repo_init__standard_repo(void) +{ + cl_set_cleanup(&cleanup_repository, "testrepo"); + ensure_repository_init("testrepo/", 0, "testrepo/.git/", "testrepo/"); +} + +void test_repo_init__standard_repo_noslash(void) +{ + cl_set_cleanup(&cleanup_repository, "testrepo"); + ensure_repository_init("testrepo", 0, "testrepo/.git/", "testrepo/"); +} + +void test_repo_init__bare_repo(void) +{ + cl_set_cleanup(&cleanup_repository, "testrepo.git"); + ensure_repository_init("testrepo.git/", 1, "testrepo.git/", NULL); +} + +void test_repo_init__bare_repo_noslash(void) +{ + cl_set_cleanup(&cleanup_repository, "testrepo.git"); + ensure_repository_init("testrepo.git", 1, "testrepo.git/", NULL); +} + +void test_repo_init__bare_repo_escaping_current_workdir(void) +{ + git_buf path_repository = GIT_BUF_INIT; + git_buf path_current_workdir = GIT_BUF_INIT; + + cl_git_pass(git_path_prettify_dir(&path_current_workdir, ".", NULL)); + + cl_git_pass(git_buf_joinpath(&path_repository, git_buf_cstr(&path_current_workdir), "a/b/c")); + cl_git_pass(git_futils_mkdir_r(git_buf_cstr(&path_repository), NULL, GIT_DIR_MODE)); + + /* Change the current working directory */ + cl_git_pass(chdir(git_buf_cstr(&path_repository))); + + /* Initialize a bare repo with a relative path escaping out of the current working directory */ + cl_git_pass(git_repository_init(&_repo, "../d/e.git", 1)); + cl_git_pass(git__suffixcmp(git_repository_path(_repo), "/a/b/d/e.git/")); + + git_repository_free(_repo); + + /* Open a bare repo with a relative path escaping out of the current working directory */ + cl_git_pass(git_repository_open(&_repo, "../d/e.git")); + + cl_git_pass(chdir(git_buf_cstr(&path_current_workdir))); + + git_buf_free(&path_current_workdir); + git_buf_free(&path_repository); + + cleanup_repository("a"); +} + +void test_repo_init__reinit_bare_repo(void) +{ + cl_set_cleanup(&cleanup_repository, "reinit.git"); + + /* Initialize the repository */ + cl_git_pass(git_repository_init(&_repo, "reinit.git", 1)); + git_repository_free(_repo); + + /* Reinitialize the repository */ + cl_git_pass(git_repository_init(&_repo, "reinit.git", 1)); +} + +void test_repo_init__reinit_too_recent_bare_repo(void) +{ + git_config *config; + + /* Initialize the repository */ + cl_git_pass(git_repository_init(&_repo, "reinit.git", 1)); + git_repository_config(&config, _repo); + + /* + * Hack the config of the repository to make it look like it has + * been created by a recenter version of git/libgit2 + */ + cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 42)); + + git_config_free(config); + git_repository_free(_repo); + + /* Try to reinitialize the repository */ + cl_git_fail(git_repository_init(&_repo, "reinit.git", 1)); + + cl_fixture_cleanup("reinit.git"); +} + +void test_repo_init__additional_templates(void) +{ + git_buf path = GIT_BUF_INIT; + + cl_set_cleanup(&cleanup_repository, "tester"); + + ensure_repository_init("tester", 0, "tester/.git/", "tester/"); + + cl_git_pass( + git_buf_joinpath(&path, git_repository_path(_repo), "description")); + cl_assert(git_path_isfile(git_buf_cstr(&path))); + + cl_git_pass( + git_buf_joinpath(&path, git_repository_path(_repo), "info/exclude")); + cl_assert(git_path_isfile(git_buf_cstr(&path))); + + cl_git_pass( + git_buf_joinpath(&path, git_repository_path(_repo), "hooks")); + cl_assert(git_path_isdir(git_buf_cstr(&path))); + /* won't confirm specific contents of hooks dir since it may vary */ + + git_buf_free(&path); +} + +static void assert_config_entry_on_init_bytype( + const char *config_key, int expected_value, bool is_bare) +{ + git_config *config; + int error, current_value; + const char *repo_path = is_bare ? + "config_entry/test.bare.git" : "config_entry/test.non.bare.git"; + + cl_set_cleanup(&cleanup_repository, "config_entry"); + + cl_git_pass(git_repository_init(&_repo, repo_path, is_bare)); + + cl_git_pass(git_repository_config(&config, _repo)); + error = git_config_get_bool(¤t_value, config, config_key); + git_config_free(config); + + if (expected_value >= 0) { + cl_assert_equal_i(0, error); + cl_assert_equal_i(expected_value, current_value); + } else { + cl_assert_equal_i(expected_value, error); + } +} + +static void assert_config_entry_on_init( + const char *config_key, int expected_value) +{ + assert_config_entry_on_init_bytype(config_key, expected_value, true); + git_repository_free(_repo); + + assert_config_entry_on_init_bytype(config_key, expected_value, false); +} + +void test_repo_init__detect_filemode(void) +{ + assert_config_entry_on_init("core.filemode", cl_is_chmod_supported()); +} + +void test_repo_init__detect_ignorecase(void) +{ + struct stat st; + bool found_without_match; + + cl_git_write2file("testCAPS", "whatever\n", 0, O_CREAT | O_WRONLY, 0666); + found_without_match = (p_stat("Testcaps", &st) == 0); + cl_must_pass(p_unlink("testCAPS")); + + assert_config_entry_on_init( + "core.ignorecase", found_without_match ? true : GIT_ENOTFOUND); +} + +void test_repo_init__detect_precompose_unicode_required(void) +{ +#ifdef GIT_USE_ICONV + char *composed = "ḱṷṓn", *decomposed = "ḱṷṓn"; + struct stat st; + bool found_with_nfd; + + cl_git_write2file(composed, "whatever\n", 0, O_CREAT | O_WRONLY, 0666); + found_with_nfd = (p_stat(decomposed, &st) == 0); + cl_must_pass(p_unlink(composed)); + + assert_config_entry_on_init("core.precomposeunicode", found_with_nfd); +#else + assert_config_entry_on_init("core.precomposeunicode", GIT_ENOTFOUND); +#endif +} + +void test_repo_init__reinit_doesnot_overwrite_ignorecase(void) +{ + git_config *config; + int current_value; + + /* Init a new repo */ + cl_set_cleanup(&cleanup_repository, "not.overwrite.git"); + cl_git_pass(git_repository_init(&_repo, "not.overwrite.git", 1)); + + /* Change the "core.ignorecase" config value to something unlikely */ + git_repository_config(&config, _repo); + git_config_set_int32(config, "core.ignorecase", 42); + git_config_free(config); + git_repository_free(_repo); + _repo = NULL; + + /* Reinit the repository */ + cl_git_pass(git_repository_init(&_repo, "not.overwrite.git", 1)); + git_repository_config(&config, _repo); + + /* Ensure the "core.ignorecase" config value hasn't been updated */ + cl_git_pass(git_config_get_int32(¤t_value, config, "core.ignorecase")); + cl_assert_equal_i(42, current_value); + + git_config_free(config); +} + +void test_repo_init__reinit_overwrites_filemode(void) +{ + int expected = cl_is_chmod_supported(), current_value; + + /* Init a new repo */ + cl_set_cleanup(&cleanup_repository, "overwrite.git"); + cl_git_pass(git_repository_init(&_repo, "overwrite.git", 1)); + + /* Change the "core.filemode" config value to something unlikely */ + cl_repo_set_bool(_repo, "core.filemode", !expected); + + git_repository_free(_repo); + _repo = NULL; + + /* Reinit the repository */ + cl_git_pass(git_repository_init(&_repo, "overwrite.git", 1)); + + /* Ensure the "core.filemode" config value has been reset */ + current_value = cl_repo_get_bool(_repo, "core.filemode"); + cl_assert_equal_i(expected, current_value); +} + +void test_repo_init__sets_logAllRefUpdates_according_to_type_of_repository(void) +{ + assert_config_entry_on_init_bytype("core.logallrefupdates", GIT_ENOTFOUND, true); + git_repository_free(_repo); + assert_config_entry_on_init_bytype("core.logallrefupdates", true, false); +} + +void test_repo_init__extended_0(void) +{ + git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; + + /* without MKDIR this should fail */ + cl_git_fail(git_repository_init_ext(&_repo, "extended", &opts)); + + /* make the directory first, then it should succeed */ + cl_git_pass(git_futils_mkdir("extended", NULL, 0775, 0)); + cl_git_pass(git_repository_init_ext(&_repo, "extended", &opts)); + + cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "/extended/")); + cl_assert(!git__suffixcmp(git_repository_path(_repo), "/extended/.git/")); + cl_assert(!git_repository_is_bare(_repo)); + cl_assert(git_repository_is_empty(_repo)); + + cleanup_repository("extended"); +} + +void test_repo_init__extended_1(void) +{ + git_reference *ref; + git_remote *remote; + struct stat st; + git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; + + opts.flags = GIT_REPOSITORY_INIT_MKPATH | + GIT_REPOSITORY_INIT_NO_DOTGIT_DIR; + opts.mode = GIT_REPOSITORY_INIT_SHARED_GROUP; + opts.workdir_path = "../c_wd"; + opts.description = "Awesomest test repository evah"; + opts.initial_head = "development"; + opts.origin_url = "https://github.com/libgit2/libgit2.git"; + + cl_git_pass(git_repository_init_ext(&_repo, "root/b/c.git", &opts)); + + cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "/c_wd/")); + cl_assert(!git__suffixcmp(git_repository_path(_repo), "/c.git/")); + cl_assert(git_path_isfile("root/b/c_wd/.git")); + cl_assert(!git_repository_is_bare(_repo)); + /* repo will not be counted as empty because we set head to "development" */ + cl_assert(!git_repository_is_empty(_repo)); + + cl_git_pass(git_path_lstat(git_repository_path(_repo), &st)); + cl_assert(S_ISDIR(st.st_mode)); + if (cl_is_chmod_supported()) + cl_assert((S_ISGID & st.st_mode) == S_ISGID); + else + cl_assert((S_ISGID & st.st_mode) == 0); + + cl_git_pass(git_reference_lookup(&ref, _repo, "HEAD")); + cl_assert(git_reference_type(ref) == GIT_REF_SYMBOLIC); + cl_assert_equal_s("refs/heads/development", git_reference_symbolic_target(ref)); + git_reference_free(ref); + + cl_git_pass(git_remote_load(&remote, _repo, "origin")); + cl_assert_equal_s("origin", git_remote_name(remote)); + cl_assert_equal_s(opts.origin_url, git_remote_url(remote)); + git_remote_free(remote); + + git_repository_free(_repo); + cl_fixture_cleanup("root"); +} + +#define CLEAR_FOR_CORE_FILEMODE(M) ((M) &= ~0177) + +static void assert_hooks_match( + const char *template_dir, + const char *repo_dir, + const char *hook_path, + bool core_filemode) +{ + git_buf expected = GIT_BUF_INIT; + git_buf actual = GIT_BUF_INIT; + struct stat expected_st, st; + + cl_git_pass(git_buf_joinpath(&expected, template_dir, hook_path)); + cl_git_pass(git_path_lstat(expected.ptr, &expected_st)); + + cl_git_pass(git_buf_joinpath(&actual, repo_dir, hook_path)); + cl_git_pass(git_path_lstat(actual.ptr, &st)); + + cl_assert(expected_st.st_size == st.st_size); + + if (GIT_MODE_TYPE(expected_st.st_mode) != GIT_FILEMODE_LINK) { + mode_t expected_mode = + GIT_MODE_TYPE(expected_st.st_mode) | + (GIT_PERMS_FOR_WRITE(expected_st.st_mode) & ~g_umask); + + if (!core_filemode) { + CLEAR_FOR_CORE_FILEMODE(expected_mode); + CLEAR_FOR_CORE_FILEMODE(st.st_mode); + } + + cl_assert_equal_i_fmt(expected_mode, st.st_mode, "%07o"); + } + + git_buf_free(&expected); + git_buf_free(&actual); +} + +static void assert_mode_seems_okay( + const char *base, const char *path, + git_filemode_t expect_mode, bool expect_setgid, bool core_filemode) +{ + git_buf full = GIT_BUF_INIT; + struct stat st; + + cl_git_pass(git_buf_joinpath(&full, base, path)); + cl_git_pass(git_path_lstat(full.ptr, &st)); + git_buf_free(&full); + + if (!core_filemode) { + CLEAR_FOR_CORE_FILEMODE(expect_mode); + CLEAR_FOR_CORE_FILEMODE(st.st_mode); + expect_setgid = false; + } + + if (S_ISGID != 0) + cl_assert_equal_b(expect_setgid, (st.st_mode & S_ISGID) != 0); + + cl_assert_equal_b( + GIT_PERMS_IS_EXEC(expect_mode), GIT_PERMS_IS_EXEC(st.st_mode)); + + cl_assert_equal_i_fmt( + GIT_MODE_TYPE(expect_mode), GIT_MODE_TYPE(st.st_mode), "%07o"); +} + +void test_repo_init__extended_with_template(void) +{ + git_buf expected = GIT_BUF_INIT; + git_buf actual = GIT_BUF_INIT; + git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; + int filemode; + + cl_set_cleanup(&cleanup_repository, "templated.git"); + + opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE | + GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE; + opts.template_path = cl_fixture("template"); + + cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts)); + + cl_assert(git_repository_is_bare(_repo)); + + cl_assert(!git__suffixcmp(git_repository_path(_repo), "/templated.git/")); + + cl_git_pass(git_futils_readbuffer( + &expected, cl_fixture("template/description"))); + cl_git_pass(git_futils_readbuffer( + &actual, "templated.git/description")); + + cl_assert_equal_s(expected.ptr, actual.ptr); + + git_buf_free(&expected); + git_buf_free(&actual); + + filemode = cl_repo_get_bool(_repo, "core.filemode"); + + assert_hooks_match( + cl_fixture("template"), git_repository_path(_repo), + "hooks/update.sample", filemode); + + assert_hooks_match( + cl_fixture("template"), git_repository_path(_repo), + "hooks/link.sample", filemode); +} + +void test_repo_init__extended_with_template_and_shared_mode(void) +{ + git_buf expected = GIT_BUF_INIT; + git_buf actual = GIT_BUF_INIT; + git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; + int filemode = true; + const char *repo_path = NULL; + + cl_set_cleanup(&cleanup_repository, "init_shared_from_tpl"); + + opts.flags = GIT_REPOSITORY_INIT_MKPATH | + GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE; + opts.template_path = cl_fixture("template"); + opts.mode = GIT_REPOSITORY_INIT_SHARED_GROUP; + + cl_git_pass(git_repository_init_ext(&_repo, "init_shared_from_tpl", &opts)); + + cl_assert(!git_repository_is_bare(_repo)); + cl_assert(!git__suffixcmp(git_repository_path(_repo), "/init_shared_from_tpl/.git/")); + + filemode = cl_repo_get_bool(_repo, "core.filemode"); + + cl_git_pass(git_futils_readbuffer( + &expected, cl_fixture("template/description"))); + cl_git_pass(git_futils_readbuffer( + &actual, "init_shared_from_tpl/.git/description")); + + cl_assert_equal_s(expected.ptr, actual.ptr); + + git_buf_free(&expected); + git_buf_free(&actual); + + repo_path = git_repository_path(_repo); + assert_mode_seems_okay(repo_path, "hooks", + GIT_FILEMODE_TREE | GIT_REPOSITORY_INIT_SHARED_GROUP, true, filemode); + assert_mode_seems_okay(repo_path, "info", + GIT_FILEMODE_TREE | GIT_REPOSITORY_INIT_SHARED_GROUP, true, filemode); + assert_mode_seems_okay(repo_path, "description", + GIT_FILEMODE_BLOB, false, filemode); + + /* for a non-symlinked hook, it should have shared permissions now */ + assert_hooks_match( + cl_fixture("template"), git_repository_path(_repo), + "hooks/update.sample", filemode); + + /* for a symlinked hook, the permissions still should match the + * source link, not the GIT_REPOSITORY_INIT_SHARED_GROUP value + */ + assert_hooks_match( + cl_fixture("template"), git_repository_path(_repo), + "hooks/link.sample", filemode); +} + +void test_repo_init__can_reinit_an_initialized_repository(void) +{ + git_repository *reinit; + + cl_set_cleanup(&cleanup_repository, "extended"); + + cl_git_pass(git_futils_mkdir("extended", NULL, 0775, 0)); + cl_git_pass(git_repository_init(&_repo, "extended", false)); + + cl_git_pass(git_repository_init(&reinit, "extended", false)); + + cl_assert_equal_s(git_repository_path(_repo), git_repository_path(reinit)); + + git_repository_free(reinit); +} + +void test_repo_init__init_with_initial_commit(void) +{ + git_index *index; + + cl_set_cleanup(&cleanup_repository, "committed"); + + /* Initialize the repository */ + cl_git_pass(git_repository_init(&_repo, "committed", 0)); + + /* Init will be automatically created when requested for a new repo */ + cl_git_pass(git_repository_index(&index, _repo)); + + /* Create a file so we can commit it + * + * If you are writing code outside the test suite, you can create this + * file any way that you like, such as: + * FILE *fp = fopen("committed/file.txt", "w"); + * fputs("some stuff\n", fp); + * fclose(fp); + * We like to use the help functions because they do error detection + * in a way that's easily compatible with our test suite. + */ + cl_git_mkfile("committed/file.txt", "some stuff\n"); + + /* Add file to the index */ + cl_git_pass(git_index_add_bypath(index, "file.txt")); + cl_git_pass(git_index_write(index)); + + /* Intentionally not using cl_repo_commit_from_index here so this code + * can be used as an example of how an initial commit is typically + * made to a repository... + */ + + /* Make sure we're ready to use git_signature_default :-) */ + { + git_config *cfg, *local; + cl_git_pass(git_repository_config(&cfg, _repo)); + cl_git_pass(git_config_open_level(&local, cfg, GIT_CONFIG_LEVEL_LOCAL)); + cl_git_pass(git_config_set_string(local, "user.name", "Test User")); + cl_git_pass(git_config_set_string(local, "user.email", "t@example.com")); + git_config_free(local); + git_config_free(cfg); + } + + /* Create a commit with the new contents of the index */ + { + git_signature *sig; + git_oid tree_id, commit_id; + git_tree *tree; + + cl_git_pass(git_signature_default(&sig, _repo)); + cl_git_pass(git_index_write_tree(&tree_id, index)); + cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id)); + + cl_git_pass(git_commit_create_v( + &commit_id, _repo, "HEAD", sig, sig, + NULL, "First", tree, 0)); + + git_tree_free(tree); + git_signature_free(sig); + } + + git_index_free(index); +} diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c new file mode 100644 index 000000000..56b51852c --- /dev/null +++ b/tests/repo/iterator.c @@ -0,0 +1,962 @@ +#include "clar_libgit2.h" +#include "iterator.h" +#include "repository.h" +#include "fileops.h" +#include <stdarg.h> + +static git_repository *g_repo; + +void test_repo_iterator__initialize(void) +{ +} + +void test_repo_iterator__cleanup(void) +{ + cl_git_sandbox_cleanup(); + g_repo = NULL; +} + +static void expect_iterator_items( + git_iterator *i, + int expected_flat, + const char **expected_flat_paths, + int expected_total, + const char **expected_total_paths) +{ + const git_index_entry *entry; + int count, error; + int no_trees = !(git_iterator_flags(i) & GIT_ITERATOR_INCLUDE_TREES); + bool v = false; + + if (expected_flat < 0) { v = true; expected_flat = -expected_flat; } + if (expected_total < 0) { v = true; expected_total = -expected_total; } + + if (v) fprintf(stderr, "== %s ==\n", no_trees ? "notrees" : "trees"); + + count = 0; + + while (!git_iterator_advance(&entry, i)) { + if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode); + + if (no_trees) + cl_assert(entry->mode != GIT_FILEMODE_TREE); + + if (expected_flat_paths) { + const char *expect_path = expected_flat_paths[count]; + size_t expect_len = strlen(expect_path); + + cl_assert_equal_s(expect_path, entry->path); + + if (expect_path[expect_len - 1] == '/') + cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode); + else + cl_assert(entry->mode != GIT_FILEMODE_TREE); + } + + if (++count > expected_flat) + break; + } + + cl_assert_equal_i(expected_flat, count); + + cl_git_pass(git_iterator_reset(i, NULL, NULL)); + + count = 0; + cl_git_pass(git_iterator_current(&entry, i)); + + if (v) fprintf(stderr, "-- %s --\n", no_trees ? "notrees" : "trees"); + + while (entry != NULL) { + if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode); + + if (no_trees) + cl_assert(entry->mode != GIT_FILEMODE_TREE); + + if (expected_total_paths) { + const char *expect_path = expected_total_paths[count]; + size_t expect_len = strlen(expect_path); + + cl_assert_equal_s(expect_path, entry->path); + + if (expect_path[expect_len - 1] == '/') + cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode); + else + cl_assert(entry->mode != GIT_FILEMODE_TREE); + } + + if (entry->mode == GIT_FILEMODE_TREE) { + error = git_iterator_advance_into(&entry, i); + + /* could return NOTFOUND if directory is empty */ + cl_assert(!error || error == GIT_ENOTFOUND); + + if (error == GIT_ENOTFOUND) { + error = git_iterator_advance(&entry, i); + cl_assert(!error || error == GIT_ITEROVER); + } + } else { + error = git_iterator_advance(&entry, i); + cl_assert(!error || error == GIT_ITEROVER); + } + + if (++count > expected_total) + break; + } + + cl_assert_equal_i(expected_total, count); +} + +/* Index contents (including pseudotrees): + * + * 0: a 5: F 10: k/ 16: L/ + * 1: B 6: g 11: k/1 17: L/1 + * 2: c 7: H 12: k/a 18: L/a + * 3: D 8: i 13: k/B 19: L/B + * 4: e 9: J 14: k/c 20: L/c + * 15: k/D 21: L/D + * + * 0: B 5: L/ 11: a 16: k/ + * 1: D 6: L/1 12: c 17: k/1 + * 2: F 7: L/B 13: e 18: k/B + * 3: H 8: L/D 14: g 19: k/D + * 4: J 9: L/a 15: i 20: k/a + * 10: L/c 21: k/c + */ + +void test_repo_iterator__index(void) +{ + git_iterator *i; + git_index *index; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + + /* autoexpand with no tree entries for index */ + cl_git_pass(git_iterator_for_index(&i, index, 0, NULL, NULL)); + expect_iterator_items(i, 20, NULL, 20, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + cl_git_pass(git_iterator_for_index( + &i, index, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 22, NULL, 22, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + cl_git_pass(git_iterator_for_index( + &i, index, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL)); + expect_iterator_items(i, 12, NULL, 22, NULL); + git_iterator_free(i); + + git_index_free(index); +} + +void test_repo_iterator__index_icase(void) +{ + git_iterator *i; + git_index *index; + unsigned int caps; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + caps = git_index_caps(index); + + /* force case sensitivity */ + cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEXCAP_IGNORE_CASE)); + + /* autoexpand with no tree entries over range */ + cl_git_pass(git_iterator_for_index(&i, index, 0, "c", "k/D")); + expect_iterator_items(i, 7, NULL, 7, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_index(&i, index, 0, "k", "k/Z")); + expect_iterator_items(i, 3, NULL, 3, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + cl_git_pass(git_iterator_for_index( + &i, index, GIT_ITERATOR_INCLUDE_TREES, "c", "k/D")); + expect_iterator_items(i, 8, NULL, 8, NULL); + git_iterator_free(i); + cl_git_pass(git_iterator_for_index( + &i, index, GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z")); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + cl_git_pass(git_iterator_for_index( + &i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D")); + expect_iterator_items(i, 5, NULL, 8, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_index( + &i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z")); + expect_iterator_items(i, 1, NULL, 4, NULL); + git_iterator_free(i); + + /* force case insensitivity */ + cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE)); + + /* autoexpand with no tree entries over range */ + cl_git_pass(git_iterator_for_index(&i, index, 0, "c", "k/D")); + expect_iterator_items(i, 13, NULL, 13, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_index(&i, index, 0, "k", "k/Z")); + expect_iterator_items(i, 5, NULL, 5, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + cl_git_pass(git_iterator_for_index( + &i, index, GIT_ITERATOR_INCLUDE_TREES, "c", "k/D")); + expect_iterator_items(i, 14, NULL, 14, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_index( + &i, index, GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z")); + expect_iterator_items(i, 6, NULL, 6, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + cl_git_pass(git_iterator_for_index( + &i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D")); + expect_iterator_items(i, 9, NULL, 14, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_index( + &i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z")); + expect_iterator_items(i, 1, NULL, 6, NULL); + git_iterator_free(i); + + cl_git_pass(git_index_set_caps(index, caps)); + git_index_free(index); +} + +void test_repo_iterator__tree(void) +{ + git_iterator *i; + git_tree *head; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_head_tree(&head, g_repo)); + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_tree(&i, head, 0, NULL, NULL)); + expect_iterator_items(i, 20, NULL, 20, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + cl_git_pass(git_iterator_for_tree( + &i, head, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 22, NULL, 22, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + cl_git_pass(git_iterator_for_tree( + &i, head, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL)); + expect_iterator_items(i, 12, NULL, 22, NULL); + git_iterator_free(i); + + git_tree_free(head); +} + +void test_repo_iterator__tree_icase(void) +{ + git_iterator *i; + git_tree *head; + git_iterator_flag_t flag; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_head_tree(&head, g_repo)); + + flag = GIT_ITERATOR_DONT_IGNORE_CASE; + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_tree(&i, head, flag, "c", "k/D")); + expect_iterator_items(i, 7, NULL, 7, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree(&i, head, flag, "k", "k/Z")); + expect_iterator_items(i, 3, NULL, 3, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + cl_git_pass(git_iterator_for_tree( + &i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D")); + expect_iterator_items(i, 8, NULL, 8, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z")); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + cl_git_pass(git_iterator_for_tree( + &i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D")); + expect_iterator_items(i, 5, NULL, 8, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z")); + expect_iterator_items(i, 1, NULL, 4, NULL); + git_iterator_free(i); + + flag = GIT_ITERATOR_IGNORE_CASE; + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_tree(&i, head, flag, "c", "k/D")); + expect_iterator_items(i, 13, NULL, 13, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree(&i, head, flag, "k", "k/Z")); + expect_iterator_items(i, 5, NULL, 5, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + cl_git_pass(git_iterator_for_tree( + &i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D")); + expect_iterator_items(i, 14, NULL, 14, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z")); + expect_iterator_items(i, 6, NULL, 6, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + cl_git_pass(git_iterator_for_tree( + &i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D")); + expect_iterator_items(i, 9, NULL, 14, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z")); + expect_iterator_items(i, 1, NULL, 6, NULL); + git_iterator_free(i); + + git_tree_free(head); +} + +void test_repo_iterator__tree_more(void) +{ + git_iterator *i; + git_tree *head; + static const char *expect_basic[] = { + "current_file", + "file_deleted", + "modified_file", + "staged_changes", + "staged_changes_file_deleted", + "staged_changes_modified_file", + "staged_delete_file_deleted", + "staged_delete_modified_file", + "subdir.txt", + "subdir/current_file", + "subdir/deleted_file", + "subdir/modified_file", + NULL, + }; + static const char *expect_trees[] = { + "current_file", + "file_deleted", + "modified_file", + "staged_changes", + "staged_changes_file_deleted", + "staged_changes_modified_file", + "staged_delete_file_deleted", + "staged_delete_modified_file", + "subdir.txt", + "subdir/", + "subdir/current_file", + "subdir/deleted_file", + "subdir/modified_file", + NULL, + }; + static const char *expect_noauto[] = { + "current_file", + "file_deleted", + "modified_file", + "staged_changes", + "staged_changes_file_deleted", + "staged_changes_modified_file", + "staged_delete_file_deleted", + "staged_delete_modified_file", + "subdir.txt", + "subdir/", + NULL + }; + + g_repo = cl_git_sandbox_init("status"); + + cl_git_pass(git_repository_head_tree(&head, g_repo)); + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_tree(&i, head, 0, NULL, NULL)); + expect_iterator_items(i, 12, expect_basic, 12, expect_basic); + git_iterator_free(i); + + /* auto expand with tree entries */ + cl_git_pass(git_iterator_for_tree( + &i, head, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 13, expect_trees, 13, expect_trees); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + cl_git_pass(git_iterator_for_tree( + &i, head, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL)); + expect_iterator_items(i, 10, expect_noauto, 13, expect_trees); + git_iterator_free(i); + + git_tree_free(head); +} + +/* "b=name,t=name", blob_id, tree_id */ +static void build_test_tree( + git_oid *out, git_repository *repo, const char *fmt, ...) +{ + git_oid *id; + git_treebuilder *builder; + const char *scan = fmt, *next; + char type, delimiter; + git_filemode_t mode = GIT_FILEMODE_BLOB; + git_buf name = GIT_BUF_INIT; + va_list arglist; + + cl_git_pass(git_treebuilder_create(&builder, NULL)); /* start builder */ + + va_start(arglist, fmt); + while (*scan) { + switch (type = *scan++) { + case 't': case 'T': mode = GIT_FILEMODE_TREE; break; + case 'b': case 'B': mode = GIT_FILEMODE_BLOB; break; + default: + cl_assert(type == 't' || type == 'T' || type == 'b' || type == 'B'); + } + + delimiter = *scan++; /* read and skip delimiter */ + for (next = scan; *next && *next != delimiter; ++next) + /* seek end */; + cl_git_pass(git_buf_set(&name, scan, (size_t)(next - scan))); + for (scan = next; *scan && (*scan == delimiter || *scan == ','); ++scan) + /* skip delimiter and optional comma */; + + id = va_arg(arglist, git_oid *); + + cl_git_pass(git_treebuilder_insert(NULL, builder, name.ptr, id, mode)); + } + va_end(arglist); + + cl_git_pass(git_treebuilder_write(out, repo, builder)); + + git_treebuilder_free(builder); + git_buf_free(&name); +} + +void test_repo_iterator__tree_case_conflicts_0(void) +{ + const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6"; + git_tree *tree; + git_oid blob_id, biga_id, littlea_id, tree_id; + git_iterator *i; + const char *expect_cs[] = { + "A/1.file", "A/3.file", "a/2.file", "a/4.file" }; + const char *expect_ci[] = { + "A/1.file", "a/2.file", "A/3.file", "a/4.file" }; + const char *expect_cs_trees[] = { + "A/", "A/1.file", "A/3.file", "a/", "a/2.file", "a/4.file" }; + const char *expect_ci_trees[] = { + "A/", "A/1.file", "a/2.file", "A/3.file", "a/4.file" }; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */ + + /* create tree with: A/1.file, A/3.file, a/2.file, a/4.file */ + build_test_tree( + &biga_id, g_repo, "b|1.file|,b|3.file|", &blob_id, &blob_id); + build_test_tree( + &littlea_id, g_repo, "b|2.file|,b|4.file|", &blob_id, &blob_id); + build_test_tree( + &tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id); + + cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)); + expect_iterator_items(i, 4, expect_cs, 4, expect_cs); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_IGNORE_CASE, NULL, NULL)); + expect_iterator_items(i, 4, expect_ci, 4, expect_ci); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 6, expect_cs_trees, 6, expect_cs_trees); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_IGNORE_CASE | + GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 5, expect_ci_trees, 5, expect_ci_trees); + git_iterator_free(i); + + git_tree_free(tree); +} + +void test_repo_iterator__tree_case_conflicts_1(void) +{ + const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6"; + git_tree *tree; + git_oid blob_id, Ab_id, biga_id, littlea_id, tree_id; + git_iterator *i; + const char *expect_cs[] = { + "A/a", "A/b/1", "A/c", "a/C", "a/a", "a/b" }; + const char *expect_ci[] = { + "A/a", "a/b", "A/b/1", "A/c" }; + const char *expect_cs_trees[] = { + "A/", "A/a", "A/b/", "A/b/1", "A/c", "a/", "a/C", "a/a", "a/b" }; + const char *expect_ci_trees[] = { + "A/", "A/a", "a/b", "A/b/", "A/b/1", "A/c" }; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */ + + /* create: A/a A/b/1 A/c a/a a/b a/C */ + build_test_tree(&Ab_id, g_repo, "b|1|", &blob_id); + build_test_tree( + &biga_id, g_repo, "b|a|,t|b|,b|c|", &blob_id, &Ab_id, &blob_id); + build_test_tree( + &littlea_id, g_repo, "b|a|,b|b|,b|C|", &blob_id, &blob_id, &blob_id); + build_test_tree( + &tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id); + + cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)); + expect_iterator_items(i, 6, expect_cs, 6, expect_cs); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_IGNORE_CASE, NULL, NULL)); + expect_iterator_items(i, 4, expect_ci, 4, expect_ci); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 9, expect_cs_trees, 9, expect_cs_trees); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_IGNORE_CASE | + GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 6, expect_ci_trees, 6, expect_ci_trees); + git_iterator_free(i); + + git_tree_free(tree); +} + +void test_repo_iterator__tree_case_conflicts_2(void) +{ + const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6"; + git_tree *tree; + git_oid blob_id, d1, d2, c1, c2, b1, b2, a1, a2, tree_id; + git_iterator *i; + const char *expect_cs[] = { + "A/B/C/D/16", "A/B/C/D/foo", "A/B/C/d/15", "A/B/C/d/FOO", + "A/B/c/D/14", "A/B/c/D/foo", "A/B/c/d/13", "A/B/c/d/FOO", + "A/b/C/D/12", "A/b/C/D/foo", "A/b/C/d/11", "A/b/C/d/FOO", + "A/b/c/D/10", "A/b/c/D/foo", "A/b/c/d/09", "A/b/c/d/FOO", + "a/B/C/D/08", "a/B/C/D/foo", "a/B/C/d/07", "a/B/C/d/FOO", + "a/B/c/D/06", "a/B/c/D/foo", "a/B/c/d/05", "a/B/c/d/FOO", + "a/b/C/D/04", "a/b/C/D/foo", "a/b/C/d/03", "a/b/C/d/FOO", + "a/b/c/D/02", "a/b/c/D/foo", "a/b/c/d/01", "a/b/c/d/FOO", }; + const char *expect_ci[] = { + "a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04", + "a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08", + "A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12", + "A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16", + "A/B/C/D/foo", }; + const char *expect_ci_trees[] = { + "A/", "A/B/", "A/B/C/", "A/B/C/D/", + "a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04", + "a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08", + "A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12", + "A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16", + "A/B/C/D/foo", }; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */ + + build_test_tree(&d1, g_repo, "b|16|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|15|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&d1, g_repo, "b|14|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|13|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2); + + build_test_tree(&d1, g_repo, "b|12|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|11|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&d1, g_repo, "b|10|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|09|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2); + + build_test_tree(&a1, g_repo, "t|B|,t|b|", &b1, &b2); + + build_test_tree(&d1, g_repo, "b|08|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|07|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&d1, g_repo, "b|06|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|05|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2); + + build_test_tree(&d1, g_repo, "b|04|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|03|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&d1, g_repo, "b|02|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|01|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2); + + build_test_tree(&a2, g_repo, "t|B|,t|b|", &b1, &b2); + + build_test_tree(&tree_id, g_repo, "t/A/,t/a/", &a1, &a2); + + cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)); + expect_iterator_items(i, 32, expect_cs, 32, expect_cs); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_IGNORE_CASE, NULL, NULL)); + expect_iterator_items(i, 17, expect_ci, 17, expect_ci); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_tree( + &i, tree, GIT_ITERATOR_IGNORE_CASE | + GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 21, expect_ci_trees, 21, expect_ci_trees); + git_iterator_free(i); + + git_tree_free(tree); +} + +void test_repo_iterator__workdir(void) +{ + git_iterator *i; + + g_repo = cl_git_sandbox_init("icase"); + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_workdir(&i, g_repo, 0, NULL, NULL)); + expect_iterator_items(i, 20, NULL, 20, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 22, NULL, 22, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL)); + expect_iterator_items(i, 12, NULL, 22, NULL); + git_iterator_free(i); +} + +void test_repo_iterator__workdir_icase(void) +{ + git_iterator *i; + git_iterator_flag_t flag; + + g_repo = cl_git_sandbox_init("icase"); + + flag = GIT_ITERATOR_DONT_IGNORE_CASE; + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "c", "k/D")); + expect_iterator_items(i, 7, NULL, 7, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "k", "k/Z")); + expect_iterator_items(i, 3, NULL, 3, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D")); + expect_iterator_items(i, 8, NULL, 8, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z")); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D")); + expect_iterator_items(i, 5, NULL, 8, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z")); + expect_iterator_items(i, 1, NULL, 4, NULL); + git_iterator_free(i); + + flag = GIT_ITERATOR_IGNORE_CASE; + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "c", "k/D")); + expect_iterator_items(i, 13, NULL, 13, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "k", "k/Z")); + expect_iterator_items(i, 5, NULL, 5, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D")); + expect_iterator_items(i, 14, NULL, 14, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z")); + expect_iterator_items(i, 6, NULL, 6, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D")); + expect_iterator_items(i, 9, NULL, 14, NULL); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z")); + expect_iterator_items(i, 1, NULL, 6, NULL); + git_iterator_free(i); +} + +static void build_workdir_tree(const char *root, int dirs, int subs) +{ + int i, j; + char buf[64], sub[64]; + + for (i = 0; i < dirs; ++i) { + if (i % 2 == 0) { + p_snprintf(buf, sizeof(buf), "%s/dir%02d", root, i); + cl_git_pass(git_futils_mkdir(buf, NULL, 0775, GIT_MKDIR_PATH)); + + p_snprintf(buf, sizeof(buf), "%s/dir%02d/file", root, i); + cl_git_mkfile(buf, buf); + buf[strlen(buf) - 5] = '\0'; + } else { + p_snprintf(buf, sizeof(buf), "%s/DIR%02d", root, i); + cl_git_pass(git_futils_mkdir(buf, NULL, 0775, GIT_MKDIR_PATH)); + } + + for (j = 0; j < subs; ++j) { + switch (j % 4) { + case 0: p_snprintf(sub, sizeof(sub), "%s/sub%02d", buf, j); break; + case 1: p_snprintf(sub, sizeof(sub), "%s/sUB%02d", buf, j); break; + case 2: p_snprintf(sub, sizeof(sub), "%s/Sub%02d", buf, j); break; + case 3: p_snprintf(sub, sizeof(sub), "%s/SUB%02d", buf, j); break; + } + cl_git_pass(git_futils_mkdir(sub, NULL, 0775, GIT_MKDIR_PATH)); + + if (j % 2 == 0) { + size_t sublen = strlen(sub); + memcpy(&sub[sublen], "/file", sizeof("/file")); + cl_git_mkfile(sub, sub); + sub[sublen] = '\0'; + } + } + } +} + +void test_repo_iterator__workdir_depth(void) +{ + git_iterator *iter; + + g_repo = cl_git_sandbox_init("icase"); + + build_workdir_tree("icase", 10, 10); + build_workdir_tree("icase/DIR01/sUB01", 50, 0); + build_workdir_tree("icase/dir02/sUB01", 50, 0); + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_workdir(&iter, g_repo, 0, NULL, NULL)); + expect_iterator_items(iter, 125, NULL, 125, NULL); + git_iterator_free(iter); + + /* auto expand with tree entries (empty dirs silently skipped) */ + cl_git_pass(git_iterator_for_workdir( + &iter, g_repo, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(iter, 337, NULL, 337, NULL); + git_iterator_free(iter); +} + +void test_repo_iterator__fs(void) +{ + git_iterator *i; + static const char *expect_base[] = { + "DIR01/Sub02/file", + "DIR01/sub00/file", + "current_file", + "dir00/Sub02/file", + "dir00/file", + "dir00/sub00/file", + "modified_file", + "new_file", + NULL, + }; + static const char *expect_trees[] = { + "DIR01/", + "DIR01/SUB03/", + "DIR01/Sub02/", + "DIR01/Sub02/file", + "DIR01/sUB01/", + "DIR01/sub00/", + "DIR01/sub00/file", + "current_file", + "dir00/", + "dir00/SUB03/", + "dir00/Sub02/", + "dir00/Sub02/file", + "dir00/file", + "dir00/sUB01/", + "dir00/sub00/", + "dir00/sub00/file", + "modified_file", + "new_file", + NULL, + }; + static const char *expect_noauto[] = { + "DIR01/", + "current_file", + "dir00/", + "modified_file", + "new_file", + NULL, + }; + + g_repo = cl_git_sandbox_init("status"); + + build_workdir_tree("status/subdir", 2, 4); + + cl_git_pass(git_iterator_for_filesystem( + &i, "status/subdir", 0, NULL, NULL)); + expect_iterator_items(i, 8, expect_base, 8, expect_base); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_filesystem( + &i, "status/subdir", GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 18, expect_trees, 18, expect_trees); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_filesystem( + &i, "status/subdir", GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL)); + expect_iterator_items(i, 5, expect_noauto, 18, expect_trees); + git_iterator_free(i); + + git__tsort((void **)expect_base, 8, (git__tsort_cmp)git__strcasecmp); + git__tsort((void **)expect_trees, 18, (git__tsort_cmp)git__strcasecmp); + git__tsort((void **)expect_noauto, 5, (git__tsort_cmp)git__strcasecmp); + + cl_git_pass(git_iterator_for_filesystem( + &i, "status/subdir", GIT_ITERATOR_IGNORE_CASE, NULL, NULL)); + expect_iterator_items(i, 8, expect_base, 8, expect_base); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_filesystem( + &i, "status/subdir", GIT_ITERATOR_IGNORE_CASE | + GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); + expect_iterator_items(i, 18, expect_trees, 18, expect_trees); + git_iterator_free(i); + + cl_git_pass(git_iterator_for_filesystem( + &i, "status/subdir", GIT_ITERATOR_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL)); + expect_iterator_items(i, 5, expect_noauto, 18, expect_trees); + git_iterator_free(i); +} + +void test_repo_iterator__fs2(void) +{ + git_iterator *i; + static const char *expect_base[] = { + "heads/br2", + "heads/dir", + "heads/long-file-name", + "heads/master", + "heads/packed-test", + "heads/subtrees", + "heads/test", + "tags/e90810b", + "tags/foo/bar", + "tags/foo/foo/bar", + "tags/point_to_blob", + "tags/test", + NULL, + }; + + g_repo = cl_git_sandbox_init("testrepo"); + + cl_git_pass(git_iterator_for_filesystem( + &i, "testrepo/.git/refs", 0, NULL, NULL)); + expect_iterator_items(i, 12, expect_base, 12, expect_base); + git_iterator_free(i); +} + +void test_repo_iterator__fs_preserves_error(void) +{ + git_iterator *i; + const git_index_entry *e; + + if (!cl_is_chmod_supported()) + return; + + g_repo = cl_git_sandbox_init("empty_standard_repo"); + + cl_must_pass(p_mkdir("empty_standard_repo/r", 0777)); + cl_git_mkfile("empty_standard_repo/r/a", "hello"); + cl_must_pass(p_mkdir("empty_standard_repo/r/b", 0777)); + cl_git_mkfile("empty_standard_repo/r/b/problem", "not me"); + cl_must_pass(p_chmod("empty_standard_repo/r/b", 0000)); + cl_must_pass(p_mkdir("empty_standard_repo/r/c", 0777)); + cl_git_mkfile("empty_standard_repo/r/d", "final"); + + cl_git_pass(git_iterator_for_filesystem( + &i, "empty_standard_repo/r", 0, NULL, NULL)); + + cl_git_pass(git_iterator_advance(&e, i)); /* a */ + cl_git_fail(git_iterator_advance(&e, i)); /* b */ + cl_assert(giterr_last()); + cl_assert(giterr_last()->message != NULL); + /* skip 'c/' empty directory */ + cl_git_pass(git_iterator_advance(&e, i)); /* d */ + cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i)); + + cl_must_pass(p_chmod("empty_standard_repo/r/b", 0777)); + + git_iterator_free(i); +} diff --git a/tests/repo/message.c b/tests/repo/message.c new file mode 100644 index 000000000..629d40c12 --- /dev/null +++ b/tests/repo/message.c @@ -0,0 +1,52 @@ +#include "clar_libgit2.h" +#include "buffer.h" +#include "refs.h" +#include "posix.h" + +static git_repository *_repo; +static git_buf _path; +static char *_actual; + +void test_repo_message__initialize(void) +{ + _repo = cl_git_sandbox_init("testrepo.git"); +} + +void test_repo_message__cleanup(void) +{ + cl_git_sandbox_cleanup(); + git_buf_free(&_path); + git__free(_actual); + _actual = NULL; +} + +void test_repo_message__none(void) +{ + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(NULL, 0, _repo)); +} + +void test_repo_message__message(void) +{ + const char expected[] = "Test\n\nThis is a test of the emergency broadcast system\n"; + ssize_t len; + + cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), "MERGE_MSG")); + cl_git_mkfile(git_buf_cstr(&_path), expected); + + len = git_repository_message(NULL, 0, _repo); + cl_assert(len > 0); + + _actual = git__malloc(len + 1); + cl_assert(_actual != NULL); + + /* Test non truncation */ + cl_assert(git_repository_message(_actual, len, _repo) > 0); + cl_assert_equal_s(expected, _actual); + + /* Test truncation and that trailing NUL is inserted */ + cl_assert(git_repository_message(_actual, 6, _repo) > 0); + cl_assert_equal_s("Test\n", _actual); + + cl_git_pass(p_unlink(git_buf_cstr(&_path))); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(NULL, 0, _repo)); +} diff --git a/tests/repo/open.c b/tests/repo/open.c new file mode 100644 index 000000000..7cfe041c2 --- /dev/null +++ b/tests/repo/open.c @@ -0,0 +1,376 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include <ctype.h> + +void test_repo_open__cleanup(void) +{ + cl_git_sandbox_cleanup(); + + if (git_path_isdir("alternate")) + git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES); +} + +void test_repo_open__bare_empty_repo(void) +{ + git_repository *repo = cl_git_sandbox_init("empty_bare.git"); + + cl_assert(git_repository_path(repo) != NULL); + cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0); + cl_assert(git_repository_workdir(repo) == NULL); +} + +void test_repo_open__standard_empty_repo_through_gitdir(void) +{ + git_repository *repo; + + cl_git_pass(git_repository_open(&repo, cl_fixture("empty_standard_repo/.gitted"))); + + cl_assert(git_repository_path(repo) != NULL); + cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0); + + cl_assert(git_repository_workdir(repo) != NULL); + cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0); + + git_repository_free(repo); +} + +void test_repo_open__standard_empty_repo_through_workdir(void) +{ + git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); + + cl_assert(git_repository_path(repo) != NULL); + cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0); + + cl_assert(git_repository_workdir(repo) != NULL); + cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0); +} + + +void test_repo_open__open_with_discover(void) +{ + static const char *variants[] = { + "attr", "attr/", "attr/.git", "attr/.git/", + "attr/sub", "attr/sub/", "attr/sub/sub", "attr/sub/sub/", + NULL + }; + git_repository *repo; + const char **scan; + + cl_fixture_sandbox("attr"); + cl_git_pass(p_rename("attr/.gitted", "attr/.git")); + + for (scan = variants; *scan != NULL; scan++) { + cl_git_pass(git_repository_open_ext(&repo, *scan, 0, NULL)); + cl_assert(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0); + cl_assert(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0); + git_repository_free(repo); + } + + cl_fixture_cleanup("attr"); +} + +static void make_gitlink_dir(const char *dir, const char *linktext) +{ + git_buf path = GIT_BUF_INIT; + + cl_git_pass(git_futils_mkdir(dir, NULL, 0777, GIT_MKDIR_VERIFY_DIR)); + cl_git_pass(git_buf_joinpath(&path, dir, ".git")); + cl_git_rewritefile(path.ptr, linktext); + git_buf_free(&path); +} + +void test_repo_open__gitlinked(void) +{ + /* need to have both repo dir and workdir set up correctly */ + git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); + git_repository *repo2; + + make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git"); + + cl_git_pass(git_repository_open(&repo2, "alternate")); + + cl_assert(git_repository_path(repo2) != NULL); + cl_assert_(git__suffixcmp(git_repository_path(repo2), "empty_standard_repo/.git/") == 0, git_repository_path(repo2)); + cl_assert_equal_s(git_repository_path(repo), git_repository_path(repo2)); + + cl_assert(git_repository_workdir(repo2) != NULL); + cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2)); + + git_repository_free(repo2); +} + +void test_repo_open__from_git_new_workdir(void) +{ + /* The git-new-workdir script that ships with git sets up a bunch of + * symlinks to create a second workdir that shares the object db with + * another checkout. Libgit2 can open a repo that has been configured + * this way. + */ + cl_git_sandbox_init("empty_standard_repo"); + +#ifndef GIT_WIN32 + git_repository *repo2; + git_buf link_tgt = GIT_BUF_INIT, link = GIT_BUF_INIT, body = GIT_BUF_INIT; + const char **scan; + int link_fd; + static const char *links[] = { + "config", "refs", "logs/refs", "objects", "info", "hooks", + "packed-refs", "remotes", "rr-cache", "svn", NULL + }; + static const char *copies[] = { + "HEAD", NULL + }; + + cl_git_pass(p_mkdir("alternate", 0777)); + cl_git_pass(p_mkdir("alternate/.git", 0777)); + + for (scan = links; *scan != NULL; scan++) { + git_buf_joinpath(&link_tgt, "empty_standard_repo/.git", *scan); + if (git_path_exists(link_tgt.ptr)) { + git_buf_joinpath(&link_tgt, "../../empty_standard_repo/.git", *scan); + git_buf_joinpath(&link, "alternate/.git", *scan); + if (strchr(*scan, '/')) + git_futils_mkpath2file(link.ptr, 0777); + cl_assert_(symlink(link_tgt.ptr, link.ptr) == 0, strerror(errno)); + } + } + for (scan = copies; *scan != NULL; scan++) { + git_buf_joinpath(&link_tgt, "empty_standard_repo/.git", *scan); + if (git_path_exists(link_tgt.ptr)) { + git_buf_joinpath(&link, "alternate/.git", *scan); + cl_git_pass(git_futils_readbuffer(&body, link_tgt.ptr)); + + cl_assert((link_fd = git_futils_creat_withpath(link.ptr, 0777, 0666)) >= 0); + cl_must_pass(p_write(link_fd, body.ptr, body.size)); + p_close(link_fd); + } + } + + git_buf_free(&link_tgt); + git_buf_free(&link); + git_buf_free(&body); + + + cl_git_pass(git_repository_open(&repo2, "alternate")); + + cl_assert(git_repository_path(repo2) != NULL); + cl_assert_(git__suffixcmp(git_repository_path(repo2), "alternate/.git/") == 0, git_repository_path(repo2)); + + cl_assert(git_repository_workdir(repo2) != NULL); + cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2)); + + git_repository_free(repo2); +#endif +} + +void test_repo_open__failures(void) +{ + git_repository *base, *repo; + git_buf ceiling = GIT_BUF_INIT; + + base = cl_git_sandbox_init("attr"); + cl_git_pass(git_buf_sets(&ceiling, git_repository_workdir(base))); + + /* fail with no searching */ + cl_git_fail(git_repository_open(&repo, "attr/sub")); + cl_git_fail(git_repository_open_ext( + &repo, "attr/sub", GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)); + + /* fail with ceiling too low */ + cl_git_pass(git_buf_joinpath(&ceiling, ceiling.ptr, "sub")); + cl_git_fail(git_repository_open_ext(&repo, "attr/sub", 0, ceiling.ptr)); + + /* fail with no repo */ + cl_git_pass(p_mkdir("alternate", 0777)); + cl_git_pass(p_mkdir("alternate/.git", 0777)); + cl_git_fail(git_repository_open_ext(&repo, "alternate", 0, NULL)); + cl_git_fail(git_repository_open_ext(&repo, "alternate/.git", 0, NULL)); + + git_buf_free(&ceiling); +} + +void test_repo_open__bad_gitlinks(void) +{ + git_repository *repo; + static const char *bad_links[] = { + "garbage\n", "gitdir", "gitdir:\n", "gitdir: foobar", + "gitdir: ../invalid", "gitdir: ../invalid2", + "gitdir: ../attr/.git with extra stuff", + NULL + }; + const char **scan; + + cl_git_sandbox_init("attr"); + + cl_git_pass(p_mkdir("invalid", 0777)); + cl_git_pass(git_futils_mkdir_r("invalid2/.git", NULL, 0777)); + + for (scan = bad_links; *scan != NULL; scan++) { + make_gitlink_dir("alternate", *scan); + cl_git_fail(git_repository_open_ext(&repo, "alternate", 0, NULL)); + } + + git_futils_rmdir_r("invalid", NULL, GIT_RMDIR_REMOVE_FILES); + git_futils_rmdir_r("invalid2", NULL, GIT_RMDIR_REMOVE_FILES); +} + +#ifdef GIT_WIN32 +static void unposix_path(git_buf *path) +{ + char *src, *tgt; + + src = tgt = path->ptr; + + /* convert "/d/..." to "d:\..." */ + if (src[0] == '/' && isalpha(src[1]) && src[2] == '/') { + *tgt++ = src[1]; + *tgt++ = ':'; + *tgt++ = '\\'; + src += 3; + } + + while (*src) { + *tgt++ = (*src == '/') ? '\\' : *src; + src++; + } + + *tgt = '\0'; +} +#endif + +void test_repo_open__win32_path(void) +{ +#ifdef GIT_WIN32 + git_repository *repo = cl_git_sandbox_init("empty_standard_repo"), *repo2; + git_buf winpath = GIT_BUF_INIT; + static const char *repo_path = "empty_standard_repo/.git/"; + static const char *repo_wd = "empty_standard_repo/"; + + cl_assert(git__suffixcmp(git_repository_path(repo), repo_path) == 0); + cl_assert(git__suffixcmp(git_repository_workdir(repo), repo_wd) == 0); + + cl_git_pass(git_buf_sets(&winpath, git_repository_path(repo))); + unposix_path(&winpath); + cl_git_pass(git_repository_open(&repo2, winpath.ptr)); + cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0); + cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0); + git_repository_free(repo2); + + cl_git_pass(git_buf_sets(&winpath, git_repository_path(repo))); + git_buf_truncate(&winpath, winpath.size - 1); /* remove trailing '/' */ + unposix_path(&winpath); + cl_git_pass(git_repository_open(&repo2, winpath.ptr)); + cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0); + cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0); + git_repository_free(repo2); + + cl_git_pass(git_buf_sets(&winpath, git_repository_workdir(repo))); + unposix_path(&winpath); + cl_git_pass(git_repository_open(&repo2, winpath.ptr)); + cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0); + cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0); + git_repository_free(repo2); + + cl_git_pass(git_buf_sets(&winpath, git_repository_workdir(repo))); + git_buf_truncate(&winpath, winpath.size - 1); /* remove trailing '/' */ + unposix_path(&winpath); + cl_git_pass(git_repository_open(&repo2, winpath.ptr)); + cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0); + cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0); + git_repository_free(repo2); + + git_buf_free(&winpath); +#endif +} + +void test_repo_open__opening_a_non_existing_repository_returns_ENOTFOUND(void) +{ + git_repository *repo; + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_open(&repo, "i-do-not/exist")); +} + +void test_repo_open__no_config(void) +{ + git_buf path = GIT_BUF_INIT; + git_repository *repo; + git_config *config; + + cl_fixture_sandbox("empty_standard_repo"); + cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git")); + + /* remove local config */ + cl_git_pass(git_futils_rmdir_r( + "empty_standard_repo/.git/config", NULL, GIT_RMDIR_REMOVE_FILES)); + + /* isolate from system level configs */ + cl_must_pass(p_mkdir("alternate", 0777)); + cl_git_pass(git_path_prettify(&path, "alternate", NULL)); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); + + git_buf_free(&path); + + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); + cl_git_pass(git_repository_config(&config, repo)); + + cl_git_pass(git_config_set_string(config, "test.set", "42")); + + git_config_free(config); + git_repository_free(repo); + cl_fixture_cleanup("empty_standard_repo"); + + git_futils_dirs_global_shutdown(); +} + +void test_repo_open__force_bare(void) +{ + /* need to have both repo dir and workdir set up correctly */ + git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); + git_repository *barerepo; + + make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git"); + + cl_assert(!git_repository_is_bare(repo)); + + cl_git_pass(git_repository_open(&barerepo, "alternate")); + cl_assert(!git_repository_is_bare(barerepo)); + git_repository_free(barerepo); + + cl_git_pass(git_repository_open_bare( + &barerepo, "empty_standard_repo/.git")); + cl_assert(git_repository_is_bare(barerepo)); + git_repository_free(barerepo); + + cl_git_fail(git_repository_open_bare(&barerepo, "alternate/.git")); + + cl_git_pass(git_repository_open_ext( + &barerepo, "alternate/.git", GIT_REPOSITORY_OPEN_BARE, NULL)); + cl_assert(git_repository_is_bare(barerepo)); + git_repository_free(barerepo); + + cl_git_pass(p_mkdir("empty_standard_repo/subdir", 0777)); + cl_git_mkfile("empty_standard_repo/subdir/something.txt", "something"); + + cl_git_fail(git_repository_open_bare( + &barerepo, "empty_standard_repo/subdir")); + + cl_git_pass(git_repository_open_ext( + &barerepo, "empty_standard_repo/subdir", GIT_REPOSITORY_OPEN_BARE, NULL)); + cl_assert(git_repository_is_bare(barerepo)); + git_repository_free(barerepo); + + cl_git_pass(p_mkdir("alternate/subdir", 0777)); + cl_git_pass(p_mkdir("alternate/subdir/sub2", 0777)); + cl_git_mkfile("alternate/subdir/sub2/something.txt", "something"); + + cl_git_fail(git_repository_open_bare(&barerepo, "alternate/subdir/sub2")); + + cl_git_pass(git_repository_open_ext( + &barerepo, "alternate/subdir/sub2", GIT_REPOSITORY_OPEN_BARE, NULL)); + cl_assert(git_repository_is_bare(barerepo)); + git_repository_free(barerepo); +} diff --git a/tests/repo/pathspec.c b/tests/repo/pathspec.c new file mode 100644 index 000000000..334066b67 --- /dev/null +++ b/tests/repo/pathspec.c @@ -0,0 +1,385 @@ +#include "clar_libgit2.h" +#include "git2/pathspec.h" + +static git_repository *g_repo; + +void test_repo_pathspec__initialize(void) +{ + g_repo = cl_git_sandbox_init("status"); +} + +void test_repo_pathspec__cleanup(void) +{ + cl_git_sandbox_cleanup(); + g_repo = NULL; +} + +static char *str0[] = { "*_file", "new_file", "garbage" }; +static char *str1[] = { "*_FILE", "NEW_FILE", "GARBAGE" }; +static char *str2[] = { "staged_*" }; +static char *str3[] = { "!subdir", "*_file", "new_file" }; +static char *str4[] = { "*" }; +static char *str5[] = { "S*" }; + +void test_repo_pathspec__workdir0(void) +{ + git_strarray s; + git_pathspec *ps; + git_pathspec_match_list *m; + + /* { "*_file", "new_file", "garbage" } */ + s.strings = str0; s.count = ARRAY_SIZE(str0); + cl_git_pass(git_pathspec_new(&ps, &s)); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, 0, ps)); + cl_assert_equal_sz(10, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(10, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m)); + cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 0)); + git_pathspec_match_list_free(m); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_FIND_FAILURES | GIT_PATHSPEC_FAILURES_ONLY, ps)); + cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + git_pathspec_free(ps); +} + +void test_repo_pathspec__workdir1(void) +{ + git_strarray s; + git_pathspec *ps; + git_pathspec_match_list *m; + + /* { "*_FILE", "NEW_FILE", "GARBAGE" } */ + s.strings = str1; s.count = ARRAY_SIZE(str1); + cl_git_pass(git_pathspec_new(&ps, &s)); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_IGNORE_CASE, ps)); + cl_assert_equal_sz(10, git_pathspec_match_list_entrycount(m)); + git_pathspec_match_list_free(m); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_USE_CASE, ps)); + cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m)); + git_pathspec_match_list_free(m); + + cl_git_fail(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_USE_CASE | GIT_PATHSPEC_NO_MATCH_ERROR, ps)); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_IGNORE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(10, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_USE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(3, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + git_pathspec_free(ps); +} + +void test_repo_pathspec__workdir2(void) +{ + git_strarray s; + git_pathspec *ps; + git_pathspec_match_list *m; + + /* { "staged_*" } */ + s.strings = str2; s.count = ARRAY_SIZE(str2); + cl_git_pass(git_pathspec_new(&ps, &s)); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, 0, ps)); + cl_assert_equal_sz(5, git_pathspec_match_list_entrycount(m)); + git_pathspec_match_list_free(m); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(5, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + cl_git_fail(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_NO_GLOB | GIT_PATHSPEC_NO_MATCH_ERROR, ps)); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_NO_GLOB | GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + git_pathspec_free(ps); +} + +void test_repo_pathspec__workdir3(void) +{ + git_strarray s; + git_pathspec *ps; + git_pathspec_match_list *m; + + /* { "!subdir", "*_file", "new_file" } */ + s.strings = str3; s.count = ARRAY_SIZE(str3); + cl_git_pass(git_pathspec_new(&ps, &s)); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, 0, ps)); + cl_assert_equal_sz(7, git_pathspec_match_list_entrycount(m)); + git_pathspec_match_list_free(m); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, + GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(7, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m)); + + cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0)); + cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1)); + cl_assert_equal_s("new_file", git_pathspec_match_list_entry(m, 2)); + cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 3)); + cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 4)); + cl_assert_equal_s("staged_new_file", git_pathspec_match_list_entry(m, 5)); + cl_assert_equal_s("staged_new_file_modified_file", git_pathspec_match_list_entry(m, 6)); + cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 7)); + + git_pathspec_match_list_free(m); + + git_pathspec_free(ps); +} + +void test_repo_pathspec__workdir4(void) +{ + git_strarray s; + git_pathspec *ps; + git_pathspec_match_list *m; + + /* { "*" } */ + s.strings = str4; s.count = ARRAY_SIZE(str4); + cl_git_pass(git_pathspec_new(&ps, &s)); + + cl_git_pass(git_pathspec_match_workdir(&m, g_repo, 0, ps)); + cl_assert_equal_sz(13, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_s("这", git_pathspec_match_list_entry(m, 12)); + git_pathspec_match_list_free(m); + + git_pathspec_free(ps); +} + + +void test_repo_pathspec__index0(void) +{ + git_index *idx; + git_strarray s; + git_pathspec *ps; + git_pathspec_match_list *m; + + cl_git_pass(git_repository_index(&idx, g_repo)); + + /* { "*_file", "new_file", "garbage" } */ + s.strings = str0; s.count = ARRAY_SIZE(str0); + cl_git_pass(git_pathspec_new(&ps, &s)); + + cl_git_pass(git_pathspec_match_index(&m, idx, 0, ps)); + cl_assert_equal_sz(9, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m)); + cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0)); + cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1)); + cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 2)); + cl_assert_equal_s("staged_new_file", git_pathspec_match_list_entry(m, 3)); + cl_assert_equal_s("staged_new_file_deleted_file", git_pathspec_match_list_entry(m, 4)); + cl_assert_equal_s("staged_new_file_modified_file", git_pathspec_match_list_entry(m, 5)); + cl_assert_equal_s("subdir/current_file", git_pathspec_match_list_entry(m, 6)); + cl_assert_equal_s("subdir/deleted_file", git_pathspec_match_list_entry(m, 7)); + cl_assert_equal_s("subdir/modified_file", git_pathspec_match_list_entry(m, 8)); + cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 9)); + git_pathspec_match_list_free(m); + + cl_git_pass(git_pathspec_match_index(&m, idx, + GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(9, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m)); + cl_assert_equal_s("new_file", git_pathspec_match_list_failed_entry(m, 0)); + cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 1)); + cl_assert_equal_s(NULL, git_pathspec_match_list_failed_entry(m, 2)); + git_pathspec_match_list_free(m); + + git_pathspec_free(ps); + git_index_free(idx); +} + +void test_repo_pathspec__index1(void) +{ + /* Currently the USE_CASE and IGNORE_CASE flags don't work on the + * index because the index sort order for the index iterator is + * set by the index itself. I think the correct fix is for the + * index not to embed a global sort order but to support traversal + * in either case sensitive or insensitive order in a stateless + * manner. + * + * Anyhow, as it is, there is no point in doing this test. + */ +#if 0 + git_index *idx; + git_strarray s; + git_pathspec *ps; + git_pathspec_match_list *m; + + cl_git_pass(git_repository_index(&idx, g_repo)); + + /* { "*_FILE", "NEW_FILE", "GARBAGE" } */ + s.strings = str1; s.count = ARRAY_SIZE(str1); + cl_git_pass(git_pathspec_new(&ps, &s)); + + cl_git_pass(git_pathspec_match_index(&m, idx, + GIT_PATHSPEC_USE_CASE, ps)); + cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + cl_git_pass(git_pathspec_match_index(&m, idx, + GIT_PATHSPEC_USE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(3, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + cl_git_pass(git_pathspec_match_index(&m, idx, + GIT_PATHSPEC_IGNORE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(10, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + git_pathspec_free(ps); + git_index_free(idx); +#endif +} + +void test_repo_pathspec__tree0(void) +{ + git_object *tree; + git_strarray s; + git_pathspec *ps; + git_pathspec_match_list *m; + + /* { "*_file", "new_file", "garbage" } */ + s.strings = str0; s.count = ARRAY_SIZE(str0); + cl_git_pass(git_pathspec_new(&ps, &s)); + + cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD~2^{tree}")); + + cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree, + GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(4, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0)); + cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1)); + cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 2)); + cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 3)); + cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 4)); + cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m)); + cl_assert_equal_s("new_file", git_pathspec_match_list_failed_entry(m, 0)); + cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 1)); + cl_assert_equal_s(NULL, git_pathspec_match_list_failed_entry(m, 2)); + git_pathspec_match_list_free(m); + + git_object_free(tree); + + cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD^{tree}")); + + cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree, + GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(7, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0)); + cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1)); + cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 2)); + cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 3)); + cl_assert_equal_s("subdir/current_file", git_pathspec_match_list_entry(m, 4)); + cl_assert_equal_s("subdir/deleted_file", git_pathspec_match_list_entry(m, 5)); + cl_assert_equal_s("subdir/modified_file", git_pathspec_match_list_entry(m, 6)); + cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 7)); + cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m)); + cl_assert_equal_s("new_file", git_pathspec_match_list_failed_entry(m, 0)); + cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 1)); + cl_assert_equal_s(NULL, git_pathspec_match_list_failed_entry(m, 2)); + git_pathspec_match_list_free(m); + + git_object_free(tree); + + git_pathspec_free(ps); +} + +void test_repo_pathspec__tree5(void) +{ + git_object *tree; + git_strarray s; + git_pathspec *ps; + git_pathspec_match_list *m; + + /* { "S*" } */ + s.strings = str5; s.count = ARRAY_SIZE(str5); + cl_git_pass(git_pathspec_new(&ps, &s)); + + cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD~2^{tree}")); + + cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree, + GIT_PATHSPEC_USE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree, + GIT_PATHSPEC_IGNORE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(5, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_s("staged_changes", git_pathspec_match_list_entry(m, 0)); + cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 4)); + cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + git_object_free(tree); + + cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD^{tree}")); + + cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree, + GIT_PATHSPEC_IGNORE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps)); + cl_assert_equal_sz(9, git_pathspec_match_list_entrycount(m)); + cl_assert_equal_s("staged_changes", git_pathspec_match_list_entry(m, 0)); + cl_assert_equal_s("subdir.txt", git_pathspec_match_list_entry(m, 5)); + cl_assert_equal_s("subdir/current_file", git_pathspec_match_list_entry(m, 6)); + cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m)); + git_pathspec_match_list_free(m); + + git_object_free(tree); + + git_pathspec_free(ps); +} + +void test_repo_pathspec__in_memory(void) +{ + static char *strings[] = { "one", "two*", "!three*", "*four" }; + git_strarray s = { strings, ARRAY_SIZE(strings) }; + git_pathspec *ps; + + cl_git_pass(git_pathspec_new(&ps, &s)); + + cl_assert(git_pathspec_matches_path(ps, 0, "one")); + cl_assert(!git_pathspec_matches_path(ps, 0, "ONE")); + cl_assert(git_pathspec_matches_path(ps, GIT_PATHSPEC_IGNORE_CASE, "ONE")); + cl_assert(git_pathspec_matches_path(ps, 0, "two")); + cl_assert(git_pathspec_matches_path(ps, 0, "two.txt")); + cl_assert(!git_pathspec_matches_path(ps, 0, "three.txt")); + cl_assert(git_pathspec_matches_path(ps, 0, "anything.four")); + cl_assert(!git_pathspec_matches_path(ps, 0, "three.four")); + cl_assert(!git_pathspec_matches_path(ps, 0, "nomatch")); + cl_assert(!git_pathspec_matches_path(ps, GIT_PATHSPEC_NO_GLOB, "two")); + cl_assert(git_pathspec_matches_path(ps, GIT_PATHSPEC_NO_GLOB, "two*")); + cl_assert(!git_pathspec_matches_path(ps, GIT_PATHSPEC_NO_GLOB, "anyfour")); + cl_assert(git_pathspec_matches_path(ps, GIT_PATHSPEC_NO_GLOB, "*four")); + + git_pathspec_free(ps); +} diff --git a/tests/repo/repo_helpers.c b/tests/repo/repo_helpers.c new file mode 100644 index 000000000..3d477ff42 --- /dev/null +++ b/tests/repo/repo_helpers.c @@ -0,0 +1,22 @@ +#include "clar_libgit2.h" +#include "refs.h" +#include "repo_helpers.h" +#include "posix.h" + +void make_head_unborn(git_repository* repo, const char *target) +{ + git_reference *head; + + cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, target, 1)); + git_reference_free(head); +} + +void delete_head(git_repository* repo) +{ + git_buf head_path = GIT_BUF_INIT; + + cl_git_pass(git_buf_joinpath(&head_path, git_repository_path(repo), GIT_HEAD_FILE)); + cl_git_pass(p_unlink(git_buf_cstr(&head_path))); + + git_buf_free(&head_path); +} diff --git a/tests/repo/repo_helpers.h b/tests/repo/repo_helpers.h new file mode 100644 index 000000000..6783d5701 --- /dev/null +++ b/tests/repo/repo_helpers.h @@ -0,0 +1,6 @@ +#include "common.h" + +#define NON_EXISTING_HEAD "refs/heads/hide/and/seek" + +extern void make_head_unborn(git_repository* repo, const char *target); +extern void delete_head(git_repository* repo); diff --git a/tests/repo/setters.c b/tests/repo/setters.c new file mode 100644 index 000000000..f34f1e471 --- /dev/null +++ b/tests/repo/setters.c @@ -0,0 +1,107 @@ +#include "clar_libgit2.h" +#include "git2/sys/repository.h" + +#include "buffer.h" +#include "posix.h" +#include "util.h" +#include "path.h" +#include "fileops.h" + +static git_repository *repo; + +void test_repo_setters__initialize(void) +{ + cl_fixture_sandbox("testrepo.git"); + cl_git_pass(git_repository_open(&repo, "testrepo.git")); + cl_must_pass(p_mkdir("new_workdir", 0777)); +} + +void test_repo_setters__cleanup(void) +{ + git_repository_free(repo); + repo = NULL; + + cl_fixture_cleanup("testrepo.git"); + cl_fixture_cleanup("new_workdir"); +} + +void test_repo_setters__setting_a_workdir_turns_a_bare_repository_into_a_standard_one(void) +{ + cl_assert(git_repository_is_bare(repo) == 1); + + cl_assert(git_repository_workdir(repo) == NULL); + cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false)); + + cl_assert(git_repository_workdir(repo) != NULL); + cl_assert(git_repository_is_bare(repo) == 0); +} + +void test_repo_setters__setting_a_workdir_prettifies_its_path(void) +{ + cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false)); + + cl_assert(git__suffixcmp(git_repository_workdir(repo), "new_workdir/") == 0); +} + +void test_repo_setters__setting_a_workdir_creates_a_gitlink(void) +{ + git_config *cfg; + const char *val; + git_buf content = GIT_BUF_INIT; + + cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", true)); + + cl_assert(git_path_isfile("./new_workdir/.git")); + + cl_git_pass(git_futils_readbuffer(&content, "./new_workdir/.git")); + cl_assert(git__prefixcmp(git_buf_cstr(&content), "gitdir: ") == 0); + cl_assert(git__suffixcmp(git_buf_cstr(&content), "testrepo.git/") == 0); + git_buf_free(&content); + + cl_git_pass(git_repository_config(&cfg, repo)); + cl_git_pass(git_config_get_string(&val, cfg, "core.worktree")); + cl_assert(git__suffixcmp(val, "new_workdir/") == 0); + git_config_free(cfg); +} + +void test_repo_setters__setting_a_new_index_on_a_repo_which_has_already_loaded_one_properly_honors_the_refcount(void) +{ + git_index *new_index; + + cl_git_pass(git_index_open(&new_index, "./my-index")); + cl_assert(((git_refcount *)new_index)->refcount.val == 1); + + git_repository_set_index(repo, new_index); + cl_assert(((git_refcount *)new_index)->refcount.val == 2); + + git_repository_free(repo); + cl_assert(((git_refcount *)new_index)->refcount.val == 1); + + git_index_free(new_index); + + /* + * Ensure the cleanup method won't try to free the repo as it's already been taken care of + */ + repo = NULL; +} + +void test_repo_setters__setting_a_new_odb_on_a_repo_which_already_loaded_one_properly_honors_the_refcount(void) +{ + git_odb *new_odb; + + cl_git_pass(git_odb_open(&new_odb, "./testrepo.git/objects")); + cl_assert(((git_refcount *)new_odb)->refcount.val == 1); + + git_repository_set_odb(repo, new_odb); + cl_assert(((git_refcount *)new_odb)->refcount.val == 2); + + git_repository_free(repo); + cl_assert(((git_refcount *)new_odb)->refcount.val == 1); + + git_odb_free(new_odb); + + /* + * Ensure the cleanup method won't try to free the repo as it's already been taken care of + */ + repo = NULL; +} diff --git a/tests/repo/shallow.c b/tests/repo/shallow.c new file mode 100644 index 000000000..1cc66ae40 --- /dev/null +++ b/tests/repo/shallow.c @@ -0,0 +1,33 @@ +#include "clar_libgit2.h" +#include "fileops.h" + +static git_repository *g_repo; + +void test_repo_shallow__initialize(void) +{ +} + +void test_repo_shallow__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +void test_repo_shallow__no_shallow_file(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); +} + +void test_repo_shallow__empty_shallow_file(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_git_mkfile("testrepo.git/shallow", ""); + cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); +} + +void test_repo_shallow__shallow_repo(void) +{ + g_repo = cl_git_sandbox_init("shallow.git"); + cl_assert_equal_i(1, git_repository_is_shallow(g_repo)); +} + diff --git a/tests/repo/state.c b/tests/repo/state.c new file mode 100644 index 000000000..5a0a5f360 --- /dev/null +++ b/tests/repo/state.c @@ -0,0 +1,96 @@ +#include "clar_libgit2.h" +#include "buffer.h" +#include "refs.h" +#include "posix.h" +#include "fileops.h" + +static git_repository *_repo; +static git_buf _path; + +void test_repo_state__initialize(void) +{ + _repo = cl_git_sandbox_init("testrepo.git"); +} + +void test_repo_state__cleanup(void) +{ + cl_git_sandbox_cleanup(); + git_buf_free(&_path); +} + +static void setup_simple_state(const char *filename) +{ + cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), filename)); + git_futils_mkpath2file(git_buf_cstr(&_path), 0777); + cl_git_mkfile(git_buf_cstr(&_path), "dummy"); +} + +static void assert_repo_state(git_repository_state_t state) +{ + cl_assert_equal_i(state, git_repository_state(_repo)); +} + +void test_repo_state__none_with_HEAD_attached(void) +{ + assert_repo_state(GIT_REPOSITORY_STATE_NONE); +} + +void test_repo_state__none_with_HEAD_detached(void) +{ + cl_git_pass(git_repository_detach_head(_repo)); + assert_repo_state(GIT_REPOSITORY_STATE_NONE); +} + +void test_repo_state__merge(void) +{ + setup_simple_state(GIT_MERGE_HEAD_FILE); + assert_repo_state(GIT_REPOSITORY_STATE_MERGE); +} + +void test_repo_state__revert(void) +{ + setup_simple_state(GIT_REVERT_HEAD_FILE); + assert_repo_state(GIT_REPOSITORY_STATE_REVERT); +} + +void test_repo_state__cherry_pick(void) +{ + setup_simple_state(GIT_CHERRY_PICK_HEAD_FILE); + assert_repo_state(GIT_REPOSITORY_STATE_CHERRY_PICK); +} + +void test_repo_state__bisect(void) +{ + setup_simple_state(GIT_BISECT_LOG_FILE); + assert_repo_state(GIT_REPOSITORY_STATE_BISECT); +} + +void test_repo_state__rebase_interactive(void) +{ + setup_simple_state(GIT_REBASE_MERGE_INTERACTIVE_FILE); + assert_repo_state(GIT_REPOSITORY_STATE_REBASE_INTERACTIVE); +} + +void test_repo_state__rebase_merge(void) +{ + setup_simple_state(GIT_REBASE_MERGE_DIR "whatever"); + assert_repo_state(GIT_REPOSITORY_STATE_REBASE_MERGE); +} + +void test_repo_state__rebase(void) +{ + setup_simple_state(GIT_REBASE_APPLY_REBASING_FILE); + assert_repo_state(GIT_REPOSITORY_STATE_REBASE); +} + +void test_repo_state__apply_mailbox(void) +{ + setup_simple_state(GIT_REBASE_APPLY_APPLYING_FILE); + assert_repo_state(GIT_REPOSITORY_STATE_APPLY_MAILBOX); +} + +void test_repo_state__apply_mailbox_or_rebase(void) +{ + setup_simple_state(GIT_REBASE_APPLY_DIR "whatever"); + assert_repo_state(GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE); +} |