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"
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 (p_fnmatch(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 (p_fnmatch(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 const char *name_slash
, *from_slash
;
232 size_t replacement_len
, star_offset
;
234 git_buf_sanitize(out
);
238 * There are two parts to each side of a refspec, the bit
239 * before the star and the bit after it. The star can be in
240 * the middle of the pattern, so we need to look at each bit
243 from_star
= strchr(from
, '*');
244 to_star
= strchr(to
, '*');
246 assert(from_star
&& to_star
);
248 /* star offset, both in 'from' and in 'name' */
249 star_offset
= from_star
- from
;
251 /* the first half is copied over */
252 git_buf_put(out
, to
, to_star
- to
);
254 /* then we copy over the replacement, from the star's offset to the next slash in 'name' */
255 name_slash
= strchr(name
+ star_offset
, '/');
257 name_slash
= strrchr(name
, '\0');
259 /* if there is no slash after the star in 'from', we want to copy everything over */
260 from_slash
= strchr(from
+ star_offset
, '/');
262 name_slash
= strrchr(name
, '\0');
264 replacement_len
= (name_slash
- name
) - star_offset
;
265 git_buf_put(out
, name
+ star_offset
, replacement_len
);
267 return git_buf_puts(out
, to_star
+ 1);
270 int git_refspec_transform(git_buf
*out
, const git_refspec
*spec
, const char *name
)
272 assert(out
&& spec
&& name
);
273 git_buf_sanitize(out
);
275 if (!git_refspec_src_matches(spec
, name
)) {
276 git_error_set(GIT_ERROR_INVALID
, "ref '%s' doesn't match the source", name
);
281 return git_buf_puts(out
, spec
->dst
? spec
->dst
: "");
283 return refspec_transform(out
, spec
->src
, spec
->dst
, name
);
286 int git_refspec_rtransform(git_buf
*out
, const git_refspec
*spec
, const char *name
)
288 assert(out
&& spec
&& name
);
289 git_buf_sanitize(out
);
291 if (!git_refspec_dst_matches(spec
, name
)) {
292 git_error_set(GIT_ERROR_INVALID
, "ref '%s' doesn't match the destination", name
);
297 return git_buf_puts(out
, spec
->src
);
299 return refspec_transform(out
, spec
->dst
, spec
->src
, name
);
302 int git_refspec__serialize(git_buf
*out
, const git_refspec
*refspec
)
305 git_buf_putc(out
, '+');
307 git_buf_printf(out
, "%s:%s",
308 refspec
->src
!= NULL
? refspec
->src
: "",
309 refspec
->dst
!= NULL
? refspec
->dst
: "");
311 return git_buf_oom(out
) == false;
314 int git_refspec_is_wildcard(const git_refspec
*spec
)
316 assert(spec
&& spec
->src
);
318 return (spec
->src
[strlen(spec
->src
) - 1] == '*');
321 git_direction
git_refspec_direction(const git_refspec
*spec
)
328 int git_refspec__dwim_one(git_vector
*out
, git_refspec
*spec
, git_vector
*refs
)
330 git_buf buf
= GIT_BUF_INIT
;
335 const char* formatters
[] = {
337 GIT_REFS_TAGS_DIR
"%s",
338 GIT_REFS_HEADS_DIR
"%s",
342 assert(out
&& spec
&& refs
);
344 cur
= git__calloc(1, sizeof(git_refspec
));
345 GIT_ERROR_CHECK_ALLOC(cur
);
347 cur
->force
= spec
->force
;
348 cur
->push
= spec
->push
;
349 cur
->pattern
= spec
->pattern
;
350 cur
->matching
= spec
->matching
;
351 cur
->string
= git__strdup(spec
->string
);
353 /* shorthand on the lhs */
354 if (git__prefixcmp(spec
->src
, GIT_REFS_DIR
)) {
355 for (j
= 0; formatters
[j
]; j
++) {
357 git_buf_printf(&buf
, formatters
[j
], spec
->src
);
358 GIT_ERROR_CHECK_ALLOC_BUF(&buf
);
360 key
.name
= (char *) git_buf_cstr(&buf
);
361 if (!git_vector_search(&pos
, refs
, &key
)) {
362 /* we found something to match the shorthand, set src to that */
363 cur
->src
= git_buf_detach(&buf
);
368 /* No shorthands found, copy over the name */
369 if (cur
->src
== NULL
&& spec
->src
!= NULL
) {
370 cur
->src
= git__strdup(spec
->src
);
371 GIT_ERROR_CHECK_ALLOC(cur
->src
);
374 if (spec
->dst
&& git__prefixcmp(spec
->dst
, GIT_REFS_DIR
)) {
375 /* if it starts with "remotes" then we just prepend "refs/" */
376 if (!git__prefixcmp(spec
->dst
, "remotes/")) {
377 git_buf_puts(&buf
, GIT_REFS_DIR
);
379 git_buf_puts(&buf
, GIT_REFS_HEADS_DIR
);
382 git_buf_puts(&buf
, spec
->dst
);
383 GIT_ERROR_CHECK_ALLOC_BUF(&buf
);
385 cur
->dst
= git_buf_detach(&buf
);
388 git_buf_dispose(&buf
);
390 if (cur
->dst
== NULL
&& spec
->dst
!= NULL
) {
391 cur
->dst
= git__strdup(spec
->dst
);
392 GIT_ERROR_CHECK_ALLOC(cur
->dst
);
395 return git_vector_insert(out
, cur
);