]>
Commit | Line | Data |
---|---|---|
b0f6e45d ET |
1 | #include "clar_libgit2.h" |
2 | ||
22a2d3d5 | 3 | #include "futils.h" |
b0f6e45d | 4 | #include "fetchhead.h" |
156cfec0 | 5 | #include "../fetchhead/fetchhead_data.h" |
b0f6e45d ET |
6 | #include "git2/clone.h" |
7 | ||
e579e0f7 | 8 | #define LIVE_REPO_URL "https://github.com/libgit2/TestGitRepository" |
b0f6e45d ET |
9 | |
10 | static git_repository *g_repo; | |
18b2d560 | 11 | static git_clone_options g_options; |
b0f6e45d | 12 | |
6443eaf2 | 13 | void test_online_fetchhead__initialize(void) |
b0f6e45d | 14 | { |
8f0104ec | 15 | git_fetch_options dummy_fetch = GIT_FETCH_OPTIONS_INIT; |
b0f6e45d | 16 | g_repo = NULL; |
18b2d560 BS |
17 | |
18 | memset(&g_options, 0, sizeof(git_clone_options)); | |
19 | g_options.version = GIT_CLONE_OPTIONS_VERSION; | |
8f0104ec | 20 | g_options.fetch_opts = dummy_fetch; |
44f36f6e BS |
21 | } |
22 | ||
6443eaf2 | 23 | void test_online_fetchhead__cleanup(void) |
b0f6e45d | 24 | { |
9094d30b | 25 | if (g_repo) { |
b0f6e45d | 26 | git_repository_free(g_repo); |
9094d30b SC |
27 | g_repo = NULL; |
28 | } | |
29 | ||
7761ce21 | 30 | cl_fixture_cleanup("./foo"); |
b0f6e45d ET |
31 | } |
32 | ||
b0f6e45d ET |
33 | static void fetchhead_test_clone(void) |
34 | { | |
b412d563 | 35 | cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options)); |
b0f6e45d ET |
36 | } |
37 | ||
22a2d3d5 | 38 | static size_t count_references(void) |
1eee631d PS |
39 | { |
40 | git_strarray array; | |
22a2d3d5 | 41 | size_t refs; |
1eee631d PS |
42 | |
43 | cl_git_pass(git_reference_list(&array, g_repo)); | |
44 | refs = array.count; | |
45 | ||
22a2d3d5 | 46 | git_strarray_dispose(&array); |
1eee631d PS |
47 | |
48 | return refs; | |
49 | } | |
50 | ||
b0f6e45d ET |
51 | static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead) |
52 | { | |
53 | git_remote *remote; | |
35a8a8c5 | 54 | git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT; |
e579e0f7 | 55 | git_str fetchhead_buf = GIT_STR_INIT; |
9c206a22 | 56 | git_strarray array, *active_refs = NULL; |
b0f6e45d | 57 | |
209425ce | 58 | cl_git_pass(git_remote_lookup(&remote, g_repo, "origin")); |
35a8a8c5 | 59 | fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO; |
b0f6e45d | 60 | |
4330ab26 | 61 | if(fetchspec != NULL) { |
9c206a22 CMN |
62 | array.count = 1; |
63 | array.strings = (char **) &fetchspec; | |
64 | active_refs = &array; | |
4330ab26 | 65 | } |
b0f6e45d | 66 | |
35a8a8c5 | 67 | cl_git_pass(git_remote_fetch(remote, active_refs, &fetch_opts, NULL)); |
b0f6e45d ET |
68 | git_remote_free(remote); |
69 | ||
a7f125cd | 70 | cl_git_pass(git_futils_readbuffer(&fetchhead_buf, "./foo/.git/FETCH_HEAD")); |
b0f6e45d | 71 | |
e579e0f7 MB |
72 | cl_assert_equal_s(fetchhead_buf.ptr, expected_fetchhead); |
73 | git_str_dispose(&fetchhead_buf); | |
b0f6e45d ET |
74 | } |
75 | ||
6443eaf2 | 76 | void test_online_fetchhead__wildcard_spec(void) |
b0f6e45d ET |
77 | { |
78 | fetchhead_test_clone(); | |
d908351a L |
79 | fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA2); |
80 | cl_git_pass(git_tag_delete(g_repo, "annotated_tag")); | |
81 | cl_git_pass(git_tag_delete(g_repo, "blob")); | |
82 | cl_git_pass(git_tag_delete(g_repo, "commit_tree")); | |
83 | cl_git_pass(git_tag_delete(g_repo, "nearly-dangling")); | |
b0f6e45d ET |
84 | fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA); |
85 | } | |
86 | ||
6443eaf2 | 87 | void test_online_fetchhead__explicit_spec(void) |
b0f6e45d ET |
88 | { |
89 | fetchhead_test_clone(); | |
90 | fetchhead_test_fetch("refs/heads/first-merge:refs/remotes/origin/first-merge", FETCH_HEAD_EXPLICIT_DATA); | |
91 | } | |
92 | ||
6443eaf2 | 93 | void test_online_fetchhead__no_merges(void) |
b0f6e45d ET |
94 | { |
95 | git_config *config; | |
96 | ||
97 | fetchhead_test_clone(); | |
98 | ||
99 | cl_git_pass(git_repository_config(&config, g_repo)); | |
48bde2f1 CMN |
100 | cl_git_pass(git_config_delete_entry(config, "branch.master.remote")); |
101 | cl_git_pass(git_config_delete_entry(config, "branch.master.merge")); | |
169fa384 | 102 | git_config_free(config); |
b0f6e45d | 103 | |
d908351a L |
104 | fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA2); |
105 | cl_git_pass(git_tag_delete(g_repo, "annotated_tag")); | |
106 | cl_git_pass(git_tag_delete(g_repo, "blob")); | |
107 | cl_git_pass(git_tag_delete(g_repo, "commit_tree")); | |
108 | cl_git_pass(git_tag_delete(g_repo, "nearly-dangling")); | |
b0f6e45d | 109 | fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA); |
d908351a L |
110 | cl_git_pass(git_tag_delete(g_repo, "commit_tree")); |
111 | fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA3); | |
b0f6e45d | 112 | } |
1eee631d PS |
113 | |
114 | void test_online_fetchhead__explicit_dst_refspec_creates_branch(void) | |
115 | { | |
116 | git_reference *ref; | |
22a2d3d5 | 117 | size_t refs; |
1eee631d PS |
118 | |
119 | fetchhead_test_clone(); | |
120 | refs = count_references(); | |
121 | fetchhead_test_fetch("refs/heads/first-merge:refs/heads/explicit-refspec", FETCH_HEAD_EXPLICIT_DATA); | |
122 | ||
123 | cl_git_pass(git_branch_lookup(&ref, g_repo, "explicit-refspec", GIT_BRANCH_ALL)); | |
124 | cl_assert_equal_i(refs + 1, count_references()); | |
30a876cd PS |
125 | |
126 | git_reference_free(ref); | |
1eee631d PS |
127 | } |
128 | ||
129 | void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void) | |
130 | { | |
131 | git_reference *ref; | |
22a2d3d5 | 132 | size_t refs; |
1eee631d PS |
133 | |
134 | fetchhead_test_clone(); | |
135 | refs = count_references(); | |
136 | ||
137 | fetchhead_test_fetch("refs/heads/first-merge", FETCH_HEAD_EXPLICIT_DATA); | |
138 | cl_git_fail(git_branch_lookup(&ref, g_repo, "first-merge", GIT_BRANCH_ALL)); | |
139 | ||
140 | cl_assert_equal_i(refs, count_references()); | |
141 | } | |
142 | ||
143 | void test_online_fetchhead__colon_only_dst_refspec_creates_no_branch(void) | |
144 | { | |
22a2d3d5 | 145 | size_t refs; |
1eee631d PS |
146 | |
147 | fetchhead_test_clone(); | |
148 | refs = count_references(); | |
149 | fetchhead_test_fetch("refs/heads/first-merge:", FETCH_HEAD_EXPLICIT_DATA); | |
150 | ||
151 | cl_assert_equal_i(refs, count_references()); | |
152 | } | |
22a2d3d5 UG |
153 | |
154 | void test_online_fetchhead__creds_get_stripped(void) | |
155 | { | |
e579e0f7 | 156 | git_str buf = GIT_STR_INIT; |
22a2d3d5 UG |
157 | git_remote *remote; |
158 | ||
159 | cl_git_pass(git_repository_init(&g_repo, "./foo", 0)); | |
160 | cl_git_pass(git_remote_create_anonymous(&remote, g_repo, "https://foo:bar@github.com/libgit2/TestGitRepository")); | |
161 | cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL)); | |
162 | ||
163 | cl_git_pass(git_futils_readbuffer(&buf, "./foo/.git/FETCH_HEAD")); | |
164 | cl_assert_equal_s(buf.ptr, | |
165 | "49322bb17d3acc9146f98c97d078513228bbf3c0\t\thttps://github.com/libgit2/TestGitRepository\n"); | |
166 | ||
167 | git_remote_free(remote); | |
e579e0f7 | 168 | git_str_dispose(&buf); |
22a2d3d5 | 169 | } |