checkout: refer to other-worktree branch, not ref
[git/gitster.git] / refspec.c
blobc3cf003443dd362390185fe471ed889e66b8dd1a
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "hash.h"
6 #include "hex.h"
7 #include "strvec.h"
8 #include "refs.h"
9 #include "refspec.h"
10 #include "strbuf.h"
13 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
14 * Returns 1 if successful and 0 if the refspec is invalid.
16 static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch)
18 size_t llen;
19 int is_glob;
20 const char *lhs, *rhs;
21 int flags;
23 is_glob = 0;
25 lhs = refspec;
26 if (*lhs == '+') {
27 item->force = 1;
28 lhs++;
29 } else if (*lhs == '^') {
30 item->negative = 1;
31 lhs++;
34 rhs = strrchr(lhs, ':');
36 /* negative refspecs only have one side */
37 if (item->negative && rhs)
38 return 0;
41 * Before going on, special case ":" (or "+:") as a refspec
42 * for pushing matching refs.
44 if (!fetch && rhs == lhs && rhs[1] == '\0') {
45 item->matching = 1;
46 return 1;
49 if (rhs) {
50 size_t rlen = strlen(++rhs);
51 is_glob = (1 <= rlen && strchr(rhs, '*'));
52 item->dst = xstrndup(rhs, rlen);
53 } else {
54 item->dst = NULL;
57 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
58 if (1 <= llen && memchr(lhs, '*', llen)) {
59 if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
60 return 0;
61 is_glob = 1;
62 } else if (rhs && is_glob) {
63 return 0;
66 item->pattern = is_glob;
67 if (llen == 1 && *lhs == '@')
68 item->src = xstrdup("HEAD");
69 else
70 item->src = xstrndup(lhs, llen);
71 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
73 if (item->negative) {
74 struct object_id unused;
77 * Negative refspecs only have a LHS, which indicates a ref
78 * (or pattern of refs) to exclude from other matches. This
79 * can either be a simple ref, or a glob pattern. Exact sha1
80 * match is not currently supported.
82 if (!*item->src)
83 return 0; /* negative refspecs must not be empty */
84 else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
85 return 0; /* negative refpsecs cannot be exact sha1 */
86 else if (!check_refname_format(item->src, flags))
87 ; /* valid looking ref is ok */
88 else
89 return 0;
91 /* the other rules below do not apply to negative refspecs */
92 return 1;
95 if (fetch) {
96 struct object_id unused;
98 /* LHS */
99 if (!*item->src)
100 ; /* empty is ok; it means "HEAD" */
101 else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
102 item->exact_sha1 = 1; /* ok */
103 else if (!check_refname_format(item->src, flags))
104 ; /* valid looking ref is ok */
105 else
106 return 0;
107 /* RHS */
108 if (!item->dst)
109 ; /* missing is ok; it is the same as empty */
110 else if (!*item->dst)
111 ; /* empty is ok; it means "do not store" */
112 else if (!check_refname_format(item->dst, flags))
113 ; /* valid looking ref is ok */
114 else
115 return 0;
116 } else {
118 * LHS
119 * - empty is allowed; it means delete.
120 * - when wildcarded, it must be a valid looking ref.
121 * - otherwise, it must be an extended SHA-1, but
122 * there is no existing way to validate this.
124 if (!*item->src)
125 ; /* empty is ok */
126 else if (is_glob) {
127 if (check_refname_format(item->src, flags))
128 return 0;
130 else
131 ; /* anything goes, for now */
133 * RHS
134 * - missing is allowed, but LHS then must be a
135 * valid looking ref.
136 * - empty is not allowed.
137 * - otherwise it must be a valid looking ref.
139 if (!item->dst) {
140 if (check_refname_format(item->src, flags))
141 return 0;
142 } else if (!*item->dst) {
143 return 0;
144 } else {
145 if (check_refname_format(item->dst, flags))
146 return 0;
150 return 1;
153 int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
155 memset(item, 0, sizeof(*item));
156 return parse_refspec(item, refspec, fetch);
159 void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
160 int fetch)
162 if (!refspec_item_init(item, refspec, fetch))
163 die(_("invalid refspec '%s'"), refspec);
166 void refspec_item_clear(struct refspec_item *item)
168 FREE_AND_NULL(item->src);
169 FREE_AND_NULL(item->dst);
170 item->force = 0;
171 item->pattern = 0;
172 item->matching = 0;
173 item->exact_sha1 = 0;
176 void refspec_init(struct refspec *rs, int fetch)
178 memset(rs, 0, sizeof(*rs));
179 rs->fetch = fetch;
182 static void refspec_append_nodup(struct refspec *rs, char *refspec)
184 struct refspec_item item;
186 refspec_item_init_or_die(&item, refspec, rs->fetch);
188 ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
189 rs->items[rs->nr++] = item;
191 ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
192 rs->raw[rs->raw_nr++] = refspec;
195 void refspec_append(struct refspec *rs, const char *refspec)
197 refspec_append_nodup(rs, xstrdup(refspec));
200 void refspec_appendf(struct refspec *rs, const char *fmt, ...)
202 va_list ap;
204 va_start(ap, fmt);
205 refspec_append_nodup(rs, xstrvfmt(fmt, ap));
206 va_end(ap);
209 void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
211 int i;
212 for (i = 0; i < nr; i++)
213 refspec_append(rs, refspecs[i]);
216 void refspec_clear(struct refspec *rs)
218 int i;
220 for (i = 0; i < rs->nr; i++)
221 refspec_item_clear(&rs->items[i]);
223 FREE_AND_NULL(rs->items);
224 rs->alloc = 0;
225 rs->nr = 0;
227 for (i = 0; i < rs->raw_nr; i++)
228 free(rs->raw[i]);
229 FREE_AND_NULL(rs->raw);
230 rs->raw_alloc = 0;
231 rs->raw_nr = 0;
233 rs->fetch = 0;
236 int valid_fetch_refspec(const char *fetch_refspec_str)
238 struct refspec_item refspec;
239 int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH);
240 refspec_item_clear(&refspec);
241 return ret;
244 int valid_remote_name(const char *name)
246 int result;
247 struct strbuf refspec = STRBUF_INIT;
248 strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name);
249 result = valid_fetch_refspec(refspec.buf);
250 strbuf_release(&refspec);
251 return result;
254 void refspec_ref_prefixes(const struct refspec *rs,
255 struct strvec *ref_prefixes)
257 int i;
258 for (i = 0; i < rs->nr; i++) {
259 const struct refspec_item *item = &rs->items[i];
260 const char *prefix = NULL;
262 if (item->exact_sha1 || item->negative)
263 continue;
264 if (rs->fetch == REFSPEC_FETCH)
265 prefix = item->src;
266 else if (item->dst)
267 prefix = item->dst;
268 else if (item->src && !item->exact_sha1)
269 prefix = item->src;
271 if (!prefix)
272 continue;
274 if (item->pattern) {
275 const char *glob = strchr(prefix, '*');
276 strvec_pushf(ref_prefixes, "%.*s",
277 (int)(glob - prefix),
278 prefix);
279 } else {
280 expand_ref_prefix(ref_prefixes, prefix);