config: add test cases for empty value and no value config variables.
[git/kusma.git] / remote.c
blob20abbc07acb2e48aefb2129683b5dc159661211e
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 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));
237 return 0;
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;
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.
257 * if (!strcmp(subkey, ".disabled")) {
258 * val = git_config_bool(key, value);
259 * return 0;
260 * } else
263 return 0; /* ignore unknown booleans */
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);
287 return 0;
290 static void read_config(void)
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);
305 git_config(handle_config);
308 struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
310 int i;
311 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
312 for (i = 0; i < nr_refspec; i++) {
313 const char *sp, *ep, *gp;
314 sp = refspec[i];
315 if (*sp == '+') {
316 rs[i].force = 1;
317 sp++;
319 gp = strchr(sp, '*');
320 ep = strchr(sp, ':');
321 if (gp && ep && gp > ep)
322 gp = NULL;
323 if (ep) {
324 if (ep[1]) {
325 const char *glob = strchr(ep + 1, '*');
326 if (!glob)
327 gp = NULL;
328 if (gp)
329 rs[i].dst = xstrndup(ep + 1,
330 glob - ep - 1);
331 else
332 rs[i].dst = xstrdup(ep + 1);
334 } else {
335 ep = sp + strlen(sp);
337 if (gp) {
338 rs[i].pattern = 1;
339 ep = gp;
341 rs[i].src = xstrndup(sp, ep - sp);
343 return rs;
346 struct remote *remote_get(const char *name)
348 struct remote *ret;
350 read_config();
351 if (!name)
352 name = default_remote_name;
353 ret = make_remote(name, 0);
354 if (name[0] != '/') {
355 if (!ret->url)
356 read_remotes_file(ret);
357 if (!ret->url)
358 read_branches_file(ret);
360 if (!ret->url)
361 add_url(ret, name);
362 if (!ret->url)
363 return NULL;
364 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
365 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
366 return ret;
369 int for_each_remote(each_remote_fn fn, void *priv)
371 int i, result = 0;
372 read_config();
373 for (i = 0; i < allocated_remotes && !result; i++) {
374 struct remote *r = remotes[i];
375 if (!r)
376 continue;
377 if (!r->fetch)
378 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
379 r->fetch_refspec);
380 if (!r->push)
381 r->push = parse_ref_spec(r->push_refspec_nr,
382 r->push_refspec);
383 result = fn(r, priv);
385 return result;
388 void ref_remove_duplicates(struct ref *ref_map)
390 struct ref **posn;
391 struct ref *next;
392 for (; ref_map; ref_map = ref_map->next) {
393 if (!ref_map->peer_ref)
394 continue;
395 posn = &ref_map->next;
396 while (*posn) {
397 if ((*posn)->peer_ref &&
398 !strcmp((*posn)->peer_ref->name,
399 ref_map->peer_ref->name)) {
400 if (strcmp((*posn)->name, ref_map->name))
401 die("%s tracks both %s and %s",
402 ref_map->peer_ref->name,
403 (*posn)->name, ref_map->name);
404 next = (*posn)->next;
405 free((*posn)->peer_ref);
406 free(*posn);
407 *posn = next;
408 } else {
409 posn = &(*posn)->next;
415 int remote_has_url(struct remote *remote, const char *url)
417 int i;
418 for (i = 0; i < remote->url_nr; i++) {
419 if (!strcmp(remote->url[i], url))
420 return 1;
422 return 0;
425 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
427 int find_src = refspec->src == NULL;
428 char *needle, **result;
429 int i;
431 if (find_src) {
432 if (!refspec->dst)
433 return error("find_tracking: need either src or dst");
434 needle = refspec->dst;
435 result = &refspec->src;
436 } else {
437 needle = refspec->src;
438 result = &refspec->dst;
441 for (i = 0; i < remote->fetch_refspec_nr; i++) {
442 struct refspec *fetch = &remote->fetch[i];
443 const char *key = find_src ? fetch->dst : fetch->src;
444 const char *value = find_src ? fetch->src : fetch->dst;
445 if (!fetch->dst)
446 continue;
447 if (fetch->pattern) {
448 if (!prefixcmp(needle, key)) {
449 *result = xmalloc(strlen(value) +
450 strlen(needle) -
451 strlen(key) + 1);
452 strcpy(*result, value);
453 strcpy(*result + strlen(value),
454 needle + strlen(key));
455 refspec->force = fetch->force;
456 return 0;
458 } else if (!strcmp(needle, key)) {
459 *result = xstrdup(value);
460 refspec->force = fetch->force;
461 return 0;
464 return -1;
467 struct ref *alloc_ref(unsigned namelen)
469 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
470 memset(ret, 0, sizeof(struct ref) + namelen);
471 return ret;
474 static struct ref *copy_ref(const struct ref *ref)
476 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
477 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
478 ret->next = NULL;
479 return ret;
482 struct ref *copy_ref_list(const struct ref *ref)
484 struct ref *ret = NULL;
485 struct ref **tail = &ret;
486 while (ref) {
487 *tail = copy_ref(ref);
488 ref = ref->next;
489 tail = &((*tail)->next);
491 return ret;
494 void free_refs(struct ref *ref)
496 struct ref *next;
497 while (ref) {
498 next = ref->next;
499 if (ref->peer_ref)
500 free(ref->peer_ref);
501 free(ref);
502 ref = next;
506 static int count_refspec_match(const char *pattern,
507 struct ref *refs,
508 struct ref **matched_ref)
510 int patlen = strlen(pattern);
511 struct ref *matched_weak = NULL;
512 struct ref *matched = NULL;
513 int weak_match = 0;
514 int match = 0;
516 for (weak_match = match = 0; refs; refs = refs->next) {
517 char *name = refs->name;
518 int namelen = strlen(name);
520 if (!refname_match(pattern, name, ref_rev_parse_rules))
521 continue;
523 /* A match is "weak" if it is with refs outside
524 * heads or tags, and did not specify the pattern
525 * in full (e.g. "refs/remotes/origin/master") or at
526 * least from the toplevel (e.g. "remotes/origin/master");
527 * otherwise "git push $URL master" would result in
528 * ambiguity between remotes/origin/master and heads/master
529 * at the remote site.
531 if (namelen != patlen &&
532 patlen != namelen - 5 &&
533 prefixcmp(name, "refs/heads/") &&
534 prefixcmp(name, "refs/tags/")) {
535 /* We want to catch the case where only weak
536 * matches are found and there are multiple
537 * matches, and where more than one strong
538 * matches are found, as ambiguous. One
539 * strong match with zero or more weak matches
540 * are acceptable as a unique match.
542 matched_weak = refs;
543 weak_match++;
545 else {
546 matched = refs;
547 match++;
550 if (!matched) {
551 *matched_ref = matched_weak;
552 return weak_match;
554 else {
555 *matched_ref = matched;
556 return match;
560 static void tail_link_ref(struct ref *ref, struct ref ***tail)
562 **tail = ref;
563 while (ref->next)
564 ref = ref->next;
565 *tail = &ref->next;
568 static struct ref *try_explicit_object_name(const char *name)
570 unsigned char sha1[20];
571 struct ref *ref;
572 int len;
574 if (!*name) {
575 ref = alloc_ref(20);
576 strcpy(ref->name, "(delete)");
577 hashclr(ref->new_sha1);
578 return ref;
580 if (get_sha1(name, sha1))
581 return NULL;
582 len = strlen(name) + 1;
583 ref = alloc_ref(len);
584 memcpy(ref->name, name, len);
585 hashcpy(ref->new_sha1, sha1);
586 return ref;
589 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
591 struct ref *ret;
592 size_t len;
594 len = strlen(name) + 1;
595 ret = alloc_ref(len);
596 memcpy(ret->name, name, len);
597 tail_link_ref(ret, tail);
598 return ret;
601 static int match_explicit(struct ref *src, struct ref *dst,
602 struct ref ***dst_tail,
603 struct refspec *rs,
604 int errs)
606 struct ref *matched_src, *matched_dst;
608 const char *dst_value = rs->dst;
610 if (rs->pattern)
611 return errs;
613 matched_src = matched_dst = NULL;
614 switch (count_refspec_match(rs->src, src, &matched_src)) {
615 case 1:
616 break;
617 case 0:
618 /* The source could be in the get_sha1() format
619 * not a reference name. :refs/other is a
620 * way to delete 'other' ref at the remote end.
622 matched_src = try_explicit_object_name(rs->src);
623 if (!matched_src)
624 error("src refspec %s does not match any.", rs->src);
625 break;
626 default:
627 matched_src = NULL;
628 error("src refspec %s matches more than one.", rs->src);
629 break;
632 if (!matched_src)
633 errs = 1;
635 if (!dst_value) {
636 if (!matched_src)
637 return errs;
638 dst_value = matched_src->name;
641 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
642 case 1:
643 break;
644 case 0:
645 if (!memcmp(dst_value, "refs/", 5))
646 matched_dst = make_linked_ref(dst_value, dst_tail);
647 else
648 error("dst refspec %s does not match any "
649 "existing ref on the remote and does "
650 "not start with refs/.", dst_value);
651 break;
652 default:
653 matched_dst = NULL;
654 error("dst refspec %s matches more than one.",
655 dst_value);
656 break;
658 if (errs || !matched_dst)
659 return 1;
660 if (matched_dst->peer_ref) {
661 errs = 1;
662 error("dst ref %s receives from more than one src.",
663 matched_dst->name);
665 else {
666 matched_dst->peer_ref = matched_src;
667 matched_dst->force = rs->force;
669 return errs;
672 static int match_explicit_refs(struct ref *src, struct ref *dst,
673 struct ref ***dst_tail, struct refspec *rs,
674 int rs_nr)
676 int i, errs;
677 for (i = errs = 0; i < rs_nr; i++)
678 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
679 return -errs;
682 static const struct refspec *check_pattern_match(const struct refspec *rs,
683 int rs_nr,
684 const struct ref *src)
686 int i;
687 for (i = 0; i < rs_nr; i++) {
688 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
689 return rs + i;
691 return NULL;
695 * Note. This is used only by "push"; refspec matching rules for
696 * push and fetch are subtly different, so do not try to reuse it
697 * without thinking.
699 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
700 int nr_refspec, const char **refspec, int flags)
702 struct refspec *rs =
703 parse_ref_spec(nr_refspec, (const char **) refspec);
704 int send_all = flags & MATCH_REFS_ALL;
705 int send_mirror = flags & MATCH_REFS_MIRROR;
707 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
708 return -1;
710 /* pick the remainder */
711 for ( ; src; src = src->next) {
712 struct ref *dst_peer;
713 const struct refspec *pat = NULL;
714 char *dst_name;
715 if (src->peer_ref)
716 continue;
717 if (nr_refspec) {
718 pat = check_pattern_match(rs, nr_refspec, src);
719 if (!pat)
720 continue;
722 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
724 * "matching refs"; traditionally we pushed everything
725 * including refs outside refs/heads/ hierarchy, but
726 * that does not make much sense these days.
728 continue;
730 if (pat) {
731 const char *dst_side = pat->dst ? pat->dst : pat->src;
732 dst_name = xmalloc(strlen(dst_side) +
733 strlen(src->name) -
734 strlen(pat->src) + 2);
735 strcpy(dst_name, dst_side);
736 strcat(dst_name, src->name + strlen(pat->src));
737 } else
738 dst_name = xstrdup(src->name);
739 dst_peer = find_ref_by_name(dst, dst_name);
740 if (dst_peer && dst_peer->peer_ref)
741 /* We're already sending something to this ref. */
742 goto free_name;
744 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
746 * Remote doesn't have it, and we have no
747 * explicit pattern, and we don't have
748 * --all nor --mirror.
750 goto free_name;
751 if (!dst_peer) {
752 /* Create a new one and link it */
753 dst_peer = make_linked_ref(dst_name, dst_tail);
754 hashcpy(dst_peer->new_sha1, src->new_sha1);
756 dst_peer->peer_ref = src;
757 if (pat)
758 dst_peer->force = pat->force;
759 free_name:
760 free(dst_name);
762 return 0;
765 struct branch *branch_get(const char *name)
767 struct branch *ret;
769 read_config();
770 if (!name || !*name || !strcmp(name, "HEAD"))
771 ret = current_branch;
772 else
773 ret = make_branch(name, 0);
774 if (ret && ret->remote_name) {
775 ret->remote = remote_get(ret->remote_name);
776 if (ret->merge_nr) {
777 int i;
778 ret->merge = xcalloc(sizeof(*ret->merge),
779 ret->merge_nr);
780 for (i = 0; i < ret->merge_nr; i++) {
781 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
782 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
783 remote_find_tracking(ret->remote,
784 ret->merge[i]);
788 return ret;
791 int branch_has_merge_config(struct branch *branch)
793 return branch && !!branch->merge;
796 int branch_merge_matches(struct branch *branch,
797 int i,
798 const char *refname)
800 if (!branch || i < 0 || i >= branch->merge_nr)
801 return 0;
802 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
805 static struct ref *get_expanded_map(const struct ref *remote_refs,
806 const struct refspec *refspec)
808 const struct ref *ref;
809 struct ref *ret = NULL;
810 struct ref **tail = &ret;
812 int remote_prefix_len = strlen(refspec->src);
813 int local_prefix_len = strlen(refspec->dst);
815 for (ref = remote_refs; ref; ref = ref->next) {
816 if (strchr(ref->name, '^'))
817 continue; /* a dereference item */
818 if (!prefixcmp(ref->name, refspec->src)) {
819 const char *match;
820 struct ref *cpy = copy_ref(ref);
821 match = ref->name + remote_prefix_len;
823 cpy->peer_ref = alloc_ref(local_prefix_len +
824 strlen(match) + 1);
825 sprintf(cpy->peer_ref->name, "%s%s",
826 refspec->dst, match);
827 if (refspec->force)
828 cpy->peer_ref->force = 1;
829 *tail = cpy;
830 tail = &cpy->next;
834 return ret;
837 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
839 const struct ref *ref;
840 for (ref = refs; ref; ref = ref->next) {
841 if (refname_match(name, ref->name, ref_fetch_rules))
842 return ref;
844 return NULL;
847 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
849 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
851 if (!ref)
852 return NULL;
854 return copy_ref(ref);
857 static struct ref *get_local_ref(const char *name)
859 struct ref *ret;
860 if (!name)
861 return NULL;
863 if (!prefixcmp(name, "refs/")) {
864 ret = alloc_ref(strlen(name) + 1);
865 strcpy(ret->name, name);
866 return ret;
869 if (!prefixcmp(name, "heads/") ||
870 !prefixcmp(name, "tags/") ||
871 !prefixcmp(name, "remotes/")) {
872 ret = alloc_ref(strlen(name) + 6);
873 sprintf(ret->name, "refs/%s", name);
874 return ret;
877 ret = alloc_ref(strlen(name) + 12);
878 sprintf(ret->name, "refs/heads/%s", name);
879 return ret;
882 int get_fetch_map(const struct ref *remote_refs,
883 const struct refspec *refspec,
884 struct ref ***tail,
885 int missing_ok)
887 struct ref *ref_map, *rm;
889 if (refspec->pattern) {
890 ref_map = get_expanded_map(remote_refs, refspec);
891 } else {
892 const char *name = refspec->src[0] ? refspec->src : "HEAD";
894 ref_map = get_remote_ref(remote_refs, name);
895 if (!missing_ok && !ref_map)
896 die("Couldn't find remote ref %s", name);
897 if (ref_map) {
898 ref_map->peer_ref = get_local_ref(refspec->dst);
899 if (ref_map->peer_ref && refspec->force)
900 ref_map->peer_ref->force = 1;
904 for (rm = ref_map; rm; rm = rm->next) {
905 if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
906 die("* refusing to create funny ref '%s' locally",
907 rm->peer_ref->name);
910 if (ref_map)
911 tail_link_ref(ref_map, tail);
913 return 0;