cocci: remove 'unused.cocci'
[git.git] / refspec.c
blob7b5c305514d2c251fdcc750186855e5093e42635
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "strvec.h"
6 #include "refs.h"
7 #include "refspec.h"
9 static struct refspec_item s_tag_refspec = {
10 .force = 0,
11 .pattern = 1,
12 .matching = 0,
13 .exact_sha1 = 0,
14 .negative = 0,
15 .src = "refs/tags/*",
16 .dst = "refs/tags/*",
19 /* See TAG_REFSPEC for the string version */
20 const struct refspec_item *tag_refspec = &s_tag_refspec;
23 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
24 * Returns 1 if successful and 0 if the refspec is invalid.
26 static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch)
28 size_t llen;
29 int is_glob;
30 const char *lhs, *rhs;
31 int flags;
33 is_glob = 0;
35 lhs = refspec;
36 if (*lhs == '+') {
37 item->force = 1;
38 lhs++;
39 } else if (*lhs == '^') {
40 item->negative = 1;
41 lhs++;
44 rhs = strrchr(lhs, ':');
46 /* negative refspecs only have one side */
47 if (item->negative && rhs)
48 return 0;
51 * Before going on, special case ":" (or "+:") as a refspec
52 * for pushing matching refs.
54 if (!fetch && rhs == lhs && rhs[1] == '\0') {
55 item->matching = 1;
56 return 1;
59 if (rhs) {
60 size_t rlen = strlen(++rhs);
61 is_glob = (1 <= rlen && strchr(rhs, '*'));
62 item->dst = xstrndup(rhs, rlen);
63 } else {
64 item->dst = NULL;
67 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
68 if (1 <= llen && memchr(lhs, '*', llen)) {
69 if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
70 return 0;
71 is_glob = 1;
72 } else if (rhs && is_glob) {
73 return 0;
76 item->pattern = is_glob;
77 if (llen == 1 && *lhs == '@')
78 item->src = xstrdup("HEAD");
79 else
80 item->src = xstrndup(lhs, llen);
81 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
83 if (item->negative) {
84 struct object_id unused;
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.
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;
101 /* the other rules below do not apply to negative refspecs */
102 return 1;
105 if (fetch) {
106 struct object_id unused;
108 /* LHS */
109 if (!*item->src)
110 ; /* empty is ok; it means "HEAD" */
111 else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
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 {
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.
134 if (!*item->src)
135 ; /* empty is ok */
136 else if (is_glob) {
137 if (check_refname_format(item->src, flags))
138 return 0;
140 else
141 ; /* anything goes, for now */
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.
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;
160 return 1;
163 int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
165 memset(item, 0, sizeof(*item));
166 return parse_refspec(item, refspec, fetch);
169 void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
170 int fetch)
172 if (!refspec_item_init(item, refspec, fetch))
173 die(_("invalid refspec '%s'"), refspec);
176 void refspec_item_clear(struct refspec_item *item)
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;
186 void refspec_init(struct refspec *rs, int fetch)
188 memset(rs, 0, sizeof(*rs));
189 rs->fetch = fetch;
192 static void refspec_append_nodup(struct refspec *rs, char *refspec)
194 struct refspec_item item;
196 refspec_item_init_or_die(&item, refspec, rs->fetch);
198 ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
199 rs->items[rs->nr++] = item;
201 ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
202 rs->raw[rs->raw_nr++] = refspec;
205 void refspec_append(struct refspec *rs, const char *refspec)
207 refspec_append_nodup(rs, xstrdup(refspec));
210 void refspec_appendf(struct refspec *rs, const char *fmt, ...)
212 va_list ap;
214 va_start(ap, fmt);
215 refspec_append_nodup(rs, xstrvfmt(fmt, ap));
216 va_end(ap);
219 void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
221 int i;
222 for (i = 0; i < nr; i++)
223 refspec_append(rs, refspecs[i]);
226 void refspec_clear(struct refspec *rs)
228 int i;
230 for (i = 0; i < rs->nr; i++)
231 refspec_item_clear(&rs->items[i]);
233 FREE_AND_NULL(rs->items);
234 rs->alloc = 0;
235 rs->nr = 0;
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;
243 rs->fetch = 0;
246 int valid_fetch_refspec(const char *fetch_refspec_str)
248 struct refspec_item refspec;
249 int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH);
250 refspec_item_clear(&refspec);
251 return ret;
254 int valid_remote_name(const char *name)
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;
264 void refspec_ref_prefixes(const struct refspec *rs,
265 struct strvec *ref_prefixes)
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;
272 if (item->exact_sha1 || item->negative)
273 continue;
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;
281 if (!prefix)
282 continue;
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);