Use ALLOC_GROW in remote.{c,h}
[git/debian.git] / remote.c
blob457d8a44c70347adbadba994de32770812ec7c57
1 #include "cache.h"
2 #include "remote.h"
3 #include "refs.h"
5 static struct remote **remotes;
6 static int remotes_alloc;
7 static int remotes_nr;
9 static struct branch **branches;
10 static int branches_alloc;
11 static int branches_nr;
13 static struct branch *current_branch;
14 static const char *default_remote_name;
16 #define BUF_SIZE (2048)
17 static char buffer[BUF_SIZE];
19 static void add_push_refspec(struct remote *remote, const char *ref)
21 ALLOC_GROW(remote->push_refspec,
22 remote->push_refspec_nr + 1,
23 remote->push_refspec_alloc);
24 remote->push_refspec[remote->push_refspec_nr++] = ref;
27 static void add_fetch_refspec(struct remote *remote, const char *ref)
29 ALLOC_GROW(remote->fetch_refspec,
30 remote->fetch_refspec_nr + 1,
31 remote->fetch_refspec_alloc);
32 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
35 static void add_url(struct remote *remote, const char *url)
37 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
38 remote->url[remote->url_nr++] = url;
41 static struct remote *make_remote(const char *name, int len)
43 struct remote *ret;
44 int i;
46 for (i = 0; i < remotes_nr; i++) {
47 if (len ? (!strncmp(name, remotes[i]->name, len) &&
48 !remotes[i]->name[len]) :
49 !strcmp(name, remotes[i]->name))
50 return remotes[i];
53 ret = xcalloc(1, sizeof(struct remote));
54 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
55 remotes[remotes_nr++] = ret;
56 if (len)
57 ret->name = xstrndup(name, len);
58 else
59 ret->name = xstrdup(name);
60 return ret;
63 static void add_merge(struct branch *branch, const char *name)
65 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
66 branch->merge_alloc);
67 branch->merge_name[branch->merge_nr++] = name;
70 static struct branch *make_branch(const char *name, int len)
72 struct branch *ret;
73 int i;
74 char *refname;
76 for (i = 0; i < branches_nr; i++) {
77 if (len ? (!strncmp(name, branches[i]->name, len) &&
78 !branches[i]->name[len]) :
79 !strcmp(name, branches[i]->name))
80 return branches[i];
83 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
84 ret = xcalloc(1, sizeof(struct branch));
85 branches[branches_nr++] = ret;
86 if (len)
87 ret->name = xstrndup(name, len);
88 else
89 ret->name = xstrdup(name);
90 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
91 strcpy(refname, "refs/heads/");
92 strcpy(refname + strlen("refs/heads/"), ret->name);
93 ret->refname = refname;
95 return ret;
98 static void read_remotes_file(struct remote *remote)
100 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
102 if (!f)
103 return;
104 while (fgets(buffer, BUF_SIZE, f)) {
105 int value_list;
106 char *s, *p;
108 if (!prefixcmp(buffer, "URL:")) {
109 value_list = 0;
110 s = buffer + 4;
111 } else if (!prefixcmp(buffer, "Push:")) {
112 value_list = 1;
113 s = buffer + 5;
114 } else if (!prefixcmp(buffer, "Pull:")) {
115 value_list = 2;
116 s = buffer + 5;
117 } else
118 continue;
120 while (isspace(*s))
121 s++;
122 if (!*s)
123 continue;
125 p = s + strlen(s);
126 while (isspace(p[-1]))
127 *--p = 0;
129 switch (value_list) {
130 case 0:
131 add_url(remote, xstrdup(s));
132 break;
133 case 1:
134 add_push_refspec(remote, xstrdup(s));
135 break;
136 case 2:
137 add_fetch_refspec(remote, xstrdup(s));
138 break;
141 fclose(f);
144 static void read_branches_file(struct remote *remote)
146 const char *slash = strchr(remote->name, '/');
147 char *frag;
148 char *branch;
149 int n = slash ? slash - remote->name : 1000;
150 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
151 char *s, *p;
152 int len;
154 if (!f)
155 return;
156 s = fgets(buffer, BUF_SIZE, f);
157 fclose(f);
158 if (!s)
159 return;
160 while (isspace(*s))
161 s++;
162 if (!*s)
163 return;
164 p = s + strlen(s);
165 while (isspace(p[-1]))
166 *--p = 0;
167 len = p - s;
168 if (slash)
169 len += strlen(slash);
170 p = xmalloc(len + 1);
171 strcpy(p, s);
172 if (slash)
173 strcat(p, slash);
174 frag = strchr(p, '#');
175 if (frag) {
176 *(frag++) = '\0';
177 branch = xmalloc(strlen(frag) + 12);
178 strcpy(branch, "refs/heads/");
179 strcat(branch, frag);
180 } else {
181 branch = "refs/heads/master";
183 add_url(remote, p);
184 add_fetch_refspec(remote, branch);
185 remote->fetch_tags = 1; /* always auto-follow */
188 static int handle_config(const char *key, const char *value)
190 const char *name;
191 const char *subkey;
192 struct remote *remote;
193 struct branch *branch;
194 if (!prefixcmp(key, "branch.")) {
195 name = key + 7;
196 subkey = strrchr(name, '.');
197 if (!subkey)
198 return 0;
199 branch = make_branch(name, subkey - name);
200 if (!strcmp(subkey, ".remote")) {
201 if (!value)
202 return config_error_nonbool(key);
203 branch->remote_name = xstrdup(value);
204 if (branch == current_branch)
205 default_remote_name = branch->remote_name;
206 } else if (!strcmp(subkey, ".merge")) {
207 if (!value)
208 return config_error_nonbool(key);
209 add_merge(branch, xstrdup(value));
211 return 0;
213 if (prefixcmp(key, "remote."))
214 return 0;
215 name = key + 7;
216 subkey = strrchr(name, '.');
217 if (!subkey)
218 return error("Config with no key for remote %s", name);
219 if (*subkey == '/') {
220 warning("Config remote shorthand cannot begin with '/': %s", name);
221 return 0;
223 remote = make_remote(name, subkey - name);
224 if (!value) {
225 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
226 * [remote "frotz"]
227 * disabled
228 * is a valid way to set it to true; we get NULL in value so
229 * we need to handle it here.
231 * if (!strcmp(subkey, ".disabled")) {
232 * val = git_config_bool(key, value);
233 * return 0;
234 * } else
237 return 0; /* ignore unknown booleans */
239 if (!strcmp(subkey, ".url")) {
240 add_url(remote, xstrdup(value));
241 } else if (!strcmp(subkey, ".push")) {
242 add_push_refspec(remote, xstrdup(value));
243 } else if (!strcmp(subkey, ".fetch")) {
244 add_fetch_refspec(remote, xstrdup(value));
245 } else if (!strcmp(subkey, ".receivepack")) {
246 if (!remote->receivepack)
247 remote->receivepack = xstrdup(value);
248 else
249 error("more than one receivepack given, using the first");
250 } else if (!strcmp(subkey, ".uploadpack")) {
251 if (!remote->uploadpack)
252 remote->uploadpack = xstrdup(value);
253 else
254 error("more than one uploadpack given, using the first");
255 } else if (!strcmp(subkey, ".tagopt")) {
256 if (!strcmp(value, "--no-tags"))
257 remote->fetch_tags = -1;
258 } else if (!strcmp(subkey, ".proxy")) {
259 remote->http_proxy = xstrdup(value);
261 return 0;
264 static void read_config(void)
266 unsigned char sha1[20];
267 const char *head_ref;
268 int flag;
269 if (default_remote_name) // did this already
270 return;
271 default_remote_name = xstrdup("origin");
272 current_branch = NULL;
273 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
274 if (head_ref && (flag & REF_ISSYMREF) &&
275 !prefixcmp(head_ref, "refs/heads/")) {
276 current_branch =
277 make_branch(head_ref + strlen("refs/heads/"), 0);
279 git_config(handle_config);
282 struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
284 int i;
285 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
286 for (i = 0; i < nr_refspec; i++) {
287 const char *sp, *ep, *gp;
288 sp = refspec[i];
289 if (*sp == '+') {
290 rs[i].force = 1;
291 sp++;
293 gp = strchr(sp, '*');
294 ep = strchr(sp, ':');
295 if (gp && ep && gp > ep)
296 gp = NULL;
297 if (ep) {
298 if (ep[1]) {
299 const char *glob = strchr(ep + 1, '*');
300 if (!glob)
301 gp = NULL;
302 if (gp)
303 rs[i].dst = xstrndup(ep + 1,
304 glob - ep - 1);
305 else
306 rs[i].dst = xstrdup(ep + 1);
308 } else {
309 ep = sp + strlen(sp);
311 if (gp) {
312 rs[i].pattern = 1;
313 ep = gp;
315 rs[i].src = xstrndup(sp, ep - sp);
317 return rs;
320 static int valid_remote_nick(const char *name)
322 if (!name[0] || /* not empty */
323 (name[0] == '.' && /* not "." */
324 (!name[1] || /* not ".." */
325 (name[1] == '.' && !name[2]))))
326 return 0;
327 return !strchr(name, '/'); /* no slash */
330 struct remote *remote_get(const char *name)
332 struct remote *ret;
334 read_config();
335 if (!name)
336 name = default_remote_name;
337 ret = make_remote(name, 0);
338 if (valid_remote_nick(name)) {
339 if (!ret->url)
340 read_remotes_file(ret);
341 if (!ret->url)
342 read_branches_file(ret);
344 if (!ret->url)
345 add_url(ret, name);
346 if (!ret->url)
347 return NULL;
348 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
349 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
350 return ret;
353 int for_each_remote(each_remote_fn fn, void *priv)
355 int i, result = 0;
356 read_config();
357 for (i = 0; i < remotes_nr && !result; i++) {
358 struct remote *r = remotes[i];
359 if (!r)
360 continue;
361 if (!r->fetch)
362 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
363 r->fetch_refspec);
364 if (!r->push)
365 r->push = parse_ref_spec(r->push_refspec_nr,
366 r->push_refspec);
367 result = fn(r, priv);
369 return result;
372 void ref_remove_duplicates(struct ref *ref_map)
374 struct ref **posn;
375 struct ref *next;
376 for (; ref_map; ref_map = ref_map->next) {
377 if (!ref_map->peer_ref)
378 continue;
379 posn = &ref_map->next;
380 while (*posn) {
381 if ((*posn)->peer_ref &&
382 !strcmp((*posn)->peer_ref->name,
383 ref_map->peer_ref->name)) {
384 if (strcmp((*posn)->name, ref_map->name))
385 die("%s tracks both %s and %s",
386 ref_map->peer_ref->name,
387 (*posn)->name, ref_map->name);
388 next = (*posn)->next;
389 free((*posn)->peer_ref);
390 free(*posn);
391 *posn = next;
392 } else {
393 posn = &(*posn)->next;
399 int remote_has_url(struct remote *remote, const char *url)
401 int i;
402 for (i = 0; i < remote->url_nr; i++) {
403 if (!strcmp(remote->url[i], url))
404 return 1;
406 return 0;
409 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
411 int find_src = refspec->src == NULL;
412 char *needle, **result;
413 int i;
415 if (find_src) {
416 if (!refspec->dst)
417 return error("find_tracking: need either src or dst");
418 needle = refspec->dst;
419 result = &refspec->src;
420 } else {
421 needle = refspec->src;
422 result = &refspec->dst;
425 for (i = 0; i < remote->fetch_refspec_nr; i++) {
426 struct refspec *fetch = &remote->fetch[i];
427 const char *key = find_src ? fetch->dst : fetch->src;
428 const char *value = find_src ? fetch->src : fetch->dst;
429 if (!fetch->dst)
430 continue;
431 if (fetch->pattern) {
432 if (!prefixcmp(needle, key)) {
433 *result = xmalloc(strlen(value) +
434 strlen(needle) -
435 strlen(key) + 1);
436 strcpy(*result, value);
437 strcpy(*result + strlen(value),
438 needle + strlen(key));
439 refspec->force = fetch->force;
440 return 0;
442 } else if (!strcmp(needle, key)) {
443 *result = xstrdup(value);
444 refspec->force = fetch->force;
445 return 0;
448 return -1;
451 struct ref *alloc_ref(unsigned namelen)
453 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
454 memset(ret, 0, sizeof(struct ref) + namelen);
455 return ret;
458 static struct ref *copy_ref(const struct ref *ref)
460 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
461 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
462 ret->next = NULL;
463 return ret;
466 struct ref *copy_ref_list(const struct ref *ref)
468 struct ref *ret = NULL;
469 struct ref **tail = &ret;
470 while (ref) {
471 *tail = copy_ref(ref);
472 ref = ref->next;
473 tail = &((*tail)->next);
475 return ret;
478 void free_refs(struct ref *ref)
480 struct ref *next;
481 while (ref) {
482 next = ref->next;
483 if (ref->peer_ref)
484 free(ref->peer_ref);
485 free(ref);
486 ref = next;
490 static int count_refspec_match(const char *pattern,
491 struct ref *refs,
492 struct ref **matched_ref)
494 int patlen = strlen(pattern);
495 struct ref *matched_weak = NULL;
496 struct ref *matched = NULL;
497 int weak_match = 0;
498 int match = 0;
500 for (weak_match = match = 0; refs; refs = refs->next) {
501 char *name = refs->name;
502 int namelen = strlen(name);
504 if (!refname_match(pattern, name, ref_rev_parse_rules))
505 continue;
507 /* A match is "weak" if it is with refs outside
508 * heads or tags, and did not specify the pattern
509 * in full (e.g. "refs/remotes/origin/master") or at
510 * least from the toplevel (e.g. "remotes/origin/master");
511 * otherwise "git push $URL master" would result in
512 * ambiguity between remotes/origin/master and heads/master
513 * at the remote site.
515 if (namelen != patlen &&
516 patlen != namelen - 5 &&
517 prefixcmp(name, "refs/heads/") &&
518 prefixcmp(name, "refs/tags/")) {
519 /* We want to catch the case where only weak
520 * matches are found and there are multiple
521 * matches, and where more than one strong
522 * matches are found, as ambiguous. One
523 * strong match with zero or more weak matches
524 * are acceptable as a unique match.
526 matched_weak = refs;
527 weak_match++;
529 else {
530 matched = refs;
531 match++;
534 if (!matched) {
535 *matched_ref = matched_weak;
536 return weak_match;
538 else {
539 *matched_ref = matched;
540 return match;
544 static void tail_link_ref(struct ref *ref, struct ref ***tail)
546 **tail = ref;
547 while (ref->next)
548 ref = ref->next;
549 *tail = &ref->next;
552 static struct ref *try_explicit_object_name(const char *name)
554 unsigned char sha1[20];
555 struct ref *ref;
556 int len;
558 if (!*name) {
559 ref = alloc_ref(20);
560 strcpy(ref->name, "(delete)");
561 hashclr(ref->new_sha1);
562 return ref;
564 if (get_sha1(name, sha1))
565 return NULL;
566 len = strlen(name) + 1;
567 ref = alloc_ref(len);
568 memcpy(ref->name, name, len);
569 hashcpy(ref->new_sha1, sha1);
570 return ref;
573 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
575 struct ref *ret;
576 size_t len;
578 len = strlen(name) + 1;
579 ret = alloc_ref(len);
580 memcpy(ret->name, name, len);
581 tail_link_ref(ret, tail);
582 return ret;
585 static int match_explicit(struct ref *src, struct ref *dst,
586 struct ref ***dst_tail,
587 struct refspec *rs,
588 int errs)
590 struct ref *matched_src, *matched_dst;
592 const char *dst_value = rs->dst;
594 if (rs->pattern)
595 return errs;
597 matched_src = matched_dst = NULL;
598 switch (count_refspec_match(rs->src, src, &matched_src)) {
599 case 1:
600 break;
601 case 0:
602 /* The source could be in the get_sha1() format
603 * not a reference name. :refs/other is a
604 * way to delete 'other' ref at the remote end.
606 matched_src = try_explicit_object_name(rs->src);
607 if (!matched_src)
608 error("src refspec %s does not match any.", rs->src);
609 break;
610 default:
611 matched_src = NULL;
612 error("src refspec %s matches more than one.", rs->src);
613 break;
616 if (!matched_src)
617 errs = 1;
619 if (!dst_value) {
620 if (!matched_src)
621 return errs;
622 dst_value = matched_src->name;
625 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
626 case 1:
627 break;
628 case 0:
629 if (!memcmp(dst_value, "refs/", 5))
630 matched_dst = make_linked_ref(dst_value, dst_tail);
631 else
632 error("dst refspec %s does not match any "
633 "existing ref on the remote and does "
634 "not start with refs/.", dst_value);
635 break;
636 default:
637 matched_dst = NULL;
638 error("dst refspec %s matches more than one.",
639 dst_value);
640 break;
642 if (errs || !matched_dst)
643 return 1;
644 if (matched_dst->peer_ref) {
645 errs = 1;
646 error("dst ref %s receives from more than one src.",
647 matched_dst->name);
649 else {
650 matched_dst->peer_ref = matched_src;
651 matched_dst->force = rs->force;
653 return errs;
656 static int match_explicit_refs(struct ref *src, struct ref *dst,
657 struct ref ***dst_tail, struct refspec *rs,
658 int rs_nr)
660 int i, errs;
661 for (i = errs = 0; i < rs_nr; i++)
662 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
663 return -errs;
666 static const struct refspec *check_pattern_match(const struct refspec *rs,
667 int rs_nr,
668 const struct ref *src)
670 int i;
671 for (i = 0; i < rs_nr; i++) {
672 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
673 return rs + i;
675 return NULL;
679 * Note. This is used only by "push"; refspec matching rules for
680 * push and fetch are subtly different, so do not try to reuse it
681 * without thinking.
683 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
684 int nr_refspec, const char **refspec, int flags)
686 struct refspec *rs =
687 parse_ref_spec(nr_refspec, (const char **) refspec);
688 int send_all = flags & MATCH_REFS_ALL;
689 int send_mirror = flags & MATCH_REFS_MIRROR;
691 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
692 return -1;
694 /* pick the remainder */
695 for ( ; src; src = src->next) {
696 struct ref *dst_peer;
697 const struct refspec *pat = NULL;
698 char *dst_name;
699 if (src->peer_ref)
700 continue;
701 if (nr_refspec) {
702 pat = check_pattern_match(rs, nr_refspec, src);
703 if (!pat)
704 continue;
706 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
708 * "matching refs"; traditionally we pushed everything
709 * including refs outside refs/heads/ hierarchy, but
710 * that does not make much sense these days.
712 continue;
714 if (pat) {
715 const char *dst_side = pat->dst ? pat->dst : pat->src;
716 dst_name = xmalloc(strlen(dst_side) +
717 strlen(src->name) -
718 strlen(pat->src) + 2);
719 strcpy(dst_name, dst_side);
720 strcat(dst_name, src->name + strlen(pat->src));
721 } else
722 dst_name = xstrdup(src->name);
723 dst_peer = find_ref_by_name(dst, dst_name);
724 if (dst_peer && dst_peer->peer_ref)
725 /* We're already sending something to this ref. */
726 goto free_name;
728 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
730 * Remote doesn't have it, and we have no
731 * explicit pattern, and we don't have
732 * --all nor --mirror.
734 goto free_name;
735 if (!dst_peer) {
736 /* Create a new one and link it */
737 dst_peer = make_linked_ref(dst_name, dst_tail);
738 hashcpy(dst_peer->new_sha1, src->new_sha1);
740 dst_peer->peer_ref = src;
741 if (pat)
742 dst_peer->force = pat->force;
743 free_name:
744 free(dst_name);
746 return 0;
749 struct branch *branch_get(const char *name)
751 struct branch *ret;
753 read_config();
754 if (!name || !*name || !strcmp(name, "HEAD"))
755 ret = current_branch;
756 else
757 ret = make_branch(name, 0);
758 if (ret && ret->remote_name) {
759 ret->remote = remote_get(ret->remote_name);
760 if (ret->merge_nr) {
761 int i;
762 ret->merge = xcalloc(sizeof(*ret->merge),
763 ret->merge_nr);
764 for (i = 0; i < ret->merge_nr; i++) {
765 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
766 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
767 remote_find_tracking(ret->remote,
768 ret->merge[i]);
772 return ret;
775 int branch_has_merge_config(struct branch *branch)
777 return branch && !!branch->merge;
780 int branch_merge_matches(struct branch *branch,
781 int i,
782 const char *refname)
784 if (!branch || i < 0 || i >= branch->merge_nr)
785 return 0;
786 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
789 static struct ref *get_expanded_map(const struct ref *remote_refs,
790 const struct refspec *refspec)
792 const struct ref *ref;
793 struct ref *ret = NULL;
794 struct ref **tail = &ret;
796 int remote_prefix_len = strlen(refspec->src);
797 int local_prefix_len = strlen(refspec->dst);
799 for (ref = remote_refs; ref; ref = ref->next) {
800 if (strchr(ref->name, '^'))
801 continue; /* a dereference item */
802 if (!prefixcmp(ref->name, refspec->src)) {
803 const char *match;
804 struct ref *cpy = copy_ref(ref);
805 match = ref->name + remote_prefix_len;
807 cpy->peer_ref = alloc_ref(local_prefix_len +
808 strlen(match) + 1);
809 sprintf(cpy->peer_ref->name, "%s%s",
810 refspec->dst, match);
811 if (refspec->force)
812 cpy->peer_ref->force = 1;
813 *tail = cpy;
814 tail = &cpy->next;
818 return ret;
821 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
823 const struct ref *ref;
824 for (ref = refs; ref; ref = ref->next) {
825 if (refname_match(name, ref->name, ref_fetch_rules))
826 return ref;
828 return NULL;
831 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
833 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
835 if (!ref)
836 return NULL;
838 return copy_ref(ref);
841 static struct ref *get_local_ref(const char *name)
843 struct ref *ret;
844 if (!name)
845 return NULL;
847 if (!prefixcmp(name, "refs/")) {
848 ret = alloc_ref(strlen(name) + 1);
849 strcpy(ret->name, name);
850 return ret;
853 if (!prefixcmp(name, "heads/") ||
854 !prefixcmp(name, "tags/") ||
855 !prefixcmp(name, "remotes/")) {
856 ret = alloc_ref(strlen(name) + 6);
857 sprintf(ret->name, "refs/%s", name);
858 return ret;
861 ret = alloc_ref(strlen(name) + 12);
862 sprintf(ret->name, "refs/heads/%s", name);
863 return ret;
866 int get_fetch_map(const struct ref *remote_refs,
867 const struct refspec *refspec,
868 struct ref ***tail,
869 int missing_ok)
871 struct ref *ref_map, *rm;
873 if (refspec->pattern) {
874 ref_map = get_expanded_map(remote_refs, refspec);
875 } else {
876 const char *name = refspec->src[0] ? refspec->src : "HEAD";
878 ref_map = get_remote_ref(remote_refs, name);
879 if (!missing_ok && !ref_map)
880 die("Couldn't find remote ref %s", name);
881 if (ref_map) {
882 ref_map->peer_ref = get_local_ref(refspec->dst);
883 if (ref_map->peer_ref && refspec->force)
884 ref_map->peer_ref->force = 1;
888 for (rm = ref_map; rm; rm = rm->next) {
889 if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
890 die("* refusing to create funny ref '%s' locally",
891 rm->peer_ref->name);
894 if (ref_map)
895 tail_link_ref(ref_map, tail);
897 return 0;