Raw
1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "hash.h"
6 #include "hex.h"
7 #include "string-list.h"
8 #include "strvec.h"
9 #include "refs.h"
10 #include "refspec.h"
11 #include "remote.h"
12 #include "strbuf.h"
13
14 /*
15 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
16 * Returns 1 if successful and 0 if the refspec is invalid.
17 */
18 static int parse_refspec(struct refspec_item *item, const char *refspec,
19 const struct git_hash_algo *algo, int fetch)
20 {
21 size_t llen;
22 int is_glob;
23 const char *lhs, *rhs;
24 int flags;
25
26 is_glob = 0;
27
28 lhs = refspec;
29 if (*lhs == '+') {
30 item->force = 1;
31 lhs++;
32 } else if (*lhs == '^') {
33 item->negative = 1;
34 lhs++;
35 }
36
37 rhs = strrchr(lhs, ':');
38
39 /* negative refspecs only have one side */
40 if (item->negative && rhs)
41 return 0;
42
43 /*
44 * Before going on, special case ":" (or "+:") as a refspec
45 * for pushing matching refs.
46 */
47 if (!fetch && rhs == lhs && rhs[1] == '\0') {
48 item->matching = 1;
49 return 1;
50 }
51
52 if (rhs) {
53 size_t rlen = strlen(++rhs);
54 is_glob = (1 <= rlen && strchr(rhs, '*'));
55 item->dst = xstrndup(rhs, rlen);
56 } else {
57 item->dst = NULL;
58 }
59
60 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
61 if (1 <= llen && memchr(lhs, '*', llen)) {
62 if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
63 return 0;
64 is_glob = 1;
65 } else if (rhs && is_glob) {
66 return 0;
67 }
68
69 item->pattern = is_glob;
70 if (llen == 1 && *lhs == '@')
71 item->src = xstrdup("HEAD");
72 else
73 item->src = xstrndup(lhs, llen);
74 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
75
76 if (item->negative) {
77 struct object_id unused;
78
79 /*
80 * Negative refspecs only have a LHS, which indicates a ref
81 * (or pattern of refs) to exclude from other matches. This
82 * can either be a simple ref, or a glob pattern. Exact sha1
83 * match is not currently supported.
84 */
85 if (!*item->src)
86 return 0; /* negative refspecs must not be empty */
87 else if (llen == algo->hexsz && !get_oid_hex_algop(item->src, &unused, algo))
88 return 0; /* negative refspecs cannot be exact sha1 */
89 else if (!check_refname_format(item->src, flags))
90 ; /* valid looking ref is ok */
91 else
92 return 0;
93
94 /* the other rules below do not apply to negative refspecs */
95 return 1;
96 }
97
98 if (fetch) {
99 struct object_id unused;
100
101 /* LHS */
102 if (!*item->src)
103 ; /* empty is ok; it means "HEAD" */
104 else if (llen == algo->hexsz && !get_oid_hex_algop(item->src, &unused, algo))
105 item->exact_sha1 = 1; /* ok */
106 else if (!check_refname_format(item->src, flags))
107 ; /* valid looking ref is ok */
108 else
109 return 0;
110 /* RHS */
111 if (!item->dst)
112 ; /* missing is ok; it is the same as empty */
113 else if (!*item->dst)
114 ; /* empty is ok; it means "do not store" */
115 else if (!check_refname_format(item->dst, flags))
116 ; /* valid looking ref is ok */
117 else
118 return 0;
119 } else {
120 /*
121 * LHS
122 * - empty is allowed; it means delete.
123 * - when wildcarded, it must be a valid looking ref.
124 * - otherwise, it must be an extended SHA-1, but
125 * there is no existing way to validate this.
126 */
127 if (!*item->src)
128 ; /* empty is ok */
129 else if (is_glob) {
130 if (check_refname_format(item->src, flags))
131 return 0;
132 }
133 else
134 ; /* anything goes, for now */
135 /*
136 * RHS
137 * - missing is allowed, but LHS then must be a
138 * valid looking ref.
139 * - empty is not allowed.
140 * - otherwise it must be a valid looking ref.
141 */
142 if (!item->dst) {
143 if (check_refname_format(item->src, flags))
144 return 0;
145 } else if (!*item->dst) {
146 return 0;
147 } else {
148 if (check_refname_format(item->dst, flags))
149 return 0;
150 }
151 }
152
153 return 1;
154 }
155
156 static int refspec_item_init(struct refspec_item *item, const char *refspec,
157 const struct git_hash_algo *algo, int fetch)
158 {
159 memset(item, 0, sizeof(*item));
160 item->raw = xstrdup(refspec);
161 return parse_refspec(item, refspec, algo, fetch);
162 }
163
164 int refspec_item_init_fetch(struct refspec_item *item, const char *refspec,
165 const struct git_hash_algo *algo)
166 {
167 return refspec_item_init(item, refspec, algo, 1);
168 }
169
170 int refspec_item_init_push(struct refspec_item *item, const char *refspec,
171 const struct git_hash_algo *algo)
172 {
173 return refspec_item_init(item, refspec, algo, 0);
174 }
175
176 void refspec_item_clear(struct refspec_item *item)
177 {
178 FREE_AND_NULL(item->src);
179 FREE_AND_NULL(item->dst);
180 FREE_AND_NULL(item->raw);
181 item->force = 0;
182 item->pattern = 0;
183 item->matching = 0;
184 item->exact_sha1 = 0;
185 }
186
187 void refspec_init_fetch(struct refspec *rs, const struct git_hash_algo *algo)
188 {
189 struct refspec blank = REFSPEC_INIT_FETCH(algo);
190 memcpy(rs, &blank, sizeof(*rs));
191 }
192
193 void refspec_init_push(struct refspec *rs, const struct git_hash_algo *algo)
194 {
195 struct refspec blank = REFSPEC_INIT_PUSH(algo);
196 memcpy(rs, &blank, sizeof(*rs));
197 }
198
199 void refspec_append(struct refspec *rs, const char *refspec)
200 {
201 struct refspec_item item;
202 int ret;
203
204 if (rs->fetch)
205 ret = refspec_item_init_fetch(&item, refspec, rs->hash_algo);
206 else
207 ret = refspec_item_init_push(&item, refspec, rs->hash_algo);
208 if (!ret)
209 die(_("invalid refspec '%s'"), refspec);
210
211 ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
212 rs->items[rs->nr] = item;
213
214 rs->nr++;
215 }
216
217 void refspec_appendf(struct refspec *rs, const char *fmt, ...)
218 {
219 va_list ap;
220 char *buf;
221
222 va_start(ap, fmt);
223 buf = xstrvfmt(fmt, ap);
224 va_end(ap);
225
226 refspec_append(rs, buf);
227 free(buf);
228 }
229
230 void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
231 {
232 int i;
233 for (i = 0; i < nr; i++)
234 refspec_append(rs, refspecs[i]);
235 }
236
237 void refspec_clear(struct refspec *rs)
238 {
239 int i;
240
241 for (i = 0; i < rs->nr; i++)
242 refspec_item_clear(&rs->items[i]);
243
244 FREE_AND_NULL(rs->items);
245 rs->alloc = 0;
246 rs->nr = 0;
247
248 rs->fetch = 0;
249 }
250
251 int valid_fetch_refspec(const char *fetch_refspec_str,
252 const struct git_hash_algo *algo)
253 {
254 struct refspec_item refspec;
255 int ret = refspec_item_init_fetch(&refspec, fetch_refspec_str, algo);
256 refspec_item_clear(&refspec);
257 return ret;
258 }
259
260 void refspec_ref_prefixes(const struct refspec *rs,
261 struct strvec *ref_prefixes)
262 {
263 int i;
264 for (i = 0; i < rs->nr; i++) {
265 const struct refspec_item *item = &rs->items[i];
266 const char *prefix = NULL;
267
268 if (item->negative)
269 continue;
270
271 if (rs->fetch) {
272 if (item->exact_sha1)
273 continue;
274 prefix = item->src;
275 } else {
276 /*
277 * Pushes can have an explicit destination like
278 * "foo:bar", or can implicitly use the src for both
279 * ("foo" is the same as "foo:foo").
280 */
281 if (item->dst)
282 prefix = item->dst;
283 else if (item->src && !item->exact_sha1)
284 prefix = item->src;
285 }
286
287 if (!prefix)
288 continue;
289
290 if (item->pattern) {
291 const char *glob = strchr(prefix, '*');
292 strvec_pushf(ref_prefixes, "%.*s",
293 (int)(glob - prefix),
294 prefix);
295 } else {
296 expand_ref_prefix(ref_prefixes, prefix);
297 }
298 }
299 }
300
301 int match_refname_with_pattern(const char *pattern, const char *refname,
302 const char *replacement, char **result)
303 {
304 const char *kstar = strchr(pattern, '*');
305 size_t klen;
306 size_t ksuffixlen;
307 size_t namelen;
308 int ret;
309 if (!kstar)
310 die(_("pattern '%s' has no '*'"), pattern);
311 klen = kstar - pattern;
312 ksuffixlen = strlen(kstar + 1);
313 namelen = strlen(refname);
314 ret = !strncmp(refname, pattern, klen) && namelen >= klen + ksuffixlen &&
315 !memcmp(refname + namelen - ksuffixlen, kstar + 1, ksuffixlen);
316 if (ret && replacement) {
317 struct strbuf sb = STRBUF_INIT;
318 const char *vstar = strchr(replacement, '*');
319 if (!vstar)
320 die(_("replacement '%s' has no '*'"), replacement);
321 strbuf_add(&sb, replacement, vstar - replacement);
322 strbuf_add(&sb, refname + klen, namelen - klen - ksuffixlen);
323 strbuf_addstr(&sb, vstar + 1);
324 *result = strbuf_detach(&sb, NULL);
325 }
326 return ret;
327 }
328
329 static int refspec_match(const struct refspec_item *refspec,
330 const char *name)
331 {
332 if (refspec->pattern)
333 return match_refname_with_pattern(refspec->src, name, NULL, NULL);
334
335 return !strcmp(refspec->src, name);
336 }
337
338 int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
339 {
340 int i;
341
342 for (i = 0; i < rs->nr; i++) {
343 if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
344 return 1;
345 }
346 return 0;
347 }
348
349 static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query)
350 {
351 int i, matched_negative = 0;
352 int find_src = !query->src;
353 struct string_list reversed = STRING_LIST_INIT_DUP;
354 const char *needle = find_src ? query->dst : query->src;
355
356 /*
357 * Check whether the queried ref matches any negative refpsec. If so,
358 * then we should ultimately treat this as not matching the query at
359 * all.
360 *
361 * Note that negative refspecs always match the source, but the query
362 * item uses the destination. To handle this, we apply pattern
363 * refspecs in reverse to figure out if the query source matches any
364 * of the negative refspecs.
365 *
366 * The first loop finds and expands all positive refspecs
367 * matched by the queried ref.
368 *
369 * The second loop checks if any of the results of the first loop
370 * match any negative refspec.
371 */
372 for (i = 0; i < rs->nr; i++) {
373 struct refspec_item *refspec = &rs->items[i];
374 char *expn_name;
375
376 if (refspec->negative)
377 continue;
378
379 /* Note the reversal of src and dst */
380 if (refspec->pattern) {
381 const char *key = refspec->dst ? refspec->dst : refspec->src;
382 const char *value = refspec->src;
383
384 if (match_refname_with_pattern(key, needle, value, &expn_name))
385 string_list_append_nodup(&reversed, expn_name);
386 } else if (refspec->matching) {
387 /* For the special matching refspec, any query should match */
388 string_list_append(&reversed, needle);
389 } else if (!refspec->src) {
390 BUG("refspec->src should not be null here");
391 } else if (!strcmp(needle, refspec->src)) {
392 string_list_append(&reversed, refspec->src);
393 }
394 }
395
396 for (i = 0; !matched_negative && i < reversed.nr; i++) {
397 if (refname_matches_negative_refspec_item(reversed.items[i].string, rs))
398 matched_negative = 1;
399 }
400
401 string_list_clear(&reversed, 0);
402
403 return matched_negative;
404 }
405
406 void refspec_find_all_matches(struct refspec *rs,
407 struct refspec_item *query,
408 struct string_list *results)
409 {
410 int i;
411 int find_src = !query->src;
412
413 if (find_src && !query->dst)
414 BUG("refspec_find_all_matches: need either src or dst");
415
416 if (refspec_find_negative_match(rs, query))
417 return;
418
419 for (i = 0; i < rs->nr; i++) {
420 struct refspec_item *refspec = &rs->items[i];
421 const char *key = find_src ? refspec->dst : refspec->src;
422 const char *value = find_src ? refspec->src : refspec->dst;
423 const char *needle = find_src ? query->dst : query->src;
424 char **result = find_src ? &query->src : &query->dst;
425
426 if (!refspec->dst || refspec->negative)
427 continue;
428 if (refspec->pattern) {
429 if (match_refname_with_pattern(key, needle, value, result))
430 string_list_append_nodup(results, *result);
431 } else if (!strcmp(needle, key)) {
432 string_list_append(results, value);
433 }
434 }
435 }
436
437 int refspec_find_match(struct refspec *rs, struct refspec_item *query)
438 {
439 int i;
440 int find_src = !query->src;
441 const char *needle = find_src ? query->dst : query->src;
442 char **result = find_src ? &query->src : &query->dst;
443
444 if (find_src && !query->dst)
445 BUG("refspec_find_match: need either src or dst");
446
447 if (refspec_find_negative_match(rs, query))
448 return -1;
449
450 for (i = 0; i < rs->nr; i++) {
451 struct refspec_item *refspec = &rs->items[i];
452 const char *key = find_src ? refspec->dst : refspec->src;
453 const char *value = find_src ? refspec->src : refspec->dst;
454
455 if (!refspec->dst || refspec->negative)
456 continue;
457 if (refspec->pattern) {
458 if (match_refname_with_pattern(key, needle, value, result)) {
459 query->force = refspec->force;
460 return 0;
461 }
462 } else if (!strcmp(needle, key)) {
463 *result = xstrdup(value);
464 query->force = refspec->force;
465 return 0;
466 }
467 }
468 return -1;
469 }
470
471 struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
472 {
473 struct ref **tail;
474
475 for (tail = &ref_map; *tail; ) {
476 struct ref *ref = *tail;
477
478 if (refname_matches_negative_refspec_item(ref->name, rs)) {
479 *tail = ref->next;
480 free(ref->peer_ref);
481 free(ref);
482 } else
483 tail = &ref->next;
484 }
485
486 return ref_map;
487 }
488
489 char *apply_refspecs(struct refspec *rs, const char *name)
490 {
491 struct refspec_item query;
492
493 memset(&query, 0, sizeof(struct refspec_item));
494 query.src = (char *)name;
495
496 if (refspec_find_match(rs, &query))
497 return NULL;
498
499 return query.dst;
500 }