branch: fix a leak in setup_tracking
[alt-git.git] / refspec.c
blob57f6c2aaf9bf2ad4d66bcbbc87c7d778383f172c
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "gettext.h"
4 #include "hash.h"
5 #include "hex.h"
6 #include "strvec.h"
7 #include "refs.h"
8 #include "refspec.h"
9 #include "strbuf.h"
11 static struct refspec_item s_tag_refspec = {
12 .force = 0,
13 .pattern = 1,
14 .matching = 0,
15 .exact_sha1 = 0,
16 .negative = 0,
17 .src = "refs/tags/*",
18 .dst = "refs/tags/*",
21 /* See TAG_REFSPEC for the string version */
22 const struct refspec_item *tag_refspec = &s_tag_refspec;
25 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
26 * Returns 1 if successful and 0 if the refspec is invalid.
28 static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch)
30 size_t llen;
31 int is_glob;
32 const char *lhs, *rhs;
33 int flags;
35 is_glob = 0;
37 lhs = refspec;
38 if (*lhs == '+') {
39 item->force = 1;
40 lhs++;
41 } else if (*lhs == '^') {
42 item->negative = 1;
43 lhs++;
46 rhs = strrchr(lhs, ':');
48 /* negative refspecs only have one side */
49 if (item->negative && rhs)
50 return 0;
53 * Before going on, special case ":" (or "+:") as a refspec
54 * for pushing matching refs.
56 if (!fetch && rhs == lhs && rhs[1] == '\0') {
57 item->matching = 1;
58 return 1;
61 if (rhs) {
62 size_t rlen = strlen(++rhs);
63 is_glob = (1 <= rlen && strchr(rhs, '*'));
64 item->dst = xstrndup(rhs, rlen);
65 } else {
66 item->dst = NULL;
69 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
70 if (1 <= llen && memchr(lhs, '*', llen)) {
71 if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
72 return 0;
73 is_glob = 1;
74 } else if (rhs && is_glob) {
75 return 0;
78 item->pattern = is_glob;
79 if (llen == 1 && *lhs == '@')
80 item->src = xstrdup("HEAD");
81 else
82 item->src = xstrndup(lhs, llen);
83 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
85 if (item->negative) {
86 struct object_id unused;
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.
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;
103 /* the other rules below do not apply to negative refspecs */
104 return 1;
107 if (fetch) {
108 struct object_id unused;
110 /* LHS */
111 if (!*item->src)
112 ; /* empty is ok; it means "HEAD" */
113 else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
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 {
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.
136 if (!*item->src)
137 ; /* empty is ok */
138 else if (is_glob) {
139 if (check_refname_format(item->src, flags))
140 return 0;
142 else
143 ; /* anything goes, for now */
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.
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;
162 return 1;
165 int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
167 memset(item, 0, sizeof(*item));
168 return parse_refspec(item, refspec, fetch);
171 void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
172 int fetch)
174 if (!refspec_item_init(item, refspec, fetch))
175 die(_("invalid refspec '%s'"), refspec);
178 void refspec_item_clear(struct refspec_item *item)
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;
188 void refspec_init(struct refspec *rs, int fetch)
190 memset(rs, 0, sizeof(*rs));
191 rs->fetch = fetch;
194 static void refspec_append_nodup(struct refspec *rs, char *refspec)
196 struct refspec_item item;
198 refspec_item_init_or_die(&item, refspec, rs->fetch);
200 ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
201 rs->items[rs->nr++] = item;
203 ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
204 rs->raw[rs->raw_nr++] = refspec;
207 void refspec_append(struct refspec *rs, const char *refspec)
209 refspec_append_nodup(rs, xstrdup(refspec));
212 void refspec_appendf(struct refspec *rs, const char *fmt, ...)
214 va_list ap;
216 va_start(ap, fmt);
217 refspec_append_nodup(rs, xstrvfmt(fmt, ap));
218 va_end(ap);
221 void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
223 int i;
224 for (i = 0; i < nr; i++)
225 refspec_append(rs, refspecs[i]);
228 void refspec_clear(struct refspec *rs)
230 int i;
232 for (i = 0; i < rs->nr; i++)
233 refspec_item_clear(&rs->items[i]);
235 FREE_AND_NULL(rs->items);
236 rs->alloc = 0;
237 rs->nr = 0;
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;
245 rs->fetch = 0;
248 int valid_fetch_refspec(const char *fetch_refspec_str)
250 struct refspec_item refspec;
251 int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH);
252 refspec_item_clear(&refspec);
253 return ret;
256 int valid_remote_name(const char *name)
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;
266 void refspec_ref_prefixes(const struct refspec *rs,
267 struct strvec *ref_prefixes)
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;
274 if (item->exact_sha1 || item->negative)
275 continue;
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;
283 if (!prefix)
284 continue;
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);