Allow HTTP proxy to be overridden in config
[git/git.git] / remote.c
CommitLineData
5751f490
DB
1#include "cache.h"
2#include "remote.h"
3#include "refs.h"
4
5static struct remote **remotes;
6static int allocated_remotes;
7
cf818348
DB
8static struct branch **branches;
9static int allocated_branches;
10
11static struct branch *current_branch;
12static const char *default_remote_name;
13
5751f490
DB
14#define BUF_SIZE (2048)
15static char buffer[BUF_SIZE];
16
17static void add_push_refspec(struct remote *remote, const char *ref)
18{
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;
24}
25
5d46c9d4
DB
26static void add_fetch_refspec(struct remote *remote, const char *ref)
27{
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;
33}
34
28b91f8a 35static void add_url(struct remote *remote, const char *url)
5751f490 36{
28b91f8a
SP
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;
5751f490
DB
42}
43
44static struct remote *make_remote(const char *name, int len)
45{
46 int i, empty = -1;
47
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];
57 }
58 }
59
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));
67 }
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];
74}
75
cf818348
DB
76static void add_merge(struct branch *branch, const char *name)
77{
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;
83}
84
85static struct branch *make_branch(const char *name, int len)
86{
87 int i, empty = -1;
88 char *refname;
89
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];
99 }
100 }
101
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));
109 }
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;
120
121 return branches[empty];
122}
123
5751f490
DB
124static void read_remotes_file(struct remote *remote)
125{
126 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
127
128 if (!f)
129 return;
130 while (fgets(buffer, BUF_SIZE, f)) {
131 int value_list;
132 char *s, *p;
133
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;
5d46c9d4
DB
140 } else if (!prefixcmp(buffer, "Pull:")) {
141 value_list = 2;
142 s = buffer + 5;
5751f490
DB
143 } else
144 continue;
145
146 while (isspace(*s))
147 s++;
148 if (!*s)
149 continue;
150
151 p = s + strlen(s);
152 while (isspace(p[-1]))
153 *--p = 0;
154
155 switch (value_list) {
156 case 0:
28b91f8a 157 add_url(remote, xstrdup(s));
5751f490
DB
158 break;
159 case 1:
160 add_push_refspec(remote, xstrdup(s));
161 break;
5d46c9d4
DB
162 case 2:
163 add_fetch_refspec(remote, xstrdup(s));
164 break;
5751f490
DB
165 }
166 }
167 fclose(f);
168}
169
170static void read_branches_file(struct remote *remote)
171{
172 const char *slash = strchr(remote->name, '/');
cf818348
DB
173 char *frag;
174 char *branch;
5751f490
DB
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;
179
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);
cf818348
DB
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";
208 }
28b91f8a 209 add_url(remote, p);
cf818348 210 add_fetch_refspec(remote, branch);
d71ab174 211 remote->fetch_tags = 1; /* always auto-follow */
5751f490
DB
212}
213
5751f490
DB
214static int handle_config(const char *key, const char *value)
215{
216 const char *name;
217 const char *subkey;
218 struct remote *remote;
cf818348
DB
219 struct branch *branch;
220 if (!prefixcmp(key, "branch.")) {
221 name = key + 7;
222 subkey = strrchr(name, '.');
223 branch = make_branch(name, subkey - name);
224 if (!subkey)
225 return 0;
226 if (!value)
227 return 0;
228 if (!strcmp(subkey, ".remote")) {
229 branch->remote_name = xstrdup(value);
230 if (branch == current_branch)
231 default_remote_name = branch->remote_name;
232 } else if (!strcmp(subkey, ".merge"))
233 add_merge(branch, xstrdup(value));
234 return 0;
5751f490
DB
235 }
236 if (prefixcmp(key, "remote."))
237 return 0;
238 name = key + 7;
239 subkey = strrchr(name, '.');
240 if (!subkey)
241 return error("Config with no key for remote %s", name);
242 if (*subkey == '/') {
243 warning("Config remote shorthand cannot begin with '/': %s", name);
244 return 0;
245 }
246 remote = make_remote(name, subkey - name);
247 if (!value) {
248 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
249 * [remote "frotz"]
250 * disabled
251 * is a valid way to set it to true; we get NULL in value so
252 * we need to handle it here.
253 *
254 * if (!strcmp(subkey, ".disabled")) {
255 * val = git_config_bool(key, value);
256 * return 0;
257 * } else
258 *
259 */
260 return 0; /* ignore unknown booleans */
261 }
262 if (!strcmp(subkey, ".url")) {
28b91f8a 263 add_url(remote, xstrdup(value));
5751f490
DB
264 } else if (!strcmp(subkey, ".push")) {
265 add_push_refspec(remote, xstrdup(value));
5d46c9d4
DB
266 } else if (!strcmp(subkey, ".fetch")) {
267 add_fetch_refspec(remote, xstrdup(value));
5751f490
DB
268 } else if (!strcmp(subkey, ".receivepack")) {
269 if (!remote->receivepack)
270 remote->receivepack = xstrdup(value);
271 else
272 error("more than one receivepack given, using the first");
0012ba21
DB
273 } else if (!strcmp(subkey, ".uploadpack")) {
274 if (!remote->uploadpack)
275 remote->uploadpack = xstrdup(value);
276 else
277 error("more than one uploadpack given, using the first");
d71ab174
DB
278 } else if (!strcmp(subkey, ".tagopt")) {
279 if (!strcmp(value, "--no-tags"))
280 remote->fetch_tags = -1;
5751f490
DB
281 }
282 return 0;
283}
284
285static void read_config(void)
286{
287 unsigned char sha1[20];
288 const char *head_ref;
289 int flag;
290 if (default_remote_name) // did this already
291 return;
292 default_remote_name = xstrdup("origin");
293 current_branch = NULL;
294 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
295 if (head_ref && (flag & REF_ISSYMREF) &&
296 !prefixcmp(head_ref, "refs/heads/")) {
cf818348
DB
297 current_branch =
298 make_branch(head_ref + strlen("refs/heads/"), 0);
5751f490
DB
299 }
300 git_config(handle_config);
301}
302
d71ab174 303struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
6b62816c
DB
304{
305 int i;
306 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
307 for (i = 0; i < nr_refspec; i++) {
308 const char *sp, *ep, *gp;
309 sp = refspec[i];
310 if (*sp == '+') {
311 rs[i].force = 1;
312 sp++;
313 }
314 gp = strchr(sp, '*');
315 ep = strchr(sp, ':');
316 if (gp && ep && gp > ep)
317 gp = NULL;
318 if (ep) {
319 if (ep[1]) {
320 const char *glob = strchr(ep + 1, '*');
321 if (!glob)
322 gp = NULL;
323 if (gp)
324 rs[i].dst = xstrndup(ep + 1,
325 glob - ep - 1);
326 else
327 rs[i].dst = xstrdup(ep + 1);
328 }
329 } else {
330 ep = sp + strlen(sp);
331 }
332 if (gp) {
333 rs[i].pattern = 1;
334 ep = gp;
335 }
336 rs[i].src = xstrndup(sp, ep - sp);
337 }
338 return rs;
339}
340
5751f490
DB
341struct remote *remote_get(const char *name)
342{
343 struct remote *ret;
344
345 read_config();
346 if (!name)
347 name = default_remote_name;
348 ret = make_remote(name, 0);
349 if (name[0] != '/') {
28b91f8a 350 if (!ret->url)
5751f490 351 read_remotes_file(ret);
28b91f8a 352 if (!ret->url)
5751f490
DB
353 read_branches_file(ret);
354 }
28b91f8a
SP
355 if (!ret->url)
356 add_url(ret, name);
357 if (!ret->url)
5751f490 358 return NULL;
5d46c9d4 359 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
6b62816c 360 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
5751f490
DB
361 return ret;
362}
6b62816c 363
b42f6927
JS
364int for_each_remote(each_remote_fn fn, void *priv)
365{
366 int i, result = 0;
367 read_config();
368 for (i = 0; i < allocated_remotes && !result; i++) {
369 struct remote *r = remotes[i];
370 if (!r)
371 continue;
372 if (!r->fetch)
373 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
374 r->fetch_refspec);
375 if (!r->push)
376 r->push = parse_ref_spec(r->push_refspec_nr,
377 r->push_refspec);
378 result = fn(r, priv);
379 }
380 return result;
381}
382
2467a4fa
DB
383void ref_remove_duplicates(struct ref *ref_map)
384{
385 struct ref **posn;
386 struct ref *next;
387 for (; ref_map; ref_map = ref_map->next) {
388 if (!ref_map->peer_ref)
389 continue;
390 posn = &ref_map->next;
391 while (*posn) {
392 if ((*posn)->peer_ref &&
393 !strcmp((*posn)->peer_ref->name,
394 ref_map->peer_ref->name)) {
395 if (strcmp((*posn)->name, ref_map->name))
396 die("%s tracks both %s and %s",
397 ref_map->peer_ref->name,
398 (*posn)->name, ref_map->name);
399 next = (*posn)->next;
400 free((*posn)->peer_ref);
401 free(*posn);
402 *posn = next;
403 } else {
404 posn = &(*posn)->next;
405 }
406 }
407 }
408}
409
28b91f8a 410int remote_has_url(struct remote *remote, const char *url)
5d46c9d4
DB
411{
412 int i;
28b91f8a
SP
413 for (i = 0; i < remote->url_nr; i++) {
414 if (!strcmp(remote->url[i], url))
5d46c9d4
DB
415 return 1;
416 }
417 return 0;
418}
419
cf818348
DB
420/*
421 * Returns true if, under the matching rules for fetching, name is the
422 * same as the given full name.
423 */
424static int ref_matches_abbrev(const char *name, const char *full)
425{
426 if (!prefixcmp(name, "refs/") || !strcmp(name, "HEAD"))
427 return !strcmp(name, full);
428 if (prefixcmp(full, "refs/"))
429 return 0;
430 if (!prefixcmp(name, "heads/") ||
431 !prefixcmp(name, "tags/") ||
432 !prefixcmp(name, "remotes/"))
433 return !strcmp(name, full + 5);
434 if (prefixcmp(full + 5, "heads/"))
435 return 0;
436 return !strcmp(full + 11, name);
437}
438
5d46c9d4
DB
439int remote_find_tracking(struct remote *remote, struct refspec *refspec)
440{
b42f6927
JS
441 int find_src = refspec->src == NULL;
442 char *needle, **result;
5d46c9d4 443 int i;
b42f6927
JS
444
445 if (find_src) {
009c5bcd 446 if (!refspec->dst)
b42f6927
JS
447 return error("find_tracking: need either src or dst");
448 needle = refspec->dst;
449 result = &refspec->src;
450 } else {
451 needle = refspec->src;
452 result = &refspec->dst;
453 }
454
5d46c9d4
DB
455 for (i = 0; i < remote->fetch_refspec_nr; i++) {
456 struct refspec *fetch = &remote->fetch[i];
b42f6927
JS
457 const char *key = find_src ? fetch->dst : fetch->src;
458 const char *value = find_src ? fetch->src : fetch->dst;
5d46c9d4
DB
459 if (!fetch->dst)
460 continue;
461 if (fetch->pattern) {
b42f6927
JS
462 if (!prefixcmp(needle, key)) {
463 *result = xmalloc(strlen(value) +
464 strlen(needle) -
465 strlen(key) + 1);
466 strcpy(*result, value);
467 strcpy(*result + strlen(value),
468 needle + strlen(key));
5d46c9d4
DB
469 refspec->force = fetch->force;
470 return 0;
471 }
b42f6927
JS
472 } else if (!strcmp(needle, key)) {
473 *result = xstrdup(value);
474 refspec->force = fetch->force;
475 return 0;
5d46c9d4
DB
476 }
477 }
5d46c9d4
DB
478 return -1;
479}
480
dfd255dd
DB
481struct ref *alloc_ref(unsigned namelen)
482{
483 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
484 memset(ret, 0, sizeof(struct ref) + namelen);
485 return ret;
486}
487
4577370e 488static struct ref *copy_ref(const struct ref *ref)
d71ab174
DB
489{
490 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
491 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
492 ret->next = NULL;
493 return ret;
494}
495
4577370e
DB
496struct ref *copy_ref_list(const struct ref *ref)
497{
498 struct ref *ret = NULL;
499 struct ref **tail = &ret;
500 while (ref) {
501 *tail = copy_ref(ref);
502 ref = ref->next;
503 tail = &((*tail)->next);
504 }
505 return ret;
506}
507
dfd255dd
DB
508void free_refs(struct ref *ref)
509{
510 struct ref *next;
511 while (ref) {
512 next = ref->next;
513 if (ref->peer_ref)
514 free(ref->peer_ref);
515 free(ref);
516 ref = next;
517 }
518}
519
6b62816c
DB
520static int count_refspec_match(const char *pattern,
521 struct ref *refs,
522 struct ref **matched_ref)
523{
524 int patlen = strlen(pattern);
525 struct ref *matched_weak = NULL;
526 struct ref *matched = NULL;
527 int weak_match = 0;
528 int match = 0;
529
530 for (weak_match = match = 0; refs; refs = refs->next) {
531 char *name = refs->name;
532 int namelen = strlen(name);
6b62816c
DB
533
534 if (namelen < patlen ||
535 memcmp(name + namelen - patlen, pattern, patlen))
536 continue;
537 if (namelen != patlen && name[namelen - patlen - 1] != '/')
538 continue;
539
540 /* A match is "weak" if it is with refs outside
541 * heads or tags, and did not specify the pattern
542 * in full (e.g. "refs/remotes/origin/master") or at
543 * least from the toplevel (e.g. "remotes/origin/master");
544 * otherwise "git push $URL master" would result in
545 * ambiguity between remotes/origin/master and heads/master
546 * at the remote site.
547 */
548 if (namelen != patlen &&
549 patlen != namelen - 5 &&
550 prefixcmp(name, "refs/heads/") &&
551 prefixcmp(name, "refs/tags/")) {
552 /* We want to catch the case where only weak
553 * matches are found and there are multiple
554 * matches, and where more than one strong
555 * matches are found, as ambiguous. One
556 * strong match with zero or more weak matches
557 * are acceptable as a unique match.
558 */
559 matched_weak = refs;
560 weak_match++;
561 }
562 else {
563 matched = refs;
564 match++;
565 }
566 }
567 if (!matched) {
568 *matched_ref = matched_weak;
569 return weak_match;
570 }
571 else {
572 *matched_ref = matched;
573 return match;
574 }
575}
576
1d735267 577static void tail_link_ref(struct ref *ref, struct ref ***tail)
6b62816c
DB
578{
579 **tail = ref;
1d735267
DB
580 while (ref->next)
581 ref = ref->next;
6b62816c 582 *tail = &ref->next;
6b62816c
DB
583}
584
585static struct ref *try_explicit_object_name(const char *name)
586{
587 unsigned char sha1[20];
588 struct ref *ref;
589 int len;
590
591 if (!*name) {
dfd255dd 592 ref = alloc_ref(20);
6b62816c
DB
593 strcpy(ref->name, "(delete)");
594 hashclr(ref->new_sha1);
595 return ref;
596 }
597 if (get_sha1(name, sha1))
598 return NULL;
599 len = strlen(name) + 1;
dfd255dd 600 ref = alloc_ref(len);
6b62816c
DB
601 memcpy(ref->name, name, len);
602 hashcpy(ref->new_sha1, sha1);
603 return ref;
604}
605
1d735267 606static struct ref *make_linked_ref(const char *name, struct ref ***tail)
6b62816c 607{
1d735267 608 struct ref *ret;
163f0ee5 609 size_t len;
6b62816c 610
163f0ee5 611 len = strlen(name) + 1;
1d735267
DB
612 ret = alloc_ref(len);
613 memcpy(ret->name, name, len);
614 tail_link_ref(ret, tail);
615 return ret;
163f0ee5 616}
8558fd9e 617
54a8ad92
JH
618static int match_explicit(struct ref *src, struct ref *dst,
619 struct ref ***dst_tail,
620 struct refspec *rs,
621 int errs)
6b62816c 622{
54a8ad92 623 struct ref *matched_src, *matched_dst;
8558fd9e 624
54a8ad92 625 const char *dst_value = rs->dst;
6b62816c 626
54a8ad92
JH
627 if (rs->pattern)
628 return errs;
8558fd9e 629
54a8ad92
JH
630 matched_src = matched_dst = NULL;
631 switch (count_refspec_match(rs->src, src, &matched_src)) {
632 case 1:
633 break;
634 case 0:
635 /* The source could be in the get_sha1() format
636 * not a reference name. :refs/other is a
637 * way to delete 'other' ref at the remote end.
638 */
639 matched_src = try_explicit_object_name(rs->src);
7dfee372
SP
640 if (!matched_src)
641 error("src refspec %s does not match any.", rs->src);
54a8ad92
JH
642 break;
643 default:
3c8b7df1 644 matched_src = NULL;
7dfee372 645 error("src refspec %s matches more than one.", rs->src);
54a8ad92
JH
646 break;
647 }
3c8b7df1
JH
648
649 if (!matched_src)
650 errs = 1;
651
4491e62a
SP
652 if (!dst_value) {
653 if (!matched_src)
654 return errs;
1ed10b88 655 dst_value = matched_src->name;
4491e62a 656 }
3c8b7df1 657
54a8ad92
JH
658 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
659 case 1:
660 break;
661 case 0:
163f0ee5 662 if (!memcmp(dst_value, "refs/", 5))
1d735267 663 matched_dst = make_linked_ref(dst_value, dst_tail);
3c8b7df1 664 else
54a8ad92
JH
665 error("dst refspec %s does not match any "
666 "existing ref on the remote and does "
667 "not start with refs/.", dst_value);
54a8ad92
JH
668 break;
669 default:
3c8b7df1 670 matched_dst = NULL;
54a8ad92
JH
671 error("dst refspec %s matches more than one.",
672 dst_value);
673 break;
674 }
009c5bcd 675 if (errs || !matched_dst)
3c8b7df1 676 return 1;
54a8ad92
JH
677 if (matched_dst->peer_ref) {
678 errs = 1;
679 error("dst ref %s receives from more than one src.",
680 matched_dst->name);
6b62816c 681 }
54a8ad92
JH
682 else {
683 matched_dst->peer_ref = matched_src;
684 matched_dst->force = rs->force;
6b62816c 685 }
54a8ad92
JH
686 return errs;
687}
688
689static int match_explicit_refs(struct ref *src, struct ref *dst,
690 struct ref ***dst_tail, struct refspec *rs,
691 int rs_nr)
692{
693 int i, errs;
694 for (i = errs = 0; i < rs_nr; i++)
695 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
6b62816c
DB
696 return -errs;
697}
698
6e66bf3c
AR
699static const struct refspec *check_pattern_match(const struct refspec *rs,
700 int rs_nr,
701 const struct ref *src)
8558fd9e
DB
702{
703 int i;
8558fd9e
DB
704 for (i = 0; i < rs_nr; i++) {
705 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
6e66bf3c 706 return rs + i;
8558fd9e 707 }
6e66bf3c 708 return NULL;
8558fd9e
DB
709}
710
54a8ad92
JH
711/*
712 * Note. This is used only by "push"; refspec matching rules for
713 * push and fetch are subtly different, so do not try to reuse it
714 * without thinking.
715 */
6b62816c 716int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
28b9d6e5 717 int nr_refspec, const char **refspec, int flags)
6b62816c
DB
718{
719 struct refspec *rs =
720 parse_ref_spec(nr_refspec, (const char **) refspec);
28b9d6e5
AW
721 int send_all = flags & MATCH_REFS_ALL;
722 int send_mirror = flags & MATCH_REFS_MIRROR;
6b62816c 723
8558fd9e
DB
724 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
725 return -1;
6b62816c
DB
726
727 /* pick the remainder */
728 for ( ; src; src = src->next) {
729 struct ref *dst_peer;
6e66bf3c
AR
730 const struct refspec *pat = NULL;
731 char *dst_name;
6b62816c
DB
732 if (src->peer_ref)
733 continue;
6e66bf3c
AR
734 if (nr_refspec) {
735 pat = check_pattern_match(rs, nr_refspec, src);
736 if (!pat)
737 continue;
738 }
28b9d6e5 739 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
098e711e
JH
740 /*
741 * "matching refs"; traditionally we pushed everything
742 * including refs outside refs/heads/ hierarchy, but
743 * that does not make much sense these days.
744 */
745 continue;
8558fd9e 746
6e66bf3c 747 if (pat) {
efd8f793
DB
748 const char *dst_side = pat->dst ? pat->dst : pat->src;
749 dst_name = xmalloc(strlen(dst_side) +
6e66bf3c
AR
750 strlen(src->name) -
751 strlen(pat->src) + 2);
efd8f793 752 strcpy(dst_name, dst_side);
6e66bf3c
AR
753 strcat(dst_name, src->name + strlen(pat->src));
754 } else
aa32eedc 755 dst_name = xstrdup(src->name);
6e66bf3c 756 dst_peer = find_ref_by_name(dst, dst_name);
8558fd9e
DB
757 if (dst_peer && dst_peer->peer_ref)
758 /* We're already sending something to this ref. */
6e66bf3c 759 goto free_name;
28b9d6e5
AW
760
761 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
762 /*
763 * Remote doesn't have it, and we have no
8558fd9e 764 * explicit pattern, and we don't have
28b9d6e5
AW
765 * --all nor --mirror.
766 */
6e66bf3c 767 goto free_name;
6b62816c
DB
768 if (!dst_peer) {
769 /* Create a new one and link it */
1d735267 770 dst_peer = make_linked_ref(dst_name, dst_tail);
6b62816c 771 hashcpy(dst_peer->new_sha1, src->new_sha1);
6b62816c
DB
772 }
773 dst_peer->peer_ref = src;
5eb73581
JK
774 if (pat)
775 dst_peer->force = pat->force;
6e66bf3c
AR
776 free_name:
777 free(dst_name);
6b62816c
DB
778 }
779 return 0;
780}
cf818348
DB
781
782struct branch *branch_get(const char *name)
783{
784 struct branch *ret;
785
786 read_config();
787 if (!name || !*name || !strcmp(name, "HEAD"))
788 ret = current_branch;
789 else
790 ret = make_branch(name, 0);
791 if (ret && ret->remote_name) {
792 ret->remote = remote_get(ret->remote_name);
793 if (ret->merge_nr) {
794 int i;
795 ret->merge = xcalloc(sizeof(*ret->merge),
796 ret->merge_nr);
797 for (i = 0; i < ret->merge_nr; i++) {
798 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
799 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
800 remote_find_tracking(ret->remote,
801 ret->merge[i]);
802 }
803 }
804 }
805 return ret;
806}
807
808int branch_has_merge_config(struct branch *branch)
809{
810 return branch && !!branch->merge;
811}
812
85682c19
SP
813int branch_merge_matches(struct branch *branch,
814 int i,
815 const char *refname)
cf818348 816{
85682c19 817 if (!branch || i < 0 || i >= branch->merge_nr)
cf818348 818 return 0;
85682c19 819 return ref_matches_abbrev(branch->merge[i]->src, refname);
cf818348 820}
d71ab174 821
4577370e 822static struct ref *get_expanded_map(const struct ref *remote_refs,
d71ab174
DB
823 const struct refspec *refspec)
824{
4577370e 825 const struct ref *ref;
d71ab174
DB
826 struct ref *ret = NULL;
827 struct ref **tail = &ret;
828
829 int remote_prefix_len = strlen(refspec->src);
830 int local_prefix_len = strlen(refspec->dst);
831
832 for (ref = remote_refs; ref; ref = ref->next) {
833 if (strchr(ref->name, '^'))
834 continue; /* a dereference item */
835 if (!prefixcmp(ref->name, refspec->src)) {
4577370e 836 const char *match;
d71ab174
DB
837 struct ref *cpy = copy_ref(ref);
838 match = ref->name + remote_prefix_len;
839
840 cpy->peer_ref = alloc_ref(local_prefix_len +
841 strlen(match) + 1);
842 sprintf(cpy->peer_ref->name, "%s%s",
843 refspec->dst, match);
844 if (refspec->force)
845 cpy->peer_ref->force = 1;
846 *tail = cpy;
847 tail = &cpy->next;
848 }
849 }
850
851 return ret;
852}
853
4577370e 854static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
d71ab174 855{
4577370e 856 const struct ref *ref;
d71ab174
DB
857 for (ref = refs; ref; ref = ref->next) {
858 if (ref_matches_abbrev(name, ref->name))
859 return ref;
860 }
861 return NULL;
862}
863
4577370e 864struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
d71ab174 865{
4577370e 866 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
d71ab174
DB
867
868 if (!ref)
9ad7c5ae 869 return NULL;
d71ab174
DB
870
871 return copy_ref(ref);
872}
873
874static struct ref *get_local_ref(const char *name)
875{
876 struct ref *ret;
877 if (!name)
878 return NULL;
879
880 if (!prefixcmp(name, "refs/")) {
881 ret = alloc_ref(strlen(name) + 1);
882 strcpy(ret->name, name);
883 return ret;
884 }
885
886 if (!prefixcmp(name, "heads/") ||
887 !prefixcmp(name, "tags/") ||
888 !prefixcmp(name, "remotes/")) {
889 ret = alloc_ref(strlen(name) + 6);
890 sprintf(ret->name, "refs/%s", name);
891 return ret;
892 }
893
894 ret = alloc_ref(strlen(name) + 12);
895 sprintf(ret->name, "refs/heads/%s", name);
896 return ret;
897}
898
4577370e 899int get_fetch_map(const struct ref *remote_refs,
d71ab174 900 const struct refspec *refspec,
9ad7c5ae
JH
901 struct ref ***tail,
902 int missing_ok)
d71ab174
DB
903{
904 struct ref *ref_map, *rm;
905
906 if (refspec->pattern) {
907 ref_map = get_expanded_map(remote_refs, refspec);
908 } else {
9ad7c5ae
JH
909 const char *name = refspec->src[0] ? refspec->src : "HEAD";
910
911 ref_map = get_remote_ref(remote_refs, name);
912 if (!missing_ok && !ref_map)
913 die("Couldn't find remote ref %s", name);
914 if (ref_map) {
915 ref_map->peer_ref = get_local_ref(refspec->dst);
916 if (ref_map->peer_ref && refspec->force)
917 ref_map->peer_ref->force = 1;
918 }
d71ab174
DB
919 }
920
921 for (rm = ref_map; rm; rm = rm->next) {
922 if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
923 die("* refusing to create funny ref '%s' locally",
924 rm->peer_ref->name);
925 }
926
8f70a765
AR
927 if (ref_map)
928 tail_link_ref(ref_map, tail);
d71ab174
DB
929
930 return 0;
931}