]> git.ipfire.org Git - thirdparty/git.git/blame - remote.c
Add support for url aliases in config files
[thirdparty/git.git] / remote.c
CommitLineData
5751f490
DB
1#include "cache.h"
2#include "remote.h"
3#include "refs.h"
4
55029ae4
DB
5struct rewrite {
6 const char *base;
7 const char **instead_of;
8 int instead_of_nr;
9 int instead_of_alloc;
10};
11
5751f490 12static struct remote **remotes;
2d31347b
DB
13static int remotes_alloc;
14static int remotes_nr;
5751f490 15
cf818348 16static struct branch **branches;
2d31347b
DB
17static int branches_alloc;
18static int branches_nr;
cf818348
DB
19
20static struct branch *current_branch;
21static const char *default_remote_name;
22
55029ae4
DB
23static struct rewrite **rewrite;
24static int rewrite_alloc;
25static int rewrite_nr;
26
5751f490
DB
27#define BUF_SIZE (2048)
28static char buffer[BUF_SIZE];
29
55029ae4
DB
30static const char *alias_url(const char *url)
31{
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;
44 }
45 }
46 }
47 return url;
48}
49
5751f490
DB
50static void add_push_refspec(struct remote *remote, const char *ref)
51{
2d31347b
DB
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;
5751f490
DB
56}
57
5d46c9d4
DB
58static void add_fetch_refspec(struct remote *remote, const char *ref)
59{
2d31347b
DB
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;
5d46c9d4
DB
64}
65
28b91f8a 66static void add_url(struct remote *remote, const char *url)
5751f490 67{
2d31347b
DB
68 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
69 remote->url[remote->url_nr++] = url;
5751f490
DB
70}
71
55029ae4
DB
72static void add_url_alias(struct remote *remote, const char *url)
73{
74 add_url(remote, alias_url(url));
75}
76
5751f490
DB
77static struct remote *make_remote(const char *name, int len)
78{
2d31347b
DB
79 struct remote *ret;
80 int i;
5751f490 81
2d31347b
DB
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];
5751f490
DB
87 }
88
2d31347b
DB
89 ret = xcalloc(1, sizeof(struct remote));
90 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
91 remotes[remotes_nr++] = ret;
5751f490 92 if (len)
2d31347b 93 ret->name = xstrndup(name, len);
5751f490 94 else
2d31347b
DB
95 ret->name = xstrdup(name);
96 return ret;
5751f490
DB
97}
98
cf818348
DB
99static void add_merge(struct branch *branch, const char *name)
100{
2d31347b
DB
101 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
102 branch->merge_alloc);
103 branch->merge_name[branch->merge_nr++] = name;
cf818348
DB
104}
105
106static struct branch *make_branch(const char *name, int len)
107{
2d31347b
DB
108 struct branch *ret;
109 int i;
cf818348
DB
110 char *refname;
111
2d31347b
DB
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];
cf818348
DB
117 }
118
2d31347b
DB
119 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
120 ret = xcalloc(1, sizeof(struct branch));
121 branches[branches_nr++] = ret;
cf818348 122 if (len)
2d31347b 123 ret->name = xstrndup(name, len);
cf818348 124 else
2d31347b 125 ret->name = xstrdup(name);
cf818348
DB
126 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
127 strcpy(refname, "refs/heads/");
2d31347b
DB
128 strcpy(refname + strlen("refs/heads/"), ret->name);
129 ret->refname = refname;
cf818348 130
2d31347b 131 return ret;
cf818348
DB
132}
133
55029ae4
DB
134static struct rewrite *make_rewrite(const char *base, int len)
135{
136 struct rewrite *ret;
137 int i;
138
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];
144 }
145
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);
153
154 return ret;
155}
156
157static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
158{
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;
161}
162
5751f490
DB
163static void read_remotes_file(struct remote *remote)
164{
165 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
166
167 if (!f)
168 return;
169 while (fgets(buffer, BUF_SIZE, f)) {
170 int value_list;
171 char *s, *p;
172
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;
5d46c9d4
DB
179 } else if (!prefixcmp(buffer, "Pull:")) {
180 value_list = 2;
181 s = buffer + 5;
5751f490
DB
182 } else
183 continue;
184
185 while (isspace(*s))
186 s++;
187 if (!*s)
188 continue;
189
190 p = s + strlen(s);
191 while (isspace(p[-1]))
192 *--p = 0;
193
194 switch (value_list) {
195 case 0:
55029ae4 196 add_url_alias(remote, xstrdup(s));
5751f490
DB
197 break;
198 case 1:
199 add_push_refspec(remote, xstrdup(s));
200 break;
5d46c9d4
DB
201 case 2:
202 add_fetch_refspec(remote, xstrdup(s));
203 break;
5751f490
DB
204 }
205 }
206 fclose(f);
207}
208
209static void read_branches_file(struct remote *remote)
210{
211 const char *slash = strchr(remote->name, '/');
cf818348
DB
212 char *frag;
213 char *branch;
5751f490
DB
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;
218
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);
cf818348
DB
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";
247 }
55029ae4 248 add_url_alias(remote, p);
cf818348 249 add_fetch_refspec(remote, branch);
d71ab174 250 remote->fetch_tags = 1; /* always auto-follow */
5751f490
DB
251}
252
5751f490
DB
253static int handle_config(const char *key, const char *value)
254{
255 const char *name;
256 const char *subkey;
257 struct remote *remote;
cf818348
DB
258 struct branch *branch;
259 if (!prefixcmp(key, "branch.")) {
260 name = key + 7;
261 subkey = strrchr(name, '.');
cf818348
DB
262 if (!subkey)
263 return 0;
896c0535 264 branch = make_branch(name, subkey - name);
cf818348 265 if (!strcmp(subkey, ".remote")) {
d2370cc2
JH
266 if (!value)
267 return config_error_nonbool(key);
cf818348
DB
268 branch->remote_name = xstrdup(value);
269 if (branch == current_branch)
270 default_remote_name = branch->remote_name;
d2370cc2
JH
271 } else if (!strcmp(subkey, ".merge")) {
272 if (!value)
273 return config_error_nonbool(key);
cf818348 274 add_merge(branch, xstrdup(value));
d2370cc2 275 }
cf818348 276 return 0;
5751f490 277 }
55029ae4
DB
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));
289 }
290 }
5751f490
DB
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;
300 }
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.
308 *
309 * if (!strcmp(subkey, ".disabled")) {
310 * val = git_config_bool(key, value);
311 * return 0;
312 * } else
313 *
314 */
315 return 0; /* ignore unknown booleans */
316 }
317 if (!strcmp(subkey, ".url")) {
28b91f8a 318 add_url(remote, xstrdup(value));
5751f490
DB
319 } else if (!strcmp(subkey, ".push")) {
320 add_push_refspec(remote, xstrdup(value));
5d46c9d4
DB
321 } else if (!strcmp(subkey, ".fetch")) {
322 add_fetch_refspec(remote, xstrdup(value));
5751f490
DB
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");
0012ba21
DB
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");
d71ab174
DB
333 } else if (!strcmp(subkey, ".tagopt")) {
334 if (!strcmp(value, "--no-tags"))
335 remote->fetch_tags = -1;
14c98218
SV
336 } else if (!strcmp(subkey, ".proxy")) {
337 remote->http_proxy = xstrdup(value);
5751f490
DB
338 }
339 return 0;
340}
341
55029ae4
DB
342static void alias_all_urls(void)
343{
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]);
350 }
351 }
352}
353
5751f490
DB
354static void read_config(void)
355{
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/")) {
cf818348
DB
366 current_branch =
367 make_branch(head_ref + strlen("refs/heads/"), 0);
5751f490
DB
368 }
369 git_config(handle_config);
55029ae4 370 alias_all_urls();
5751f490
DB
371}
372
d71ab174 373struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
6b62816c
DB
374{
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++;
383 }
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);
398 }
399 } else {
400 ep = sp + strlen(sp);
401 }
402 if (gp) {
403 rs[i].pattern = 1;
404 ep = gp;
405 }
406 rs[i].src = xstrndup(sp, ep - sp);
407 }
408 return rs;
409}
410
df93e33c
DB
411static int valid_remote_nick(const char *name)
412{
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 */
419}
420
5751f490
DB
421struct remote *remote_get(const char *name)
422{
423 struct remote *ret;
424
425 read_config();
426 if (!name)
427 name = default_remote_name;
428 ret = make_remote(name, 0);
df93e33c 429 if (valid_remote_nick(name)) {
28b91f8a 430 if (!ret->url)
5751f490 431 read_remotes_file(ret);
28b91f8a 432 if (!ret->url)
5751f490
DB
433 read_branches_file(ret);
434 }
28b91f8a 435 if (!ret->url)
55029ae4 436 add_url_alias(ret, name);
28b91f8a 437 if (!ret->url)
5751f490 438 return NULL;
5d46c9d4 439 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
6b62816c 440 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
5751f490
DB
441 return ret;
442}
6b62816c 443
b42f6927
JS
444int for_each_remote(each_remote_fn fn, void *priv)
445{
446 int i, result = 0;
447 read_config();
2d31347b 448 for (i = 0; i < remotes_nr && !result; i++) {
b42f6927
JS
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);
459 }
460 return result;
461}
462
2467a4fa
DB
463void ref_remove_duplicates(struct ref *ref_map)
464{
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;
485 }
486 }
487 }
488}
489
28b91f8a 490int remote_has_url(struct remote *remote, const char *url)
5d46c9d4
DB
491{
492 int i;
28b91f8a
SP
493 for (i = 0; i < remote->url_nr; i++) {
494 if (!strcmp(remote->url[i], url))
5d46c9d4
DB
495 return 1;
496 }
497 return 0;
498}
499
500int remote_find_tracking(struct remote *remote, struct refspec *refspec)
501{
b42f6927
JS
502 int find_src = refspec->src == NULL;
503 char *needle, **result;
5d46c9d4 504 int i;
b42f6927
JS
505
506 if (find_src) {
009c5bcd 507 if (!refspec->dst)
b42f6927
JS
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;
514 }
515
5d46c9d4
DB
516 for (i = 0; i < remote->fetch_refspec_nr; i++) {
517 struct refspec *fetch = &remote->fetch[i];
b42f6927
JS
518 const char *key = find_src ? fetch->dst : fetch->src;
519 const char *value = find_src ? fetch->src : fetch->dst;
5d46c9d4
DB
520 if (!fetch->dst)
521 continue;
522 if (fetch->pattern) {
b42f6927
JS
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));
5d46c9d4
DB
530 refspec->force = fetch->force;
531 return 0;
532 }
b42f6927
JS
533 } else if (!strcmp(needle, key)) {
534 *result = xstrdup(value);
535 refspec->force = fetch->force;
536 return 0;
5d46c9d4
DB
537 }
538 }
5d46c9d4
DB
539 return -1;
540}
541
dfd255dd
DB
542struct ref *alloc_ref(unsigned namelen)
543{
544 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
545 memset(ret, 0, sizeof(struct ref) + namelen);
546 return ret;
547}
548
4577370e 549static struct ref *copy_ref(const struct ref *ref)
d71ab174
DB
550{
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;
555}
556
4577370e
DB
557struct ref *copy_ref_list(const struct ref *ref)
558{
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);
565 }
566 return ret;
567}
568
dfd255dd
DB
569void free_refs(struct ref *ref)
570{
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;
578 }
579}
580
6b62816c
DB
581static int count_refspec_match(const char *pattern,
582 struct ref *refs,
583 struct ref **matched_ref)
584{
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;
590
591 for (weak_match = match = 0; refs; refs = refs->next) {
592 char *name = refs->name;
593 int namelen = strlen(name);
6b62816c 594
ae36bdcf 595 if (!refname_match(pattern, name, ref_rev_parse_rules))
6b62816c
DB
596 continue;
597
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.
605 */
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.
616 */
617 matched_weak = refs;
618 weak_match++;
619 }
620 else {
621 matched = refs;
622 match++;
623 }
624 }
625 if (!matched) {
626 *matched_ref = matched_weak;
627 return weak_match;
628 }
629 else {
630 *matched_ref = matched;
631 return match;
632 }
633}
634
1d735267 635static void tail_link_ref(struct ref *ref, struct ref ***tail)
6b62816c
DB
636{
637 **tail = ref;
1d735267
DB
638 while (ref->next)
639 ref = ref->next;
6b62816c 640 *tail = &ref->next;
6b62816c
DB
641}
642
643static struct ref *try_explicit_object_name(const char *name)
644{
645 unsigned char sha1[20];
646 struct ref *ref;
647 int len;
648
649 if (!*name) {
dfd255dd 650 ref = alloc_ref(20);
6b62816c
DB
651 strcpy(ref->name, "(delete)");
652 hashclr(ref->new_sha1);
653 return ref;
654 }
655 if (get_sha1(name, sha1))
656 return NULL;
657 len = strlen(name) + 1;
dfd255dd 658 ref = alloc_ref(len);
6b62816c
DB
659 memcpy(ref->name, name, len);
660 hashcpy(ref->new_sha1, sha1);
661 return ref;
662}
663
1d735267 664static struct ref *make_linked_ref(const char *name, struct ref ***tail)
6b62816c 665{
1d735267 666 struct ref *ret;
163f0ee5 667 size_t len;
6b62816c 668
163f0ee5 669 len = strlen(name) + 1;
1d735267
DB
670 ret = alloc_ref(len);
671 memcpy(ret->name, name, len);
672 tail_link_ref(ret, tail);
673 return ret;
163f0ee5 674}
8558fd9e 675
54a8ad92
JH
676static int match_explicit(struct ref *src, struct ref *dst,
677 struct ref ***dst_tail,
678 struct refspec *rs,
679 int errs)
6b62816c 680{
54a8ad92 681 struct ref *matched_src, *matched_dst;
8558fd9e 682
54a8ad92 683 const char *dst_value = rs->dst;
6b62816c 684
54a8ad92
JH
685 if (rs->pattern)
686 return errs;
8558fd9e 687
54a8ad92
JH
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.
696 */
697 matched_src = try_explicit_object_name(rs->src);
7dfee372
SP
698 if (!matched_src)
699 error("src refspec %s does not match any.", rs->src);
54a8ad92
JH
700 break;
701 default:
3c8b7df1 702 matched_src = NULL;
7dfee372 703 error("src refspec %s matches more than one.", rs->src);
54a8ad92
JH
704 break;
705 }
3c8b7df1
JH
706
707 if (!matched_src)
708 errs = 1;
709
4491e62a
SP
710 if (!dst_value) {
711 if (!matched_src)
712 return errs;
1ed10b88 713 dst_value = matched_src->name;
4491e62a 714 }
3c8b7df1 715
54a8ad92
JH
716 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
717 case 1:
718 break;
719 case 0:
163f0ee5 720 if (!memcmp(dst_value, "refs/", 5))
1d735267 721 matched_dst = make_linked_ref(dst_value, dst_tail);
3c8b7df1 722 else
54a8ad92
JH
723 error("dst refspec %s does not match any "
724 "existing ref on the remote and does "
725 "not start with refs/.", dst_value);
54a8ad92
JH
726 break;
727 default:
3c8b7df1 728 matched_dst = NULL;
54a8ad92
JH
729 error("dst refspec %s matches more than one.",
730 dst_value);
731 break;
732 }
009c5bcd 733 if (errs || !matched_dst)
3c8b7df1 734 return 1;
54a8ad92
JH
735 if (matched_dst->peer_ref) {
736 errs = 1;
737 error("dst ref %s receives from more than one src.",
738 matched_dst->name);
6b62816c 739 }
54a8ad92
JH
740 else {
741 matched_dst->peer_ref = matched_src;
742 matched_dst->force = rs->force;
6b62816c 743 }
54a8ad92
JH
744 return errs;
745}
746
747static int match_explicit_refs(struct ref *src, struct ref *dst,
748 struct ref ***dst_tail, struct refspec *rs,
749 int rs_nr)
750{
751 int i, errs;
752 for (i = errs = 0; i < rs_nr; i++)
753 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
6b62816c
DB
754 return -errs;
755}
756
6e66bf3c
AR
757static const struct refspec *check_pattern_match(const struct refspec *rs,
758 int rs_nr,
759 const struct ref *src)
8558fd9e
DB
760{
761 int i;
8558fd9e
DB
762 for (i = 0; i < rs_nr; i++) {
763 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
6e66bf3c 764 return rs + i;
8558fd9e 765 }
6e66bf3c 766 return NULL;
8558fd9e
DB
767}
768
54a8ad92
JH
769/*
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.
773 */
6b62816c 774int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
28b9d6e5 775 int nr_refspec, const char **refspec, int flags)
6b62816c
DB
776{
777 struct refspec *rs =
778 parse_ref_spec(nr_refspec, (const char **) refspec);
28b9d6e5
AW
779 int send_all = flags & MATCH_REFS_ALL;
780 int send_mirror = flags & MATCH_REFS_MIRROR;
6b62816c 781
8558fd9e
DB
782 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
783 return -1;
6b62816c
DB
784
785 /* pick the remainder */
786 for ( ; src; src = src->next) {
787 struct ref *dst_peer;
6e66bf3c
AR
788 const struct refspec *pat = NULL;
789 char *dst_name;
6b62816c
DB
790 if (src->peer_ref)
791 continue;
6e66bf3c
AR
792 if (nr_refspec) {
793 pat = check_pattern_match(rs, nr_refspec, src);
794 if (!pat)
795 continue;
796 }
28b9d6e5 797 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
098e711e
JH
798 /*
799 * "matching refs"; traditionally we pushed everything
800 * including refs outside refs/heads/ hierarchy, but
801 * that does not make much sense these days.
802 */
803 continue;
8558fd9e 804
6e66bf3c 805 if (pat) {
efd8f793
DB
806 const char *dst_side = pat->dst ? pat->dst : pat->src;
807 dst_name = xmalloc(strlen(dst_side) +
6e66bf3c
AR
808 strlen(src->name) -
809 strlen(pat->src) + 2);
efd8f793 810 strcpy(dst_name, dst_side);
6e66bf3c
AR
811 strcat(dst_name, src->name + strlen(pat->src));
812 } else
aa32eedc 813 dst_name = xstrdup(src->name);
6e66bf3c 814 dst_peer = find_ref_by_name(dst, dst_name);
8558fd9e
DB
815 if (dst_peer && dst_peer->peer_ref)
816 /* We're already sending something to this ref. */
6e66bf3c 817 goto free_name;
28b9d6e5
AW
818
819 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
820 /*
821 * Remote doesn't have it, and we have no
8558fd9e 822 * explicit pattern, and we don't have
28b9d6e5
AW
823 * --all nor --mirror.
824 */
6e66bf3c 825 goto free_name;
6b62816c
DB
826 if (!dst_peer) {
827 /* Create a new one and link it */
1d735267 828 dst_peer = make_linked_ref(dst_name, dst_tail);
6b62816c 829 hashcpy(dst_peer->new_sha1, src->new_sha1);
6b62816c
DB
830 }
831 dst_peer->peer_ref = src;
5eb73581
JK
832 if (pat)
833 dst_peer->force = pat->force;
6e66bf3c
AR
834 free_name:
835 free(dst_name);
6b62816c
DB
836 }
837 return 0;
838}
cf818348
DB
839
840struct branch *branch_get(const char *name)
841{
842 struct branch *ret;
843
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]);
860 }
861 }
862 }
863 return ret;
864}
865
866int branch_has_merge_config(struct branch *branch)
867{
868 return branch && !!branch->merge;
869}
870
85682c19
SP
871int branch_merge_matches(struct branch *branch,
872 int i,
873 const char *refname)
cf818348 874{
85682c19 875 if (!branch || i < 0 || i >= branch->merge_nr)
cf818348 876 return 0;
605b4978 877 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
cf818348 878}
d71ab174 879
4577370e 880static struct ref *get_expanded_map(const struct ref *remote_refs,
d71ab174
DB
881 const struct refspec *refspec)
882{
4577370e 883 const struct ref *ref;
d71ab174
DB
884 struct ref *ret = NULL;
885 struct ref **tail = &ret;
886
887 int remote_prefix_len = strlen(refspec->src);
888 int local_prefix_len = strlen(refspec->dst);
889
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)) {
4577370e 894 const char *match;
d71ab174
DB
895 struct ref *cpy = copy_ref(ref);
896 match = ref->name + remote_prefix_len;
897
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;
906 }
907 }
908
909 return ret;
910}
911
4577370e 912static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
d71ab174 913{
4577370e 914 const struct ref *ref;
d71ab174 915 for (ref = refs; ref; ref = ref->next) {
605b4978 916 if (refname_match(name, ref->name, ref_fetch_rules))
d71ab174
DB
917 return ref;
918 }
919 return NULL;
920}
921
4577370e 922struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
d71ab174 923{
4577370e 924 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
d71ab174
DB
925
926 if (!ref)
9ad7c5ae 927 return NULL;
d71ab174
DB
928
929 return copy_ref(ref);
930}
931
932static struct ref *get_local_ref(const char *name)
933{
934 struct ref *ret;
935 if (!name)
936 return NULL;
937
938 if (!prefixcmp(name, "refs/")) {
939 ret = alloc_ref(strlen(name) + 1);
940 strcpy(ret->name, name);
941 return ret;
942 }
943
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;
950 }
951
952 ret = alloc_ref(strlen(name) + 12);
953 sprintf(ret->name, "refs/heads/%s", name);
954 return ret;
955}
956
4577370e 957int get_fetch_map(const struct ref *remote_refs,
d71ab174 958 const struct refspec *refspec,
9ad7c5ae
JH
959 struct ref ***tail,
960 int missing_ok)
d71ab174
DB
961{
962 struct ref *ref_map, *rm;
963
964 if (refspec->pattern) {
965 ref_map = get_expanded_map(remote_refs, refspec);
966 } else {
9ad7c5ae
JH
967 const char *name = refspec->src[0] ? refspec->src : "HEAD";
968
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;
976 }
d71ab174
DB
977 }
978
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);
983 }
984
8f70a765
AR
985 if (ref_map)
986 tail_link_ref(ref_map, tail);
d71ab174
DB
987
988 return 0;
989}