1 #include "git-compat-util.h"
8 static struct refspec_item s_tag_refspec
= {
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
)
29 const char *lhs
, *rhs
;
38 } else if (*lhs
== '^') {
43 rhs
= strrchr(lhs
, ':');
45 /* negative refspecs only have one side */
46 if (item
->negative
&& rhs
)
50 * Before going on, special case ":" (or "+:") as a refspec
51 * for pushing matching refs.
53 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
59 size_t rlen
= strlen(++rhs
);
60 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
61 item
->dst
= xstrndup(rhs
, rlen
);
66 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
67 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
68 if ((rhs
&& !is_glob
) || (!rhs
&& !item
->negative
&& fetch
))
71 } else if (rhs
&& is_glob
) {
75 item
->pattern
= is_glob
;
76 if (llen
== 1 && *lhs
== '@')
77 item
->src
= xstrdup("HEAD");
79 item
->src
= xstrndup(lhs
, llen
);
80 flags
= REFNAME_ALLOW_ONELEVEL
| (is_glob
? REFNAME_REFSPEC_PATTERN
: 0);
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.
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 */
100 /* the other rules below do not apply to negative refspecs */
105 struct object_id unused
;
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 */
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 */
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.
136 if (check_refname_format(item
->src
, flags
))
140 ; /* anything goes, for now */
143 * - missing is allowed, but LHS then must be a
145 * - empty is not allowed.
146 * - otherwise it must be a valid looking ref.
149 if (check_refname_format(item
->src
, flags
))
151 } else if (!*item
->dst
) {
154 if (check_refname_format(item
->dst
, flags
))
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
,
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
);
182 item
->exact_sha1
= 0;
185 void refspec_init(struct refspec
*rs
, int fetch
)
187 memset(rs
, 0, sizeof(*rs
));
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
, ...)
214 refspec_append_nodup(rs
, xstrvfmt(fmt
, ap
));
218 void refspec_appendn(struct refspec
*rs
, const char **refspecs
, int nr
)
221 for (i
= 0; i
< nr
; i
++)
222 refspec_append(rs
, refspecs
[i
]);
225 void refspec_clear(struct refspec
*rs
)
229 for (i
= 0; i
< rs
->nr
; i
++)
230 refspec_item_clear(&rs
->items
[i
]);
232 FREE_AND_NULL(rs
->items
);
236 for (i
= 0; i
< rs
->raw_nr
; i
++)
237 free((char *)rs
->raw
[i
]);
238 FREE_AND_NULL(rs
->raw
);
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
);
253 int valid_remote_name(const char *name
)
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
);
263 void refspec_ref_prefixes(const struct refspec
*rs
,
264 struct strvec
*ref_prefixes
)
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
)
273 if (rs
->fetch
== REFSPEC_FETCH
)
277 else if (item
->src
&& !item
->exact_sha1
)
284 const char *glob
= strchr(prefix
, '*');
285 strvec_pushf(ref_prefixes
, "%.*s",
286 (int)(glob
- prefix
),
289 expand_ref_prefix(ref_prefixes
, prefix
);