]> git.proxmox.com Git - libgit2.git/blob - tests/online/remotes.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / online / remotes.c
1 #include "clar_libgit2.h"
2
3 #define URL "https://github.com/libgit2/TestGitRepository"
4 #define REFSPEC "refs/heads/first-merge:refs/remotes/origin/first-merge"
5
6 static int remote_single_branch(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
7 {
8 GIT_UNUSED(payload);
9
10 cl_git_pass(git_remote_create_with_fetchspec(out, repo, name, url, REFSPEC));
11
12 return 0;
13 }
14
15 void test_online_remotes__single_branch(void)
16 {
17 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
18 git_repository *repo;
19 git_remote *remote;
20 git_strarray refs;
21 size_t i, count = 0;
22
23 opts.remote_cb = remote_single_branch;
24 opts.checkout_branch = "first-merge";
25
26 cl_git_pass(git_clone(&repo, URL, "./single-branch", &opts));
27 cl_git_pass(git_reference_list(&refs, repo));
28
29 for (i = 0; i < refs.count; i++) {
30 if (!git__prefixcmp(refs.strings[i], "refs/heads/"))
31 count++;
32 }
33 cl_assert_equal_i(1, count);
34
35 git_strarray_dispose(&refs);
36
37 cl_git_pass(git_remote_lookup(&remote, repo, "origin"));
38 cl_git_pass(git_remote_get_fetch_refspecs(&refs, remote));
39
40 cl_assert_equal_i(1, refs.count);
41 cl_assert_equal_s(REFSPEC, refs.strings[0]);
42
43 git_strarray_dispose(&refs);
44 git_remote_free(remote);
45 git_repository_free(repo);
46 }
47
48 void test_online_remotes__restricted_refspecs(void)
49 {
50 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
51 git_repository *repo;
52
53 opts.remote_cb = remote_single_branch;
54
55 cl_git_fail_with(GIT_EINVALIDSPEC, git_clone(&repo, URL, "./restrict-refspec", &opts));
56 }
57
58 void test_online_remotes__detached_remote_fails_downloading(void)
59 {
60 git_remote *remote;
61
62 cl_git_pass(git_remote_create_detached(&remote, URL));
63 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
64 cl_git_fail(git_remote_download(remote, NULL, NULL));
65
66 git_remote_free(remote);
67 }
68
69 void test_online_remotes__detached_remote_fails_uploading(void)
70 {
71 git_remote *remote;
72
73 cl_git_pass(git_remote_create_detached(&remote, URL));
74 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
75 cl_git_fail(git_remote_upload(remote, NULL, NULL));
76
77 git_remote_free(remote);
78 }
79
80 void test_online_remotes__detached_remote_fails_pushing(void)
81 {
82 git_remote *remote;
83
84 cl_git_pass(git_remote_create_detached(&remote, URL));
85 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
86 cl_git_fail(git_remote_push(remote, NULL, NULL));
87
88 git_remote_free(remote);
89 }
90
91 void test_online_remotes__detached_remote_succeeds_ls(void)
92 {
93 const char *refs[] = {
94 "HEAD",
95 "refs/heads/first-merge",
96 "refs/heads/master",
97 "refs/heads/no-parent",
98 "refs/tags/annotated_tag",
99 "refs/tags/annotated_tag^{}",
100 "refs/tags/blob",
101 "refs/tags/commit_tree",
102 "refs/tags/nearly-dangling",
103 };
104 const git_remote_head **heads;
105 git_remote *remote;
106 size_t i, j, n;
107
108 cl_git_pass(git_remote_create_detached(&remote, URL));
109 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
110 cl_git_pass(git_remote_ls(&heads, &n, remote));
111
112 cl_assert_equal_sz(n, 9);
113 for (i = 0; i < n; i++) {
114 char found = false;
115
116 for (j = 0; j < ARRAY_SIZE(refs); j++) {
117 if (!strcmp(heads[i]->name, refs[j])) {
118 found = true;
119 break;
120 }
121 }
122
123 cl_assert_(found, heads[i]->name);
124 }
125
126 git_remote_free(remote);
127 }