1 #include "git-compat-util.h"
11 static struct refspec_item s_tag_refspec
= {
21 /* See TAG_REFSPEC for the string version */
22 const struct refspec_item
*tag_refspec
= &s_tag_refspec
;
25 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
26 * Returns 1 if successful and 0 if the refspec is invalid.
28 static int parse_refspec(struct refspec_item
*item
, const char *refspec
, int fetch
)
32 const char *lhs
, *rhs
;
41 } else if (*lhs
== '^') {
46 rhs
= strrchr(lhs
, ':');
48 /* negative refspecs only have one side */
49 if (item
->negative
&& rhs
)
53 * Before going on, special case ":" (or "+:") as a refspec
54 * for pushing matching refs.
56 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
62 size_t rlen
= strlen(++rhs
);
63 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
64 item
->dst
= xstrndup(rhs
, rlen
);
69 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
70 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
71 if ((rhs
&& !is_glob
) || (!rhs
&& !item
->negative
&& fetch
))
74 } else if (rhs
&& is_glob
) {
78 item
->pattern
= is_glob
;
79 if (llen
== 1 && *lhs
== '@')
80 item
->src
= xstrdup("HEAD");
82 item
->src
= xstrndup(lhs
, llen
);
83 flags
= REFNAME_ALLOW_ONELEVEL
| (is_glob
? REFNAME_REFSPEC_PATTERN
: 0);
86 struct object_id unused
;
89 * Negative refspecs only have a LHS, which indicates a ref
90 * (or pattern of refs) to exclude from other matches. This
91 * can either be a simple ref, or a glob pattern. Exact sha1
92 * match is not currently supported.
95 return 0; /* negative refspecs must not be empty */
96 else if (llen
== the_hash_algo
->hexsz
&& !get_oid_hex(item
->src
, &unused
))
97 return 0; /* negative refpsecs cannot be exact sha1 */
98 else if (!check_refname_format(item
->src
, flags
))
99 ; /* valid looking ref is ok */
103 /* the other rules below do not apply to negative refspecs */
108 struct object_id unused
;
112 ; /* empty is ok; it means "HEAD" */
113 else if (llen
== the_hash_algo
->hexsz
&& !get_oid_hex(item
->src
, &unused
))
114 item
->exact_sha1
= 1; /* ok */
115 else if (!check_refname_format(item
->src
, flags
))
116 ; /* valid looking ref is ok */
121 ; /* missing is ok; it is the same as empty */
122 else if (!*item
->dst
)
123 ; /* empty is ok; it means "do not store" */
124 else if (!check_refname_format(item
->dst
, flags
))
125 ; /* valid looking ref is ok */
131 * - empty is allowed; it means delete.
132 * - when wildcarded, it must be a valid looking ref.
133 * - otherwise, it must be an extended SHA-1, but
134 * there is no existing way to validate this.
139 if (check_refname_format(item
->src
, flags
))
143 ; /* anything goes, for now */
146 * - missing is allowed, but LHS then must be a
148 * - empty is not allowed.
149 * - otherwise it must be a valid looking ref.
152 if (check_refname_format(item
->src
, flags
))
154 } else if (!*item
->dst
) {
157 if (check_refname_format(item
->dst
, flags
))
165 int refspec_item_init(struct refspec_item
*item
, const char *refspec
, int fetch
)
167 memset(item
, 0, sizeof(*item
));
168 return parse_refspec(item
, refspec
, fetch
);
171 void refspec_item_init_or_die(struct refspec_item
*item
, const char *refspec
,
174 if (!refspec_item_init(item
, refspec
, fetch
))
175 die(_("invalid refspec '%s'"), refspec
);
178 void refspec_item_clear(struct refspec_item
*item
)
180 FREE_AND_NULL(item
->src
);
181 FREE_AND_NULL(item
->dst
);
185 item
->exact_sha1
= 0;
188 void refspec_init(struct refspec
*rs
, int fetch
)
190 memset(rs
, 0, sizeof(*rs
));
194 static void refspec_append_nodup(struct refspec
*rs
, char *refspec
)
196 struct refspec_item item
;
198 refspec_item_init_or_die(&item
, refspec
, rs
->fetch
);
200 ALLOC_GROW(rs
->items
, rs
->nr
+ 1, rs
->alloc
);
201 rs
->items
[rs
->nr
++] = item
;
203 ALLOC_GROW(rs
->raw
, rs
->raw_nr
+ 1, rs
->raw_alloc
);
204 rs
->raw
[rs
->raw_nr
++] = refspec
;
207 void refspec_append(struct refspec
*rs
, const char *refspec
)
209 refspec_append_nodup(rs
, xstrdup(refspec
));
212 void refspec_appendf(struct refspec
*rs
, const char *fmt
, ...)
217 refspec_append_nodup(rs
, xstrvfmt(fmt
, ap
));
221 void refspec_appendn(struct refspec
*rs
, const char **refspecs
, int nr
)
224 for (i
= 0; i
< nr
; i
++)
225 refspec_append(rs
, refspecs
[i
]);
228 void refspec_clear(struct refspec
*rs
)
232 for (i
= 0; i
< rs
->nr
; i
++)
233 refspec_item_clear(&rs
->items
[i
]);
235 FREE_AND_NULL(rs
->items
);
239 for (i
= 0; i
< rs
->raw_nr
; i
++)
240 free((char *)rs
->raw
[i
]);
241 FREE_AND_NULL(rs
->raw
);
248 int valid_fetch_refspec(const char *fetch_refspec_str
)
250 struct refspec_item refspec
;
251 int ret
= refspec_item_init(&refspec
, fetch_refspec_str
, REFSPEC_FETCH
);
252 refspec_item_clear(&refspec
);
256 int valid_remote_name(const char *name
)
259 struct strbuf refspec
= STRBUF_INIT
;
260 strbuf_addf(&refspec
, "refs/heads/test:refs/remotes/%s/test", name
);
261 result
= valid_fetch_refspec(refspec
.buf
);
262 strbuf_release(&refspec
);
266 void refspec_ref_prefixes(const struct refspec
*rs
,
267 struct strvec
*ref_prefixes
)
270 for (i
= 0; i
< rs
->nr
; i
++) {
271 const struct refspec_item
*item
= &rs
->items
[i
];
272 const char *prefix
= NULL
;
274 if (item
->exact_sha1
|| item
->negative
)
276 if (rs
->fetch
== REFSPEC_FETCH
)
280 else if (item
->src
&& !item
->exact_sha1
)
287 const char *glob
= strchr(prefix
, '*');
288 strvec_pushf(ref_prefixes
, "%.*s",
289 (int)(glob
- prefix
),
292 expand_ref_prefix(ref_prefixes
, prefix
);