make it easier for people who just want to get rid of 'git gc --auto'
[git/spearce.git] / remote.c
blob9700a33c57d796144a3b2f2e47b53d1d32d14c0d
1 #include "cache.h"
2 #include "remote.h"
3 #include "refs.h"
5 struct counted_string {
6 size_t len;
7 const char *s;
8 };
9 struct rewrite {
10 const char *base;
11 size_t baselen;
12 struct counted_string *instead_of;
13 int instead_of_nr;
14 int instead_of_alloc;
17 static struct remote **remotes;
18 static int remotes_alloc;
19 static int remotes_nr;
21 static struct branch **branches;
22 static int branches_alloc;
23 static int branches_nr;
25 static struct branch *current_branch;
26 static const char *default_remote_name;
28 static struct rewrite **rewrite;
29 static int rewrite_alloc;
30 static int rewrite_nr;
32 #define BUF_SIZE (2048)
33 static char buffer[BUF_SIZE];
35 static const char *alias_url(const char *url)
37 int i, j;
38 char *ret;
39 struct counted_string *longest;
40 int longest_i;
42 longest = NULL;
43 longest_i = -1;
44 for (i = 0; i < rewrite_nr; i++) {
45 if (!rewrite[i])
46 continue;
47 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
48 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
49 (!longest ||
50 longest->len < rewrite[i]->instead_of[j].len)) {
51 longest = &(rewrite[i]->instead_of[j]);
52 longest_i = i;
56 if (!longest)
57 return url;
59 ret = malloc(rewrite[longest_i]->baselen +
60 (strlen(url) - longest->len) + 1);
61 strcpy(ret, rewrite[longest_i]->base);
62 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
63 return ret;
66 static void add_push_refspec(struct remote *remote, const char *ref)
68 ALLOC_GROW(remote->push_refspec,
69 remote->push_refspec_nr + 1,
70 remote->push_refspec_alloc);
71 remote->push_refspec[remote->push_refspec_nr++] = ref;
74 static void add_fetch_refspec(struct remote *remote, const char *ref)
76 ALLOC_GROW(remote->fetch_refspec,
77 remote->fetch_refspec_nr + 1,
78 remote->fetch_refspec_alloc);
79 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
82 static void add_url(struct remote *remote, const char *url)
84 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
85 remote->url[remote->url_nr++] = url;
88 static void add_url_alias(struct remote *remote, const char *url)
90 add_url(remote, alias_url(url));
93 static struct remote *make_remote(const char *name, int len)
95 struct remote *ret;
96 int i;
98 for (i = 0; i < remotes_nr; i++) {
99 if (len ? (!strncmp(name, remotes[i]->name, len) &&
100 !remotes[i]->name[len]) :
101 !strcmp(name, remotes[i]->name))
102 return remotes[i];
105 ret = xcalloc(1, sizeof(struct remote));
106 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
107 remotes[remotes_nr++] = ret;
108 if (len)
109 ret->name = xstrndup(name, len);
110 else
111 ret->name = xstrdup(name);
112 return ret;
115 static void add_merge(struct branch *branch, const char *name)
117 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
118 branch->merge_alloc);
119 branch->merge_name[branch->merge_nr++] = name;
122 static struct branch *make_branch(const char *name, int len)
124 struct branch *ret;
125 int i;
126 char *refname;
128 for (i = 0; i < branches_nr; i++) {
129 if (len ? (!strncmp(name, branches[i]->name, len) &&
130 !branches[i]->name[len]) :
131 !strcmp(name, branches[i]->name))
132 return branches[i];
135 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
136 ret = xcalloc(1, sizeof(struct branch));
137 branches[branches_nr++] = ret;
138 if (len)
139 ret->name = xstrndup(name, len);
140 else
141 ret->name = xstrdup(name);
142 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
143 strcpy(refname, "refs/heads/");
144 strcpy(refname + strlen("refs/heads/"), ret->name);
145 ret->refname = refname;
147 return ret;
150 static struct rewrite *make_rewrite(const char *base, int len)
152 struct rewrite *ret;
153 int i;
155 for (i = 0; i < rewrite_nr; i++) {
156 if (len
157 ? (len == rewrite[i]->baselen &&
158 !strncmp(base, rewrite[i]->base, len))
159 : !strcmp(base, rewrite[i]->base))
160 return rewrite[i];
163 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
164 ret = xcalloc(1, sizeof(struct rewrite));
165 rewrite[rewrite_nr++] = ret;
166 if (len) {
167 ret->base = xstrndup(base, len);
168 ret->baselen = len;
170 else {
171 ret->base = xstrdup(base);
172 ret->baselen = strlen(base);
174 return ret;
177 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
179 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
180 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
181 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
182 rewrite->instead_of_nr++;
185 static void read_remotes_file(struct remote *remote)
187 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
189 if (!f)
190 return;
191 while (fgets(buffer, BUF_SIZE, f)) {
192 int value_list;
193 char *s, *p;
195 if (!prefixcmp(buffer, "URL:")) {
196 value_list = 0;
197 s = buffer + 4;
198 } else if (!prefixcmp(buffer, "Push:")) {
199 value_list = 1;
200 s = buffer + 5;
201 } else if (!prefixcmp(buffer, "Pull:")) {
202 value_list = 2;
203 s = buffer + 5;
204 } else
205 continue;
207 while (isspace(*s))
208 s++;
209 if (!*s)
210 continue;
212 p = s + strlen(s);
213 while (isspace(p[-1]))
214 *--p = 0;
216 switch (value_list) {
217 case 0:
218 add_url_alias(remote, xstrdup(s));
219 break;
220 case 1:
221 add_push_refspec(remote, xstrdup(s));
222 break;
223 case 2:
224 add_fetch_refspec(remote, xstrdup(s));
225 break;
228 fclose(f);
231 static void read_branches_file(struct remote *remote)
233 const char *slash = strchr(remote->name, '/');
234 char *frag;
235 char *branch;
236 int n = slash ? slash - remote->name : 1000;
237 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
238 char *s, *p;
239 int len;
241 if (!f)
242 return;
243 s = fgets(buffer, BUF_SIZE, f);
244 fclose(f);
245 if (!s)
246 return;
247 while (isspace(*s))
248 s++;
249 if (!*s)
250 return;
251 p = s + strlen(s);
252 while (isspace(p[-1]))
253 *--p = 0;
254 len = p - s;
255 if (slash)
256 len += strlen(slash);
257 p = xmalloc(len + 1);
258 strcpy(p, s);
259 if (slash)
260 strcat(p, slash);
261 frag = strchr(p, '#');
262 if (frag) {
263 *(frag++) = '\0';
264 branch = xmalloc(strlen(frag) + 12);
265 strcpy(branch, "refs/heads/");
266 strcat(branch, frag);
267 } else {
268 branch = "refs/heads/master";
270 add_url_alias(remote, p);
271 add_fetch_refspec(remote, branch);
272 remote->fetch_tags = 1; /* always auto-follow */
275 static int handle_config(const char *key, const char *value)
277 const char *name;
278 const char *subkey;
279 struct remote *remote;
280 struct branch *branch;
281 if (!prefixcmp(key, "branch.")) {
282 name = key + 7;
283 subkey = strrchr(name, '.');
284 if (!subkey)
285 return 0;
286 branch = make_branch(name, subkey - name);
287 if (!strcmp(subkey, ".remote")) {
288 if (!value)
289 return config_error_nonbool(key);
290 branch->remote_name = xstrdup(value);
291 if (branch == current_branch)
292 default_remote_name = branch->remote_name;
293 } else if (!strcmp(subkey, ".merge")) {
294 if (!value)
295 return config_error_nonbool(key);
296 add_merge(branch, xstrdup(value));
298 return 0;
300 if (!prefixcmp(key, "url.")) {
301 struct rewrite *rewrite;
302 name = key + 5;
303 subkey = strrchr(name, '.');
304 if (!subkey)
305 return 0;
306 rewrite = make_rewrite(name, subkey - name);
307 if (!strcmp(subkey, ".insteadof")) {
308 if (!value)
309 return config_error_nonbool(key);
310 add_instead_of(rewrite, xstrdup(value));
313 if (prefixcmp(key, "remote."))
314 return 0;
315 name = key + 7;
316 subkey = strrchr(name, '.');
317 if (!subkey)
318 return error("Config with no key for remote %s", name);
319 if (*subkey == '/') {
320 warning("Config remote shorthand cannot begin with '/': %s", name);
321 return 0;
323 remote = make_remote(name, subkey - name);
324 if (!value) {
325 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
326 * [remote "frotz"]
327 * disabled
328 * is a valid way to set it to true; we get NULL in value so
329 * we need to handle it here.
331 * if (!strcmp(subkey, ".disabled")) {
332 * val = git_config_bool(key, value);
333 * return 0;
334 * } else
337 return 0; /* ignore unknown booleans */
339 if (!strcmp(subkey, ".url")) {
340 add_url(remote, xstrdup(value));
341 } else if (!strcmp(subkey, ".push")) {
342 add_push_refspec(remote, xstrdup(value));
343 } else if (!strcmp(subkey, ".fetch")) {
344 add_fetch_refspec(remote, xstrdup(value));
345 } else if (!strcmp(subkey, ".receivepack")) {
346 if (!remote->receivepack)
347 remote->receivepack = xstrdup(value);
348 else
349 error("more than one receivepack given, using the first");
350 } else if (!strcmp(subkey, ".uploadpack")) {
351 if (!remote->uploadpack)
352 remote->uploadpack = xstrdup(value);
353 else
354 error("more than one uploadpack given, using the first");
355 } else if (!strcmp(subkey, ".tagopt")) {
356 if (!strcmp(value, "--no-tags"))
357 remote->fetch_tags = -1;
358 } else if (!strcmp(subkey, ".proxy")) {
359 remote->http_proxy = xstrdup(value);
360 } else if (!strcmp(subkey, ".skipdefaultupdate"))
361 remote->skip_default_update = 1;
362 return 0;
365 static void alias_all_urls(void)
367 int i, j;
368 for (i = 0; i < remotes_nr; i++) {
369 if (!remotes[i])
370 continue;
371 for (j = 0; j < remotes[i]->url_nr; j++) {
372 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
377 static void read_config(void)
379 unsigned char sha1[20];
380 const char *head_ref;
381 int flag;
382 if (default_remote_name) // did this already
383 return;
384 default_remote_name = xstrdup("origin");
385 current_branch = NULL;
386 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
387 if (head_ref && (flag & REF_ISSYMREF) &&
388 !prefixcmp(head_ref, "refs/heads/")) {
389 current_branch =
390 make_branch(head_ref + strlen("refs/heads/"), 0);
392 git_config(handle_config);
393 alias_all_urls();
396 struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
398 int i;
399 int st;
400 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
401 for (i = 0; i < nr_refspec; i++) {
402 const char *sp, *ep, *gp;
403 sp = refspec[i];
404 if (*sp == '+') {
405 rs[i].force = 1;
406 sp++;
408 gp = strstr(sp, "/*");
409 ep = strchr(sp, ':');
410 if (gp && ep && gp > ep)
411 gp = NULL;
412 if (ep) {
413 if (ep[1]) {
414 const char *glob = strstr(ep + 1, "/*");
415 if (glob && glob[2])
416 glob = NULL;
417 if (!glob)
418 gp = NULL;
419 if (gp)
420 rs[i].dst = xstrndup(ep + 1,
421 glob - ep - 1);
422 else
423 rs[i].dst = xstrdup(ep + 1);
425 } else {
426 ep = sp + strlen(sp);
428 if (gp && gp + 2 != ep)
429 gp = NULL;
430 if (gp) {
431 rs[i].pattern = 1;
432 ep = gp;
434 rs[i].src = xstrndup(sp, ep - sp);
436 if (*rs[i].src) {
437 st = check_ref_format(rs[i].src);
438 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
439 die("Invalid refspec '%s'", refspec[i]);
441 if (rs[i].dst && *rs[i].dst) {
442 st = check_ref_format(rs[i].dst);
443 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
444 die("Invalid refspec '%s'", refspec[i]);
447 return rs;
450 static int valid_remote_nick(const char *name)
452 if (!name[0] || /* not empty */
453 (name[0] == '.' && /* not "." */
454 (!name[1] || /* not ".." */
455 (name[1] == '.' && !name[2]))))
456 return 0;
457 return !strchr(name, '/'); /* no slash */
460 struct remote *remote_get(const char *name)
462 struct remote *ret;
464 read_config();
465 if (!name)
466 name = default_remote_name;
467 ret = make_remote(name, 0);
468 if (valid_remote_nick(name)) {
469 if (!ret->url)
470 read_remotes_file(ret);
471 if (!ret->url)
472 read_branches_file(ret);
474 if (!ret->url)
475 add_url_alias(ret, name);
476 if (!ret->url)
477 return NULL;
478 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
479 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
480 return ret;
483 int for_each_remote(each_remote_fn fn, void *priv)
485 int i, result = 0;
486 read_config();
487 for (i = 0; i < remotes_nr && !result; i++) {
488 struct remote *r = remotes[i];
489 if (!r)
490 continue;
491 if (!r->fetch)
492 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
493 r->fetch_refspec);
494 if (!r->push)
495 r->push = parse_ref_spec(r->push_refspec_nr,
496 r->push_refspec);
497 result = fn(r, priv);
499 return result;
502 void ref_remove_duplicates(struct ref *ref_map)
504 struct ref **posn;
505 struct ref *next;
506 for (; ref_map; ref_map = ref_map->next) {
507 if (!ref_map->peer_ref)
508 continue;
509 posn = &ref_map->next;
510 while (*posn) {
511 if ((*posn)->peer_ref &&
512 !strcmp((*posn)->peer_ref->name,
513 ref_map->peer_ref->name)) {
514 if (strcmp((*posn)->name, ref_map->name))
515 die("%s tracks both %s and %s",
516 ref_map->peer_ref->name,
517 (*posn)->name, ref_map->name);
518 next = (*posn)->next;
519 free((*posn)->peer_ref);
520 free(*posn);
521 *posn = next;
522 } else {
523 posn = &(*posn)->next;
529 int remote_has_url(struct remote *remote, const char *url)
531 int i;
532 for (i = 0; i < remote->url_nr; i++) {
533 if (!strcmp(remote->url[i], url))
534 return 1;
536 return 0;
539 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
541 int find_src = refspec->src == NULL;
542 char *needle, **result;
543 int i;
545 if (find_src) {
546 if (!refspec->dst)
547 return error("find_tracking: need either src or dst");
548 needle = refspec->dst;
549 result = &refspec->src;
550 } else {
551 needle = refspec->src;
552 result = &refspec->dst;
555 for (i = 0; i < remote->fetch_refspec_nr; i++) {
556 struct refspec *fetch = &remote->fetch[i];
557 const char *key = find_src ? fetch->dst : fetch->src;
558 const char *value = find_src ? fetch->src : fetch->dst;
559 if (!fetch->dst)
560 continue;
561 if (fetch->pattern) {
562 if (!prefixcmp(needle, key) &&
563 needle[strlen(key)] == '/') {
564 *result = xmalloc(strlen(value) +
565 strlen(needle) -
566 strlen(key) + 1);
567 strcpy(*result, value);
568 strcpy(*result + strlen(value),
569 needle + strlen(key));
570 refspec->force = fetch->force;
571 return 0;
573 } else if (!strcmp(needle, key)) {
574 *result = xstrdup(value);
575 refspec->force = fetch->force;
576 return 0;
579 return -1;
582 struct ref *alloc_ref(unsigned namelen)
584 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
585 memset(ret, 0, sizeof(struct ref) + namelen);
586 return ret;
589 static struct ref *copy_ref(const struct ref *ref)
591 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
592 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
593 ret->next = NULL;
594 return ret;
597 struct ref *copy_ref_list(const struct ref *ref)
599 struct ref *ret = NULL;
600 struct ref **tail = &ret;
601 while (ref) {
602 *tail = copy_ref(ref);
603 ref = ref->next;
604 tail = &((*tail)->next);
606 return ret;
609 void free_refs(struct ref *ref)
611 struct ref *next;
612 while (ref) {
613 next = ref->next;
614 free(ref->peer_ref);
615 free(ref);
616 ref = next;
620 static int count_refspec_match(const char *pattern,
621 struct ref *refs,
622 struct ref **matched_ref)
624 int patlen = strlen(pattern);
625 struct ref *matched_weak = NULL;
626 struct ref *matched = NULL;
627 int weak_match = 0;
628 int match = 0;
630 for (weak_match = match = 0; refs; refs = refs->next) {
631 char *name = refs->name;
632 int namelen = strlen(name);
634 if (!refname_match(pattern, name, ref_rev_parse_rules))
635 continue;
637 /* A match is "weak" if it is with refs outside
638 * heads or tags, and did not specify the pattern
639 * in full (e.g. "refs/remotes/origin/master") or at
640 * least from the toplevel (e.g. "remotes/origin/master");
641 * otherwise "git push $URL master" would result in
642 * ambiguity between remotes/origin/master and heads/master
643 * at the remote site.
645 if (namelen != patlen &&
646 patlen != namelen - 5 &&
647 prefixcmp(name, "refs/heads/") &&
648 prefixcmp(name, "refs/tags/")) {
649 /* We want to catch the case where only weak
650 * matches are found and there are multiple
651 * matches, and where more than one strong
652 * matches are found, as ambiguous. One
653 * strong match with zero or more weak matches
654 * are acceptable as a unique match.
656 matched_weak = refs;
657 weak_match++;
659 else {
660 matched = refs;
661 match++;
664 if (!matched) {
665 *matched_ref = matched_weak;
666 return weak_match;
668 else {
669 *matched_ref = matched;
670 return match;
674 static void tail_link_ref(struct ref *ref, struct ref ***tail)
676 **tail = ref;
677 while (ref->next)
678 ref = ref->next;
679 *tail = &ref->next;
682 static struct ref *try_explicit_object_name(const char *name)
684 unsigned char sha1[20];
685 struct ref *ref;
686 int len;
688 if (!*name) {
689 ref = alloc_ref(20);
690 strcpy(ref->name, "(delete)");
691 hashclr(ref->new_sha1);
692 return ref;
694 if (get_sha1(name, sha1))
695 return NULL;
696 len = strlen(name) + 1;
697 ref = alloc_ref(len);
698 memcpy(ref->name, name, len);
699 hashcpy(ref->new_sha1, sha1);
700 return ref;
703 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
705 struct ref *ret;
706 size_t len;
708 len = strlen(name) + 1;
709 ret = alloc_ref(len);
710 memcpy(ret->name, name, len);
711 tail_link_ref(ret, tail);
712 return ret;
715 static int match_explicit(struct ref *src, struct ref *dst,
716 struct ref ***dst_tail,
717 struct refspec *rs,
718 int errs)
720 struct ref *matched_src, *matched_dst;
722 const char *dst_value = rs->dst;
724 if (rs->pattern)
725 return errs;
727 matched_src = matched_dst = NULL;
728 switch (count_refspec_match(rs->src, src, &matched_src)) {
729 case 1:
730 break;
731 case 0:
732 /* The source could be in the get_sha1() format
733 * not a reference name. :refs/other is a
734 * way to delete 'other' ref at the remote end.
736 matched_src = try_explicit_object_name(rs->src);
737 if (!matched_src)
738 error("src refspec %s does not match any.", rs->src);
739 break;
740 default:
741 matched_src = NULL;
742 error("src refspec %s matches more than one.", rs->src);
743 break;
746 if (!matched_src)
747 errs = 1;
749 if (!dst_value) {
750 unsigned char sha1[20];
751 int flag;
753 if (!matched_src)
754 return errs;
755 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
756 if (!dst_value ||
757 ((flag & REF_ISSYMREF) &&
758 prefixcmp(dst_value, "refs/heads/")))
759 die("%s cannot be resolved to branch.",
760 matched_src->name);
763 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
764 case 1:
765 break;
766 case 0:
767 if (!memcmp(dst_value, "refs/", 5))
768 matched_dst = make_linked_ref(dst_value, dst_tail);
769 else
770 error("dst refspec %s does not match any "
771 "existing ref on the remote and does "
772 "not start with refs/.", dst_value);
773 break;
774 default:
775 matched_dst = NULL;
776 error("dst refspec %s matches more than one.",
777 dst_value);
778 break;
780 if (errs || !matched_dst)
781 return 1;
782 if (matched_dst->peer_ref) {
783 errs = 1;
784 error("dst ref %s receives from more than one src.",
785 matched_dst->name);
787 else {
788 matched_dst->peer_ref = matched_src;
789 matched_dst->force = rs->force;
791 return errs;
794 static int match_explicit_refs(struct ref *src, struct ref *dst,
795 struct ref ***dst_tail, struct refspec *rs,
796 int rs_nr)
798 int i, errs;
799 for (i = errs = 0; i < rs_nr; i++)
800 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
801 return -errs;
804 static const struct refspec *check_pattern_match(const struct refspec *rs,
805 int rs_nr,
806 const struct ref *src)
808 int i;
809 for (i = 0; i < rs_nr; i++) {
810 if (rs[i].pattern &&
811 !prefixcmp(src->name, rs[i].src) &&
812 src->name[strlen(rs[i].src)] == '/')
813 return rs + i;
815 return NULL;
819 * Note. This is used only by "push"; refspec matching rules for
820 * push and fetch are subtly different, so do not try to reuse it
821 * without thinking.
823 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
824 int nr_refspec, const char **refspec, int flags)
826 struct refspec *rs =
827 parse_ref_spec(nr_refspec, (const char **) refspec);
828 int send_all = flags & MATCH_REFS_ALL;
829 int send_mirror = flags & MATCH_REFS_MIRROR;
831 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
832 return -1;
834 /* pick the remainder */
835 for ( ; src; src = src->next) {
836 struct ref *dst_peer;
837 const struct refspec *pat = NULL;
838 char *dst_name;
839 if (src->peer_ref)
840 continue;
841 if (nr_refspec) {
842 pat = check_pattern_match(rs, nr_refspec, src);
843 if (!pat)
844 continue;
846 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
848 * "matching refs"; traditionally we pushed everything
849 * including refs outside refs/heads/ hierarchy, but
850 * that does not make much sense these days.
852 continue;
854 if (pat) {
855 const char *dst_side = pat->dst ? pat->dst : pat->src;
856 dst_name = xmalloc(strlen(dst_side) +
857 strlen(src->name) -
858 strlen(pat->src) + 2);
859 strcpy(dst_name, dst_side);
860 strcat(dst_name, src->name + strlen(pat->src));
861 } else
862 dst_name = xstrdup(src->name);
863 dst_peer = find_ref_by_name(dst, dst_name);
864 if (dst_peer && dst_peer->peer_ref)
865 /* We're already sending something to this ref. */
866 goto free_name;
868 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
870 * Remote doesn't have it, and we have no
871 * explicit pattern, and we don't have
872 * --all nor --mirror.
874 goto free_name;
875 if (!dst_peer) {
876 /* Create a new one and link it */
877 dst_peer = make_linked_ref(dst_name, dst_tail);
878 hashcpy(dst_peer->new_sha1, src->new_sha1);
880 dst_peer->peer_ref = src;
881 if (pat)
882 dst_peer->force = pat->force;
883 free_name:
884 free(dst_name);
886 return 0;
889 struct branch *branch_get(const char *name)
891 struct branch *ret;
893 read_config();
894 if (!name || !*name || !strcmp(name, "HEAD"))
895 ret = current_branch;
896 else
897 ret = make_branch(name, 0);
898 if (ret && ret->remote_name) {
899 ret->remote = remote_get(ret->remote_name);
900 if (ret->merge_nr) {
901 int i;
902 ret->merge = xcalloc(sizeof(*ret->merge),
903 ret->merge_nr);
904 for (i = 0; i < ret->merge_nr; i++) {
905 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
906 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
907 remote_find_tracking(ret->remote,
908 ret->merge[i]);
912 return ret;
915 int branch_has_merge_config(struct branch *branch)
917 return branch && !!branch->merge;
920 int branch_merge_matches(struct branch *branch,
921 int i,
922 const char *refname)
924 if (!branch || i < 0 || i >= branch->merge_nr)
925 return 0;
926 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
929 static struct ref *get_expanded_map(const struct ref *remote_refs,
930 const struct refspec *refspec)
932 const struct ref *ref;
933 struct ref *ret = NULL;
934 struct ref **tail = &ret;
936 int remote_prefix_len = strlen(refspec->src);
937 int local_prefix_len = strlen(refspec->dst);
939 for (ref = remote_refs; ref; ref = ref->next) {
940 if (strchr(ref->name, '^'))
941 continue; /* a dereference item */
942 if (!prefixcmp(ref->name, refspec->src)) {
943 const char *match;
944 struct ref *cpy = copy_ref(ref);
945 match = ref->name + remote_prefix_len;
947 cpy->peer_ref = alloc_ref(local_prefix_len +
948 strlen(match) + 1);
949 sprintf(cpy->peer_ref->name, "%s%s",
950 refspec->dst, match);
951 if (refspec->force)
952 cpy->peer_ref->force = 1;
953 *tail = cpy;
954 tail = &cpy->next;
958 return ret;
961 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
963 const struct ref *ref;
964 for (ref = refs; ref; ref = ref->next) {
965 if (refname_match(name, ref->name, ref_fetch_rules))
966 return ref;
968 return NULL;
971 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
973 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
975 if (!ref)
976 return NULL;
978 return copy_ref(ref);
981 static struct ref *get_local_ref(const char *name)
983 struct ref *ret;
984 if (!name)
985 return NULL;
987 if (!prefixcmp(name, "refs/")) {
988 ret = alloc_ref(strlen(name) + 1);
989 strcpy(ret->name, name);
990 return ret;
993 if (!prefixcmp(name, "heads/") ||
994 !prefixcmp(name, "tags/") ||
995 !prefixcmp(name, "remotes/")) {
996 ret = alloc_ref(strlen(name) + 6);
997 sprintf(ret->name, "refs/%s", name);
998 return ret;
1001 ret = alloc_ref(strlen(name) + 12);
1002 sprintf(ret->name, "refs/heads/%s", name);
1003 return ret;
1006 int get_fetch_map(const struct ref *remote_refs,
1007 const struct refspec *refspec,
1008 struct ref ***tail,
1009 int missing_ok)
1011 struct ref *ref_map, **rmp;
1013 if (refspec->pattern) {
1014 ref_map = get_expanded_map(remote_refs, refspec);
1015 } else {
1016 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1018 ref_map = get_remote_ref(remote_refs, name);
1019 if (!missing_ok && !ref_map)
1020 die("Couldn't find remote ref %s", name);
1021 if (ref_map) {
1022 ref_map->peer_ref = get_local_ref(refspec->dst);
1023 if (ref_map->peer_ref && refspec->force)
1024 ref_map->peer_ref->force = 1;
1028 for (rmp = &ref_map; *rmp; ) {
1029 if ((*rmp)->peer_ref) {
1030 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1031 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1032 struct ref *ignore = *rmp;
1033 error("* Ignoring funny ref '%s' locally",
1034 (*rmp)->peer_ref->name);
1035 *rmp = (*rmp)->next;
1036 free(ignore->peer_ref);
1037 free(ignore);
1038 continue;
1041 rmp = &((*rmp)->next);
1044 if (ref_map)
1045 tail_link_ref(ref_map, tail);
1047 return 0;