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