Add support for url aliases in config files
[git/spearce.git] / remote.c
blob0012954b6f795d75d442f4c58f29dc194888d022
1 #include "cache.h"
2 #include "remote.h"
3 #include "refs.h"
5 struct rewrite {
6 const char *base;
7 const char **instead_of;
8 int instead_of_nr;
9 int instead_of_alloc;
12 static struct remote **remotes;
13 static int remotes_alloc;
14 static int remotes_nr;
16 static struct branch **branches;
17 static int branches_alloc;
18 static int branches_nr;
20 static struct branch *current_branch;
21 static const char *default_remote_name;
23 static struct rewrite **rewrite;
24 static int rewrite_alloc;
25 static int rewrite_nr;
27 #define BUF_SIZE (2048)
28 static char buffer[BUF_SIZE];
30 static const char *alias_url(const char *url)
32 int i, j;
33 for (i = 0; i < rewrite_nr; i++) {
34 if (!rewrite[i])
35 continue;
36 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
37 if (!prefixcmp(url, rewrite[i]->instead_of[j])) {
38 char *ret = malloc(strlen(rewrite[i]->base) -
39 strlen(rewrite[i]->instead_of[j]) +
40 strlen(url) + 1);
41 strcpy(ret, rewrite[i]->base);
42 strcat(ret, url + strlen(rewrite[i]->instead_of[j]));
43 return ret;
47 return url;
50 static void add_push_refspec(struct remote *remote, const char *ref)
52 ALLOC_GROW(remote->push_refspec,
53 remote->push_refspec_nr + 1,
54 remote->push_refspec_alloc);
55 remote->push_refspec[remote->push_refspec_nr++] = ref;
58 static void add_fetch_refspec(struct remote *remote, const char *ref)
60 ALLOC_GROW(remote->fetch_refspec,
61 remote->fetch_refspec_nr + 1,
62 remote->fetch_refspec_alloc);
63 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
66 static void add_url(struct remote *remote, const char *url)
68 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
69 remote->url[remote->url_nr++] = url;
72 static void add_url_alias(struct remote *remote, const char *url)
74 add_url(remote, alias_url(url));
77 static struct remote *make_remote(const char *name, int len)
79 struct remote *ret;
80 int i;
82 for (i = 0; i < remotes_nr; i++) {
83 if (len ? (!strncmp(name, remotes[i]->name, len) &&
84 !remotes[i]->name[len]) :
85 !strcmp(name, remotes[i]->name))
86 return remotes[i];
89 ret = xcalloc(1, sizeof(struct remote));
90 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
91 remotes[remotes_nr++] = ret;
92 if (len)
93 ret->name = xstrndup(name, len);
94 else
95 ret->name = xstrdup(name);
96 return ret;
99 static void add_merge(struct branch *branch, const char *name)
101 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
102 branch->merge_alloc);
103 branch->merge_name[branch->merge_nr++] = name;
106 static struct branch *make_branch(const char *name, int len)
108 struct branch *ret;
109 int i;
110 char *refname;
112 for (i = 0; i < branches_nr; i++) {
113 if (len ? (!strncmp(name, branches[i]->name, len) &&
114 !branches[i]->name[len]) :
115 !strcmp(name, branches[i]->name))
116 return branches[i];
119 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
120 ret = xcalloc(1, sizeof(struct branch));
121 branches[branches_nr++] = ret;
122 if (len)
123 ret->name = xstrndup(name, len);
124 else
125 ret->name = xstrdup(name);
126 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
127 strcpy(refname, "refs/heads/");
128 strcpy(refname + strlen("refs/heads/"), ret->name);
129 ret->refname = refname;
131 return ret;
134 static struct rewrite *make_rewrite(const char *base, int len)
136 struct rewrite *ret;
137 int i;
139 for (i = 0; i < rewrite_nr; i++) {
140 if (len ? (!strncmp(base, rewrite[i]->base, len) &&
141 !rewrite[i]->base[len]) :
142 !strcmp(base, rewrite[i]->base))
143 return rewrite[i];
146 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
147 ret = xcalloc(1, sizeof(struct rewrite));
148 rewrite[rewrite_nr++] = ret;
149 if (len)
150 ret->base = xstrndup(base, len);
151 else
152 ret->base = xstrdup(base);
154 return ret;
157 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
159 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
160 rewrite->instead_of[rewrite->instead_of_nr++] = instead_of;
163 static void read_remotes_file(struct remote *remote)
165 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
167 if (!f)
168 return;
169 while (fgets(buffer, BUF_SIZE, f)) {
170 int value_list;
171 char *s, *p;
173 if (!prefixcmp(buffer, "URL:")) {
174 value_list = 0;
175 s = buffer + 4;
176 } else if (!prefixcmp(buffer, "Push:")) {
177 value_list = 1;
178 s = buffer + 5;
179 } else if (!prefixcmp(buffer, "Pull:")) {
180 value_list = 2;
181 s = buffer + 5;
182 } else
183 continue;
185 while (isspace(*s))
186 s++;
187 if (!*s)
188 continue;
190 p = s + strlen(s);
191 while (isspace(p[-1]))
192 *--p = 0;
194 switch (value_list) {
195 case 0:
196 add_url_alias(remote, xstrdup(s));
197 break;
198 case 1:
199 add_push_refspec(remote, xstrdup(s));
200 break;
201 case 2:
202 add_fetch_refspec(remote, xstrdup(s));
203 break;
206 fclose(f);
209 static void read_branches_file(struct remote *remote)
211 const char *slash = strchr(remote->name, '/');
212 char *frag;
213 char *branch;
214 int n = slash ? slash - remote->name : 1000;
215 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
216 char *s, *p;
217 int len;
219 if (!f)
220 return;
221 s = fgets(buffer, BUF_SIZE, f);
222 fclose(f);
223 if (!s)
224 return;
225 while (isspace(*s))
226 s++;
227 if (!*s)
228 return;
229 p = s + strlen(s);
230 while (isspace(p[-1]))
231 *--p = 0;
232 len = p - s;
233 if (slash)
234 len += strlen(slash);
235 p = xmalloc(len + 1);
236 strcpy(p, s);
237 if (slash)
238 strcat(p, slash);
239 frag = strchr(p, '#');
240 if (frag) {
241 *(frag++) = '\0';
242 branch = xmalloc(strlen(frag) + 12);
243 strcpy(branch, "refs/heads/");
244 strcat(branch, frag);
245 } else {
246 branch = "refs/heads/master";
248 add_url_alias(remote, p);
249 add_fetch_refspec(remote, branch);
250 remote->fetch_tags = 1; /* always auto-follow */
253 static int handle_config(const char *key, const char *value)
255 const char *name;
256 const char *subkey;
257 struct remote *remote;
258 struct branch *branch;
259 if (!prefixcmp(key, "branch.")) {
260 name = key + 7;
261 subkey = strrchr(name, '.');
262 if (!subkey)
263 return 0;
264 branch = make_branch(name, subkey - name);
265 if (!strcmp(subkey, ".remote")) {
266 if (!value)
267 return config_error_nonbool(key);
268 branch->remote_name = xstrdup(value);
269 if (branch == current_branch)
270 default_remote_name = branch->remote_name;
271 } else if (!strcmp(subkey, ".merge")) {
272 if (!value)
273 return config_error_nonbool(key);
274 add_merge(branch, xstrdup(value));
276 return 0;
278 if (!prefixcmp(key, "url.")) {
279 struct rewrite *rewrite;
280 name = key + 5;
281 subkey = strrchr(name, '.');
282 if (!subkey)
283 return 0;
284 rewrite = make_rewrite(name, subkey - name);
285 if (!strcmp(subkey, ".insteadof")) {
286 if (!value)
287 return config_error_nonbool(key);
288 add_instead_of(rewrite, xstrdup(value));
291 if (prefixcmp(key, "remote."))
292 return 0;
293 name = key + 7;
294 subkey = strrchr(name, '.');
295 if (!subkey)
296 return error("Config with no key for remote %s", name);
297 if (*subkey == '/') {
298 warning("Config remote shorthand cannot begin with '/': %s", name);
299 return 0;
301 remote = make_remote(name, subkey - name);
302 if (!value) {
303 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
304 * [remote "frotz"]
305 * disabled
306 * is a valid way to set it to true; we get NULL in value so
307 * we need to handle it here.
309 * if (!strcmp(subkey, ".disabled")) {
310 * val = git_config_bool(key, value);
311 * return 0;
312 * } else
315 return 0; /* ignore unknown booleans */
317 if (!strcmp(subkey, ".url")) {
318 add_url(remote, xstrdup(value));
319 } else if (!strcmp(subkey, ".push")) {
320 add_push_refspec(remote, xstrdup(value));
321 } else if (!strcmp(subkey, ".fetch")) {
322 add_fetch_refspec(remote, xstrdup(value));
323 } else if (!strcmp(subkey, ".receivepack")) {
324 if (!remote->receivepack)
325 remote->receivepack = xstrdup(value);
326 else
327 error("more than one receivepack given, using the first");
328 } else if (!strcmp(subkey, ".uploadpack")) {
329 if (!remote->uploadpack)
330 remote->uploadpack = xstrdup(value);
331 else
332 error("more than one uploadpack given, using the first");
333 } else if (!strcmp(subkey, ".tagopt")) {
334 if (!strcmp(value, "--no-tags"))
335 remote->fetch_tags = -1;
336 } else if (!strcmp(subkey, ".proxy")) {
337 remote->http_proxy = xstrdup(value);
339 return 0;
342 static void alias_all_urls(void)
344 int i, j;
345 for (i = 0; i < remotes_nr; i++) {
346 if (!remotes[i])
347 continue;
348 for (j = 0; j < remotes[i]->url_nr; j++) {
349 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
354 static void read_config(void)
356 unsigned char sha1[20];
357 const char *head_ref;
358 int flag;
359 if (default_remote_name) // did this already
360 return;
361 default_remote_name = xstrdup("origin");
362 current_branch = NULL;
363 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
364 if (head_ref && (flag & REF_ISSYMREF) &&
365 !prefixcmp(head_ref, "refs/heads/")) {
366 current_branch =
367 make_branch(head_ref + strlen("refs/heads/"), 0);
369 git_config(handle_config);
370 alias_all_urls();
373 struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
375 int i;
376 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
377 for (i = 0; i < nr_refspec; i++) {
378 const char *sp, *ep, *gp;
379 sp = refspec[i];
380 if (*sp == '+') {
381 rs[i].force = 1;
382 sp++;
384 gp = strchr(sp, '*');
385 ep = strchr(sp, ':');
386 if (gp && ep && gp > ep)
387 gp = NULL;
388 if (ep) {
389 if (ep[1]) {
390 const char *glob = strchr(ep + 1, '*');
391 if (!glob)
392 gp = NULL;
393 if (gp)
394 rs[i].dst = xstrndup(ep + 1,
395 glob - ep - 1);
396 else
397 rs[i].dst = xstrdup(ep + 1);
399 } else {
400 ep = sp + strlen(sp);
402 if (gp) {
403 rs[i].pattern = 1;
404 ep = gp;
406 rs[i].src = xstrndup(sp, ep - sp);
408 return rs;
411 static int valid_remote_nick(const char *name)
413 if (!name[0] || /* not empty */
414 (name[0] == '.' && /* not "." */
415 (!name[1] || /* not ".." */
416 (name[1] == '.' && !name[2]))))
417 return 0;
418 return !strchr(name, '/'); /* no slash */
421 struct remote *remote_get(const char *name)
423 struct remote *ret;
425 read_config();
426 if (!name)
427 name = default_remote_name;
428 ret = make_remote(name, 0);
429 if (valid_remote_nick(name)) {
430 if (!ret->url)
431 read_remotes_file(ret);
432 if (!ret->url)
433 read_branches_file(ret);
435 if (!ret->url)
436 add_url_alias(ret, name);
437 if (!ret->url)
438 return NULL;
439 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
440 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
441 return ret;
444 int for_each_remote(each_remote_fn fn, void *priv)
446 int i, result = 0;
447 read_config();
448 for (i = 0; i < remotes_nr && !result; i++) {
449 struct remote *r = remotes[i];
450 if (!r)
451 continue;
452 if (!r->fetch)
453 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
454 r->fetch_refspec);
455 if (!r->push)
456 r->push = parse_ref_spec(r->push_refspec_nr,
457 r->push_refspec);
458 result = fn(r, priv);
460 return result;
463 void ref_remove_duplicates(struct ref *ref_map)
465 struct ref **posn;
466 struct ref *next;
467 for (; ref_map; ref_map = ref_map->next) {
468 if (!ref_map->peer_ref)
469 continue;
470 posn = &ref_map->next;
471 while (*posn) {
472 if ((*posn)->peer_ref &&
473 !strcmp((*posn)->peer_ref->name,
474 ref_map->peer_ref->name)) {
475 if (strcmp((*posn)->name, ref_map->name))
476 die("%s tracks both %s and %s",
477 ref_map->peer_ref->name,
478 (*posn)->name, ref_map->name);
479 next = (*posn)->next;
480 free((*posn)->peer_ref);
481 free(*posn);
482 *posn = next;
483 } else {
484 posn = &(*posn)->next;
490 int remote_has_url(struct remote *remote, const char *url)
492 int i;
493 for (i = 0; i < remote->url_nr; i++) {
494 if (!strcmp(remote->url[i], url))
495 return 1;
497 return 0;
500 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
502 int find_src = refspec->src == NULL;
503 char *needle, **result;
504 int i;
506 if (find_src) {
507 if (!refspec->dst)
508 return error("find_tracking: need either src or dst");
509 needle = refspec->dst;
510 result = &refspec->src;
511 } else {
512 needle = refspec->src;
513 result = &refspec->dst;
516 for (i = 0; i < remote->fetch_refspec_nr; i++) {
517 struct refspec *fetch = &remote->fetch[i];
518 const char *key = find_src ? fetch->dst : fetch->src;
519 const char *value = find_src ? fetch->src : fetch->dst;
520 if (!fetch->dst)
521 continue;
522 if (fetch->pattern) {
523 if (!prefixcmp(needle, key)) {
524 *result = xmalloc(strlen(value) +
525 strlen(needle) -
526 strlen(key) + 1);
527 strcpy(*result, value);
528 strcpy(*result + strlen(value),
529 needle + strlen(key));
530 refspec->force = fetch->force;
531 return 0;
533 } else if (!strcmp(needle, key)) {
534 *result = xstrdup(value);
535 refspec->force = fetch->force;
536 return 0;
539 return -1;
542 struct ref *alloc_ref(unsigned namelen)
544 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
545 memset(ret, 0, sizeof(struct ref) + namelen);
546 return ret;
549 static struct ref *copy_ref(const struct ref *ref)
551 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
552 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
553 ret->next = NULL;
554 return ret;
557 struct ref *copy_ref_list(const struct ref *ref)
559 struct ref *ret = NULL;
560 struct ref **tail = &ret;
561 while (ref) {
562 *tail = copy_ref(ref);
563 ref = ref->next;
564 tail = &((*tail)->next);
566 return ret;
569 void free_refs(struct ref *ref)
571 struct ref *next;
572 while (ref) {
573 next = ref->next;
574 if (ref->peer_ref)
575 free(ref->peer_ref);
576 free(ref);
577 ref = next;
581 static int count_refspec_match(const char *pattern,
582 struct ref *refs,
583 struct ref **matched_ref)
585 int patlen = strlen(pattern);
586 struct ref *matched_weak = NULL;
587 struct ref *matched = NULL;
588 int weak_match = 0;
589 int match = 0;
591 for (weak_match = match = 0; refs; refs = refs->next) {
592 char *name = refs->name;
593 int namelen = strlen(name);
595 if (!refname_match(pattern, name, ref_rev_parse_rules))
596 continue;
598 /* A match is "weak" if it is with refs outside
599 * heads or tags, and did not specify the pattern
600 * in full (e.g. "refs/remotes/origin/master") or at
601 * least from the toplevel (e.g. "remotes/origin/master");
602 * otherwise "git push $URL master" would result in
603 * ambiguity between remotes/origin/master and heads/master
604 * at the remote site.
606 if (namelen != patlen &&
607 patlen != namelen - 5 &&
608 prefixcmp(name, "refs/heads/") &&
609 prefixcmp(name, "refs/tags/")) {
610 /* We want to catch the case where only weak
611 * matches are found and there are multiple
612 * matches, and where more than one strong
613 * matches are found, as ambiguous. One
614 * strong match with zero or more weak matches
615 * are acceptable as a unique match.
617 matched_weak = refs;
618 weak_match++;
620 else {
621 matched = refs;
622 match++;
625 if (!matched) {
626 *matched_ref = matched_weak;
627 return weak_match;
629 else {
630 *matched_ref = matched;
631 return match;
635 static void tail_link_ref(struct ref *ref, struct ref ***tail)
637 **tail = ref;
638 while (ref->next)
639 ref = ref->next;
640 *tail = &ref->next;
643 static struct ref *try_explicit_object_name(const char *name)
645 unsigned char sha1[20];
646 struct ref *ref;
647 int len;
649 if (!*name) {
650 ref = alloc_ref(20);
651 strcpy(ref->name, "(delete)");
652 hashclr(ref->new_sha1);
653 return ref;
655 if (get_sha1(name, sha1))
656 return NULL;
657 len = strlen(name) + 1;
658 ref = alloc_ref(len);
659 memcpy(ref->name, name, len);
660 hashcpy(ref->new_sha1, sha1);
661 return ref;
664 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
666 struct ref *ret;
667 size_t len;
669 len = strlen(name) + 1;
670 ret = alloc_ref(len);
671 memcpy(ret->name, name, len);
672 tail_link_ref(ret, tail);
673 return ret;
676 static int match_explicit(struct ref *src, struct ref *dst,
677 struct ref ***dst_tail,
678 struct refspec *rs,
679 int errs)
681 struct ref *matched_src, *matched_dst;
683 const char *dst_value = rs->dst;
685 if (rs->pattern)
686 return errs;
688 matched_src = matched_dst = NULL;
689 switch (count_refspec_match(rs->src, src, &matched_src)) {
690 case 1:
691 break;
692 case 0:
693 /* The source could be in the get_sha1() format
694 * not a reference name. :refs/other is a
695 * way to delete 'other' ref at the remote end.
697 matched_src = try_explicit_object_name(rs->src);
698 if (!matched_src)
699 error("src refspec %s does not match any.", rs->src);
700 break;
701 default:
702 matched_src = NULL;
703 error("src refspec %s matches more than one.", rs->src);
704 break;
707 if (!matched_src)
708 errs = 1;
710 if (!dst_value) {
711 if (!matched_src)
712 return errs;
713 dst_value = matched_src->name;
716 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
717 case 1:
718 break;
719 case 0:
720 if (!memcmp(dst_value, "refs/", 5))
721 matched_dst = make_linked_ref(dst_value, dst_tail);
722 else
723 error("dst refspec %s does not match any "
724 "existing ref on the remote and does "
725 "not start with refs/.", dst_value);
726 break;
727 default:
728 matched_dst = NULL;
729 error("dst refspec %s matches more than one.",
730 dst_value);
731 break;
733 if (errs || !matched_dst)
734 return 1;
735 if (matched_dst->peer_ref) {
736 errs = 1;
737 error("dst ref %s receives from more than one src.",
738 matched_dst->name);
740 else {
741 matched_dst->peer_ref = matched_src;
742 matched_dst->force = rs->force;
744 return errs;
747 static int match_explicit_refs(struct ref *src, struct ref *dst,
748 struct ref ***dst_tail, struct refspec *rs,
749 int rs_nr)
751 int i, errs;
752 for (i = errs = 0; i < rs_nr; i++)
753 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
754 return -errs;
757 static const struct refspec *check_pattern_match(const struct refspec *rs,
758 int rs_nr,
759 const struct ref *src)
761 int i;
762 for (i = 0; i < rs_nr; i++) {
763 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
764 return rs + i;
766 return NULL;
770 * Note. This is used only by "push"; refspec matching rules for
771 * push and fetch are subtly different, so do not try to reuse it
772 * without thinking.
774 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
775 int nr_refspec, const char **refspec, int flags)
777 struct refspec *rs =
778 parse_ref_spec(nr_refspec, (const char **) refspec);
779 int send_all = flags & MATCH_REFS_ALL;
780 int send_mirror = flags & MATCH_REFS_MIRROR;
782 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
783 return -1;
785 /* pick the remainder */
786 for ( ; src; src = src->next) {
787 struct ref *dst_peer;
788 const struct refspec *pat = NULL;
789 char *dst_name;
790 if (src->peer_ref)
791 continue;
792 if (nr_refspec) {
793 pat = check_pattern_match(rs, nr_refspec, src);
794 if (!pat)
795 continue;
797 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
799 * "matching refs"; traditionally we pushed everything
800 * including refs outside refs/heads/ hierarchy, but
801 * that does not make much sense these days.
803 continue;
805 if (pat) {
806 const char *dst_side = pat->dst ? pat->dst : pat->src;
807 dst_name = xmalloc(strlen(dst_side) +
808 strlen(src->name) -
809 strlen(pat->src) + 2);
810 strcpy(dst_name, dst_side);
811 strcat(dst_name, src->name + strlen(pat->src));
812 } else
813 dst_name = xstrdup(src->name);
814 dst_peer = find_ref_by_name(dst, dst_name);
815 if (dst_peer && dst_peer->peer_ref)
816 /* We're already sending something to this ref. */
817 goto free_name;
819 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
821 * Remote doesn't have it, and we have no
822 * explicit pattern, and we don't have
823 * --all nor --mirror.
825 goto free_name;
826 if (!dst_peer) {
827 /* Create a new one and link it */
828 dst_peer = make_linked_ref(dst_name, dst_tail);
829 hashcpy(dst_peer->new_sha1, src->new_sha1);
831 dst_peer->peer_ref = src;
832 if (pat)
833 dst_peer->force = pat->force;
834 free_name:
835 free(dst_name);
837 return 0;
840 struct branch *branch_get(const char *name)
842 struct branch *ret;
844 read_config();
845 if (!name || !*name || !strcmp(name, "HEAD"))
846 ret = current_branch;
847 else
848 ret = make_branch(name, 0);
849 if (ret && ret->remote_name) {
850 ret->remote = remote_get(ret->remote_name);
851 if (ret->merge_nr) {
852 int i;
853 ret->merge = xcalloc(sizeof(*ret->merge),
854 ret->merge_nr);
855 for (i = 0; i < ret->merge_nr; i++) {
856 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
857 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
858 remote_find_tracking(ret->remote,
859 ret->merge[i]);
863 return ret;
866 int branch_has_merge_config(struct branch *branch)
868 return branch && !!branch->merge;
871 int branch_merge_matches(struct branch *branch,
872 int i,
873 const char *refname)
875 if (!branch || i < 0 || i >= branch->merge_nr)
876 return 0;
877 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
880 static struct ref *get_expanded_map(const struct ref *remote_refs,
881 const struct refspec *refspec)
883 const struct ref *ref;
884 struct ref *ret = NULL;
885 struct ref **tail = &ret;
887 int remote_prefix_len = strlen(refspec->src);
888 int local_prefix_len = strlen(refspec->dst);
890 for (ref = remote_refs; ref; ref = ref->next) {
891 if (strchr(ref->name, '^'))
892 continue; /* a dereference item */
893 if (!prefixcmp(ref->name, refspec->src)) {
894 const char *match;
895 struct ref *cpy = copy_ref(ref);
896 match = ref->name + remote_prefix_len;
898 cpy->peer_ref = alloc_ref(local_prefix_len +
899 strlen(match) + 1);
900 sprintf(cpy->peer_ref->name, "%s%s",
901 refspec->dst, match);
902 if (refspec->force)
903 cpy->peer_ref->force = 1;
904 *tail = cpy;
905 tail = &cpy->next;
909 return ret;
912 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
914 const struct ref *ref;
915 for (ref = refs; ref; ref = ref->next) {
916 if (refname_match(name, ref->name, ref_fetch_rules))
917 return ref;
919 return NULL;
922 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
924 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
926 if (!ref)
927 return NULL;
929 return copy_ref(ref);
932 static struct ref *get_local_ref(const char *name)
934 struct ref *ret;
935 if (!name)
936 return NULL;
938 if (!prefixcmp(name, "refs/")) {
939 ret = alloc_ref(strlen(name) + 1);
940 strcpy(ret->name, name);
941 return ret;
944 if (!prefixcmp(name, "heads/") ||
945 !prefixcmp(name, "tags/") ||
946 !prefixcmp(name, "remotes/")) {
947 ret = alloc_ref(strlen(name) + 6);
948 sprintf(ret->name, "refs/%s", name);
949 return ret;
952 ret = alloc_ref(strlen(name) + 12);
953 sprintf(ret->name, "refs/heads/%s", name);
954 return ret;
957 int get_fetch_map(const struct ref *remote_refs,
958 const struct refspec *refspec,
959 struct ref ***tail,
960 int missing_ok)
962 struct ref *ref_map, *rm;
964 if (refspec->pattern) {
965 ref_map = get_expanded_map(remote_refs, refspec);
966 } else {
967 const char *name = refspec->src[0] ? refspec->src : "HEAD";
969 ref_map = get_remote_ref(remote_refs, name);
970 if (!missing_ok && !ref_map)
971 die("Couldn't find remote ref %s", name);
972 if (ref_map) {
973 ref_map->peer_ref = get_local_ref(refspec->dst);
974 if (ref_map->peer_ref && refspec->force)
975 ref_map->peer_ref->force = 1;
979 for (rm = ref_map; rm; rm = rm->next) {
980 if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
981 die("* refusing to create funny ref '%s' locally",
982 rm->peer_ref->name);
985 if (ref_map)
986 tail_link_ref(ref_map, tail);
988 return 0;