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