git
/
refspec.c
283 строки · 6.3 Кб
1#define USE_THE_REPOSITORY_VARIABLE
2
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"
11
12/*
13* Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
14* Returns 1 if successful and 0 if the refspec is invalid.
15*/
16static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch)
17{
18size_t llen;
19int is_glob;
20const char *lhs, *rhs;
21int flags;
22
23is_glob = 0;
24
25lhs = refspec;
26if (*lhs == '+') {
27item->force = 1;
28lhs++;
29} else if (*lhs == '^') {
30item->negative = 1;
31lhs++;
32}
33
34rhs = strrchr(lhs, ':');
35
36/* negative refspecs only have one side */
37if (item->negative && rhs)
38return 0;
39
40/*
41* Before going on, special case ":" (or "+:") as a refspec
42* for pushing matching refs.
43*/
44if (!fetch && rhs == lhs && rhs[1] == '\0') {
45item->matching = 1;
46return 1;
47}
48
49if (rhs) {
50size_t rlen = strlen(++rhs);
51is_glob = (1 <= rlen && strchr(rhs, '*'));
52item->dst = xstrndup(rhs, rlen);
53} else {
54item->dst = NULL;
55}
56
57llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
58if (1 <= llen && memchr(lhs, '*', llen)) {
59if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
60return 0;
61is_glob = 1;
62} else if (rhs && is_glob) {
63return 0;
64}
65
66item->pattern = is_glob;
67if (llen == 1 && *lhs == '@')
68item->src = xstrdup("HEAD");
69else
70item->src = xstrndup(lhs, llen);
71flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
72
73if (item->negative) {
74struct object_id unused;
75
76/*
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.
81*/
82if (!*item->src)
83return 0; /* negative refspecs must not be empty */
84else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
85return 0; /* negative refpsecs cannot be exact sha1 */
86else if (!check_refname_format(item->src, flags))
87; /* valid looking ref is ok */
88else
89return 0;
90
91/* the other rules below do not apply to negative refspecs */
92return 1;
93}
94
95if (fetch) {
96struct object_id unused;
97
98/* LHS */
99if (!*item->src)
100; /* empty is ok; it means "HEAD" */
101else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
102item->exact_sha1 = 1; /* ok */
103else if (!check_refname_format(item->src, flags))
104; /* valid looking ref is ok */
105else
106return 0;
107/* RHS */
108if (!item->dst)
109; /* missing is ok; it is the same as empty */
110else if (!*item->dst)
111; /* empty is ok; it means "do not store" */
112else if (!check_refname_format(item->dst, flags))
113; /* valid looking ref is ok */
114else
115return 0;
116} else {
117/*
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.
123*/
124if (!*item->src)
125; /* empty is ok */
126else if (is_glob) {
127if (check_refname_format(item->src, flags))
128return 0;
129}
130else
131; /* anything goes, for now */
132/*
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.
138*/
139if (!item->dst) {
140if (check_refname_format(item->src, flags))
141return 0;
142} else if (!*item->dst) {
143return 0;
144} else {
145if (check_refname_format(item->dst, flags))
146return 0;
147}
148}
149
150return 1;
151}
152
153int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
154{
155memset(item, 0, sizeof(*item));
156return parse_refspec(item, refspec, fetch);
157}
158
159void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
160int fetch)
161{
162if (!refspec_item_init(item, refspec, fetch))
163die(_("invalid refspec '%s'"), refspec);
164}
165
166void refspec_item_clear(struct refspec_item *item)
167{
168FREE_AND_NULL(item->src);
169FREE_AND_NULL(item->dst);
170item->force = 0;
171item->pattern = 0;
172item->matching = 0;
173item->exact_sha1 = 0;
174}
175
176void refspec_init(struct refspec *rs, int fetch)
177{
178memset(rs, 0, sizeof(*rs));
179rs->fetch = fetch;
180}
181
182static void refspec_append_nodup(struct refspec *rs, char *refspec)
183{
184struct refspec_item item;
185
186refspec_item_init_or_die(&item, refspec, rs->fetch);
187
188ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
189rs->items[rs->nr++] = item;
190
191ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
192rs->raw[rs->raw_nr++] = refspec;
193}
194
195void refspec_append(struct refspec *rs, const char *refspec)
196{
197refspec_append_nodup(rs, xstrdup(refspec));
198}
199
200void refspec_appendf(struct refspec *rs, const char *fmt, ...)
201{
202va_list ap;
203
204va_start(ap, fmt);
205refspec_append_nodup(rs, xstrvfmt(fmt, ap));
206va_end(ap);
207}
208
209void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
210{
211int i;
212for (i = 0; i < nr; i++)
213refspec_append(rs, refspecs[i]);
214}
215
216void refspec_clear(struct refspec *rs)
217{
218int i;
219
220for (i = 0; i < rs->nr; i++)
221refspec_item_clear(&rs->items[i]);
222
223FREE_AND_NULL(rs->items);
224rs->alloc = 0;
225rs->nr = 0;
226
227for (i = 0; i < rs->raw_nr; i++)
228free((char *)rs->raw[i]);
229FREE_AND_NULL(rs->raw);
230rs->raw_alloc = 0;
231rs->raw_nr = 0;
232
233rs->fetch = 0;
234}
235
236int valid_fetch_refspec(const char *fetch_refspec_str)
237{
238struct refspec_item refspec;
239int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH);
240refspec_item_clear(&refspec);
241return ret;
242}
243
244int valid_remote_name(const char *name)
245{
246int result;
247struct strbuf refspec = STRBUF_INIT;
248strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name);
249result = valid_fetch_refspec(refspec.buf);
250strbuf_release(&refspec);
251return result;
252}
253
254void refspec_ref_prefixes(const struct refspec *rs,
255struct strvec *ref_prefixes)
256{
257int i;
258for (i = 0; i < rs->nr; i++) {
259const struct refspec_item *item = &rs->items[i];
260const char *prefix = NULL;
261
262if (item->exact_sha1 || item->negative)
263continue;
264if (rs->fetch == REFSPEC_FETCH)
265prefix = item->src;
266else if (item->dst)
267prefix = item->dst;
268else if (item->src && !item->exact_sha1)
269prefix = item->src;
270
271if (!prefix)
272continue;
273
274if (item->pattern) {
275const char *glob = strchr(prefix, '*');
276strvec_pushf(ref_prefixes, "%.*s",
277(int)(glob - prefix),
278prefix);
279} else {
280expand_ref_prefix(ref_prefixes, prefix);
281}
282}
283}
284