]> git.proxmox.com Git - libgit2.git/blobdiff - tests/online/fetch.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / online / fetch.c
index 67dfd69ed62f6aaaaea64130eae4336abb2d06f3..5beb5b618adec4a72402a842ef43faab6c56466b 100644 (file)
@@ -1,4 +1,5 @@
 #include "clar_libgit2.h"
+#include "futils.h"
 
 static git_repository *_repo;
 static int counter;
@@ -7,15 +8,19 @@ static char *_remote_proxy_scheme = NULL;
 static char *_remote_proxy_host = NULL;
 static char *_remote_proxy_user = NULL;
 static char *_remote_proxy_pass = NULL;
+static char *_remote_redirect_initial = NULL;
+static char *_remote_redirect_subsequent = NULL;
 
 void test_online_fetch__initialize(void)
 {
        cl_git_pass(git_repository_init(&_repo, "./fetch", 0));
 
-    _remote_proxy_scheme = cl_getenv("GITTEST_REMOTE_PROXY_SCHEME");
-    _remote_proxy_host = cl_getenv("GITTEST_REMOTE_PROXY_HOST");
-    _remote_proxy_user = cl_getenv("GITTEST_REMOTE_PROXY_USER");
-    _remote_proxy_pass = cl_getenv("GITTEST_REMOTE_PROXY_PASS");
+       _remote_proxy_scheme = cl_getenv("GITTEST_REMOTE_PROXY_SCHEME");
+       _remote_proxy_host = cl_getenv("GITTEST_REMOTE_PROXY_HOST");
+       _remote_proxy_user = cl_getenv("GITTEST_REMOTE_PROXY_USER");
+       _remote_proxy_pass = cl_getenv("GITTEST_REMOTE_PROXY_PASS");
+       _remote_redirect_initial = cl_getenv("GITTEST_REMOTE_REDIRECT_INITIAL");
+       _remote_redirect_subsequent = cl_getenv("GITTEST_REMOTE_REDIRECT_SUBSEQUENT");
 }
 
 void test_online_fetch__cleanup(void)
@@ -24,11 +29,14 @@ void test_online_fetch__cleanup(void)
        _repo = NULL;
 
        cl_fixture_cleanup("./fetch");
-
-    git__free(_remote_proxy_scheme);
-    git__free(_remote_proxy_host);
-    git__free(_remote_proxy_user);
-    git__free(_remote_proxy_pass);
+       cl_fixture_cleanup("./redirected");
+
+       git__free(_remote_proxy_scheme);
+       git__free(_remote_proxy_host);
+       git__free(_remote_proxy_user);
+       git__free(_remote_proxy_pass);
+       git__free(_remote_redirect_initial);
+       git__free(_remote_redirect_subsequent);
 }
 
 static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *data)
@@ -67,11 +75,6 @@ static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n)
        git_remote_free(remote);
 }
 
-void test_online_fetch__default_git(void)
-{
-       do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
-}
-
 void test_online_fetch__default_http(void)
 {
        do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
@@ -84,7 +87,7 @@ void test_online_fetch__default_https(void)
 
 void test_online_fetch__no_tags_git(void)
 {
-       do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
+       do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
 }
 
 void test_online_fetch__no_tags_http(void)
@@ -95,7 +98,7 @@ void test_online_fetch__no_tags_http(void)
 void test_online_fetch__fetch_twice(void)
 {
        git_remote *remote;
-       cl_git_pass(git_remote_create(&remote, _repo, "test", "git://github.com/libgit2/TestGitRepository.git"));
+       cl_git_pass(git_remote_create(&remote, _repo, "test", "https://github.com/libgit2/TestGitRepository.git"));
        cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
        cl_git_pass(git_remote_download(remote, NULL, NULL));
        git_remote_disconnect(remote);
@@ -226,13 +229,13 @@ void test_online_fetch__twice(void)
 void test_online_fetch__proxy(void)
 {
     git_remote *remote;
-    git_buf url = GIT_BUF_INIT;
+    git_str url = GIT_STR_INIT;
     git_fetch_options fetch_opts;
 
     if (!_remote_proxy_host || !_remote_proxy_user || !_remote_proxy_pass)
         cl_skip();
 
-    cl_git_pass(git_buf_printf(&url, "%s://%s:%s@%s/",
+    cl_git_pass(git_str_printf(&url, "%s://%s:%s@%s/",
         _remote_proxy_scheme ? _remote_proxy_scheme : "http",
         _remote_proxy_user, _remote_proxy_pass, _remote_proxy_host));
 
@@ -245,5 +248,105 @@ void test_online_fetch__proxy(void)
     cl_git_pass(git_remote_fetch(remote, NULL, &fetch_opts, NULL));
 
     git_remote_free(remote);
-    git_buf_dispose(&url);
+    git_str_dispose(&url);
+}
+
+static int do_redirected_fetch(const char *url, const char *name, const char *config)
+{
+       git_repository *repo;
+       git_remote *remote;
+       int error;
+
+       cl_git_pass(git_repository_init(&repo, "./redirected", 0));
+       cl_fixture_cleanup(name);
+
+       if (config)
+               cl_repo_set_string(repo, "http.followRedirects", config);
+
+       cl_git_pass(git_remote_create(&remote, repo, name, url));
+       error = git_remote_fetch(remote, NULL, NULL, NULL);
+
+       git_remote_free(remote);
+       git_repository_free(repo);
+
+       cl_fixture_cleanup("./redirected");
+
+       return error;
+}
+
+void test_online_fetch__redirect_config(void)
+{
+       if (!_remote_redirect_initial || !_remote_redirect_subsequent)
+               cl_skip();
+
+       /* config defaults */
+       cl_git_pass(do_redirected_fetch(_remote_redirect_initial, "initial", NULL));
+       cl_git_fail(do_redirected_fetch(_remote_redirect_subsequent, "subsequent", NULL));
+
+       /* redirect=initial */
+       cl_git_pass(do_redirected_fetch(_remote_redirect_initial, "initial", "initial"));
+       cl_git_fail(do_redirected_fetch(_remote_redirect_subsequent, "subsequent", "initial"));
+
+       /* redirect=false */
+       cl_git_fail(do_redirected_fetch(_remote_redirect_initial, "initial", "false"));
+       cl_git_fail(do_redirected_fetch(_remote_redirect_subsequent, "subsequent", "false"));
+}
+
+void test_online_fetch__reachable_commit(void)
+{
+       git_remote *remote;
+       git_strarray refspecs;
+       git_object *obj;
+       git_oid expected_id;
+       git_str fetchhead = GIT_STR_INIT;
+       char *refspec = "+2c349335b7f797072cf729c4f3bb0914ecb6dec9:refs/success";
+
+       refspecs.strings = &refspec;
+       refspecs.count = 1;
+
+       git_oid_fromstr(&expected_id, "2c349335b7f797072cf729c4f3bb0914ecb6dec9");
+
+       cl_git_pass(git_remote_create(&remote, _repo, "test",
+               "https://github.com/libgit2/TestGitRepository"));
+       cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, NULL));
+
+       cl_git_pass(git_revparse_single(&obj, _repo, "refs/success"));
+       cl_assert_equal_oid(&expected_id, git_object_id(obj));
+
+       cl_git_pass(git_futils_readbuffer(&fetchhead, "./fetch/.git/FETCH_HEAD"));
+       cl_assert_equal_s(fetchhead.ptr,
+               "2c349335b7f797072cf729c4f3bb0914ecb6dec9\t\t'2c349335b7f797072cf729c4f3bb0914ecb6dec9' of https://github.com/libgit2/TestGitRepository\n");
+
+       git_str_dispose(&fetchhead);
+       git_object_free(obj);
+       git_remote_free(remote);
+}
+
+void test_online_fetch__reachable_commit_without_destination(void)
+{
+       git_remote *remote;
+       git_strarray refspecs;
+       git_object *obj;
+       git_oid expected_id;
+       git_str fetchhead = GIT_STR_INIT;
+       char *refspec = "2c349335b7f797072cf729c4f3bb0914ecb6dec9";
+
+       refspecs.strings = &refspec;
+       refspecs.count = 1;
+
+       git_oid_fromstr(&expected_id, "2c349335b7f797072cf729c4f3bb0914ecb6dec9");
+
+       cl_git_pass(git_remote_create(&remote, _repo, "test",
+               "https://github.com/libgit2/TestGitRepository"));
+       cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, NULL));
+
+       cl_git_fail_with(GIT_ENOTFOUND, git_revparse_single(&obj, _repo, "refs/success"));
+
+       cl_git_pass(git_futils_readbuffer(&fetchhead, "./fetch/.git/FETCH_HEAD"));
+       cl_assert_equal_s(fetchhead.ptr,
+               "2c349335b7f797072cf729c4f3bb0914ecb6dec9\t\t'2c349335b7f797072cf729c4f3bb0914ecb6dec9' of https://github.com/libgit2/TestGitRepository\n");
+
+       git_str_dispose(&fetchhead);
+       git_object_free(obj);
+       git_remote_free(remote);
 }