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
;
27 GIT_ASSERT_ARG(refspec
);
28 GIT_ASSERT_ARG(input
);
30 memset(refspec
, 0x0, sizeof(git_refspec
));
31 refspec
->push
= !is_fetch
;
39 rhs
= strrchr(lhs
, ':');
42 * Before going on, special case ":" (or "+:") as a refspec
45 if (!is_fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
46 refspec
->matching
= 1;
47 refspec
->string
= git__strdup(input
);
48 GIT_ERROR_CHECK_ALLOC(refspec
->string
);
49 refspec
->src
= git__strdup("");
50 GIT_ERROR_CHECK_ALLOC(refspec
->src
);
51 refspec
->dst
= git__strdup("");
52 GIT_ERROR_CHECK_ALLOC(refspec
->dst
);
57 size_t rlen
= strlen(++rhs
);
58 if (rlen
|| !is_fetch
) {
59 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
60 refspec
->dst
= git__strndup(rhs
, rlen
);
64 llen
= (rhs
? (size_t)(rhs
- lhs
- 1) : strlen(lhs
));
65 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
66 if ((rhs
&& !is_glob
) || (!rhs
&& is_fetch
))
69 } else if (rhs
&& is_glob
)
72 refspec
->pattern
= is_glob
;
73 refspec
->src
= git__strndup(lhs
, llen
);
74 flags
= GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL
|
75 GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND
|
76 (is_glob
? GIT_REFERENCE_FORMAT_REFSPEC_PATTERN
: 0);
81 * - empty is allowed; it means HEAD.
82 * - otherwise it must be a valid looking ref.
86 else if (git_reference__name_is_valid(&valid
, refspec
->src
, flags
) < 0)
93 * - missing is ok, and is same as empty.
94 * - empty is ok; it means not to store.
95 * - otherwise it must be a valid looking ref.
99 else if (!*refspec
->dst
)
101 else if (git_reference__name_is_valid(&valid
, refspec
->dst
, flags
) < 0)
108 * - empty is allowed; it means delete.
109 * - when wildcarded, it must be a valid looking ref.
110 * - otherwise, it must be an extended SHA-1, but
111 * there is no existing way to validate this.
116 if (git_reference__name_is_valid(&valid
, refspec
->src
, flags
) < 0)
122 ; /* anything goes, for now */
127 * - missing is allowed, but LHS then must be a
129 * - empty is not allowed.
130 * - otherwise it must be a valid looking ref.
133 if (git_reference__name_is_valid(&valid
, refspec
->src
, flags
) < 0)
137 } else if (!*refspec
->dst
) {
140 if (git_reference__name_is_valid(&valid
, refspec
->dst
, flags
) < 0)
146 /* if the RHS is empty, then it's a copy of the LHS */
148 refspec
->dst
= git__strdup(refspec
->src
);
149 GIT_ERROR_CHECK_ALLOC(refspec
->dst
);
153 refspec
->string
= git__strdup(input
);
154 GIT_ERROR_CHECK_ALLOC(refspec
->string
);
159 git_error_set(GIT_ERROR_INVALID
,
160 "'%s' is not a valid refspec.", input
);
161 git_refspec__dispose(refspec
);
162 return GIT_EINVALIDSPEC
;
165 git_refspec__dispose(refspec
);
169 void git_refspec__dispose(git_refspec
*refspec
)
174 git__free(refspec
->src
);
175 git__free(refspec
->dst
);
176 git__free(refspec
->string
);
178 memset(refspec
, 0x0, sizeof(git_refspec
));
181 int git_refspec_parse(git_refspec
**out_refspec
, const char *input
, int is_fetch
)
183 git_refspec
*refspec
;
184 GIT_ASSERT_ARG(out_refspec
);
185 GIT_ASSERT_ARG(input
);
189 refspec
= git__malloc(sizeof(git_refspec
));
190 GIT_ERROR_CHECK_ALLOC(refspec
);
192 if (git_refspec__parse(refspec
, input
, !!is_fetch
) != 0) {
197 *out_refspec
= refspec
;
201 void git_refspec_free(git_refspec
*refspec
)
203 git_refspec__dispose(refspec
);
207 const char *git_refspec_src(const git_refspec
*refspec
)
209 return refspec
== NULL
? NULL
: refspec
->src
;
212 const char *git_refspec_dst(const git_refspec
*refspec
)
214 return refspec
== NULL
? NULL
: refspec
->dst
;
217 const char *git_refspec_string(const git_refspec
*refspec
)
219 return refspec
== NULL
? NULL
: refspec
->string
;
222 int git_refspec_force(const git_refspec
*refspec
)
224 GIT_ASSERT_ARG(refspec
);
226 return refspec
->force
;
229 int git_refspec_src_matches(const git_refspec
*refspec
, const char *refname
)
231 if (refspec
== NULL
|| refspec
->src
== NULL
)
234 return (wildmatch(refspec
->src
, refname
, 0) == 0);
237 int git_refspec_dst_matches(const git_refspec
*refspec
, const char *refname
)
239 if (refspec
== NULL
|| refspec
->dst
== NULL
)
242 return (wildmatch(refspec
->dst
, refname
, 0) == 0);
245 static int refspec_transform(
246 git_buf
*out
, const char *from
, const char *to
, const char *name
)
248 const char *from_star
, *to_star
;
249 size_t replacement_len
, star_offset
;
252 if ((error
= git_buf_sanitize(out
)) < 0)
258 * There are two parts to each side of a refspec, the bit
259 * before the star and the bit after it. The star can be in
260 * the middle of the pattern, so we need to look at each bit
263 from_star
= strchr(from
, '*');
264 to_star
= strchr(to
, '*');
266 GIT_ASSERT(from_star
&& to_star
);
268 /* star offset, both in 'from' and in 'name' */
269 star_offset
= from_star
- from
;
271 /* the first half is copied over */
272 git_buf_put(out
, to
, to_star
- to
);
275 * Copy over the name, but exclude the trailing part in "from" starting
278 replacement_len
= strlen(name
+ star_offset
) - strlen(from_star
+ 1);
279 git_buf_put(out
, name
+ star_offset
, replacement_len
);
281 return git_buf_puts(out
, to_star
+ 1);
284 int git_refspec_transform(git_buf
*out
, const git_refspec
*spec
, const char *name
)
289 GIT_ASSERT_ARG(spec
);
290 GIT_ASSERT_ARG(name
);
292 if ((error
= git_buf_sanitize(out
)) < 0)
295 if (!git_refspec_src_matches(spec
, name
)) {
296 git_error_set(GIT_ERROR_INVALID
, "ref '%s' doesn't match the source", name
);
301 return git_buf_puts(out
, spec
->dst
? spec
->dst
: "");
303 return refspec_transform(out
, spec
->src
, spec
->dst
, name
);
306 int git_refspec_rtransform(git_buf
*out
, const git_refspec
*spec
, const char *name
)
311 GIT_ASSERT_ARG(spec
);
312 GIT_ASSERT_ARG(name
);
314 if ((error
= git_buf_sanitize(out
)) < 0)
317 if (!git_refspec_dst_matches(spec
, name
)) {
318 git_error_set(GIT_ERROR_INVALID
, "ref '%s' doesn't match the destination", name
);
323 return git_buf_puts(out
, spec
->src
);
325 return refspec_transform(out
, spec
->dst
, spec
->src
, name
);
328 int git_refspec__serialize(git_buf
*out
, const git_refspec
*refspec
)
331 git_buf_putc(out
, '+');
333 git_buf_printf(out
, "%s:%s",
334 refspec
->src
!= NULL
? refspec
->src
: "",
335 refspec
->dst
!= NULL
? refspec
->dst
: "");
337 return git_buf_oom(out
) == false;
340 int git_refspec_is_wildcard(const git_refspec
*spec
)
342 GIT_ASSERT_ARG(spec
);
343 GIT_ASSERT_ARG(spec
->src
);
345 return (spec
->src
[strlen(spec
->src
) - 1] == '*');
348 git_direction
git_refspec_direction(const git_refspec
*spec
)
350 GIT_ASSERT_ARG(spec
);
355 int git_refspec__dwim_one(git_vector
*out
, git_refspec
*spec
, git_vector
*refs
)
357 git_buf buf
= GIT_BUF_INIT
;
362 const char *formatters
[] = {
364 GIT_REFS_TAGS_DIR
"%s",
365 GIT_REFS_HEADS_DIR
"%s",
370 GIT_ASSERT_ARG(spec
);
371 GIT_ASSERT_ARG(refs
);
373 cur
= git__calloc(1, sizeof(git_refspec
));
374 GIT_ERROR_CHECK_ALLOC(cur
);
376 cur
->force
= spec
->force
;
377 cur
->push
= spec
->push
;
378 cur
->pattern
= spec
->pattern
;
379 cur
->matching
= spec
->matching
;
380 cur
->string
= git__strdup(spec
->string
);
382 /* shorthand on the lhs */
383 if (git__prefixcmp(spec
->src
, GIT_REFS_DIR
)) {
384 for (j
= 0; formatters
[j
]; j
++) {
386 git_buf_printf(&buf
, formatters
[j
], spec
->src
);
387 GIT_ERROR_CHECK_ALLOC_BUF(&buf
);
389 key
.name
= (char *) git_buf_cstr(&buf
);
390 if (!git_vector_search(&pos
, refs
, &key
)) {
391 /* we found something to match the shorthand, set src to that */
392 cur
->src
= git_buf_detach(&buf
);
397 /* No shorthands found, copy over the name */
398 if (cur
->src
== NULL
&& spec
->src
!= NULL
) {
399 cur
->src
= git__strdup(spec
->src
);
400 GIT_ERROR_CHECK_ALLOC(cur
->src
);
403 if (spec
->dst
&& git__prefixcmp(spec
->dst
, GIT_REFS_DIR
)) {
404 /* if it starts with "remotes" then we just prepend "refs/" */
405 if (!git__prefixcmp(spec
->dst
, "remotes/")) {
406 git_buf_puts(&buf
, GIT_REFS_DIR
);
408 git_buf_puts(&buf
, GIT_REFS_HEADS_DIR
);
411 git_buf_puts(&buf
, spec
->dst
);
412 GIT_ERROR_CHECK_ALLOC_BUF(&buf
);
414 cur
->dst
= git_buf_detach(&buf
);
417 git_buf_dispose(&buf
);
419 if (cur
->dst
== NULL
&& spec
->dst
!= NULL
) {
420 cur
->dst
= git__strdup(spec
->dst
);
421 GIT_ERROR_CHECK_ALLOC(cur
->dst
);
424 return git_vector_insert(out
, cur
);