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.
10 #include "git2/errors.h"
15 #include "wildmatch.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 GIT_ERROR_CHECK_ALLOC(refspec
->string
);
47 refspec
->src
= git__strdup("");
48 GIT_ERROR_CHECK_ALLOC(refspec
->src
);
49 refspec
->dst
= git__strdup("");
50 GIT_ERROR_CHECK_ALLOC(refspec
->dst
);
55 size_t rlen
= strlen(++rhs
);
56 if (rlen
|| !is_fetch
) {
57 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
58 refspec
->dst
= git__strndup(rhs
, rlen
);
62 llen
= (rhs
? (size_t)(rhs
- lhs
- 1) : strlen(lhs
));
63 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
64 if ((rhs
&& !is_glob
) || (!rhs
&& is_fetch
))
67 } else if (rhs
&& is_glob
)
70 refspec
->pattern
= is_glob
;
71 refspec
->src
= git__strndup(lhs
, llen
);
72 flags
= GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL
|
73 GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND
|
74 (is_glob
? GIT_REFERENCE_FORMAT_REFSPEC_PATTERN
: 0);
79 * - empty is allowed; it means HEAD.
80 * - otherwise it must be a valid looking ref.
84 else if (!git_reference__is_valid_name(refspec
->src
, flags
))
88 * - missing is ok, and is same as empty.
89 * - empty is ok; it means not to store.
90 * - otherwise it must be a valid looking ref.
94 else if (!*refspec
->dst
)
96 else if (!git_reference__is_valid_name(refspec
->dst
, flags
))
101 * - empty is allowed; it means delete.
102 * - when wildcarded, it must be a valid looking ref.
103 * - otherwise, it must be an extended SHA-1, but
104 * there is no existing way to validate this.
109 if (!git_reference__is_valid_name(refspec
->src
, flags
))
113 ; /* anything goes, for now */
117 * - missing is allowed, but LHS then must be a
119 * - empty is not allowed.
120 * - otherwise it must be a valid looking ref.
123 if (!git_reference__is_valid_name(refspec
->src
, flags
))
125 } else if (!*refspec
->dst
) {
128 if (!git_reference__is_valid_name(refspec
->dst
, flags
))
132 /* if the RHS is empty, then it's a copy of the LHS */
134 refspec
->dst
= git__strdup(refspec
->src
);
135 GIT_ERROR_CHECK_ALLOC(refspec
->dst
);
139 refspec
->string
= git__strdup(input
);
140 GIT_ERROR_CHECK_ALLOC(refspec
->string
);
147 "'%s' is not a valid refspec.", input
);
148 git_refspec__dispose(refspec
);
152 void git_refspec__dispose(git_refspec
*refspec
)
157 git__free(refspec
->src
);
158 git__free(refspec
->dst
);
159 git__free(refspec
->string
);
161 memset(refspec
, 0x0, sizeof(git_refspec
));
164 int git_refspec_parse(git_refspec
**out_refspec
, const char *input
, int is_fetch
)
166 git_refspec
*refspec
;
167 assert(out_refspec
&& input
);
171 refspec
= git__malloc(sizeof(git_refspec
));
172 GIT_ERROR_CHECK_ALLOC(refspec
);
174 if (git_refspec__parse(refspec
, input
, !!is_fetch
) != 0) {
179 *out_refspec
= refspec
;
183 void git_refspec_free(git_refspec
*refspec
)
185 git_refspec__dispose(refspec
);
189 const char *git_refspec_src(const git_refspec
*refspec
)
191 return refspec
== NULL
? NULL
: refspec
->src
;
194 const char *git_refspec_dst(const git_refspec
*refspec
)
196 return refspec
== NULL
? NULL
: refspec
->dst
;
199 const char *git_refspec_string(const git_refspec
*refspec
)
201 return refspec
== NULL
? NULL
: refspec
->string
;
204 int git_refspec_force(const git_refspec
*refspec
)
208 return refspec
->force
;
211 int git_refspec_src_matches(const git_refspec
*refspec
, const char *refname
)
213 if (refspec
== NULL
|| refspec
->src
== NULL
)
216 return (wildmatch(refspec
->src
, refname
, 0) == 0);
219 int git_refspec_dst_matches(const git_refspec
*refspec
, const char *refname
)
221 if (refspec
== NULL
|| refspec
->dst
== NULL
)
224 return (wildmatch(refspec
->dst
, refname
, 0) == 0);
227 static int refspec_transform(
228 git_buf
*out
, const char *from
, const char *to
, const char *name
)
230 const char *from_star
, *to_star
;
231 size_t replacement_len
, star_offset
;
233 git_buf_sanitize(out
);
237 * There are two parts to each side of a refspec, the bit
238 * before the star and the bit after it. The star can be in
239 * the middle of the pattern, so we need to look at each bit
242 from_star
= strchr(from
, '*');
243 to_star
= strchr(to
, '*');
245 assert(from_star
&& to_star
);
247 /* star offset, both in 'from' and in 'name' */
248 star_offset
= from_star
- from
;
250 /* the first half is copied over */
251 git_buf_put(out
, to
, to_star
- to
);
254 * Copy over the name, but exclude the trailing part in "from" starting
257 replacement_len
= strlen(name
+ star_offset
) - strlen(from_star
+ 1);
258 git_buf_put(out
, name
+ star_offset
, replacement_len
);
260 return git_buf_puts(out
, to_star
+ 1);
263 int git_refspec_transform(git_buf
*out
, const git_refspec
*spec
, const char *name
)
265 assert(out
&& spec
&& name
);
266 git_buf_sanitize(out
);
268 if (!git_refspec_src_matches(spec
, name
)) {
269 git_error_set(GIT_ERROR_INVALID
, "ref '%s' doesn't match the source", name
);
274 return git_buf_puts(out
, spec
->dst
? spec
->dst
: "");
276 return refspec_transform(out
, spec
->src
, spec
->dst
, name
);
279 int git_refspec_rtransform(git_buf
*out
, const git_refspec
*spec
, const char *name
)
281 assert(out
&& spec
&& name
);
282 git_buf_sanitize(out
);
284 if (!git_refspec_dst_matches(spec
, name
)) {
285 git_error_set(GIT_ERROR_INVALID
, "ref '%s' doesn't match the destination", name
);
290 return git_buf_puts(out
, spec
->src
);
292 return refspec_transform(out
, spec
->dst
, spec
->src
, name
);
295 int git_refspec__serialize(git_buf
*out
, const git_refspec
*refspec
)
298 git_buf_putc(out
, '+');
300 git_buf_printf(out
, "%s:%s",
301 refspec
->src
!= NULL
? refspec
->src
: "",
302 refspec
->dst
!= NULL
? refspec
->dst
: "");
304 return git_buf_oom(out
) == false;
307 int git_refspec_is_wildcard(const git_refspec
*spec
)
309 assert(spec
&& spec
->src
);
311 return (spec
->src
[strlen(spec
->src
) - 1] == '*');
314 git_direction
git_refspec_direction(const git_refspec
*spec
)
321 int git_refspec__dwim_one(git_vector
*out
, git_refspec
*spec
, git_vector
*refs
)
323 git_buf buf
= GIT_BUF_INIT
;
328 const char* formatters
[] = {
330 GIT_REFS_TAGS_DIR
"%s",
331 GIT_REFS_HEADS_DIR
"%s",
335 assert(out
&& spec
&& refs
);
337 cur
= git__calloc(1, sizeof(git_refspec
));
338 GIT_ERROR_CHECK_ALLOC(cur
);
340 cur
->force
= spec
->force
;
341 cur
->push
= spec
->push
;
342 cur
->pattern
= spec
->pattern
;
343 cur
->matching
= spec
->matching
;
344 cur
->string
= git__strdup(spec
->string
);
346 /* shorthand on the lhs */
347 if (git__prefixcmp(spec
->src
, GIT_REFS_DIR
)) {
348 for (j
= 0; formatters
[j
]; j
++) {
350 git_buf_printf(&buf
, formatters
[j
], spec
->src
);
351 GIT_ERROR_CHECK_ALLOC_BUF(&buf
);
353 key
.name
= (char *) git_buf_cstr(&buf
);
354 if (!git_vector_search(&pos
, refs
, &key
)) {
355 /* we found something to match the shorthand, set src to that */
356 cur
->src
= git_buf_detach(&buf
);
361 /* No shorthands found, copy over the name */
362 if (cur
->src
== NULL
&& spec
->src
!= NULL
) {
363 cur
->src
= git__strdup(spec
->src
);
364 GIT_ERROR_CHECK_ALLOC(cur
->src
);
367 if (spec
->dst
&& git__prefixcmp(spec
->dst
, GIT_REFS_DIR
)) {
368 /* if it starts with "remotes" then we just prepend "refs/" */
369 if (!git__prefixcmp(spec
->dst
, "remotes/")) {
370 git_buf_puts(&buf
, GIT_REFS_DIR
);
372 git_buf_puts(&buf
, GIT_REFS_HEADS_DIR
);
375 git_buf_puts(&buf
, spec
->dst
);
376 GIT_ERROR_CHECK_ALLOC_BUF(&buf
);
378 cur
->dst
= git_buf_detach(&buf
);
381 git_buf_dispose(&buf
);
383 if (cur
->dst
== NULL
&& spec
->dst
!= NULL
) {
384 cur
->dst
= git__strdup(spec
->dst
);
385 GIT_ERROR_CHECK_ALLOC(cur
->dst
);
388 return git_vector_insert(out
, cur
);