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