]> git.ipfire.org Git - thirdparty/git.git/blame - refspec.c
cmake: also build unit tests
[thirdparty/git.git] / refspec.c
CommitLineData
36bf1958
EN
1#include "git-compat-util.h"
2#include "alloc.h"
a6dc3d36 3#include "gettext.h"
d1cbe1e6 4#include "hash.h"
41771fa4 5#include "hex.h"
dbbcd44f 6#include "strvec.h"
ec0cb496
BW
7#include "refs.h"
8#include "refspec.h"
d4a4f929 9#include "strbuf.h"
ec0cb496 10
0ad4a5ff 11static struct refspec_item s_tag_refspec = {
b3454e2d
ÆAB
12 .force = 0,
13 .pattern = 1,
14 .matching = 0,
15 .exact_sha1 = 0,
16 .negative = 0,
17 .src = "refs/tags/*",
18 .dst = "refs/tags/*",
ec0cb496
BW
19};
20
21/* See TAG_REFSPEC for the string version */
0ad4a5ff 22const struct refspec_item *tag_refspec = &s_tag_refspec;
ec0cb496 23
3eec3700
BW
24/*
25 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
26 * Returns 1 if successful and 0 if the refspec is invalid.
27 */
28static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch)
ec0cb496 29{
3eec3700
BW
30 size_t llen;
31 int is_glob;
32 const char *lhs, *rhs;
33 int flags;
ec0cb496 34
3eec3700 35 is_glob = 0;
ec0cb496 36
3eec3700
BW
37 lhs = refspec;
38 if (*lhs == '+') {
39 item->force = 1;
40 lhs++;
c0192df6
JK
41 } else if (*lhs == '^') {
42 item->negative = 1;
43 lhs++;
3eec3700 44 }
ec0cb496 45
3eec3700 46 rhs = strrchr(lhs, ':');
ec0cb496 47
c0192df6
JK
48 /* negative refspecs only have one side */
49 if (item->negative && rhs)
50 return 0;
51
3eec3700
BW
52 /*
53 * Before going on, special case ":" (or "+:") as a refspec
54 * for pushing matching refs.
55 */
56 if (!fetch && rhs == lhs && rhs[1] == '\0') {
57 item->matching = 1;
58 return 1;
59 }
ec0cb496 60
3eec3700
BW
61 if (rhs) {
62 size_t rlen = strlen(++rhs);
63 is_glob = (1 <= rlen && strchr(rhs, '*'));
64 item->dst = xstrndup(rhs, rlen);
c3072c6e
JH
65 } else {
66 item->dst = NULL;
3eec3700
BW
67 }
68
69 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
70 if (1 <= llen && memchr(lhs, '*', llen)) {
c0192df6 71 if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
3eec3700
BW
72 return 0;
73 is_glob = 1;
74 } else if (rhs && is_glob) {
75 return 0;
76 }
77
78 item->pattern = is_glob;
374fbaef
FC
79 if (llen == 1 && *lhs == '@')
80 item->src = xstrdup("HEAD");
81 else
82 item->src = xstrndup(lhs, llen);
3eec3700
BW
83 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
84
c0192df6
JK
85 if (item->negative) {
86 struct object_id unused;
87
88 /*
89 * Negative refspecs only have a LHS, which indicates a ref
90 * (or pattern of refs) to exclude from other matches. This
91 * can either be a simple ref, or a glob pattern. Exact sha1
92 * match is not currently supported.
93 */
94 if (!*item->src)
95 return 0; /* negative refspecs must not be empty */
96 else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
97 return 0; /* negative refpsecs cannot be exact sha1 */
98 else if (!check_refname_format(item->src, flags))
99 ; /* valid looking ref is ok */
100 else
101 return 0;
102
103 /* the other rules below do not apply to negative refspecs */
104 return 1;
105 }
106
3eec3700
BW
107 if (fetch) {
108 struct object_id unused;
109
110 /* LHS */
111 if (!*item->src)
112 ; /* empty is ok; it means "HEAD" */
b8d45d03 113 else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
3eec3700
BW
114 item->exact_sha1 = 1; /* ok */
115 else if (!check_refname_format(item->src, flags))
116 ; /* valid looking ref is ok */
117 else
118 return 0;
119 /* RHS */
120 if (!item->dst)
121 ; /* missing is ok; it is the same as empty */
122 else if (!*item->dst)
123 ; /* empty is ok; it means "do not store" */
124 else if (!check_refname_format(item->dst, flags))
125 ; /* valid looking ref is ok */
126 else
127 return 0;
128 } else {
ec0cb496 129 /*
3eec3700
BW
130 * LHS
131 * - empty is allowed; it means delete.
132 * - when wildcarded, it must be a valid looking ref.
133 * - otherwise, it must be an extended SHA-1, but
134 * there is no existing way to validate this.
ec0cb496 135 */
3eec3700
BW
136 if (!*item->src)
137 ; /* empty is ok */
138 else if (is_glob) {
139 if (check_refname_format(item->src, flags))
140 return 0;
ec0cb496 141 }
3eec3700
BW
142 else
143 ; /* anything goes, for now */
144 /*
145 * RHS
146 * - missing is allowed, but LHS then must be a
147 * valid looking ref.
148 * - empty is not allowed.
149 * - otherwise it must be a valid looking ref.
150 */
151 if (!item->dst) {
152 if (check_refname_format(item->src, flags))
153 return 0;
154 } else if (!*item->dst) {
155 return 0;
156 } else {
157 if (check_refname_format(item->dst, flags))
158 return 0;
ec0cb496 159 }
3eec3700 160 }
ec0cb496 161
3eec3700
BW
162 return 1;
163}
ec0cb496 164
c495fd3d 165int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
6d4c0578
BW
166{
167 memset(item, 0, sizeof(*item));
c495fd3d
ÆAB
168 return parse_refspec(item, refspec, fetch);
169}
6d4c0578 170
c495fd3d
ÆAB
171void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
172 int fetch)
173{
174 if (!refspec_item_init(item, refspec, fetch))
1b5e07bb 175 die(_("invalid refspec '%s'"), refspec);
6d4c0578
BW
176}
177
178void refspec_item_clear(struct refspec_item *item)
179{
180 FREE_AND_NULL(item->src);
181 FREE_AND_NULL(item->dst);
182 item->force = 0;
183 item->pattern = 0;
184 item->matching = 0;
185 item->exact_sha1 = 0;
186}
187
188void refspec_init(struct refspec *rs, int fetch)
189{
190 memset(rs, 0, sizeof(*rs));
191 rs->fetch = fetch;
192}
193
1af8b8c0 194static void refspec_append_nodup(struct refspec *rs, char *refspec)
6d4c0578
BW
195{
196 struct refspec_item item;
197
dc064221 198 refspec_item_init_or_die(&item, refspec, rs->fetch);
6d4c0578
BW
199
200 ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
201 rs->items[rs->nr++] = item;
202
203 ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
1af8b8c0
RS
204 rs->raw[rs->raw_nr++] = refspec;
205}
206
207void refspec_append(struct refspec *rs, const char *refspec)
208{
209 refspec_append_nodup(rs, xstrdup(refspec));
210}
211
212void refspec_appendf(struct refspec *rs, const char *fmt, ...)
213{
214 va_list ap;
215
216 va_start(ap, fmt);
217 refspec_append_nodup(rs, xstrvfmt(fmt, ap));
218 va_end(ap);
6d4c0578
BW
219}
220
221void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
222{
223 int i;
224 for (i = 0; i < nr; i++)
225 refspec_append(rs, refspecs[i]);
226}
227
228void refspec_clear(struct refspec *rs)
229{
230 int i;
231
232 for (i = 0; i < rs->nr; i++)
233 refspec_item_clear(&rs->items[i]);
234
235 FREE_AND_NULL(rs->items);
236 rs->alloc = 0;
237 rs->nr = 0;
238
239 for (i = 0; i < rs->raw_nr; i++)
240 free((char *)rs->raw[i]);
241 FREE_AND_NULL(rs->raw);
242 rs->raw_alloc = 0;
243 rs->raw_nr = 0;
244
245 rs->fetch = 0;
246}
c8fa9efe
BW
247
248int valid_fetch_refspec(const char *fetch_refspec_str)
249{
250 struct refspec_item refspec;
7865d157 251 int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH);
c8fa9efe
BW
252 refspec_item_clear(&refspec);
253 return ret;
254}
6373cb59 255
f2c6fda8
SB
256int valid_remote_name(const char *name)
257{
258 int result;
259 struct strbuf refspec = STRBUF_INIT;
260 strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name);
261 result = valid_fetch_refspec(refspec.buf);
262 strbuf_release(&refspec);
263 return result;
264}
265
6373cb59 266void refspec_ref_prefixes(const struct refspec *rs,
c972bf4c 267 struct strvec *ref_prefixes)
6373cb59
BW
268{
269 int i;
270 for (i = 0; i < rs->nr; i++) {
271 const struct refspec_item *item = &rs->items[i];
272 const char *prefix = NULL;
273
c0192df6 274 if (item->exact_sha1 || item->negative)
6c301adb 275 continue;
6373cb59
BW
276 if (rs->fetch == REFSPEC_FETCH)
277 prefix = item->src;
278 else if (item->dst)
279 prefix = item->dst;
280 else if (item->src && !item->exact_sha1)
281 prefix = item->src;
282
bfded875
FC
283 if (!prefix)
284 continue;
285
286 if (item->pattern) {
287 const char *glob = strchr(prefix, '*');
288 strvec_pushf(ref_prefixes, "%.*s",
289 (int)(glob - prefix),
290 prefix);
291 } else {
292 expand_ref_prefix(ref_prefixes, prefix);
6373cb59
BW
293 }
294 }
295}