remotes.c 3.21 KB
Newer Older
1 2
#include "clar_libgit2.h"

3 4
#define URL "git://github.com/libgit2/TestGitRepository"
#define REFSPEC "refs/heads/first-merge:refs/remotes/origin/first-merge"
5

6 7 8 9
static int remote_single_branch(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
{
	GIT_UNUSED(payload);

10
	cl_git_pass(git_remote_create_with_fetchspec(out, repo, name, url, REFSPEC));
11 12 13 14 15 16 17 18

	return 0;
}

void test_online_remotes__single_branch(void)
{
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
	git_repository *repo;
19
	git_remote *remote;
20 21 22 23 24 25
	git_strarray refs;
	size_t i, count = 0;

	opts.remote_cb = remote_single_branch;
	opts.checkout_branch = "first-merge";

26
	cl_git_pass(git_clone(&repo, URL, "./single-branch", &opts));
27 28 29 30 31 32 33 34 35
	cl_git_pass(git_reference_list(&refs, repo));

	for (i = 0; i < refs.count; i++) {
		if (!git__prefixcmp(refs.strings[i], "refs/heads/"))
			count++;
	}
	cl_assert_equal_i(1, count);

	git_strarray_free(&refs);
36 37 38 39 40

	cl_git_pass(git_remote_lookup(&remote, repo, "origin"));
	cl_git_pass(git_remote_get_fetch_refspecs(&refs, remote));

	cl_assert_equal_i(1, refs.count);
41
	cl_assert_equal_s(REFSPEC, refs.strings[0]);
42 43 44

	git_strarray_free(&refs);
	git_remote_free(remote);
45 46 47 48 49 50 51 52 53 54
	git_repository_free(repo);
}

void test_online_remotes__restricted_refspecs(void)
{
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
	git_repository *repo;

	opts.remote_cb = remote_single_branch;

55
	cl_git_fail_with(GIT_EINVALIDSPEC, git_clone(&repo, URL, "./restrict-refspec", &opts));
56
}
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

void test_online_remotes__detached_remote_fails_downloading(void)
{
	git_remote *remote;

	cl_git_pass(git_remote_create_detached(&remote, URL));
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
	cl_git_fail(git_remote_download(remote, NULL, NULL));

	git_remote_free(remote);
}

void test_online_remotes__detached_remote_fails_uploading(void)
{
	git_remote *remote;

	cl_git_pass(git_remote_create_detached(&remote, URL));
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
	cl_git_fail(git_remote_upload(remote, NULL, NULL));

	git_remote_free(remote);
}

void test_online_remotes__detached_remote_fails_pushing(void)
{
	git_remote *remote;

	cl_git_pass(git_remote_create_detached(&remote, URL));
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
	cl_git_fail(git_remote_push(remote, NULL, NULL));

	git_remote_free(remote);
}
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

void test_online_remotes__detached_remote_succeeds_ls(void)
{
	const char *refs[] = {
	    "HEAD",
	    "refs/heads/first-merge",
	    "refs/heads/master",
	    "refs/heads/no-parent",
	    "refs/tags/annotated_tag",
	    "refs/tags/annotated_tag^{}",
	    "refs/tags/blob",
	    "refs/tags/commit_tree",
	    "refs/tags/nearly-dangling",
	};
	const git_remote_head **heads;
	git_remote *remote;
	size_t i, j, n;

	cl_git_pass(git_remote_create_detached(&remote, URL));
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
	cl_git_pass(git_remote_ls(&heads, &n, remote));

	cl_assert_equal_sz(n, 9);
	for (i = 0; i < n; i++) {
		char found = false;

		for (j = 0; j < ARRAY_SIZE(refs); j++) {
			if (!strcmp(heads[i]->name, refs[j])) {
				found = true;
				break;
			}
		}

		cl_assert_(found, heads[i]->name);
	}

	git_remote_free(remote);
}