]> git.proxmox.com Git - libgit2.git/blobdiff - tests/clone/nonetwork.c
New upstream version 1.3.0+dfsg.1
[libgit2.git] / tests / clone / nonetwork.c
index 4bdc6e13b74bcb98095777bd22a1068a00f279f3..ec12fee18a13a7d963362c97d347f0388fa47fd7 100644 (file)
@@ -1,8 +1,9 @@
 #include "clar_libgit2.h"
 
 #include "git2/clone.h"
+#include "../submodule/submodule_helpers.h"
 #include "remote.h"
-#include "fileops.h"
+#include "futils.h"
 #include "repository.h"
 
 #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
@@ -15,16 +16,15 @@ static git_remote* g_remote;
 void test_clone_nonetwork__initialize(void)
 {
        git_checkout_options dummy_opts = GIT_CHECKOUT_OPTIONS_INIT;
-       git_remote_callbacks dummy_callbacks = GIT_REMOTE_CALLBACKS_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_CREATE;
-       g_options.remote_callbacks = dummy_callbacks;
-       cl_git_pass(git_signature_now(&g_options.signature, "Me", "foo@example.com"));
+       g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
+       g_options.fetch_opts = dummy_fetch;
 }
 
 void test_clone_nonetwork__cleanup(void)
@@ -44,7 +44,6 @@ void test_clone_nonetwork__cleanup(void)
                g_remote = NULL;
        }
 
-       git_signature_free(g_options.signature);
        cl_fixture_cleanup("./foo");
 }
 
@@ -110,19 +109,32 @@ void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(vo
        cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
 }
 
+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_name = "my_origin";
-       cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
+       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_load(&g_remote, g_repo, "my_origin"));
+       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_load(&g_remote, g_repo, "origin"));
+       cl_git_pass(git_remote_lookup(&g_remote, g_repo, "origin"));
 }
 
 void test_clone_nonetwork__cope_with_already_existing_directory(void)
@@ -141,11 +153,13 @@ void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void)
        cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
        cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path)));
 
-       git_buf_free(&path);
+       git_buf_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));
 
@@ -155,10 +169,16 @@ void test_clone_nonetwork__can_checkout_given_branch(void)
        cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test");
 
        cl_assert(git_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_transfer_progress *stats, void *data)
+       const git_indexer_progress *stats, void *data)
 {
        GIT_UNUSED(stats); GIT_UNUSED(data);
        return -54321;
@@ -168,7 +188,7 @@ void test_clone_nonetwork__can_cancel_clone_in_fetch(void)
 {
        g_options.checkout_branch = "test";
 
-       g_options.remote_callbacks.transfer_progress =
+       g_options.fetch_opts.callbacks.transfer_progress =
                clone_cancel_fetch_transfer_progress_cb;
 
        cl_git_fail_with(git_clone(
@@ -215,48 +235,88 @@ void test_clone_nonetwork__can_detached_head(void)
        git_object *obj;
        git_repository *cloned;
        git_reference *cloned_head;
-       git_reflog *log;
-       const git_reflog_entry *entry;
 
        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), NULL, NULL));
+       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(!git_oid_cmp(git_object_id(obj), git_reference_target(cloned_head)));
-
-       cl_git_pass(git_reflog_read(&log, cloned, "HEAD"));
-       entry = git_reflog_entry_byindex(log, 0);
-       cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
+       cl_assert_equal_oid(git_object_id(obj), git_reference_target(cloned_head));
 
        git_object_free(obj);
        git_reference_free(cloned_head);
-       git_reflog_free(log);
        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;
-       char expected_log_message[128] = {0};
+       git_buf expected_message = GIT_BUF_INIT;
 
-       sprintf(expected_log_message, "clone: from %s", cl_git_fixture_url("testrepo.git"));
+       git_buf_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_log_message, git_reflog_entry_message(entry));
-       cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
+       cl_assert_equal_s(expected_message.ptr, git_reflog_entry_message(entry));
 
        git_reflog_free(log);
+
+       git_buf_dispose(&expected_message);
 }
 
 void test_clone_nonetwork__clone_updates_reflog_properly(void)
@@ -266,23 +326,6 @@ void test_clone_nonetwork__clone_updates_reflog_properly(void)
        assert_correct_reflog("refs/heads/master");
 }
 
-void test_clone_nonetwork__clone_into_updates_reflog_properly(void)
-{
-       git_remote *remote;
-       git_signature *sig;
-       cl_git_pass(git_signature_now(&sig, "Me", "foo@example.com"));
-
-       cl_git_pass(git_repository_init(&g_repo, "./foo", false));
-       cl_git_pass(git_remote_create(&remote, g_repo, "origin", cl_git_fixture_url("testrepo.git")));
-       cl_git_pass(git_clone_into(g_repo, remote, NULL, NULL, sig));
-
-       assert_correct_reflog("HEAD");
-       assert_correct_reflog("refs/heads/master");
-
-       git_remote_free(remote);
-       git_signature_free(sig);
-}
-
 static void cleanup_repository(void *path)
 {
        if (g_repo) {
@@ -305,7 +348,7 @@ void test_clone_nonetwork__clone_from_empty_sets_upstream(void)
        cl_set_cleanup(&cleanup_repository, "./repowithunborn");
        cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL));
 
-       cl_git_pass(git_repository_config(&config, repo));
+       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);