]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - remote.c
Tighten refspec processing
[thirdparty/git.git] / remote.c
... / ...
CommitLineData
1#include "cache.h"
2#include "remote.h"
3#include "refs.h"
4
5static struct remote **remotes;
6static int allocated_remotes;
7
8static struct branch **branches;
9static int allocated_branches;
10
11static struct branch *current_branch;
12static const char *default_remote_name;
13
14#define BUF_SIZE (2048)
15static char buffer[BUF_SIZE];
16
17static void add_push_refspec(struct remote *remote, const char *ref)
18{
19 int nr = remote->push_refspec_nr + 1;
20 remote->push_refspec =
21 xrealloc(remote->push_refspec, nr * sizeof(char *));
22 remote->push_refspec[nr-1] = ref;
23 remote->push_refspec_nr = nr;
24}
25
26static void add_fetch_refspec(struct remote *remote, const char *ref)
27{
28 int nr = remote->fetch_refspec_nr + 1;
29 remote->fetch_refspec =
30 xrealloc(remote->fetch_refspec, nr * sizeof(char *));
31 remote->fetch_refspec[nr-1] = ref;
32 remote->fetch_refspec_nr = nr;
33}
34
35static void add_url(struct remote *remote, const char *url)
36{
37 int nr = remote->url_nr + 1;
38 remote->url =
39 xrealloc(remote->url, nr * sizeof(char *));
40 remote->url[nr-1] = url;
41 remote->url_nr = nr;
42}
43
44static struct remote *make_remote(const char *name, int len)
45{
46 int i, empty = -1;
47
48 for (i = 0; i < allocated_remotes; i++) {
49 if (!remotes[i]) {
50 if (empty < 0)
51 empty = i;
52 } else {
53 if (len ? (!strncmp(name, remotes[i]->name, len) &&
54 !remotes[i]->name[len]) :
55 !strcmp(name, remotes[i]->name))
56 return remotes[i];
57 }
58 }
59
60 if (empty < 0) {
61 empty = allocated_remotes;
62 allocated_remotes += allocated_remotes ? allocated_remotes : 1;
63 remotes = xrealloc(remotes,
64 sizeof(*remotes) * allocated_remotes);
65 memset(remotes + empty, 0,
66 (allocated_remotes - empty) * sizeof(*remotes));
67 }
68 remotes[empty] = xcalloc(1, sizeof(struct remote));
69 if (len)
70 remotes[empty]->name = xstrndup(name, len);
71 else
72 remotes[empty]->name = xstrdup(name);
73 return remotes[empty];
74}
75
76static void add_merge(struct branch *branch, const char *name)
77{
78 int nr = branch->merge_nr + 1;
79 branch->merge_name =
80 xrealloc(branch->merge_name, nr * sizeof(char *));
81 branch->merge_name[nr-1] = name;
82 branch->merge_nr = nr;
83}
84
85static struct branch *make_branch(const char *name, int len)
86{
87 int i, empty = -1;
88 char *refname;
89
90 for (i = 0; i < allocated_branches; i++) {
91 if (!branches[i]) {
92 if (empty < 0)
93 empty = i;
94 } else {
95 if (len ? (!strncmp(name, branches[i]->name, len) &&
96 !branches[i]->name[len]) :
97 !strcmp(name, branches[i]->name))
98 return branches[i];
99 }
100 }
101
102 if (empty < 0) {
103 empty = allocated_branches;
104 allocated_branches += allocated_branches ? allocated_branches : 1;
105 branches = xrealloc(branches,
106 sizeof(*branches) * allocated_branches);
107 memset(branches + empty, 0,
108 (allocated_branches - empty) * sizeof(*branches));
109 }
110 branches[empty] = xcalloc(1, sizeof(struct branch));
111 if (len)
112 branches[empty]->name = xstrndup(name, len);
113 else
114 branches[empty]->name = xstrdup(name);
115 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
116 strcpy(refname, "refs/heads/");
117 strcpy(refname + strlen("refs/heads/"),
118 branches[empty]->name);
119 branches[empty]->refname = refname;
120
121 return branches[empty];
122}
123
124static void read_remotes_file(struct remote *remote)
125{
126 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
127
128 if (!f)
129 return;
130 while (fgets(buffer, BUF_SIZE, f)) {
131 int value_list;
132 char *s, *p;
133
134 if (!prefixcmp(buffer, "URL:")) {
135 value_list = 0;
136 s = buffer + 4;
137 } else if (!prefixcmp(buffer, "Push:")) {
138 value_list = 1;
139 s = buffer + 5;
140 } else if (!prefixcmp(buffer, "Pull:")) {
141 value_list = 2;
142 s = buffer + 5;
143 } else
144 continue;
145
146 while (isspace(*s))
147 s++;
148 if (!*s)
149 continue;
150
151 p = s + strlen(s);
152 while (isspace(p[-1]))
153 *--p = 0;
154
155 switch (value_list) {
156 case 0:
157 add_url(remote, xstrdup(s));
158 break;
159 case 1:
160 add_push_refspec(remote, xstrdup(s));
161 break;
162 case 2:
163 add_fetch_refspec(remote, xstrdup(s));
164 break;
165 }
166 }
167 fclose(f);
168}
169
170static void read_branches_file(struct remote *remote)
171{
172 const char *slash = strchr(remote->name, '/');
173 char *frag;
174 char *branch;
175 int n = slash ? slash - remote->name : 1000;
176 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
177 char *s, *p;
178 int len;
179
180 if (!f)
181 return;
182 s = fgets(buffer, BUF_SIZE, f);
183 fclose(f);
184 if (!s)
185 return;
186 while (isspace(*s))
187 s++;
188 if (!*s)
189 return;
190 p = s + strlen(s);
191 while (isspace(p[-1]))
192 *--p = 0;
193 len = p - s;
194 if (slash)
195 len += strlen(slash);
196 p = xmalloc(len + 1);
197 strcpy(p, s);
198 if (slash)
199 strcat(p, slash);
200 frag = strchr(p, '#');
201 if (frag) {
202 *(frag++) = '\0';
203 branch = xmalloc(strlen(frag) + 12);
204 strcpy(branch, "refs/heads/");
205 strcat(branch, frag);
206 } else {
207 branch = "refs/heads/master";
208 }
209 add_url(remote, p);
210 add_fetch_refspec(remote, branch);
211 remote->fetch_tags = 1; /* always auto-follow */
212}
213
214static int handle_config(const char *key, const char *value)
215{
216 const char *name;
217 const char *subkey;
218 struct remote *remote;
219 struct branch *branch;
220 if (!prefixcmp(key, "branch.")) {
221 name = key + 7;
222 subkey = strrchr(name, '.');
223 if (!subkey)
224 return 0;
225 branch = make_branch(name, subkey - name);
226 if (!strcmp(subkey, ".remote")) {
227 if (!value)
228 return config_error_nonbool(key);
229 branch->remote_name = xstrdup(value);
230 if (branch == current_branch)
231 default_remote_name = branch->remote_name;
232 } else if (!strcmp(subkey, ".merge")) {
233 if (!value)
234 return config_error_nonbool(key);
235 add_merge(branch, xstrdup(value));
236 }
237 return 0;
238 }
239 if (prefixcmp(key, "remote."))
240 return 0;
241 name = key + 7;
242 subkey = strrchr(name, '.');
243 if (!subkey)
244 return error("Config with no key for remote %s", name);
245 if (*subkey == '/') {
246 warning("Config remote shorthand cannot begin with '/': %s", name);
247 return 0;
248 }
249 remote = make_remote(name, subkey - name);
250 if (!value) {
251 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
252 * [remote "frotz"]
253 * disabled
254 * is a valid way to set it to true; we get NULL in value so
255 * we need to handle it here.
256 *
257 * if (!strcmp(subkey, ".disabled")) {
258 * val = git_config_bool(key, value);
259 * return 0;
260 * } else
261 *
262 */
263 return 0; /* ignore unknown booleans */
264 }
265 if (!strcmp(subkey, ".url")) {
266 add_url(remote, xstrdup(value));
267 } else if (!strcmp(subkey, ".push")) {
268 add_push_refspec(remote, xstrdup(value));
269 } else if (!strcmp(subkey, ".fetch")) {
270 add_fetch_refspec(remote, xstrdup(value));
271 } else if (!strcmp(subkey, ".receivepack")) {
272 if (!remote->receivepack)
273 remote->receivepack = xstrdup(value);
274 else
275 error("more than one receivepack given, using the first");
276 } else if (!strcmp(subkey, ".uploadpack")) {
277 if (!remote->uploadpack)
278 remote->uploadpack = xstrdup(value);
279 else
280 error("more than one uploadpack given, using the first");
281 } else if (!strcmp(subkey, ".tagopt")) {
282 if (!strcmp(value, "--no-tags"))
283 remote->fetch_tags = -1;
284 } else if (!strcmp(subkey, ".proxy")) {
285 remote->http_proxy = xstrdup(value);
286 }
287 return 0;
288}
289
290static void read_config(void)
291{
292 unsigned char sha1[20];
293 const char *head_ref;
294 int flag;
295 if (default_remote_name) // did this already
296 return;
297 default_remote_name = xstrdup("origin");
298 current_branch = NULL;
299 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
300 if (head_ref && (flag & REF_ISSYMREF) &&
301 !prefixcmp(head_ref, "refs/heads/")) {
302 current_branch =
303 make_branch(head_ref + strlen("refs/heads/"), 0);
304 }
305 git_config(handle_config);
306}
307
308static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch)
309{
310 int i;
311 int st;
312 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
313
314 for (i = 0; i < nr_refspec; i++) {
315 size_t llen, rlen;
316 int is_glob;
317 const char *lhs, *rhs;
318
319 llen = rlen = is_glob = 0;
320
321 lhs = refspec[i];
322 if (*lhs == '+') {
323 rs[i].force = 1;
324 lhs++;
325 }
326
327 rhs = strrchr(lhs, ':');
328 if (rhs) {
329 rhs++;
330 rlen = strlen(rhs);
331 is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
332 if (is_glob)
333 rlen -= 2;
334 rs[i].dst = xstrndup(rhs, rlen);
335 }
336
337 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
338 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
339 if ((rhs && !is_glob) || (!rhs && fetch))
340 goto invalid;
341 is_glob = 1;
342 llen -= 2;
343 } else if (rhs && is_glob) {
344 goto invalid;
345 }
346
347 rs[i].pattern = is_glob;
348 rs[i].src = xstrndup(lhs, llen);
349
350 if (fetch) {
351 /*
352 * LHS
353 * - empty is allowed; it means HEAD.
354 * - otherwise it must be a valid looking ref.
355 */
356 if (!*rs[i].src)
357 ; /* empty is ok */
358 else {
359 st = check_ref_format(rs[i].src);
360 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
361 goto invalid;
362 }
363 /*
364 * RHS
365 * - missing is ok, and is same as empty.
366 * - empty is ok; it means not to store.
367 * - otherwise it must be a valid looking ref.
368 */
369 if (!rs[i].dst) {
370 ; /* ok */
371 } else if (!*rs[i].dst) {
372 ; /* ok */
373 } else {
374 st = check_ref_format(rs[i].dst);
375 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
376 goto invalid;
377 }
378 } else {
379 /*
380 * LHS
381 * - empty is allowed; it means delete.
382 * - when wildcarded, it must be a valid looking ref.
383 * - otherwise, it must be an extended SHA-1, but
384 * there is no existing way to validate this.
385 */
386 if (!*rs[i].src)
387 ; /* empty is ok */
388 else if (is_glob) {
389 st = check_ref_format(rs[i].src);
390 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
391 goto invalid;
392 }
393 else
394 ; /* anything goes, for now */
395 /*
396 * RHS
397 * - missing is allowed, but LHS then must be a
398 * valid looking ref.
399 * - empty is not allowed.
400 * - otherwise it must be a valid looking ref.
401 */
402 if (!rs[i].dst) {
403 st = check_ref_format(rs[i].src);
404 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
405 goto invalid;
406 } else if (!*rs[i].dst) {
407 goto invalid;
408 } else {
409 st = check_ref_format(rs[i].dst);
410 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
411 goto invalid;
412 }
413 }
414 }
415 return rs;
416
417 invalid:
418 die("Invalid refspec '%s'", refspec[i]);
419}
420
421struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
422{
423 return parse_refspec_internal(nr_refspec, refspec, 1);
424}
425
426struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
427{
428 return parse_refspec_internal(nr_refspec, refspec, 0);
429}
430
431static int valid_remote_nick(const char *name)
432{
433 if (!name[0] || /* not empty */
434 (name[0] == '.' && /* not "." */
435 (!name[1] || /* not ".." */
436 (name[1] == '.' && !name[2]))))
437 return 0;
438 return !strchr(name, '/'); /* no slash */
439}
440
441struct remote *remote_get(const char *name)
442{
443 struct remote *ret;
444
445 read_config();
446 if (!name)
447 name = default_remote_name;
448 ret = make_remote(name, 0);
449 if (valid_remote_nick(name)) {
450 if (!ret->url)
451 read_remotes_file(ret);
452 if (!ret->url)
453 read_branches_file(ret);
454 }
455 if (!ret->url)
456 add_url(ret, name);
457 if (!ret->url)
458 return NULL;
459 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
460 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
461 return ret;
462}
463
464int for_each_remote(each_remote_fn fn, void *priv)
465{
466 int i, result = 0;
467 read_config();
468 for (i = 0; i < allocated_remotes && !result; i++) {
469 struct remote *r = remotes[i];
470 if (!r)
471 continue;
472 if (!r->fetch)
473 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
474 r->fetch_refspec);
475 if (!r->push)
476 r->push = parse_push_refspec(r->push_refspec_nr,
477 r->push_refspec);
478 result = fn(r, priv);
479 }
480 return result;
481}
482
483void ref_remove_duplicates(struct ref *ref_map)
484{
485 struct ref **posn;
486 struct ref *next;
487 for (; ref_map; ref_map = ref_map->next) {
488 if (!ref_map->peer_ref)
489 continue;
490 posn = &ref_map->next;
491 while (*posn) {
492 if ((*posn)->peer_ref &&
493 !strcmp((*posn)->peer_ref->name,
494 ref_map->peer_ref->name)) {
495 if (strcmp((*posn)->name, ref_map->name))
496 die("%s tracks both %s and %s",
497 ref_map->peer_ref->name,
498 (*posn)->name, ref_map->name);
499 next = (*posn)->next;
500 free((*posn)->peer_ref);
501 free(*posn);
502 *posn = next;
503 } else {
504 posn = &(*posn)->next;
505 }
506 }
507 }
508}
509
510int remote_has_url(struct remote *remote, const char *url)
511{
512 int i;
513 for (i = 0; i < remote->url_nr; i++) {
514 if (!strcmp(remote->url[i], url))
515 return 1;
516 }
517 return 0;
518}
519
520int remote_find_tracking(struct remote *remote, struct refspec *refspec)
521{
522 int find_src = refspec->src == NULL;
523 char *needle, **result;
524 int i;
525
526 if (find_src) {
527 if (!refspec->dst)
528 return error("find_tracking: need either src or dst");
529 needle = refspec->dst;
530 result = &refspec->src;
531 } else {
532 needle = refspec->src;
533 result = &refspec->dst;
534 }
535
536 for (i = 0; i < remote->fetch_refspec_nr; i++) {
537 struct refspec *fetch = &remote->fetch[i];
538 const char *key = find_src ? fetch->dst : fetch->src;
539 const char *value = find_src ? fetch->src : fetch->dst;
540 if (!fetch->dst)
541 continue;
542 if (fetch->pattern) {
543 if (!prefixcmp(needle, key) &&
544 needle[strlen(key)] == '/') {
545 *result = xmalloc(strlen(value) +
546 strlen(needle) -
547 strlen(key) + 1);
548 strcpy(*result, value);
549 strcpy(*result + strlen(value),
550 needle + strlen(key));
551 refspec->force = fetch->force;
552 return 0;
553 }
554 } else if (!strcmp(needle, key)) {
555 *result = xstrdup(value);
556 refspec->force = fetch->force;
557 return 0;
558 }
559 }
560 return -1;
561}
562
563struct ref *alloc_ref(unsigned namelen)
564{
565 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
566 memset(ret, 0, sizeof(struct ref) + namelen);
567 return ret;
568}
569
570static struct ref *copy_ref(const struct ref *ref)
571{
572 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
573 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
574 ret->next = NULL;
575 return ret;
576}
577
578struct ref *copy_ref_list(const struct ref *ref)
579{
580 struct ref *ret = NULL;
581 struct ref **tail = &ret;
582 while (ref) {
583 *tail = copy_ref(ref);
584 ref = ref->next;
585 tail = &((*tail)->next);
586 }
587 return ret;
588}
589
590void free_refs(struct ref *ref)
591{
592 struct ref *next;
593 while (ref) {
594 next = ref->next;
595 if (ref->peer_ref)
596 free(ref->peer_ref);
597 free(ref);
598 ref = next;
599 }
600}
601
602static int count_refspec_match(const char *pattern,
603 struct ref *refs,
604 struct ref **matched_ref)
605{
606 int patlen = strlen(pattern);
607 struct ref *matched_weak = NULL;
608 struct ref *matched = NULL;
609 int weak_match = 0;
610 int match = 0;
611
612 for (weak_match = match = 0; refs; refs = refs->next) {
613 char *name = refs->name;
614 int namelen = strlen(name);
615
616 if (!refname_match(pattern, name, ref_rev_parse_rules))
617 continue;
618
619 /* A match is "weak" if it is with refs outside
620 * heads or tags, and did not specify the pattern
621 * in full (e.g. "refs/remotes/origin/master") or at
622 * least from the toplevel (e.g. "remotes/origin/master");
623 * otherwise "git push $URL master" would result in
624 * ambiguity between remotes/origin/master and heads/master
625 * at the remote site.
626 */
627 if (namelen != patlen &&
628 patlen != namelen - 5 &&
629 prefixcmp(name, "refs/heads/") &&
630 prefixcmp(name, "refs/tags/")) {
631 /* We want to catch the case where only weak
632 * matches are found and there are multiple
633 * matches, and where more than one strong
634 * matches are found, as ambiguous. One
635 * strong match with zero or more weak matches
636 * are acceptable as a unique match.
637 */
638 matched_weak = refs;
639 weak_match++;
640 }
641 else {
642 matched = refs;
643 match++;
644 }
645 }
646 if (!matched) {
647 *matched_ref = matched_weak;
648 return weak_match;
649 }
650 else {
651 *matched_ref = matched;
652 return match;
653 }
654}
655
656static void tail_link_ref(struct ref *ref, struct ref ***tail)
657{
658 **tail = ref;
659 while (ref->next)
660 ref = ref->next;
661 *tail = &ref->next;
662}
663
664static struct ref *try_explicit_object_name(const char *name)
665{
666 unsigned char sha1[20];
667 struct ref *ref;
668 int len;
669
670 if (!*name) {
671 ref = alloc_ref(20);
672 strcpy(ref->name, "(delete)");
673 hashclr(ref->new_sha1);
674 return ref;
675 }
676 if (get_sha1(name, sha1))
677 return NULL;
678 len = strlen(name) + 1;
679 ref = alloc_ref(len);
680 memcpy(ref->name, name, len);
681 hashcpy(ref->new_sha1, sha1);
682 return ref;
683}
684
685static struct ref *make_linked_ref(const char *name, struct ref ***tail)
686{
687 struct ref *ret;
688 size_t len;
689
690 len = strlen(name) + 1;
691 ret = alloc_ref(len);
692 memcpy(ret->name, name, len);
693 tail_link_ref(ret, tail);
694 return ret;
695}
696
697static int match_explicit(struct ref *src, struct ref *dst,
698 struct ref ***dst_tail,
699 struct refspec *rs,
700 int errs)
701{
702 struct ref *matched_src, *matched_dst;
703
704 const char *dst_value = rs->dst;
705
706 if (rs->pattern)
707 return errs;
708
709 matched_src = matched_dst = NULL;
710 switch (count_refspec_match(rs->src, src, &matched_src)) {
711 case 1:
712 break;
713 case 0:
714 /* The source could be in the get_sha1() format
715 * not a reference name. :refs/other is a
716 * way to delete 'other' ref at the remote end.
717 */
718 matched_src = try_explicit_object_name(rs->src);
719 if (!matched_src)
720 error("src refspec %s does not match any.", rs->src);
721 break;
722 default:
723 matched_src = NULL;
724 error("src refspec %s matches more than one.", rs->src);
725 break;
726 }
727
728 if (!matched_src)
729 errs = 1;
730
731 if (!dst_value) {
732 if (!matched_src)
733 return errs;
734 dst_value = matched_src->name;
735 }
736
737 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
738 case 1:
739 break;
740 case 0:
741 if (!memcmp(dst_value, "refs/", 5))
742 matched_dst = make_linked_ref(dst_value, dst_tail);
743 else
744 error("dst refspec %s does not match any "
745 "existing ref on the remote and does "
746 "not start with refs/.", dst_value);
747 break;
748 default:
749 matched_dst = NULL;
750 error("dst refspec %s matches more than one.",
751 dst_value);
752 break;
753 }
754 if (errs || !matched_dst)
755 return 1;
756 if (matched_dst->peer_ref) {
757 errs = 1;
758 error("dst ref %s receives from more than one src.",
759 matched_dst->name);
760 }
761 else {
762 matched_dst->peer_ref = matched_src;
763 matched_dst->force = rs->force;
764 }
765 return errs;
766}
767
768static int match_explicit_refs(struct ref *src, struct ref *dst,
769 struct ref ***dst_tail, struct refspec *rs,
770 int rs_nr)
771{
772 int i, errs;
773 for (i = errs = 0; i < rs_nr; i++)
774 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
775 return -errs;
776}
777
778static const struct refspec *check_pattern_match(const struct refspec *rs,
779 int rs_nr,
780 const struct ref *src)
781{
782 int i;
783 for (i = 0; i < rs_nr; i++) {
784 if (rs[i].pattern &&
785 !prefixcmp(src->name, rs[i].src) &&
786 src->name[strlen(rs[i].src)] == '/')
787 return rs + i;
788 }
789 return NULL;
790}
791
792/*
793 * Note. This is used only by "push"; refspec matching rules for
794 * push and fetch are subtly different, so do not try to reuse it
795 * without thinking.
796 */
797int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
798 int nr_refspec, const char **refspec, int flags)
799{
800 struct refspec *rs =
801 parse_push_refspec(nr_refspec, (const char **) refspec);
802 int send_all = flags & MATCH_REFS_ALL;
803 int send_mirror = flags & MATCH_REFS_MIRROR;
804
805 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
806 return -1;
807
808 /* pick the remainder */
809 for ( ; src; src = src->next) {
810 struct ref *dst_peer;
811 const struct refspec *pat = NULL;
812 char *dst_name;
813 if (src->peer_ref)
814 continue;
815 if (nr_refspec) {
816 pat = check_pattern_match(rs, nr_refspec, src);
817 if (!pat)
818 continue;
819 }
820 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
821 /*
822 * "matching refs"; traditionally we pushed everything
823 * including refs outside refs/heads/ hierarchy, but
824 * that does not make much sense these days.
825 */
826 continue;
827
828 if (pat) {
829 const char *dst_side = pat->dst ? pat->dst : pat->src;
830 dst_name = xmalloc(strlen(dst_side) +
831 strlen(src->name) -
832 strlen(pat->src) + 2);
833 strcpy(dst_name, dst_side);
834 strcat(dst_name, src->name + strlen(pat->src));
835 } else
836 dst_name = xstrdup(src->name);
837 dst_peer = find_ref_by_name(dst, dst_name);
838 if (dst_peer && dst_peer->peer_ref)
839 /* We're already sending something to this ref. */
840 goto free_name;
841
842 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
843 /*
844 * Remote doesn't have it, and we have no
845 * explicit pattern, and we don't have
846 * --all nor --mirror.
847 */
848 goto free_name;
849 if (!dst_peer) {
850 /* Create a new one and link it */
851 dst_peer = make_linked_ref(dst_name, dst_tail);
852 hashcpy(dst_peer->new_sha1, src->new_sha1);
853 }
854 dst_peer->peer_ref = src;
855 if (pat)
856 dst_peer->force = pat->force;
857 free_name:
858 free(dst_name);
859 }
860 return 0;
861}
862
863struct branch *branch_get(const char *name)
864{
865 struct branch *ret;
866
867 read_config();
868 if (!name || !*name || !strcmp(name, "HEAD"))
869 ret = current_branch;
870 else
871 ret = make_branch(name, 0);
872 if (ret && ret->remote_name) {
873 ret->remote = remote_get(ret->remote_name);
874 if (ret->merge_nr) {
875 int i;
876 ret->merge = xcalloc(sizeof(*ret->merge),
877 ret->merge_nr);
878 for (i = 0; i < ret->merge_nr; i++) {
879 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
880 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
881 remote_find_tracking(ret->remote,
882 ret->merge[i]);
883 }
884 }
885 }
886 return ret;
887}
888
889int branch_has_merge_config(struct branch *branch)
890{
891 return branch && !!branch->merge;
892}
893
894int branch_merge_matches(struct branch *branch,
895 int i,
896 const char *refname)
897{
898 if (!branch || i < 0 || i >= branch->merge_nr)
899 return 0;
900 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
901}
902
903static struct ref *get_expanded_map(const struct ref *remote_refs,
904 const struct refspec *refspec)
905{
906 const struct ref *ref;
907 struct ref *ret = NULL;
908 struct ref **tail = &ret;
909
910 int remote_prefix_len = strlen(refspec->src);
911 int local_prefix_len = strlen(refspec->dst);
912
913 for (ref = remote_refs; ref; ref = ref->next) {
914 if (strchr(ref->name, '^'))
915 continue; /* a dereference item */
916 if (!prefixcmp(ref->name, refspec->src)) {
917 const char *match;
918 struct ref *cpy = copy_ref(ref);
919 match = ref->name + remote_prefix_len;
920
921 cpy->peer_ref = alloc_ref(local_prefix_len +
922 strlen(match) + 1);
923 sprintf(cpy->peer_ref->name, "%s%s",
924 refspec->dst, match);
925 if (refspec->force)
926 cpy->peer_ref->force = 1;
927 *tail = cpy;
928 tail = &cpy->next;
929 }
930 }
931
932 return ret;
933}
934
935static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
936{
937 const struct ref *ref;
938 for (ref = refs; ref; ref = ref->next) {
939 if (refname_match(name, ref->name, ref_fetch_rules))
940 return ref;
941 }
942 return NULL;
943}
944
945struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
946{
947 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
948
949 if (!ref)
950 return NULL;
951
952 return copy_ref(ref);
953}
954
955static struct ref *get_local_ref(const char *name)
956{
957 struct ref *ret;
958 if (!name)
959 return NULL;
960
961 if (!prefixcmp(name, "refs/")) {
962 ret = alloc_ref(strlen(name) + 1);
963 strcpy(ret->name, name);
964 return ret;
965 }
966
967 if (!prefixcmp(name, "heads/") ||
968 !prefixcmp(name, "tags/") ||
969 !prefixcmp(name, "remotes/")) {
970 ret = alloc_ref(strlen(name) + 6);
971 sprintf(ret->name, "refs/%s", name);
972 return ret;
973 }
974
975 ret = alloc_ref(strlen(name) + 12);
976 sprintf(ret->name, "refs/heads/%s", name);
977 return ret;
978}
979
980int get_fetch_map(const struct ref *remote_refs,
981 const struct refspec *refspec,
982 struct ref ***tail,
983 int missing_ok)
984{
985 struct ref *ref_map, **rmp;
986
987 if (refspec->pattern) {
988 ref_map = get_expanded_map(remote_refs, refspec);
989 } else {
990 const char *name = refspec->src[0] ? refspec->src : "HEAD";
991
992 ref_map = get_remote_ref(remote_refs, name);
993 if (!missing_ok && !ref_map)
994 die("Couldn't find remote ref %s", name);
995 if (ref_map) {
996 ref_map->peer_ref = get_local_ref(refspec->dst);
997 if (ref_map->peer_ref && refspec->force)
998 ref_map->peer_ref->force = 1;
999 }
1000 }
1001
1002 for (rmp = &ref_map; *rmp; ) {
1003 if ((*rmp)->peer_ref) {
1004 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1005 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1006 struct ref *ignore = *rmp;
1007 error("* Ignoring funny ref '%s' locally",
1008 (*rmp)->peer_ref->name);
1009 *rmp = (*rmp)->next;
1010 free(ignore->peer_ref);
1011 free(ignore);
1012 continue;
1013 }
1014 }
1015 rmp = &((*rmp)->next);
1016 }
1017
1018 if (ref_map)
1019 tail_link_ref(ref_map, tail);
1020
1021 return 0;
1022}