1 #include "git-compat-util.h"
9 static struct refspec_item s_tag_refspec
= {
19 /* See TAG_REFSPEC for the string version */
20 const struct refspec_item
*tag_refspec
= &s_tag_refspec
;
23 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
24 * Returns 1 if successful and 0 if the refspec is invalid.
26 static int parse_refspec(struct refspec_item
*item
, const char *refspec
, int fetch
)
30 const char *lhs
, *rhs
;
39 } else if (*lhs
== '^') {
44 rhs
= strrchr(lhs
, ':');
46 /* negative refspecs only have one side */
47 if (item
->negative
&& rhs
)
51 * Before going on, special case ":" (or "+:") as a refspec
52 * for pushing matching refs.
54 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
60 size_t rlen
= strlen(++rhs
);
61 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
62 item
->dst
= xstrndup(rhs
, rlen
);
67 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
68 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
69 if ((rhs
&& !is_glob
) || (!rhs
&& !item
->negative
&& fetch
))
72 } else if (rhs
&& is_glob
) {
76 item
->pattern
= is_glob
;
77 if (llen
== 1 && *lhs
== '@')
78 item
->src
= xstrdup("HEAD");
80 item
->src
= xstrndup(lhs
, llen
);
81 flags
= REFNAME_ALLOW_ONELEVEL
| (is_glob
? REFNAME_REFSPEC_PATTERN
: 0);
84 struct object_id unused
;
87 * Negative refspecs only have a LHS, which indicates a ref
88 * (or pattern of refs) to exclude from other matches. This
89 * can either be a simple ref, or a glob pattern. Exact sha1
90 * match is not currently supported.
93 return 0; /* negative refspecs must not be empty */
94 else if (llen
== the_hash_algo
->hexsz
&& !get_oid_hex(item
->src
, &unused
))
95 return 0; /* negative refpsecs cannot be exact sha1 */
96 else if (!check_refname_format(item
->src
, flags
))
97 ; /* valid looking ref is ok */
101 /* the other rules below do not apply to negative refspecs */
106 struct object_id unused
;
110 ; /* empty is ok; it means "HEAD" */
111 else if (llen
== the_hash_algo
->hexsz
&& !get_oid_hex(item
->src
, &unused
))
112 item
->exact_sha1
= 1; /* ok */
113 else if (!check_refname_format(item
->src
, flags
))
114 ; /* valid looking ref is ok */
119 ; /* missing is ok; it is the same as empty */
120 else if (!*item
->dst
)
121 ; /* empty is ok; it means "do not store" */
122 else if (!check_refname_format(item
->dst
, flags
))
123 ; /* valid looking ref is ok */
129 * - empty is allowed; it means delete.
130 * - when wildcarded, it must be a valid looking ref.
131 * - otherwise, it must be an extended SHA-1, but
132 * there is no existing way to validate this.
137 if (check_refname_format(item
->src
, flags
))
141 ; /* anything goes, for now */
144 * - missing is allowed, but LHS then must be a
146 * - empty is not allowed.
147 * - otherwise it must be a valid looking ref.
150 if (check_refname_format(item
->src
, flags
))
152 } else if (!*item
->dst
) {
155 if (check_refname_format(item
->dst
, flags
))
163 int refspec_item_init(struct refspec_item
*item
, const char *refspec
, int fetch
)
165 memset(item
, 0, sizeof(*item
));
166 return parse_refspec(item
, refspec
, fetch
);
169 void refspec_item_init_or_die(struct refspec_item
*item
, const char *refspec
,
172 if (!refspec_item_init(item
, refspec
, fetch
))
173 die(_("invalid refspec '%s'"), refspec
);
176 void refspec_item_clear(struct refspec_item
*item
)
178 FREE_AND_NULL(item
->src
);
179 FREE_AND_NULL(item
->dst
);
183 item
->exact_sha1
= 0;
186 void refspec_init(struct refspec
*rs
, int fetch
)
188 memset(rs
, 0, sizeof(*rs
));
192 static void refspec_append_nodup(struct refspec
*rs
, char *refspec
)
194 struct refspec_item item
;
196 refspec_item_init_or_die(&item
, refspec
, rs
->fetch
);
198 ALLOC_GROW(rs
->items
, rs
->nr
+ 1, rs
->alloc
);
199 rs
->items
[rs
->nr
++] = item
;
201 ALLOC_GROW(rs
->raw
, rs
->raw_nr
+ 1, rs
->raw_alloc
);
202 rs
->raw
[rs
->raw_nr
++] = refspec
;
205 void refspec_append(struct refspec
*rs
, const char *refspec
)
207 refspec_append_nodup(rs
, xstrdup(refspec
));
210 void refspec_appendf(struct refspec
*rs
, const char *fmt
, ...)
215 refspec_append_nodup(rs
, xstrvfmt(fmt
, ap
));
219 void refspec_appendn(struct refspec
*rs
, const char **refspecs
, int nr
)
222 for (i
= 0; i
< nr
; i
++)
223 refspec_append(rs
, refspecs
[i
]);
226 void refspec_clear(struct refspec
*rs
)
230 for (i
= 0; i
< rs
->nr
; i
++)
231 refspec_item_clear(&rs
->items
[i
]);
233 FREE_AND_NULL(rs
->items
);
237 for (i
= 0; i
< rs
->raw_nr
; i
++)
238 free((char *)rs
->raw
[i
]);
239 FREE_AND_NULL(rs
->raw
);
246 int valid_fetch_refspec(const char *fetch_refspec_str
)
248 struct refspec_item refspec
;
249 int ret
= refspec_item_init(&refspec
, fetch_refspec_str
, REFSPEC_FETCH
);
250 refspec_item_clear(&refspec
);
254 int valid_remote_name(const char *name
)
257 struct strbuf refspec
= STRBUF_INIT
;
258 strbuf_addf(&refspec
, "refs/heads/test:refs/remotes/%s/test", name
);
259 result
= valid_fetch_refspec(refspec
.buf
);
260 strbuf_release(&refspec
);
264 void refspec_ref_prefixes(const struct refspec
*rs
,
265 struct strvec
*ref_prefixes
)
268 for (i
= 0; i
< rs
->nr
; i
++) {
269 const struct refspec_item
*item
= &rs
->items
[i
];
270 const char *prefix
= NULL
;
272 if (item
->exact_sha1
|| item
->negative
)
274 if (rs
->fetch
== REFSPEC_FETCH
)
278 else if (item
->src
&& !item
->exact_sha1
)
285 const char *glob
= strchr(prefix
, '*');
286 strvec_pushf(ref_prefixes
, "%.*s",
287 (int)(glob
- prefix
),
290 expand_ref_prefix(ref_prefixes
, prefix
);