refactor fetch's ref matching to use refname_match()
[git/raj.git] / remote.c
blob48812a713e56bbc74418d2d484ee345b4dc5aa32
1 #include "cache.h"
2 #include "remote.h"
3 #include "refs.h"
5 static struct remote **remotes;
6 static int allocated_remotes;
8 static struct branch **branches;
9 static int allocated_branches;
11 static struct branch *current_branch;
12 static const char *default_remote_name;
14 #define BUF_SIZE (2048)
15 static char buffer[BUF_SIZE];
17 static void add_push_refspec(struct remote *remote, const char *ref)
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;
26 static void add_fetch_refspec(struct remote *remote, const char *ref)
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;
35 static void add_url(struct remote *remote, const char *url)
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;
44 static struct remote *make_remote(const char *name, int len)
46 int i, empty = -1;
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];
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));
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];
76 static void add_merge(struct branch *branch, const char *name)
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;
85 static struct branch *make_branch(const char *name, int len)
87 int i, empty = -1;
88 char *refname;
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];
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));
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;
121 return branches[empty];
124 static void read_remotes_file(struct remote *remote)
126 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
128 if (!f)
129 return;
130 while (fgets(buffer, BUF_SIZE, f)) {
131 int value_list;
132 char *s, *p;
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;
146 while (isspace(*s))
147 s++;
148 if (!*s)
149 continue;
151 p = s + strlen(s);
152 while (isspace(p[-1]))
153 *--p = 0;
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;
167 fclose(f);
170 static void read_branches_file(struct remote *remote)
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;
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";
209 add_url(remote, p);
210 add_fetch_refspec(remote, branch);
211 remote->fetch_tags = 1; /* always auto-follow */
214 static int handle_config(const char *key, const char *value)
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 branch = make_branch(name, subkey - name);
224 if (!subkey)
225 return 0;
226 if (!value)
227 return 0;
228 if (!strcmp(subkey, ".remote")) {
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 add_merge(branch, xstrdup(value));
234 return 0;
236 if (prefixcmp(key, "remote."))
237 return 0;
238 name = key + 7;
239 subkey = strrchr(name, '.');
240 if (!subkey)
241 return error("Config with no key for remote %s", name);
242 if (*subkey == '/') {
243 warning("Config remote shorthand cannot begin with '/': %s", name);
244 return 0;
246 remote = make_remote(name, subkey - name);
247 if (!value) {
248 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
249 * [remote "frotz"]
250 * disabled
251 * is a valid way to set it to true; we get NULL in value so
252 * we need to handle it here.
254 * if (!strcmp(subkey, ".disabled")) {
255 * val = git_config_bool(key, value);
256 * return 0;
257 * } else
260 return 0; /* ignore unknown booleans */
262 if (!strcmp(subkey, ".url")) {
263 add_url(remote, xstrdup(value));
264 } else if (!strcmp(subkey, ".push")) {
265 add_push_refspec(remote, xstrdup(value));
266 } else if (!strcmp(subkey, ".fetch")) {
267 add_fetch_refspec(remote, xstrdup(value));
268 } else if (!strcmp(subkey, ".receivepack")) {
269 if (!remote->receivepack)
270 remote->receivepack = xstrdup(value);
271 else
272 error("more than one receivepack given, using the first");
273 } else if (!strcmp(subkey, ".uploadpack")) {
274 if (!remote->uploadpack)
275 remote->uploadpack = xstrdup(value);
276 else
277 error("more than one uploadpack given, using the first");
278 } else if (!strcmp(subkey, ".tagopt")) {
279 if (!strcmp(value, "--no-tags"))
280 remote->fetch_tags = -1;
282 return 0;
285 static void read_config(void)
287 unsigned char sha1[20];
288 const char *head_ref;
289 int flag;
290 if (default_remote_name) // did this already
291 return;
292 default_remote_name = xstrdup("origin");
293 current_branch = NULL;
294 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
295 if (head_ref && (flag & REF_ISSYMREF) &&
296 !prefixcmp(head_ref, "refs/heads/")) {
297 current_branch =
298 make_branch(head_ref + strlen("refs/heads/"), 0);
300 git_config(handle_config);
303 struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
305 int i;
306 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
307 for (i = 0; i < nr_refspec; i++) {
308 const char *sp, *ep, *gp;
309 sp = refspec[i];
310 if (*sp == '+') {
311 rs[i].force = 1;
312 sp++;
314 gp = strchr(sp, '*');
315 ep = strchr(sp, ':');
316 if (gp && ep && gp > ep)
317 gp = NULL;
318 if (ep) {
319 if (ep[1]) {
320 const char *glob = strchr(ep + 1, '*');
321 if (!glob)
322 gp = NULL;
323 if (gp)
324 rs[i].dst = xstrndup(ep + 1,
325 glob - ep - 1);
326 else
327 rs[i].dst = xstrdup(ep + 1);
329 } else {
330 ep = sp + strlen(sp);
332 if (gp) {
333 rs[i].pattern = 1;
334 ep = gp;
336 rs[i].src = xstrndup(sp, ep - sp);
338 return rs;
341 struct remote *remote_get(const char *name)
343 struct remote *ret;
345 read_config();
346 if (!name)
347 name = default_remote_name;
348 ret = make_remote(name, 0);
349 if (name[0] != '/') {
350 if (!ret->url)
351 read_remotes_file(ret);
352 if (!ret->url)
353 read_branches_file(ret);
355 if (!ret->url)
356 add_url(ret, name);
357 if (!ret->url)
358 return NULL;
359 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
360 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
361 return ret;
364 int for_each_remote(each_remote_fn fn, void *priv)
366 int i, result = 0;
367 read_config();
368 for (i = 0; i < allocated_remotes && !result; i++) {
369 struct remote *r = remotes[i];
370 if (!r)
371 continue;
372 if (!r->fetch)
373 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
374 r->fetch_refspec);
375 if (!r->push)
376 r->push = parse_ref_spec(r->push_refspec_nr,
377 r->push_refspec);
378 result = fn(r, priv);
380 return result;
383 void ref_remove_duplicates(struct ref *ref_map)
385 struct ref **posn;
386 struct ref *next;
387 for (; ref_map; ref_map = ref_map->next) {
388 if (!ref_map->peer_ref)
389 continue;
390 posn = &ref_map->next;
391 while (*posn) {
392 if ((*posn)->peer_ref &&
393 !strcmp((*posn)->peer_ref->name,
394 ref_map->peer_ref->name)) {
395 if (strcmp((*posn)->name, ref_map->name))
396 die("%s tracks both %s and %s",
397 ref_map->peer_ref->name,
398 (*posn)->name, ref_map->name);
399 next = (*posn)->next;
400 free((*posn)->peer_ref);
401 free(*posn);
402 *posn = next;
403 } else {
404 posn = &(*posn)->next;
410 int remote_has_url(struct remote *remote, const char *url)
412 int i;
413 for (i = 0; i < remote->url_nr; i++) {
414 if (!strcmp(remote->url[i], url))
415 return 1;
417 return 0;
420 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
422 int find_src = refspec->src == NULL;
423 char *needle, **result;
424 int i;
426 if (find_src) {
427 if (!refspec->dst)
428 return error("find_tracking: need either src or dst");
429 needle = refspec->dst;
430 result = &refspec->src;
431 } else {
432 needle = refspec->src;
433 result = &refspec->dst;
436 for (i = 0; i < remote->fetch_refspec_nr; i++) {
437 struct refspec *fetch = &remote->fetch[i];
438 const char *key = find_src ? fetch->dst : fetch->src;
439 const char *value = find_src ? fetch->src : fetch->dst;
440 if (!fetch->dst)
441 continue;
442 if (fetch->pattern) {
443 if (!prefixcmp(needle, key)) {
444 *result = xmalloc(strlen(value) +
445 strlen(needle) -
446 strlen(key) + 1);
447 strcpy(*result, value);
448 strcpy(*result + strlen(value),
449 needle + strlen(key));
450 refspec->force = fetch->force;
451 return 0;
453 } else if (!strcmp(needle, key)) {
454 *result = xstrdup(value);
455 refspec->force = fetch->force;
456 return 0;
459 return -1;
462 struct ref *alloc_ref(unsigned namelen)
464 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
465 memset(ret, 0, sizeof(struct ref) + namelen);
466 return ret;
469 static struct ref *copy_ref(struct ref *ref)
471 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
472 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
473 ret->next = NULL;
474 return ret;
477 void free_refs(struct ref *ref)
479 struct ref *next;
480 while (ref) {
481 next = ref->next;
482 if (ref->peer_ref)
483 free(ref->peer_ref);
484 free(ref);
485 ref = next;
489 static int count_refspec_match(const char *pattern,
490 struct ref *refs,
491 struct ref **matched_ref)
493 int patlen = strlen(pattern);
494 struct ref *matched_weak = NULL;
495 struct ref *matched = NULL;
496 int weak_match = 0;
497 int match = 0;
499 for (weak_match = match = 0; refs; refs = refs->next) {
500 char *name = refs->name;
501 int namelen = strlen(name);
503 if (!refname_match(pattern, name, ref_rev_parse_rules))
504 continue;
506 /* A match is "weak" if it is with refs outside
507 * heads or tags, and did not specify the pattern
508 * in full (e.g. "refs/remotes/origin/master") or at
509 * least from the toplevel (e.g. "remotes/origin/master");
510 * otherwise "git push $URL master" would result in
511 * ambiguity between remotes/origin/master and heads/master
512 * at the remote site.
514 if (namelen != patlen &&
515 patlen != namelen - 5 &&
516 prefixcmp(name, "refs/heads/") &&
517 prefixcmp(name, "refs/tags/")) {
518 /* We want to catch the case where only weak
519 * matches are found and there are multiple
520 * matches, and where more than one strong
521 * matches are found, as ambiguous. One
522 * strong match with zero or more weak matches
523 * are acceptable as a unique match.
525 matched_weak = refs;
526 weak_match++;
528 else {
529 matched = refs;
530 match++;
533 if (!matched) {
534 *matched_ref = matched_weak;
535 return weak_match;
537 else {
538 *matched_ref = matched;
539 return match;
543 static void tail_link_ref(struct ref *ref, struct ref ***tail)
545 **tail = ref;
546 while (ref->next)
547 ref = ref->next;
548 *tail = &ref->next;
551 static struct ref *try_explicit_object_name(const char *name)
553 unsigned char sha1[20];
554 struct ref *ref;
555 int len;
557 if (!*name) {
558 ref = alloc_ref(20);
559 strcpy(ref->name, "(delete)");
560 hashclr(ref->new_sha1);
561 return ref;
563 if (get_sha1(name, sha1))
564 return NULL;
565 len = strlen(name) + 1;
566 ref = alloc_ref(len);
567 memcpy(ref->name, name, len);
568 hashcpy(ref->new_sha1, sha1);
569 return ref;
572 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
574 struct ref *ret;
575 size_t len;
577 len = strlen(name) + 1;
578 ret = alloc_ref(len);
579 memcpy(ret->name, name, len);
580 tail_link_ref(ret, tail);
581 return ret;
584 static int match_explicit(struct ref *src, struct ref *dst,
585 struct ref ***dst_tail,
586 struct refspec *rs,
587 int errs)
589 struct ref *matched_src, *matched_dst;
591 const char *dst_value = rs->dst;
593 if (rs->pattern)
594 return errs;
596 matched_src = matched_dst = NULL;
597 switch (count_refspec_match(rs->src, src, &matched_src)) {
598 case 1:
599 break;
600 case 0:
601 /* The source could be in the get_sha1() format
602 * not a reference name. :refs/other is a
603 * way to delete 'other' ref at the remote end.
605 matched_src = try_explicit_object_name(rs->src);
606 if (!matched_src)
607 error("src refspec %s does not match any.", rs->src);
608 break;
609 default:
610 matched_src = NULL;
611 error("src refspec %s matches more than one.", rs->src);
612 break;
615 if (!matched_src)
616 errs = 1;
618 if (!dst_value) {
619 if (!matched_src)
620 return errs;
621 dst_value = matched_src->name;
624 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
625 case 1:
626 break;
627 case 0:
628 if (!memcmp(dst_value, "refs/", 5))
629 matched_dst = make_linked_ref(dst_value, dst_tail);
630 else
631 error("dst refspec %s does not match any "
632 "existing ref on the remote and does "
633 "not start with refs/.", dst_value);
634 break;
635 default:
636 matched_dst = NULL;
637 error("dst refspec %s matches more than one.",
638 dst_value);
639 break;
641 if (errs || !matched_dst)
642 return 1;
643 if (matched_dst->peer_ref) {
644 errs = 1;
645 error("dst ref %s receives from more than one src.",
646 matched_dst->name);
648 else {
649 matched_dst->peer_ref = matched_src;
650 matched_dst->force = rs->force;
652 return errs;
655 static int match_explicit_refs(struct ref *src, struct ref *dst,
656 struct ref ***dst_tail, struct refspec *rs,
657 int rs_nr)
659 int i, errs;
660 for (i = errs = 0; i < rs_nr; i++)
661 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
662 return -errs;
665 static struct ref *find_ref_by_name(struct ref *list, const char *name)
667 for ( ; list; list = list->next)
668 if (!strcmp(list->name, name))
669 return list;
670 return NULL;
673 static const struct refspec *check_pattern_match(const struct refspec *rs,
674 int rs_nr,
675 const struct ref *src)
677 int i;
678 for (i = 0; i < rs_nr; i++) {
679 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
680 return rs + i;
682 return NULL;
686 * Note. This is used only by "push"; refspec matching rules for
687 * push and fetch are subtly different, so do not try to reuse it
688 * without thinking.
690 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
691 int nr_refspec, char **refspec, int all)
693 struct refspec *rs =
694 parse_ref_spec(nr_refspec, (const char **) refspec);
696 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
697 return -1;
699 /* pick the remainder */
700 for ( ; src; src = src->next) {
701 struct ref *dst_peer;
702 const struct refspec *pat = NULL;
703 char *dst_name;
704 if (src->peer_ref)
705 continue;
706 if (nr_refspec) {
707 pat = check_pattern_match(rs, nr_refspec, src);
708 if (!pat)
709 continue;
711 else if (prefixcmp(src->name, "refs/heads/"))
713 * "matching refs"; traditionally we pushed everything
714 * including refs outside refs/heads/ hierarchy, but
715 * that does not make much sense these days.
717 continue;
719 if (pat) {
720 const char *dst_side = pat->dst ? pat->dst : pat->src;
721 dst_name = xmalloc(strlen(dst_side) +
722 strlen(src->name) -
723 strlen(pat->src) + 2);
724 strcpy(dst_name, dst_side);
725 strcat(dst_name, src->name + strlen(pat->src));
726 } else
727 dst_name = xstrdup(src->name);
728 dst_peer = find_ref_by_name(dst, dst_name);
729 if (dst_peer && dst_peer->peer_ref)
730 /* We're already sending something to this ref. */
731 goto free_name;
732 if (!dst_peer && !nr_refspec && !all)
733 /* Remote doesn't have it, and we have no
734 * explicit pattern, and we don't have
735 * --all. */
736 goto free_name;
737 if (!dst_peer) {
738 /* Create a new one and link it */
739 dst_peer = make_linked_ref(dst_name, dst_tail);
740 hashcpy(dst_peer->new_sha1, src->new_sha1);
742 dst_peer->peer_ref = src;
743 if (pat)
744 dst_peer->force = pat->force;
745 free_name:
746 free(dst_name);
748 return 0;
751 struct branch *branch_get(const char *name)
753 struct branch *ret;
755 read_config();
756 if (!name || !*name || !strcmp(name, "HEAD"))
757 ret = current_branch;
758 else
759 ret = make_branch(name, 0);
760 if (ret && ret->remote_name) {
761 ret->remote = remote_get(ret->remote_name);
762 if (ret->merge_nr) {
763 int i;
764 ret->merge = xcalloc(sizeof(*ret->merge),
765 ret->merge_nr);
766 for (i = 0; i < ret->merge_nr; i++) {
767 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
768 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
769 remote_find_tracking(ret->remote,
770 ret->merge[i]);
774 return ret;
777 int branch_has_merge_config(struct branch *branch)
779 return branch && !!branch->merge;
782 int branch_merge_matches(struct branch *branch,
783 int i,
784 const char *refname)
786 if (!branch || i < 0 || i >= branch->merge_nr)
787 return 0;
788 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
791 static struct ref *get_expanded_map(struct ref *remote_refs,
792 const struct refspec *refspec)
794 struct ref *ref;
795 struct ref *ret = NULL;
796 struct ref **tail = &ret;
798 int remote_prefix_len = strlen(refspec->src);
799 int local_prefix_len = strlen(refspec->dst);
801 for (ref = remote_refs; ref; ref = ref->next) {
802 if (strchr(ref->name, '^'))
803 continue; /* a dereference item */
804 if (!prefixcmp(ref->name, refspec->src)) {
805 char *match;
806 struct ref *cpy = copy_ref(ref);
807 match = ref->name + remote_prefix_len;
809 cpy->peer_ref = alloc_ref(local_prefix_len +
810 strlen(match) + 1);
811 sprintf(cpy->peer_ref->name, "%s%s",
812 refspec->dst, match);
813 if (refspec->force)
814 cpy->peer_ref->force = 1;
815 *tail = cpy;
816 tail = &cpy->next;
820 return ret;
823 static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name)
825 struct ref *ref;
826 for (ref = refs; ref; ref = ref->next) {
827 if (refname_match(name, ref->name, ref_fetch_rules))
828 return ref;
830 return NULL;
833 struct ref *get_remote_ref(struct ref *remote_refs, const char *name)
835 struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
837 if (!ref)
838 return NULL;
840 return copy_ref(ref);
843 static struct ref *get_local_ref(const char *name)
845 struct ref *ret;
846 if (!name)
847 return NULL;
849 if (!prefixcmp(name, "refs/")) {
850 ret = alloc_ref(strlen(name) + 1);
851 strcpy(ret->name, name);
852 return ret;
855 if (!prefixcmp(name, "heads/") ||
856 !prefixcmp(name, "tags/") ||
857 !prefixcmp(name, "remotes/")) {
858 ret = alloc_ref(strlen(name) + 6);
859 sprintf(ret->name, "refs/%s", name);
860 return ret;
863 ret = alloc_ref(strlen(name) + 12);
864 sprintf(ret->name, "refs/heads/%s", name);
865 return ret;
868 int get_fetch_map(struct ref *remote_refs,
869 const struct refspec *refspec,
870 struct ref ***tail,
871 int missing_ok)
873 struct ref *ref_map, *rm;
875 if (refspec->pattern) {
876 ref_map = get_expanded_map(remote_refs, refspec);
877 } else {
878 const char *name = refspec->src[0] ? refspec->src : "HEAD";
880 ref_map = get_remote_ref(remote_refs, name);
881 if (!missing_ok && !ref_map)
882 die("Couldn't find remote ref %s", name);
883 if (ref_map) {
884 ref_map->peer_ref = get_local_ref(refspec->dst);
885 if (ref_map->peer_ref && refspec->force)
886 ref_map->peer_ref->force = 1;
890 for (rm = ref_map; rm; rm = rm->next) {
891 if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
892 die("* refusing to create funny ref '%s' locally",
893 rm->peer_ref->name);
896 if (ref_map)
897 tail_link_ref(ref_map, tail);
899 return 0;