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