]> git.proxmox.com Git - libgit2.git/blame - tests/network/refspecs.c
New upstream version 1.0.0+dfsg.1
[libgit2.git] / tests / network / refspecs.c
CommitLineData
0adfa20a 1#include "clar_libgit2.h"
2#include "refspec.h"
3#include "remote.h"
4
5static void assert_refspec(unsigned int direction, const char *input, bool is_expected_to_be_valid)
6{
7 git_refspec refspec;
8 int error;
9
df705148 10 error = git_refspec__parse(&refspec, input, direction == GIT_DIRECTION_FETCH);
ac3d33df 11 git_refspec__dispose(&refspec);
0adfa20a 12
13 if (is_expected_to_be_valid)
14 cl_assert_equal_i(0, error);
15 else
16 cl_assert_equal_i(GIT_ERROR, error);
17}
18
19void test_network_refspecs__parsing(void)
20{
ac3d33df 21 /* Ported from https://github.com/git/git/blob/abd2bde78bd994166900290434a2048e660dabed/t/t5511-refspec.sh */
0adfa20a 22
df705148
BS
23 assert_refspec(GIT_DIRECTION_PUSH, "", false);
24 assert_refspec(GIT_DIRECTION_PUSH, ":", true);
25 assert_refspec(GIT_DIRECTION_PUSH, "::", false);
26 assert_refspec(GIT_DIRECTION_PUSH, "+:", true);
0adfa20a 27
df705148
BS
28 assert_refspec(GIT_DIRECTION_FETCH, "", true);
29 assert_refspec(GIT_DIRECTION_PUSH, ":", true);
30 assert_refspec(GIT_DIRECTION_FETCH, "::", false);
0adfa20a 31
df705148
BS
32 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*:refs/remotes/frotz/*", true);
33 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*:refs/remotes/frotz", false);
34 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads:refs/remotes/frotz/*", false);
35 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master:refs/remotes/frotz/xyzzy", true);
0adfa20a 36
37 /*
38 * These have invalid LHS, but we do not have a formal "valid sha-1
39 * expression syntax checker" so they are not checked with the current
40 * code. They will be caught downstream anyway, but we may want to
41 * have tighter check later...
42 */
ac3d33df
JK
43 /*assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master::refs/remotes/frotz/xyzzy", false); */
44 /*assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false); */
df705148
BS
45
46 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz/*", true);
47 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz", false);
48 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads:refs/remotes/frotz/*", false);
49 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/master:refs/remotes/frotz/xyzzy", true);
50 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/master::refs/remotes/frotz/xyzzy", false);
51 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false);
52
53 assert_refspec(GIT_DIRECTION_PUSH, "master~1:refs/remotes/frotz/backup", true);
54 assert_refspec(GIT_DIRECTION_FETCH, "master~1:refs/remotes/frotz/backup", false);
55 assert_refspec(GIT_DIRECTION_PUSH, "HEAD~4:refs/remotes/frotz/new", true);
56 assert_refspec(GIT_DIRECTION_FETCH, "HEAD~4:refs/remotes/frotz/new", false);
57
58 assert_refspec(GIT_DIRECTION_PUSH, "HEAD", true);
59 assert_refspec(GIT_DIRECTION_FETCH, "HEAD", true);
60 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/ nitfol", false);
61 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/ nitfol", false);
62
63 assert_refspec(GIT_DIRECTION_PUSH, "HEAD:", false);
64 assert_refspec(GIT_DIRECTION_FETCH, "HEAD:", true);
65 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/ nitfol:", false);
66 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/ nitfol:", false);
67
68 assert_refspec(GIT_DIRECTION_PUSH, ":refs/remotes/frotz/deleteme", true);
69 assert_refspec(GIT_DIRECTION_FETCH, ":refs/remotes/frotz/HEAD-to-me", true);
70 assert_refspec(GIT_DIRECTION_PUSH, ":refs/remotes/frotz/delete me", false);
71 assert_refspec(GIT_DIRECTION_FETCH, ":refs/remotes/frotz/HEAD to me", false);
72
0c9c969a
UG
73 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", true);
74 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", true);
df705148 75
0c9c969a
UG
76 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads*/for-linus:refs/remotes/mine/*", true);
77 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads*/for-linus:refs/remotes/mine/*", true);
df705148
BS
78
79 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/*/for-linus:refs/remotes/mine/*", false);
80 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/*/for-linus:refs/remotes/mine/*", false);
81
0c9c969a
UG
82 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*g*/for-linus:refs/remotes/mine/*", false);
83 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*g*/for-linus:refs/remotes/mine/*", false);
84
df705148
BS
85 assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*", true);
86 assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*", true);
528a4e24
CMN
87
88 assert_refspec(GIT_DIRECTION_FETCH, "master", true);
89 assert_refspec(GIT_DIRECTION_PUSH, "master", true);
f5287fa6
CMN
90
91 assert_refspec(GIT_DIRECTION_FETCH, "refs/pull/*/head:refs/remotes/origin/pr/*", true);
92}
93
4e53c280 94static void assert_valid_transform(const char *refspec, const char *name, const char *result)
f5287fa6
CMN
95{
96 git_refspec spec;
97 git_buf buf = GIT_BUF_INIT;
98
0c9c969a 99 cl_git_pass(git_refspec__parse(&spec, refspec, true));
f5287fa6
CMN
100 cl_git_pass(git_refspec_transform(&buf, &spec, name));
101 cl_assert_equal_s(result, buf.ptr);
102
ac3d33df
JK
103 git_buf_dispose(&buf);
104 git_refspec__dispose(&spec);
f5287fa6
CMN
105}
106
107void test_network_refspecs__transform_mid_star(void)
108{
4e53c280
JG
109 assert_valid_transform("refs/pull/*/head:refs/remotes/origin/pr/*", "refs/pull/23/head", "refs/remotes/origin/pr/23");
110 assert_valid_transform("refs/heads/*:refs/remotes/origin/*", "refs/heads/master", "refs/remotes/origin/master");
111 assert_valid_transform("refs/heads/*:refs/remotes/origin/*", "refs/heads/user/feature", "refs/remotes/origin/user/feature");
112 assert_valid_transform("refs/heads/*:refs/heads/*", "refs/heads/master", "refs/heads/master");
113 assert_valid_transform("refs/heads/*:refs/heads/*", "refs/heads/user/feature", "refs/heads/user/feature");
114 assert_valid_transform("refs/*:refs/*", "refs/heads/master", "refs/heads/master");
115}
116
0c9c969a
UG
117void test_network_refspecs__transform_loosened_star(void)
118{
119 assert_valid_transform("refs/heads/branch-*:refs/remotes/origin/branch-*", "refs/heads/branch-a", "refs/remotes/origin/branch-a");
120 assert_valid_transform("refs/heads/branch-*/head:refs/remotes/origin/branch-*/head", "refs/heads/branch-a/head", "refs/remotes/origin/branch-a/head");
121}
122
123void test_network_refspecs__transform_nested_star(void)
124{
125 assert_valid_transform("refs/heads/x*x/for-linus:refs/remotes/mine/*", "refs/heads/xbranchx/for-linus", "refs/remotes/mine/branch");
126}
127
ac3d33df
JK
128void test_network_refspecs__no_dst(void)
129{
130 assert_valid_transform("refs/heads/master:", "refs/heads/master", "");
131}
132
4e53c280
JG
133static void assert_invalid_transform(const char *refspec, const char *name)
134{
135 git_refspec spec;
136 git_buf buf = GIT_BUF_INIT;
137
138 git_refspec__parse(&spec, refspec, true);
139 cl_git_fail(git_refspec_transform(&buf, &spec, name));
140
ac3d33df
JK
141 git_buf_dispose(&buf);
142 git_refspec__dispose(&spec);
4e53c280
JG
143}
144
145void test_network_refspecs__invalid(void)
146{
147 assert_invalid_transform("refs/heads/*:refs/remotes/origin/*", "master");
148 assert_invalid_transform("refs/heads/*:refs/remotes/origin/*", "refs/headz/master");
149}
150
151static void assert_invalid_rtransform(const char *refspec, const char *name)
152{
153 git_refspec spec;
154 git_buf buf = GIT_BUF_INIT;
155
66a70851 156 cl_git_pass(git_refspec__parse(&spec, refspec, true));
4e53c280
JG
157 cl_git_fail(git_refspec_rtransform(&buf, &spec, name));
158
ac3d33df
JK
159 git_buf_dispose(&buf);
160 git_refspec__dispose(&spec);
4e53c280
JG
161}
162
163void test_network_refspecs__invalid_reverse(void)
164{
165 assert_invalid_rtransform("refs/heads/*:refs/remotes/origin/*", "master");
166 assert_invalid_rtransform("refs/heads/*:refs/remotes/origin/*", "refs/remotes/o/master");
0adfa20a 167}
7cd4ba1b
CMN
168
169void test_network_refspecs__matching(void)
170{
171 git_refspec spec;
172
173 cl_git_pass(git_refspec__parse(&spec, ":", false));
174 cl_assert_equal_s(":", spec.string);
175 cl_assert_equal_s("", spec.src);
176 cl_assert_equal_s("", spec.dst);
534d136d 177
ac3d33df
JK
178 git_refspec__dispose(&spec);
179}
180
181void test_network_refspecs__parse_free(void)
182{
183 git_refspec *spec = NULL;
184
185 cl_git_fail(git_refspec_parse(&spec, "", 0));
186 cl_git_fail(git_refspec_parse(&spec, ":::", 0));
187 cl_git_pass(git_refspec_parse(&spec, "HEAD:", 1));
188
189 cl_assert(spec != NULL);
190 git_refspec_free(spec);
7cd4ba1b 191}