replace-object.h: move read_replace_refs declaration from cache.h to here
[git.git] / refspec.c
blob28d90911aa5ed383afac90f6a444891cad7877ac
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "hex.h"
4 #include "strvec.h"
5 #include "refs.h"
6 #include "refspec.h"
8 static struct refspec_item s_tag_refspec = {
9 .force = 0,
10 .pattern = 1,
11 .matching = 0,
12 .exact_sha1 = 0,
13 .negative = 0,
14 .src = "refs/tags/*",
15 .dst = "refs/tags/*",
18 /* See TAG_REFSPEC for the string version */
19 const struct refspec_item *tag_refspec = &s_tag_refspec;
22 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
23 * Returns 1 if successful and 0 if the refspec is invalid.
25 static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch)
27 size_t llen;
28 int is_glob;
29 const char *lhs, *rhs;
30 int flags;
32 is_glob = 0;
34 lhs = refspec;
35 if (*lhs == '+') {
36 item->force = 1;
37 lhs++;
38 } else if (*lhs == '^') {
39 item->negative = 1;
40 lhs++;
43 rhs = strrchr(lhs, ':');
45 /* negative refspecs only have one side */
46 if (item->negative && rhs)
47 return 0;
50 * Before going on, special case ":" (or "+:") as a refspec
51 * for pushing matching refs.
53 if (!fetch && rhs == lhs && rhs[1] == '\0') {
54 item->matching = 1;
55 return 1;
58 if (rhs) {
59 size_t rlen = strlen(++rhs);
60 is_glob = (1 <= rlen && strchr(rhs, '*'));
61 item->dst = xstrndup(rhs, rlen);
62 } else {
63 item->dst = NULL;
66 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
67 if (1 <= llen && memchr(lhs, '*', llen)) {
68 if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
69 return 0;
70 is_glob = 1;
71 } else if (rhs && is_glob) {
72 return 0;
75 item->pattern = is_glob;
76 if (llen == 1 && *lhs == '@')
77 item->src = xstrdup("HEAD");
78 else
79 item->src = xstrndup(lhs, llen);
80 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
82 if (item->negative) {
83 struct object_id unused;
86 * Negative refspecs only have a LHS, which indicates a ref
87 * (or pattern of refs) to exclude from other matches. This
88 * can either be a simple ref, or a glob pattern. Exact sha1
89 * match is not currently supported.
91 if (!*item->src)
92 return 0; /* negative refspecs must not be empty */
93 else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
94 return 0; /* negative refpsecs cannot be exact sha1 */
95 else if (!check_refname_format(item->src, flags))
96 ; /* valid looking ref is ok */
97 else
98 return 0;
100 /* the other rules below do not apply to negative refspecs */
101 return 1;
104 if (fetch) {
105 struct object_id unused;
107 /* LHS */
108 if (!*item->src)
109 ; /* empty is ok; it means "HEAD" */
110 else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
111 item->exact_sha1 = 1; /* ok */
112 else if (!check_refname_format(item->src, flags))
113 ; /* valid looking ref is ok */
114 else
115 return 0;
116 /* RHS */
117 if (!item->dst)
118 ; /* missing is ok; it is the same as empty */
119 else if (!*item->dst)
120 ; /* empty is ok; it means "do not store" */
121 else if (!check_refname_format(item->dst, flags))
122 ; /* valid looking ref is ok */
123 else
124 return 0;
125 } else {
127 * LHS
128 * - empty is allowed; it means delete.
129 * - when wildcarded, it must be a valid looking ref.
130 * - otherwise, it must be an extended SHA-1, but
131 * there is no existing way to validate this.
133 if (!*item->src)
134 ; /* empty is ok */
135 else if (is_glob) {
136 if (check_refname_format(item->src, flags))
137 return 0;
139 else
140 ; /* anything goes, for now */
142 * RHS
143 * - missing is allowed, but LHS then must be a
144 * valid looking ref.
145 * - empty is not allowed.
146 * - otherwise it must be a valid looking ref.
148 if (!item->dst) {
149 if (check_refname_format(item->src, flags))
150 return 0;
151 } else if (!*item->dst) {
152 return 0;
153 } else {
154 if (check_refname_format(item->dst, flags))
155 return 0;
159 return 1;
162 int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
164 memset(item, 0, sizeof(*item));
165 return parse_refspec(item, refspec, fetch);
168 void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
169 int fetch)
171 if (!refspec_item_init(item, refspec, fetch))
172 die(_("invalid refspec '%s'"), refspec);
175 void refspec_item_clear(struct refspec_item *item)
177 FREE_AND_NULL(item->src);
178 FREE_AND_NULL(item->dst);
179 item->force = 0;
180 item->pattern = 0;
181 item->matching = 0;
182 item->exact_sha1 = 0;
185 void refspec_init(struct refspec *rs, int fetch)
187 memset(rs, 0, sizeof(*rs));
188 rs->fetch = fetch;
191 static void refspec_append_nodup(struct refspec *rs, char *refspec)
193 struct refspec_item item;
195 refspec_item_init_or_die(&item, refspec, rs->fetch);
197 ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
198 rs->items[rs->nr++] = item;
200 ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
201 rs->raw[rs->raw_nr++] = refspec;
204 void refspec_append(struct refspec *rs, const char *refspec)
206 refspec_append_nodup(rs, xstrdup(refspec));
209 void refspec_appendf(struct refspec *rs, const char *fmt, ...)
211 va_list ap;
213 va_start(ap, fmt);
214 refspec_append_nodup(rs, xstrvfmt(fmt, ap));
215 va_end(ap);
218 void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
220 int i;
221 for (i = 0; i < nr; i++)
222 refspec_append(rs, refspecs[i]);
225 void refspec_clear(struct refspec *rs)
227 int i;
229 for (i = 0; i < rs->nr; i++)
230 refspec_item_clear(&rs->items[i]);
232 FREE_AND_NULL(rs->items);
233 rs->alloc = 0;
234 rs->nr = 0;
236 for (i = 0; i < rs->raw_nr; i++)
237 free((char *)rs->raw[i]);
238 FREE_AND_NULL(rs->raw);
239 rs->raw_alloc = 0;
240 rs->raw_nr = 0;
242 rs->fetch = 0;
245 int valid_fetch_refspec(const char *fetch_refspec_str)
247 struct refspec_item refspec;
248 int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH);
249 refspec_item_clear(&refspec);
250 return ret;
253 int valid_remote_name(const char *name)
255 int result;
256 struct strbuf refspec = STRBUF_INIT;
257 strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name);
258 result = valid_fetch_refspec(refspec.buf);
259 strbuf_release(&refspec);
260 return result;
263 void refspec_ref_prefixes(const struct refspec *rs,
264 struct strvec *ref_prefixes)
266 int i;
267 for (i = 0; i < rs->nr; i++) {
268 const struct refspec_item *item = &rs->items[i];
269 const char *prefix = NULL;
271 if (item->exact_sha1 || item->negative)
272 continue;
273 if (rs->fetch == REFSPEC_FETCH)
274 prefix = item->src;
275 else if (item->dst)
276 prefix = item->dst;
277 else if (item->src && !item->exact_sha1)
278 prefix = item->src;
280 if (!prefix)
281 continue;
283 if (item->pattern) {
284 const char *glob = strchr(prefix, '*');
285 strvec_pushf(ref_prefixes, "%.*s",
286 (int)(glob - prefix),
287 prefix);
288 } else {
289 expand_ref_prefix(ref_prefixes, prefix);