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;
45 refspec
->string
= git__strdup(input
);
46 GITERR_CHECK_ALLOC(refspec
->string
);
47 refspec
->src
= git__strdup("");
48 GITERR_CHECK_ALLOC(refspec
->src
);
49 refspec
->dst
= git__strdup("");
50 GITERR_CHECK_ALLOC(refspec
->dst
);
55 size_t rlen
= strlen(++rhs
);
56 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
57 refspec
->dst
= git__strndup(rhs
, rlen
);
60 llen
= (rhs
? (size_t)(rhs
- lhs
- 1) : strlen(lhs
));
61 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
62 if ((rhs
&& !is_glob
) || (!rhs
&& is_fetch
))
65 } else if (rhs
&& is_glob
)
68 refspec
->pattern
= is_glob
;
69 refspec
->src
= git__strndup(lhs
, llen
);
70 flags
= GIT_REF_FORMAT_ALLOW_ONELEVEL
| GIT_REF_FORMAT_REFSPEC_SHORTHAND
71 | (is_glob
? GIT_REF_FORMAT_REFSPEC_PATTERN
: 0);
76 * - empty is allowed; it means HEAD.
77 * - otherwise it must be a valid looking ref.
81 else if (!git_reference__is_valid_name(refspec
->src
, flags
))
85 * - missing is ok, and is same as empty.
86 * - empty is ok; it means not to store.
87 * - otherwise it must be a valid looking ref.
91 else if (!*refspec
->dst
)
93 else if (!git_reference__is_valid_name(refspec
->dst
, flags
))
98 * - empty is allowed; it means delete.
99 * - when wildcarded, it must be a valid looking ref.
100 * - otherwise, it must be an extended SHA-1, but
101 * there is no existing way to validate this.
106 if (!git_reference__is_valid_name(refspec
->src
, flags
))
110 ; /* anything goes, for now */
114 * - missing is allowed, but LHS then must be a
116 * - empty is not allowed.
117 * - otherwise it must be a valid looking ref.
120 if (!git_reference__is_valid_name(refspec
->src
, flags
))
122 } else if (!*refspec
->dst
) {
125 if (!git_reference__is_valid_name(refspec
->dst
, flags
))
129 /* if the RHS is empty, then it's a copy of the LHS */
131 refspec
->dst
= git__strdup(refspec
->src
);
132 GITERR_CHECK_ALLOC(refspec
->dst
);
136 refspec
->string
= git__strdup(input
);
137 GITERR_CHECK_ALLOC(refspec
->string
);
144 "'%s' is not a valid refspec.", input
);
145 git_refspec__free(refspec
);
149 void git_refspec__free(git_refspec
*refspec
)
154 git__free(refspec
->src
);
155 git__free(refspec
->dst
);
156 git__free(refspec
->string
);
158 memset(refspec
, 0x0, sizeof(git_refspec
));
161 const char *git_refspec_src(const git_refspec
*refspec
)
163 return refspec
== NULL
? NULL
: refspec
->src
;
166 const char *git_refspec_dst(const git_refspec
*refspec
)
168 return refspec
== NULL
? NULL
: refspec
->dst
;
171 const char *git_refspec_string(const git_refspec
*refspec
)
173 return refspec
== NULL
? NULL
: refspec
->string
;
176 int git_refspec_force(const git_refspec
*refspec
)
180 return refspec
->force
;
183 int git_refspec_src_matches(const git_refspec
*refspec
, const char *refname
)
185 if (refspec
== NULL
|| refspec
->src
== NULL
)
188 return (p_fnmatch(refspec
->src
, refname
, 0) == 0);
191 int git_refspec_dst_matches(const git_refspec
*refspec
, const char *refname
)
193 if (refspec
== NULL
|| refspec
->dst
== NULL
)
196 return (p_fnmatch(refspec
->dst
, refname
, 0) == 0);
199 static int refspec_transform(
200 git_buf
*out
, const char *from
, const char *to
, const char *name
)
202 const char *from_star
, *to_star
;
203 const char *name_slash
, *from_slash
;
204 size_t replacement_len
, star_offset
;
206 git_buf_sanitize(out
);
210 * There are two parts to each side of a refspec, the bit
211 * before the star and the bit after it. The star can be in
212 * the middle of the pattern, so we need to look at each bit
215 from_star
= strchr(from
, '*');
216 to_star
= strchr(to
, '*');
218 assert(from_star
&& to_star
);
220 /* star offset, both in 'from' and in 'name' */
221 star_offset
= from_star
- from
;
223 /* the first half is copied over */
224 git_buf_put(out
, to
, to_star
- to
);
226 /* then we copy over the replacement, from the star's offset to the next slash in 'name' */
227 name_slash
= strchr(name
+ star_offset
, '/');
229 name_slash
= strrchr(name
, '\0');
231 /* if there is no slash after the star in 'from', we want to copy everything over */
232 from_slash
= strchr(from
+ star_offset
, '/');
234 name_slash
= strrchr(name
, '\0');
236 replacement_len
= (name_slash
- name
) - star_offset
;
237 git_buf_put(out
, name
+ star_offset
, replacement_len
);
239 return git_buf_puts(out
, to_star
+ 1);
242 int git_refspec_transform(git_buf
*out
, const git_refspec
*spec
, const char *name
)
244 assert(out
&& spec
&& name
);
245 git_buf_sanitize(out
);
247 if (!git_refspec_src_matches(spec
, name
)) {
248 giterr_set(GITERR_INVALID
, "ref '%s' doesn't match the source", name
);
253 return git_buf_puts(out
, spec
->dst
);
255 return refspec_transform(out
, spec
->src
, spec
->dst
, name
);
258 int git_refspec_rtransform(git_buf
*out
, const git_refspec
*spec
, const char *name
)
260 assert(out
&& spec
&& name
);
261 git_buf_sanitize(out
);
263 if (!git_refspec_dst_matches(spec
, name
)) {
264 giterr_set(GITERR_INVALID
, "ref '%s' doesn't match the destination", name
);
269 return git_buf_puts(out
, spec
->src
);
271 return refspec_transform(out
, spec
->dst
, spec
->src
, name
);
274 int git_refspec__serialize(git_buf
*out
, const git_refspec
*refspec
)
277 git_buf_putc(out
, '+');
279 git_buf_printf(out
, "%s:%s",
280 refspec
->src
!= NULL
? refspec
->src
: "",
281 refspec
->dst
!= NULL
? refspec
->dst
: "");
283 return git_buf_oom(out
) == false;
286 int git_refspec_is_wildcard(const git_refspec
*spec
)
288 assert(spec
&& spec
->src
);
290 return (spec
->src
[strlen(spec
->src
) - 1] == '*');
293 git_direction
git_refspec_direction(const git_refspec
*spec
)
300 int git_refspec__dwim_one(git_vector
*out
, git_refspec
*spec
, git_vector
*refs
)
302 git_buf buf
= GIT_BUF_INIT
;
306 const char* formatters
[] = {
308 GIT_REFS_TAGS_DIR
"%s",
309 GIT_REFS_HEADS_DIR
"%s",
313 git_refspec
*cur
= git__calloc(1, sizeof(git_refspec
));
314 GITERR_CHECK_ALLOC(cur
);
316 cur
->force
= spec
->force
;
317 cur
->push
= spec
->push
;
318 cur
->pattern
= spec
->pattern
;
319 cur
->matching
= spec
->matching
;
320 cur
->string
= git__strdup(spec
->string
);
322 /* shorthand on the lhs */
323 if (git__prefixcmp(spec
->src
, GIT_REFS_DIR
)) {
324 for (j
= 0; formatters
[j
]; j
++) {
326 git_buf_printf(&buf
, formatters
[j
], spec
->src
);
327 GITERR_CHECK_ALLOC_BUF(&buf
);
329 key
.name
= (char *) git_buf_cstr(&buf
);
330 if (!git_vector_search(&pos
, refs
, &key
)) {
331 /* we found something to match the shorthand, set src to that */
332 cur
->src
= git_buf_detach(&buf
);
337 /* No shorthands found, copy over the name */
338 if (cur
->src
== NULL
&& spec
->src
!= NULL
) {
339 cur
->src
= git__strdup(spec
->src
);
340 GITERR_CHECK_ALLOC(cur
->src
);
343 if (spec
->dst
&& git__prefixcmp(spec
->dst
, GIT_REFS_DIR
)) {
344 /* if it starts with "remotes" then we just prepend "refs/" */
345 if (!git__prefixcmp(spec
->dst
, "remotes/")) {
346 git_buf_puts(&buf
, GIT_REFS_DIR
);
348 git_buf_puts(&buf
, GIT_REFS_HEADS_DIR
);
351 git_buf_puts(&buf
, spec
->dst
);
352 GITERR_CHECK_ALLOC_BUF(&buf
);
354 cur
->dst
= git_buf_detach(&buf
);
359 if (cur
->dst
== NULL
&& spec
->dst
!= NULL
) {
360 cur
->dst
= git__strdup(spec
->dst
);
361 GITERR_CHECK_ALLOC(cur
->dst
);
364 return git_vector_insert(out
, cur
);