]>
Commit | Line | Data |
---|---|---|
b0f6e45d ET |
1 | #include "clar_libgit2.h" |
2 | ||
114f5a6c | 3 | #include "fileops.h" |
b0f6e45d | 4 | #include "fetchhead.h" |
156cfec0 | 5 | #include "../fetchhead/fetchhead_data.h" |
b0f6e45d ET |
6 | #include "git2/clone.h" |
7 | ||
b0f6e45d ET |
8 | #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository" |
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 | ||
1eee631d PS |
38 | static int count_references(void) |
39 | { | |
40 | git_strarray array; | |
41 | int refs; | |
42 | ||
43 | cl_git_pass(git_reference_list(&array, g_repo)); | |
44 | refs = array.count; | |
45 | ||
46 | git_strarray_free(&array); | |
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; |
b0f6e45d ET |
55 | git_buf fetchhead_buf = GIT_BUF_INIT; |
56 | int equals = 0; | |
9c206a22 | 57 | git_strarray array, *active_refs = NULL; |
b0f6e45d | 58 | |
209425ce | 59 | cl_git_pass(git_remote_lookup(&remote, g_repo, "origin")); |
35a8a8c5 | 60 | fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO; |
b0f6e45d | 61 | |
4330ab26 | 62 | if(fetchspec != NULL) { |
9c206a22 CMN |
63 | array.count = 1; |
64 | array.strings = (char **) &fetchspec; | |
65 | active_refs = &array; | |
4330ab26 | 66 | } |
b0f6e45d | 67 | |
35a8a8c5 | 68 | cl_git_pass(git_remote_fetch(remote, active_refs, &fetch_opts, NULL)); |
b0f6e45d ET |
69 | git_remote_free(remote); |
70 | ||
a7f125cd | 71 | cl_git_pass(git_futils_readbuffer(&fetchhead_buf, "./foo/.git/FETCH_HEAD")); |
b0f6e45d ET |
72 | |
73 | equals = (strcmp(fetchhead_buf.ptr, expected_fetchhead) == 0); | |
74 | ||
ac3d33df | 75 | git_buf_dispose(&fetchhead_buf); |
b0f6e45d ET |
76 | |
77 | cl_assert(equals); | |
78 | } | |
79 | ||
6443eaf2 | 80 | void test_online_fetchhead__wildcard_spec(void) |
b0f6e45d ET |
81 | { |
82 | fetchhead_test_clone(); | |
d908351a L |
83 | fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA2); |
84 | cl_git_pass(git_tag_delete(g_repo, "annotated_tag")); | |
85 | cl_git_pass(git_tag_delete(g_repo, "blob")); | |
86 | cl_git_pass(git_tag_delete(g_repo, "commit_tree")); | |
87 | cl_git_pass(git_tag_delete(g_repo, "nearly-dangling")); | |
b0f6e45d ET |
88 | fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA); |
89 | } | |
90 | ||
6443eaf2 | 91 | void test_online_fetchhead__explicit_spec(void) |
b0f6e45d ET |
92 | { |
93 | fetchhead_test_clone(); | |
94 | fetchhead_test_fetch("refs/heads/first-merge:refs/remotes/origin/first-merge", FETCH_HEAD_EXPLICIT_DATA); | |
95 | } | |
96 | ||
6443eaf2 | 97 | void test_online_fetchhead__no_merges(void) |
b0f6e45d ET |
98 | { |
99 | git_config *config; | |
100 | ||
101 | fetchhead_test_clone(); | |
102 | ||
103 | cl_git_pass(git_repository_config(&config, g_repo)); | |
48bde2f1 CMN |
104 | cl_git_pass(git_config_delete_entry(config, "branch.master.remote")); |
105 | cl_git_pass(git_config_delete_entry(config, "branch.master.merge")); | |
169fa384 | 106 | git_config_free(config); |
b0f6e45d | 107 | |
d908351a L |
108 | fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA2); |
109 | cl_git_pass(git_tag_delete(g_repo, "annotated_tag")); | |
110 | cl_git_pass(git_tag_delete(g_repo, "blob")); | |
111 | cl_git_pass(git_tag_delete(g_repo, "commit_tree")); | |
112 | cl_git_pass(git_tag_delete(g_repo, "nearly-dangling")); | |
b0f6e45d | 113 | fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA); |
d908351a L |
114 | cl_git_pass(git_tag_delete(g_repo, "commit_tree")); |
115 | fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA3); | |
b0f6e45d | 116 | } |
1eee631d PS |
117 | |
118 | void test_online_fetchhead__explicit_dst_refspec_creates_branch(void) | |
119 | { | |
120 | git_reference *ref; | |
121 | int refs; | |
122 | ||
123 | fetchhead_test_clone(); | |
124 | refs = count_references(); | |
125 | fetchhead_test_fetch("refs/heads/first-merge:refs/heads/explicit-refspec", FETCH_HEAD_EXPLICIT_DATA); | |
126 | ||
127 | cl_git_pass(git_branch_lookup(&ref, g_repo, "explicit-refspec", GIT_BRANCH_ALL)); | |
128 | cl_assert_equal_i(refs + 1, count_references()); | |
30a876cd PS |
129 | |
130 | git_reference_free(ref); | |
1eee631d PS |
131 | } |
132 | ||
133 | void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void) | |
134 | { | |
135 | git_reference *ref; | |
136 | int refs; | |
137 | ||
138 | fetchhead_test_clone(); | |
139 | refs = count_references(); | |
140 | ||
141 | fetchhead_test_fetch("refs/heads/first-merge", FETCH_HEAD_EXPLICIT_DATA); | |
142 | cl_git_fail(git_branch_lookup(&ref, g_repo, "first-merge", GIT_BRANCH_ALL)); | |
143 | ||
144 | cl_assert_equal_i(refs, count_references()); | |
145 | } | |
146 | ||
147 | void test_online_fetchhead__colon_only_dst_refspec_creates_no_branch(void) | |
148 | { | |
149 | int refs; | |
150 | ||
151 | fetchhead_test_clone(); | |
152 | refs = count_references(); | |
153 | fetchhead_test_fetch("refs/heads/first-merge:", FETCH_HEAD_EXPLICIT_DATA); | |
154 | ||
155 | cl_assert_equal_i(refs, count_references()); | |
156 | } |