1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
13 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
14 * Returns 1 if successful and 0 if the refspec is invalid.
16 static int parse_refspec(struct refspec_item
*item
, const char *refspec
, int fetch
)
20 const char *lhs
, *rhs
;
29 } else if (*lhs
== '^') {
34 rhs
= strrchr(lhs
, ':');
36 /* negative refspecs only have one side */
37 if (item
->negative
&& rhs
)
41 * Before going on, special case ":" (or "+:") as a refspec
42 * for pushing matching refs.
44 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
50 size_t rlen
= strlen(++rhs
);
51 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
52 item
->dst
= xstrndup(rhs
, rlen
);
57 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
58 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
59 if ((rhs
&& !is_glob
) || (!rhs
&& !item
->negative
&& fetch
))
62 } else if (rhs
&& is_glob
) {
66 item
->pattern
= is_glob
;
67 if (llen
== 1 && *lhs
== '@')
68 item
->src
= xstrdup("HEAD");
70 item
->src
= xstrndup(lhs
, llen
);
71 flags
= REFNAME_ALLOW_ONELEVEL
| (is_glob
? REFNAME_REFSPEC_PATTERN
: 0);
74 struct object_id unused
;
77 * Negative refspecs only have a LHS, which indicates a ref
78 * (or pattern of refs) to exclude from other matches. This
79 * can either be a simple ref, or a glob pattern. Exact sha1
80 * match is not currently supported.
83 return 0; /* negative refspecs must not be empty */
84 else if (llen
== the_hash_algo
->hexsz
&& !get_oid_hex(item
->src
, &unused
))
85 return 0; /* negative refpsecs cannot be exact sha1 */
86 else if (!check_refname_format(item
->src
, flags
))
87 ; /* valid looking ref is ok */
91 /* the other rules below do not apply to negative refspecs */
96 struct object_id unused
;
100 ; /* empty is ok; it means "HEAD" */
101 else if (llen
== the_hash_algo
->hexsz
&& !get_oid_hex(item
->src
, &unused
))
102 item
->exact_sha1
= 1; /* ok */
103 else if (!check_refname_format(item
->src
, flags
))
104 ; /* valid looking ref is ok */
109 ; /* missing is ok; it is the same as empty */
110 else if (!*item
->dst
)
111 ; /* empty is ok; it means "do not store" */
112 else if (!check_refname_format(item
->dst
, flags
))
113 ; /* valid looking ref is ok */
119 * - empty is allowed; it means delete.
120 * - when wildcarded, it must be a valid looking ref.
121 * - otherwise, it must be an extended SHA-1, but
122 * there is no existing way to validate this.
127 if (check_refname_format(item
->src
, flags
))
131 ; /* anything goes, for now */
134 * - missing is allowed, but LHS then must be a
136 * - empty is not allowed.
137 * - otherwise it must be a valid looking ref.
140 if (check_refname_format(item
->src
, flags
))
142 } else if (!*item
->dst
) {
145 if (check_refname_format(item
->dst
, flags
))
153 int refspec_item_init(struct refspec_item
*item
, const char *refspec
, int fetch
)
155 memset(item
, 0, sizeof(*item
));
156 return parse_refspec(item
, refspec
, fetch
);
159 void refspec_item_init_or_die(struct refspec_item
*item
, const char *refspec
,
162 if (!refspec_item_init(item
, refspec
, fetch
))
163 die(_("invalid refspec '%s'"), refspec
);
166 void refspec_item_clear(struct refspec_item
*item
)
168 FREE_AND_NULL(item
->src
);
169 FREE_AND_NULL(item
->dst
);
173 item
->exact_sha1
= 0;
176 void refspec_init(struct refspec
*rs
, int fetch
)
178 memset(rs
, 0, sizeof(*rs
));
182 static void refspec_append_nodup(struct refspec
*rs
, char *refspec
)
184 struct refspec_item item
;
186 refspec_item_init_or_die(&item
, refspec
, rs
->fetch
);
188 ALLOC_GROW(rs
->items
, rs
->nr
+ 1, rs
->alloc
);
189 rs
->items
[rs
->nr
++] = item
;
191 ALLOC_GROW(rs
->raw
, rs
->raw_nr
+ 1, rs
->raw_alloc
);
192 rs
->raw
[rs
->raw_nr
++] = refspec
;
195 void refspec_append(struct refspec
*rs
, const char *refspec
)
197 refspec_append_nodup(rs
, xstrdup(refspec
));
200 void refspec_appendf(struct refspec
*rs
, const char *fmt
, ...)
205 refspec_append_nodup(rs
, xstrvfmt(fmt
, ap
));
209 void refspec_appendn(struct refspec
*rs
, const char **refspecs
, int nr
)
212 for (i
= 0; i
< nr
; i
++)
213 refspec_append(rs
, refspecs
[i
]);
216 void refspec_clear(struct refspec
*rs
)
220 for (i
= 0; i
< rs
->nr
; i
++)
221 refspec_item_clear(&rs
->items
[i
]);
223 FREE_AND_NULL(rs
->items
);
227 for (i
= 0; i
< rs
->raw_nr
; i
++)
228 free((char *)rs
->raw
[i
]);
229 FREE_AND_NULL(rs
->raw
);
236 int valid_fetch_refspec(const char *fetch_refspec_str
)
238 struct refspec_item refspec
;
239 int ret
= refspec_item_init(&refspec
, fetch_refspec_str
, REFSPEC_FETCH
);
240 refspec_item_clear(&refspec
);
244 int valid_remote_name(const char *name
)
247 struct strbuf refspec
= STRBUF_INIT
;
248 strbuf_addf(&refspec
, "refs/heads/test:refs/remotes/%s/test", name
);
249 result
= valid_fetch_refspec(refspec
.buf
);
250 strbuf_release(&refspec
);
254 void refspec_ref_prefixes(const struct refspec
*rs
,
255 struct strvec
*ref_prefixes
)
258 for (i
= 0; i
< rs
->nr
; i
++) {
259 const struct refspec_item
*item
= &rs
->items
[i
];
260 const char *prefix
= NULL
;
262 if (item
->exact_sha1
|| item
->negative
)
264 if (rs
->fetch
== REFSPEC_FETCH
)
268 else if (item
->src
&& !item
->exact_sha1
)
275 const char *glob
= strchr(prefix
, '*');
276 strvec_pushf(ref_prefixes
, "%.*s",
277 (int)(glob
- prefix
),
280 expand_ref_prefix(ref_prefixes
, prefix
);