6 static struct refspec_item s_tag_refspec
= {
16 /* See TAG_REFSPEC for the string version */
17 const struct refspec_item
*tag_refspec
= &s_tag_refspec
;
20 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
21 * Returns 1 if successful and 0 if the refspec is invalid.
23 static int parse_refspec(struct refspec_item
*item
, const char *refspec
, int fetch
)
27 const char *lhs
, *rhs
;
36 } else if (*lhs
== '^') {
41 rhs
= strrchr(lhs
, ':');
43 /* negative refspecs only have one side */
44 if (item
->negative
&& rhs
)
48 * Before going on, special case ":" (or "+:") as a refspec
49 * for pushing matching refs.
51 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
57 size_t rlen
= strlen(++rhs
);
58 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
59 item
->dst
= xstrndup(rhs
, rlen
);
64 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
65 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
66 if ((rhs
&& !is_glob
) || (!rhs
&& !item
->negative
&& fetch
))
69 } else if (rhs
&& is_glob
) {
73 item
->pattern
= is_glob
;
74 if (llen
== 1 && *lhs
== '@')
75 item
->src
= xstrdup("HEAD");
77 item
->src
= xstrndup(lhs
, llen
);
78 flags
= REFNAME_ALLOW_ONELEVEL
| (is_glob
? REFNAME_REFSPEC_PATTERN
: 0);
81 struct object_id unused
;
84 * Negative refspecs only have a LHS, which indicates a ref
85 * (or pattern of refs) to exclude from other matches. This
86 * can either be a simple ref, or a glob pattern. Exact sha1
87 * match is not currently supported.
90 return 0; /* negative refspecs must not be empty */
91 else if (llen
== the_hash_algo
->hexsz
&& !get_oid_hex(item
->src
, &unused
))
92 return 0; /* negative refpsecs cannot be exact sha1 */
93 else if (!check_refname_format(item
->src
, flags
))
94 ; /* valid looking ref is ok */
98 /* the other rules below do not apply to negative refspecs */
103 struct object_id unused
;
107 ; /* empty is ok; it means "HEAD" */
108 else if (llen
== the_hash_algo
->hexsz
&& !get_oid_hex(item
->src
, &unused
))
109 item
->exact_sha1
= 1; /* ok */
110 else if (!check_refname_format(item
->src
, flags
))
111 ; /* valid looking ref is ok */
116 ; /* missing is ok; it is the same as empty */
117 else if (!*item
->dst
)
118 ; /* empty is ok; it means "do not store" */
119 else if (!check_refname_format(item
->dst
, flags
))
120 ; /* valid looking ref is ok */
126 * - empty is allowed; it means delete.
127 * - when wildcarded, it must be a valid looking ref.
128 * - otherwise, it must be an extended SHA-1, but
129 * there is no existing way to validate this.
134 if (check_refname_format(item
->src
, flags
))
138 ; /* anything goes, for now */
141 * - missing is allowed, but LHS then must be a
143 * - empty is not allowed.
144 * - otherwise it must be a valid looking ref.
147 if (check_refname_format(item
->src
, flags
))
149 } else if (!*item
->dst
) {
152 if (check_refname_format(item
->dst
, flags
))
160 int refspec_item_init(struct refspec_item
*item
, const char *refspec
, int fetch
)
162 memset(item
, 0, sizeof(*item
));
163 return parse_refspec(item
, refspec
, fetch
);
166 void refspec_item_init_or_die(struct refspec_item
*item
, const char *refspec
,
169 if (!refspec_item_init(item
, refspec
, fetch
))
170 die(_("invalid refspec '%s'"), refspec
);
173 void refspec_item_clear(struct refspec_item
*item
)
175 FREE_AND_NULL(item
->src
);
176 FREE_AND_NULL(item
->dst
);
180 item
->exact_sha1
= 0;
183 void refspec_init(struct refspec
*rs
, int fetch
)
185 memset(rs
, 0, sizeof(*rs
));
189 static void refspec_append_nodup(struct refspec
*rs
, char *refspec
)
191 struct refspec_item item
;
193 refspec_item_init_or_die(&item
, refspec
, rs
->fetch
);
195 ALLOC_GROW(rs
->items
, rs
->nr
+ 1, rs
->alloc
);
196 rs
->items
[rs
->nr
++] = item
;
198 ALLOC_GROW(rs
->raw
, rs
->raw_nr
+ 1, rs
->raw_alloc
);
199 rs
->raw
[rs
->raw_nr
++] = refspec
;
202 void refspec_append(struct refspec
*rs
, const char *refspec
)
204 refspec_append_nodup(rs
, xstrdup(refspec
));
207 void refspec_appendf(struct refspec
*rs
, const char *fmt
, ...)
212 refspec_append_nodup(rs
, xstrvfmt(fmt
, ap
));
216 void refspec_appendn(struct refspec
*rs
, const char **refspecs
, int nr
)
219 for (i
= 0; i
< nr
; i
++)
220 refspec_append(rs
, refspecs
[i
]);
223 void refspec_clear(struct refspec
*rs
)
227 for (i
= 0; i
< rs
->nr
; i
++)
228 refspec_item_clear(&rs
->items
[i
]);
230 FREE_AND_NULL(rs
->items
);
234 for (i
= 0; i
< rs
->raw_nr
; i
++)
235 free((char *)rs
->raw
[i
]);
236 FREE_AND_NULL(rs
->raw
);
243 int valid_fetch_refspec(const char *fetch_refspec_str
)
245 struct refspec_item refspec
;
246 int ret
= refspec_item_init(&refspec
, fetch_refspec_str
, REFSPEC_FETCH
);
247 refspec_item_clear(&refspec
);
251 int valid_remote_name(const char *name
)
254 struct strbuf refspec
= STRBUF_INIT
;
255 strbuf_addf(&refspec
, "refs/heads/test:refs/remotes/%s/test", name
);
256 result
= valid_fetch_refspec(refspec
.buf
);
257 strbuf_release(&refspec
);
261 void refspec_ref_prefixes(const struct refspec
*rs
,
262 struct strvec
*ref_prefixes
)
265 for (i
= 0; i
< rs
->nr
; i
++) {
266 const struct refspec_item
*item
= &rs
->items
[i
];
267 const char *prefix
= NULL
;
269 if (item
->exact_sha1
|| item
->negative
)
271 if (rs
->fetch
== REFSPEC_FETCH
)
275 else if (item
->src
&& !item
->exact_sha1
)
282 const char *glob
= strchr(prefix
, '*');
283 strvec_pushf(ref_prefixes
, "%.*s",
284 (int)(glob
- prefix
),
287 expand_ref_prefix(ref_prefixes
, prefix
);