2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #include "git2/errors.h"
17 int git_refspec__parse(git_refspec
*refspec
, const char *input
, bool is_fetch
)
19 // Ported from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/remote.c#L518-636
23 const char *lhs
, *rhs
;
26 assert(refspec
&& input
);
28 memset(refspec
, 0x0, sizeof(git_refspec
));
29 refspec
->push
= !is_fetch
;
37 rhs
= strrchr(lhs
, ':');
40 * Before going on, special case ":" (or "+:") as a refspec
43 if (!is_fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
44 refspec
->matching
= 1;
49 size_t rlen
= strlen(++rhs
);
50 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
51 refspec
->dst
= git__strndup(rhs
, rlen
);
54 llen
= (rhs
? (size_t)(rhs
- lhs
- 1) : strlen(lhs
));
55 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
56 if ((rhs
&& !is_glob
) || (!rhs
&& is_fetch
))
59 } else if (rhs
&& is_glob
)
62 refspec
->pattern
= is_glob
;
63 refspec
->src
= git__strndup(lhs
, llen
);
64 flags
= GIT_REF_FORMAT_ALLOW_ONELEVEL
| GIT_REF_FORMAT_REFSPEC_SHORTHAND
65 | (is_glob
? GIT_REF_FORMAT_REFSPEC_PATTERN
: 0);
70 * - empty is allowed; it means HEAD.
71 * - otherwise it must be a valid looking ref.
75 else if (!git_reference__is_valid_name(refspec
->src
, flags
))
79 * - missing is ok, and is same as empty.
80 * - empty is ok; it means not to store.
81 * - otherwise it must be a valid looking ref.
85 else if (!*refspec
->dst
)
87 else if (!git_reference__is_valid_name(refspec
->dst
, flags
))
92 * - empty is allowed; it means delete.
93 * - when wildcarded, it must be a valid looking ref.
94 * - otherwise, it must be an extended SHA-1, but
95 * there is no existing way to validate this.
100 if (!git_reference__is_valid_name(refspec
->src
, flags
))
104 ; /* anything goes, for now */
108 * - missing is allowed, but LHS then must be a
110 * - empty is not allowed.
111 * - otherwise it must be a valid looking ref.
114 if (!git_reference__is_valid_name(refspec
->src
, flags
))
116 } else if (!*refspec
->dst
) {
119 if (!git_reference__is_valid_name(refspec
->dst
, flags
))
124 refspec
->string
= git__strdup(input
);
125 GITERR_CHECK_ALLOC(refspec
->string
);
133 void git_refspec__free(git_refspec
*refspec
)
138 git__free(refspec
->src
);
139 git__free(refspec
->dst
);
140 git__free(refspec
->string
);
143 const char *git_refspec_src(const git_refspec
*refspec
)
145 return refspec
== NULL
? NULL
: refspec
->src
;
148 const char *git_refspec_dst(const git_refspec
*refspec
)
150 return refspec
== NULL
? NULL
: refspec
->dst
;
153 const char *git_refspec_string(const git_refspec
*refspec
)
155 return refspec
== NULL
? NULL
: refspec
->string
;
158 int git_refspec_force(const git_refspec
*refspec
)
162 return refspec
->force
;
165 int git_refspec_src_matches(const git_refspec
*refspec
, const char *refname
)
167 if (refspec
== NULL
|| refspec
->src
== NULL
)
170 return (p_fnmatch(refspec
->src
, refname
, 0) == 0);
173 int git_refspec_dst_matches(const git_refspec
*refspec
, const char *refname
)
175 if (refspec
== NULL
|| refspec
->dst
== NULL
)
178 return (p_fnmatch(refspec
->dst
, refname
, 0) == 0);
181 static int refspec_transform(
182 git_buf
*out
, const char *from
, const char *to
, const char *name
)
184 const char *from_star
, *to_star
;
185 const char *name_slash
, *from_slash
;
186 size_t replacement_len
, star_offset
;
188 git_buf_sanitize(out
);
192 * There are two parts to each side of a refspec, the bit
193 * before the star and the bit after it. The star can be in
194 * the middle of the pattern, so we need to look at each bit
197 from_star
= strchr(from
, '*');
198 to_star
= strchr(to
, '*');
200 assert(from_star
&& to_star
);
202 /* star offset, both in 'from' and in 'name' */
203 star_offset
= from_star
- from
;
205 /* the first half is copied over */
206 git_buf_put(out
, to
, to_star
- to
);
208 /* then we copy over the replacement, from the star's offset to the next slash in 'name' */
209 name_slash
= strchr(name
+ star_offset
, '/');
211 name_slash
= strrchr(name
, '\0');
213 /* if there is no slash after the star in 'from', we want to copy everything over */
214 from_slash
= strchr(from
+ star_offset
, '/');
216 name_slash
= strrchr(name
, '\0');
218 replacement_len
= (name_slash
- name
) - star_offset
;
219 git_buf_put(out
, name
+ star_offset
, replacement_len
);
221 return git_buf_puts(out
, to_star
+ 1);
224 int git_refspec_transform(git_buf
*out
, const git_refspec
*spec
, const char *name
)
226 assert(out
&& spec
&& name
);
227 git_buf_sanitize(out
);
229 if (!git_refspec_src_matches(spec
, name
)) {
230 giterr_set(GITERR_INVALID
, "ref '%s' doesn't match the source", name
);
235 return git_buf_puts(out
, spec
->dst
);
237 return refspec_transform(out
, spec
->src
, spec
->dst
, name
);
240 int git_refspec_rtransform(git_buf
*out
, const git_refspec
*spec
, const char *name
)
242 assert(out
&& spec
&& name
);
243 git_buf_sanitize(out
);
245 if (!git_refspec_dst_matches(spec
, name
)) {
246 giterr_set(GITERR_INVALID
, "ref '%s' doesn't match the destination", name
);
251 return git_buf_puts(out
, spec
->src
);
253 return refspec_transform(out
, spec
->dst
, spec
->src
, name
);
256 int git_refspec__serialize(git_buf
*out
, const git_refspec
*refspec
)
259 git_buf_putc(out
, '+');
261 git_buf_printf(out
, "%s:%s",
262 refspec
->src
!= NULL
? refspec
->src
: "",
263 refspec
->dst
!= NULL
? refspec
->dst
: "");
265 return git_buf_oom(out
) == false;
268 int git_refspec_is_wildcard(const git_refspec
*spec
)
270 assert(spec
&& spec
->src
);
272 return (spec
->src
[strlen(spec
->src
) - 1] == '*');
275 git_direction
git_refspec_direction(const git_refspec
*spec
)
282 int git_refspec__dwim_one(git_vector
*out
, git_refspec
*spec
, git_vector
*refs
)
284 git_buf buf
= GIT_BUF_INIT
;
288 const char* formatters
[] = {
290 GIT_REFS_TAGS_DIR
"%s",
291 GIT_REFS_HEADS_DIR
"%s",
295 git_refspec
*cur
= git__calloc(1, sizeof(git_refspec
));
296 GITERR_CHECK_ALLOC(cur
);
298 cur
->force
= spec
->force
;
299 cur
->push
= spec
->push
;
300 cur
->pattern
= spec
->pattern
;
301 cur
->matching
= spec
->matching
;
302 cur
->string
= git__strdup(spec
->string
);
304 /* shorthand on the lhs */
305 if (git__prefixcmp(spec
->src
, GIT_REFS_DIR
)) {
306 for (j
= 0; formatters
[j
]; j
++) {
308 if (git_buf_printf(&buf
, formatters
[j
], spec
->src
) < 0)
311 key
.name
= (char *) git_buf_cstr(&buf
);
312 if (!git_vector_search(&pos
, refs
, &key
)) {
313 /* we found something to match the shorthand, set src to that */
314 cur
->src
= git_buf_detach(&buf
);
319 /* No shorthands found, copy over the name */
320 if (cur
->src
== NULL
&& spec
->src
!= NULL
) {
321 cur
->src
= git__strdup(spec
->src
);
322 GITERR_CHECK_ALLOC(cur
->src
);
325 if (spec
->dst
&& git__prefixcmp(spec
->dst
, GIT_REFS_DIR
)) {
326 /* if it starts with "remotes" then we just prepend "refs/" */
327 if (!git__prefixcmp(spec
->dst
, "remotes/")) {
328 git_buf_puts(&buf
, GIT_REFS_DIR
);
330 git_buf_puts(&buf
, GIT_REFS_HEADS_DIR
);
333 if (git_buf_puts(&buf
, spec
->dst
) < 0)
336 cur
->dst
= git_buf_detach(&buf
);
341 if (cur
->dst
== NULL
&& spec
->dst
!= NULL
) {
342 cur
->dst
= git__strdup(spec
->dst
);
343 GITERR_CHECK_ALLOC(cur
->dst
);
346 return git_vector_insert(out
, cur
);