diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2021-11-16 23:29:22 -0500 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2022-02-22 22:07:45 -0500 |
| commit | 3344fddc97bbdea9c1b6ebb6f7fb6dbd70b41dfb (patch) | |
| tree | fd6368a72944571c51627b40c592e7d58e0036e1 /tests/libgit2/clone | |
| parent | 91ba089663f5efc3bd4ba14a5099372cf5ce57a6 (diff) | |
| download | libgit2-3344fddc97bbdea9c1b6ebb6f7fb6dbd70b41dfb.tar.gz | |
refactor: `tests` is now `tests/libgit2`
Like we want to separate libgit2 and utility source code, we want to
separate libgit2 and utility tests. Start by moving all the tests into
libgit2.
Diffstat (limited to 'tests/libgit2/clone')
| -rw-r--r-- | tests/libgit2/clone/empty.c | 102 | ||||
| -rw-r--r-- | tests/libgit2/clone/local.c | 212 | ||||
| -rw-r--r-- | tests/libgit2/clone/nonetwork.c | 361 | ||||
| -rw-r--r-- | tests/libgit2/clone/transport.c | 51 |
4 files changed, 726 insertions, 0 deletions
diff --git a/tests/libgit2/clone/empty.c b/tests/libgit2/clone/empty.c new file mode 100644 index 000000000..94847bc73 --- /dev/null +++ b/tests/libgit2/clone/empty.c @@ -0,0 +1,102 @@ +#include "clar_libgit2.h" + +#include "git2/clone.h" +#include "repository.h" +#include "repo/repo_helpers.h" + +static git_clone_options g_options; +static git_repository *g_repo; +static git_repository *g_repo_cloned; + +void test_clone_empty__initialize(void) +{ + git_repository *sandbox = cl_git_sandbox_init("empty_bare.git"); + git_fetch_options dummy_options = GIT_FETCH_OPTIONS_INIT; + cl_git_remove_placeholders(git_repository_path(sandbox), "dummy-marker.txt"); + + g_repo = NULL; + + memset(&g_options, 0, sizeof(git_clone_options)); + g_options.version = GIT_CLONE_OPTIONS_VERSION; + g_options.fetch_opts = dummy_options; +} + +void test_clone_empty__cleanup(void) +{ + cl_fixture_cleanup("tmp_global_path"); + cl_git_sandbox_cleanup(); +} + +static void cleanup_repository(void *path) +{ + cl_fixture_cleanup((const char *)path); + + git_repository_free(g_repo_cloned); + g_repo_cloned = NULL; +} + +void test_clone_empty__can_clone_an_empty_local_repo_barely(void) +{ + char *local_name = "refs/heads/master"; + const char *expected_tracked_branch_name = "refs/remotes/origin/master"; + const char *expected_remote_name = "origin"; + git_buf buf = GIT_BUF_INIT; + git_reference *ref; + + cl_set_cleanup(&cleanup_repository, "./empty"); + + g_options.bare = true; + cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options)); + + /* Although the HEAD is unborn... */ + cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned, local_name)); + + /* ...one can still retrieve the name of the remote tracking reference */ + cl_git_pass(git_branch_upstream_name(&buf, g_repo_cloned, local_name)); + + cl_assert_equal_s(expected_tracked_branch_name, buf.ptr); + git_buf_dispose(&buf); + + /* ...and the name of the remote... */ + cl_git_pass(git_branch_remote_name(&buf, g_repo_cloned, expected_tracked_branch_name)); + + cl_assert_equal_s(expected_remote_name, buf.ptr); + git_buf_dispose(&buf); + + /* ...even when the remote HEAD is unborn as well */ + cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned, + expected_tracked_branch_name)); +} + +void test_clone_empty__respects_initialbranch_config(void) +{ + git_buf buf = GIT_BUF_INIT; + + create_tmp_global_config("tmp_global_path", "init.defaultbranch", "my_default_branch"); + + cl_set_cleanup(&cleanup_repository, "./empty"); + + g_options.bare = true; + cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options)); + cl_git_pass(git_branch_upstream_name(&buf, g_repo_cloned, "refs/heads/my_default_branch")); + cl_assert_equal_s("refs/remotes/origin/my_default_branch", buf.ptr); + git_buf_dispose(&buf); +} + +void test_clone_empty__can_clone_an_empty_local_repo(void) +{ + cl_set_cleanup(&cleanup_repository, "./empty"); + + cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options)); +} + +void test_clone_empty__can_clone_an_empty_standard_repo(void) +{ + cl_git_sandbox_cleanup(); + g_repo = cl_git_sandbox_init("empty_standard_repo"); + cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt"); + + cl_set_cleanup(&cleanup_repository, "./empty"); + + cl_git_pass(git_clone(&g_repo_cloned, "./empty_standard_repo", "./empty", &g_options)); +} diff --git a/tests/libgit2/clone/local.c b/tests/libgit2/clone/local.c new file mode 100644 index 000000000..e0bd74df7 --- /dev/null +++ b/tests/libgit2/clone/local.c @@ -0,0 +1,212 @@ +#include "clar_libgit2.h" + +#include "git2/clone.h" +#include "clone.h" +#include "path.h" +#include "posix.h" +#include "futils.h" + +static int file_url(git_str *buf, const char *host, const char *path) +{ + if (path[0] == '/') + path++; + + git_str_clear(buf); + return git_str_printf(buf, "file://%s/%s", host, path); +} + +#ifdef GIT_WIN32 +static int git_style_unc_path(git_str *buf, const char *host, const char *path) +{ + git_str_clear(buf); + + if (host) + git_str_printf(buf, "//%s/", host); + + if (path[0] == '/') + path++; + + if (git__isalpha(path[0]) && path[1] == ':' && path[2] == '/') { + git_str_printf(buf, "%c$/", path[0]); + path += 3; + } + + git_str_puts(buf, path); + + return git_str_oom(buf) ? -1 : 0; +} + +static int unc_path(git_str *buf, const char *host, const char *path) +{ + char *c; + + if (git_style_unc_path(buf, host, path) < 0) + return -1; + + for (c = buf->ptr; *c; c++) + if (*c == '/') + *c = '\\'; + + return 0; +} +#endif + +void test_clone_local__should_clone_local(void) +{ + git_str buf = GIT_STR_INIT; + + /* we use a fixture path because it needs to exist for us to want to clone */ + const char *path = cl_fixture("testrepo.git"); + + cl_git_pass(file_url(&buf, "", path)); + cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO)); + cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL)); + cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS)); + cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL)); + + cl_git_pass(file_url(&buf, "localhost", path)); + cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO)); + cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL)); + cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS)); + cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL)); + + cl_git_pass(file_url(&buf, "other-host.mycompany.com", path)); + cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO)); + cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL)); + cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS)); + cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL)); + + /* Ensure that file:/// urls are percent decoded: .git == %2e%67%69%74 */ + cl_git_pass(file_url(&buf, "", path)); + git_str_shorten(&buf, 4); + cl_git_pass(git_str_puts(&buf, "%2e%67%69%74")); + cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO)); + cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL)); + cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS)); + cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL)); + + cl_assert_equal_i(1, git_clone__should_clone_local(path, GIT_CLONE_LOCAL_AUTO)); + cl_assert_equal_i(1, git_clone__should_clone_local(path, GIT_CLONE_LOCAL)); + cl_assert_equal_i(1, git_clone__should_clone_local(path, GIT_CLONE_LOCAL_NO_LINKS)); + cl_assert_equal_i(0, git_clone__should_clone_local(path, GIT_CLONE_NO_LOCAL)); + + git_str_dispose(&buf); +} + +void test_clone_local__hardlinks(void) +{ + git_repository *repo; + git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + git_str buf = GIT_STR_INIT; + struct stat st; + + /* + * In this first clone, we just copy over, since the temp dir + * will often be in a different filesystem, so we cannot + * link. It also allows us to control the number of links + */ + opts.bare = true; + opts.local = GIT_CLONE_LOCAL_NO_LINKS; + cl_git_pass(git_clone(&repo, cl_fixture("testrepo.git"), "./clone.git", &opts)); + git_repository_free(repo); + + /* This second clone is in the same filesystem, so we can hardlink */ + + opts.local = GIT_CLONE_LOCAL; + cl_git_pass(git_clone(&repo, cl_git_path_url("clone.git"), "./clone2.git", &opts)); + +#ifndef GIT_WIN32 + git_str_clear(&buf); + cl_git_pass(git_str_join_n(&buf, '/', 4, git_repository_path(repo), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125")); + + cl_git_pass(p_stat(buf.ptr, &st)); + cl_assert_equal_i(2, st.st_nlink); +#endif + + git_repository_free(repo); + git_str_clear(&buf); + + opts.local = GIT_CLONE_LOCAL_NO_LINKS; + cl_git_pass(git_clone(&repo, cl_git_path_url("clone.git"), "./clone3.git", &opts)); + + git_str_clear(&buf); + cl_git_pass(git_str_join_n(&buf, '/', 4, git_repository_path(repo), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125")); + + cl_git_pass(p_stat(buf.ptr, &st)); + cl_assert_equal_i(1, st.st_nlink); + + git_repository_free(repo); + + /* this one should automatically use links */ + cl_git_pass(git_clone(&repo, "./clone.git", "./clone4.git", NULL)); + +#ifndef GIT_WIN32 + git_str_clear(&buf); + cl_git_pass(git_str_join_n(&buf, '/', 4, git_repository_path(repo), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125")); + + cl_git_pass(p_stat(buf.ptr, &st)); + cl_assert_equal_i(3, st.st_nlink); +#endif + + git_str_dispose(&buf); + git_repository_free(repo); + + cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES)); + cl_git_pass(git_futils_rmdir_r("./clone2.git", NULL, GIT_RMDIR_REMOVE_FILES)); + cl_git_pass(git_futils_rmdir_r("./clone3.git", NULL, GIT_RMDIR_REMOVE_FILES)); + cl_git_pass(git_futils_rmdir_r("./clone4.git", NULL, GIT_RMDIR_REMOVE_FILES)); +} + +void test_clone_local__standard_unc_paths_are_written_git_style(void) +{ +#ifdef GIT_WIN32 + git_repository *repo; + git_remote *remote; + git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + git_str unc = GIT_STR_INIT, git_unc = GIT_STR_INIT; + + /* we use a fixture path because it needs to exist for us to want to clone */ + const char *path = cl_fixture("testrepo.git"); + + cl_git_pass(unc_path(&unc, "localhost", path)); + cl_git_pass(git_style_unc_path(&git_unc, "localhost", path)); + + cl_git_pass(git_clone(&repo, unc.ptr, "./clone.git", &opts)); + cl_git_pass(git_remote_lookup(&remote, repo, "origin")); + + cl_assert_equal_s(git_unc.ptr, git_remote_url(remote)); + + git_remote_free(remote); + git_repository_free(repo); + git_str_dispose(&unc); + git_str_dispose(&git_unc); + + cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES)); +#endif +} + +void test_clone_local__git_style_unc_paths(void) +{ +#ifdef GIT_WIN32 + git_repository *repo; + git_remote *remote; + git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + git_str git_unc = GIT_STR_INIT; + + /* we use a fixture path because it needs to exist for us to want to clone */ + const char *path = cl_fixture("testrepo.git"); + + cl_git_pass(git_style_unc_path(&git_unc, "localhost", path)); + + cl_git_pass(git_clone(&repo, git_unc.ptr, "./clone.git", &opts)); + cl_git_pass(git_remote_lookup(&remote, repo, "origin")); + + cl_assert_equal_s(git_unc.ptr, git_remote_url(remote)); + + git_remote_free(remote); + git_repository_free(repo); + git_str_dispose(&git_unc); + + cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES)); +#endif +} diff --git a/tests/libgit2/clone/nonetwork.c b/tests/libgit2/clone/nonetwork.c new file mode 100644 index 000000000..eab633635 --- /dev/null +++ b/tests/libgit2/clone/nonetwork.c @@ -0,0 +1,361 @@ +#include "clar_libgit2.h" + +#include "git2/clone.h" +#include "../submodule/submodule_helpers.h" +#include "remote.h" +#include "futils.h" +#include "repository.h" + +#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository" + +static git_clone_options g_options; +static git_repository *g_repo; +static git_reference* g_ref; +static git_remote* g_remote; + +void test_clone_nonetwork__initialize(void) +{ + git_checkout_options dummy_opts = GIT_CHECKOUT_OPTIONS_INIT; + git_fetch_options dummy_fetch = GIT_FETCH_OPTIONS_INIT; + + g_repo = NULL; + + memset(&g_options, 0, sizeof(git_clone_options)); + g_options.version = GIT_CLONE_OPTIONS_VERSION; + g_options.checkout_opts = dummy_opts; + g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; + g_options.fetch_opts = dummy_fetch; +} + +void test_clone_nonetwork__cleanup(void) +{ + if (g_repo) { + git_repository_free(g_repo); + g_repo = NULL; + } + + if (g_ref) { + git_reference_free(g_ref); + g_ref = NULL; + } + + if (g_remote) { + git_remote_free(g_remote); + g_remote = NULL; + } + + cl_fixture_cleanup("./foo"); +} + +void test_clone_nonetwork__bad_urls(void) +{ + /* Clone should clean up the mess if the URL isn't a git repository */ + cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); + cl_assert(!git_fs_path_exists("./foo")); + g_options.bare = true; + cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); + cl_assert(!git_fs_path_exists("./foo")); + + cl_git_fail(git_clone(&g_repo, "git://example.com:asdf", "./foo", &g_options)); + cl_git_fail(git_clone(&g_repo, "https://example.com:asdf/foo", "./foo", &g_options)); + cl_git_fail(git_clone(&g_repo, "git://github.com/git://github.com/foo/bar.git.git", + "./foo", &g_options)); + cl_git_fail(git_clone(&g_repo, "arrbee:my/bad:password@github.com:1111/strange:words.git", + "./foo", &g_options)); +} + +void test_clone_nonetwork__do_not_clean_existing_directory(void) +{ + /* Clone should not remove the directory if it already exists, but + * Should clean up entries it creates. */ + p_mkdir("./foo", GIT_DIR_MODE); + cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); + cl_assert(git_fs_path_is_empty_dir("./foo")); + + /* Try again with a bare repository. */ + g_options.bare = true; + cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); + cl_assert(git_fs_path_is_empty_dir("./foo")); +} + +void test_clone_nonetwork__local(void) +{ + cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); +} + +void test_clone_nonetwork__local_absolute_path(void) +{ + const char *local_src; + local_src = cl_fixture("testrepo.git"); + cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options)); +} + +void test_clone_nonetwork__local_bare(void) +{ + g_options.bare = true; + cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); +} + +void test_clone_nonetwork__fail_when_the_target_is_a_file(void) +{ + cl_git_mkfile("./foo", "Bar!"); + cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); +} + +void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void) +{ + p_mkdir("./foo", GIT_DIR_MODE); + cl_git_mkfile("./foo/bar", "Baz!"); + cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); +} + +static int custom_origin_name_remote_create( + git_remote **out, + git_repository *repo, + const char *name, + const char *url, + void *payload) +{ + GIT_UNUSED(name); + GIT_UNUSED(payload); + + return git_remote_create(out, repo, "my_origin", url); +} + +void test_clone_nonetwork__custom_origin_name(void) +{ + g_options.remote_cb = custom_origin_name_remote_create; + cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); + + cl_git_pass(git_remote_lookup(&g_remote, g_repo, "my_origin")); +} + +void test_clone_nonetwork__defaults(void) +{ + cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", NULL)); + cl_assert(g_repo); + cl_git_pass(git_remote_lookup(&g_remote, g_repo, "origin")); +} + +void test_clone_nonetwork__cope_with_already_existing_directory(void) +{ + p_mkdir("./foo", GIT_DIR_MODE); + cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); +} + +void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void) +{ + git_str path = GIT_STR_INIT; + + g_options.checkout_opts.checkout_strategy = 0; + cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); + + cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "master.txt")); + cl_assert_equal_i(false, git_fs_path_isfile(git_str_cstr(&path))); + + git_str_dispose(&path); +} + +void test_clone_nonetwork__can_checkout_given_branch(void) +{ + git_reference *remote_head; + + g_options.checkout_branch = "test"; + cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); + + cl_assert_equal_i(0, git_repository_head_unborn(g_repo)); + + cl_git_pass(git_repository_head(&g_ref, g_repo)); + cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test"); + + cl_assert(git_fs_path_exists("foo/readme.txt")); + + cl_git_pass(git_reference_lookup(&remote_head, g_repo, "refs/remotes/origin/HEAD")); + cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(remote_head)); + cl_assert_equal_s("refs/remotes/origin/master", git_reference_symbolic_target(remote_head)); + + git_reference_free(remote_head); +} + +static int clone_cancel_fetch_transfer_progress_cb( + const git_indexer_progress *stats, void *data) +{ + GIT_UNUSED(stats); GIT_UNUSED(data); + return -54321; +} + +void test_clone_nonetwork__can_cancel_clone_in_fetch(void) +{ + g_options.checkout_branch = "test"; + + g_options.fetch_opts.callbacks.transfer_progress = + clone_cancel_fetch_transfer_progress_cb; + + cl_git_fail_with(git_clone( + &g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options), + -54321); + + cl_assert(!g_repo); + cl_assert(!git_fs_path_exists("foo/readme.txt")); +} + +static int clone_cancel_checkout_cb( + git_checkout_notify_t why, + const char *path, + const git_diff_file *b, + const git_diff_file *t, + const git_diff_file *w, + void *payload) +{ + const char *at_file = payload; + GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w); + if (!strcmp(path, at_file)) + return -12345; + return 0; +} + +void test_clone_nonetwork__can_cancel_clone_in_checkout(void) +{ + g_options.checkout_branch = "test"; + + g_options.checkout_opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED; + g_options.checkout_opts.notify_cb = clone_cancel_checkout_cb; + g_options.checkout_opts.notify_payload = "readme.txt"; + + cl_git_fail_with(git_clone( + &g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options), + -12345); + + cl_assert(!g_repo); + cl_assert(!git_fs_path_exists("foo/readme.txt")); +} + +void test_clone_nonetwork__can_detached_head(void) +{ + git_object *obj; + git_repository *cloned; + git_reference *cloned_head; + + cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); + + cl_git_pass(git_revparse_single(&obj, g_repo, "master~1")); + cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj))); + + cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options)); + + cl_assert(git_repository_head_detached(cloned)); + + cl_git_pass(git_repository_head(&cloned_head, cloned)); + cl_assert_equal_oid(git_object_id(obj), git_reference_target(cloned_head)); + + git_object_free(obj); + git_reference_free(cloned_head); + git_repository_free(cloned); + + cl_fixture_cleanup("./foo1"); +} + +void test_clone_nonetwork__clone_tag_to_tree(void) +{ + git_repository *stage; + git_index_entry entry; + git_index *index; + git_odb *odb; + git_oid tree_id; + git_tree *tree; + git_reference *tag; + git_tree_entry *tentry; + const char *file_path = "some/deep/path.txt"; + const char *file_content = "some content\n"; + const char *tag_name = "refs/tags/tree-tag"; + + stage = cl_git_sandbox_init("testrepo.git"); + cl_git_pass(git_repository_odb(&odb, stage)); + cl_git_pass(git_index_new(&index)); + + memset(&entry, 0, sizeof(git_index_entry)); + entry.path = file_path; + entry.mode = GIT_FILEMODE_BLOB; + cl_git_pass(git_odb_write(&entry.id, odb, file_content, strlen(file_content), GIT_OBJECT_BLOB)); + + cl_git_pass(git_index_add(index, &entry)); + cl_git_pass(git_index_write_tree_to(&tree_id, index, stage)); + cl_git_pass(git_reference_create(&tag, stage, tag_name, &tree_id, 0, NULL)); + git_reference_free(tag); + git_odb_free(odb); + git_index_free(index); + + g_options.local = GIT_CLONE_NO_LOCAL; + cl_git_pass(git_clone(&g_repo, cl_git_path_url(git_repository_path(stage)), "./foo", &g_options)); + git_repository_free(stage); + + cl_git_pass(git_reference_lookup(&tag, g_repo, tag_name)); + cl_git_pass(git_tree_lookup(&tree, g_repo, git_reference_target(tag))); + git_reference_free(tag); + + cl_git_pass(git_tree_entry_bypath(&tentry, tree, file_path)); + git_tree_entry_free(tentry); + git_tree_free(tree); + + cl_fixture_cleanup("testrepo.git"); +} + +static void assert_correct_reflog(const char *name) +{ + git_reflog *log; + const git_reflog_entry *entry; + git_str expected_message = GIT_STR_INIT; + + git_str_printf(&expected_message, + "clone: from %s", cl_git_fixture_url("testrepo.git")); + + cl_git_pass(git_reflog_read(&log, g_repo, name)); + cl_assert_equal_i(1, git_reflog_entrycount(log)); + entry = git_reflog_entry_byindex(log, 0); + cl_assert_equal_s(expected_message.ptr, git_reflog_entry_message(entry)); + + git_reflog_free(log); + + git_str_dispose(&expected_message); +} + +void test_clone_nonetwork__clone_updates_reflog_properly(void) +{ + cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options)); + assert_correct_reflog("HEAD"); + assert_correct_reflog("refs/heads/master"); +} + +static void cleanup_repository(void *path) +{ + if (g_repo) { + git_repository_free(g_repo); + g_repo = NULL; + } + + cl_fixture_cleanup((const char *)path); +} + +void test_clone_nonetwork__clone_from_empty_sets_upstream(void) +{ + git_config *config; + git_repository *repo; + const char *str; + + /* Create an empty repo to clone from */ + cl_set_cleanup(&cleanup_repository, "./test1"); + cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); + cl_set_cleanup(&cleanup_repository, "./repowithunborn"); + cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL)); + + cl_git_pass(git_repository_config_snapshot(&config, repo)); + + cl_git_pass(git_config_get_string(&str, config, "branch.master.remote")); + cl_assert_equal_s("origin", str); + cl_git_pass(git_config_get_string(&str, config, "branch.master.merge")); + cl_assert_equal_s("refs/heads/master", str); + + git_config_free(config); + git_repository_free(repo); + cl_fixture_cleanup("./repowithunborn"); +} diff --git a/tests/libgit2/clone/transport.c b/tests/libgit2/clone/transport.c new file mode 100644 index 000000000..fa4f65357 --- /dev/null +++ b/tests/libgit2/clone/transport.c @@ -0,0 +1,51 @@ +#include "clar_libgit2.h" + +#include "git2/clone.h" +#include "git2/transport.h" +#include "git2/sys/transport.h" +#include "futils.h" + +static int custom_transport( + git_transport **out, + git_remote *owner, + void *payload) +{ + *((int*)payload) = 1; + + return git_transport_local(out, owner, payload); +} + +static int custom_transport_remote_create( + git_remote **out, + git_repository *repo, + const char *name, + const char *url, + void *payload) +{ + int error; + + GIT_UNUSED(payload); + + if ((error = git_remote_create(out, repo, name, url)) < 0) + return error; + + return 0; +} + +void test_clone_transport__custom_transport(void) +{ + git_repository *repo; + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; + int custom_transport_used = 0; + + clone_opts.remote_cb = custom_transport_remote_create; + clone_opts.fetch_opts.callbacks.transport = custom_transport; + clone_opts.fetch_opts.callbacks.payload = &custom_transport_used; + + cl_git_pass(git_clone(&repo, cl_fixture("testrepo.git"), "./custom_transport.git", &clone_opts)); + git_repository_free(repo); + + cl_git_pass(git_futils_rmdir_r("./custom_transport.git", NULL, GIT_RMDIR_REMOVE_FILES)); + + cl_assert(custom_transport_used == 1); +} |
