Add uploadpack configuration info to remote.
[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
8#define BUF_SIZE (2048)
9static char buffer[BUF_SIZE];
10
11static void add_push_refspec(struct remote *remote, const char *ref)
12{
13 int nr = remote->push_refspec_nr + 1;
14 remote->push_refspec =
15 xrealloc(remote->push_refspec, nr * sizeof(char *));
16 remote->push_refspec[nr-1] = ref;
17 remote->push_refspec_nr = nr;
18}
19
5d46c9d4
DB
20static void add_fetch_refspec(struct remote *remote, const char *ref)
21{
22 int nr = remote->fetch_refspec_nr + 1;
23 remote->fetch_refspec =
24 xrealloc(remote->fetch_refspec, nr * sizeof(char *));
25 remote->fetch_refspec[nr-1] = ref;
26 remote->fetch_refspec_nr = nr;
27}
28
5751f490
DB
29static void add_uri(struct remote *remote, const char *uri)
30{
31 int nr = remote->uri_nr + 1;
32 remote->uri =
33 xrealloc(remote->uri, nr * sizeof(char *));
34 remote->uri[nr-1] = uri;
35 remote->uri_nr = nr;
36}
37
38static struct remote *make_remote(const char *name, int len)
39{
40 int i, empty = -1;
41
42 for (i = 0; i < allocated_remotes; i++) {
43 if (!remotes[i]) {
44 if (empty < 0)
45 empty = i;
46 } else {
47 if (len ? (!strncmp(name, remotes[i]->name, len) &&
48 !remotes[i]->name[len]) :
49 !strcmp(name, remotes[i]->name))
50 return remotes[i];
51 }
52 }
53
54 if (empty < 0) {
55 empty = allocated_remotes;
56 allocated_remotes += allocated_remotes ? allocated_remotes : 1;
57 remotes = xrealloc(remotes,
58 sizeof(*remotes) * allocated_remotes);
59 memset(remotes + empty, 0,
60 (allocated_remotes - empty) * sizeof(*remotes));
61 }
62 remotes[empty] = xcalloc(1, sizeof(struct remote));
63 if (len)
64 remotes[empty]->name = xstrndup(name, len);
65 else
66 remotes[empty]->name = xstrdup(name);
67 return remotes[empty];
68}
69
70static void read_remotes_file(struct remote *remote)
71{
72 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
73
74 if (!f)
75 return;
76 while (fgets(buffer, BUF_SIZE, f)) {
77 int value_list;
78 char *s, *p;
79
80 if (!prefixcmp(buffer, "URL:")) {
81 value_list = 0;
82 s = buffer + 4;
83 } else if (!prefixcmp(buffer, "Push:")) {
84 value_list = 1;
85 s = buffer + 5;
5d46c9d4
DB
86 } else if (!prefixcmp(buffer, "Pull:")) {
87 value_list = 2;
88 s = buffer + 5;
5751f490
DB
89 } else
90 continue;
91
92 while (isspace(*s))
93 s++;
94 if (!*s)
95 continue;
96
97 p = s + strlen(s);
98 while (isspace(p[-1]))
99 *--p = 0;
100
101 switch (value_list) {
102 case 0:
103 add_uri(remote, xstrdup(s));
104 break;
105 case 1:
106 add_push_refspec(remote, xstrdup(s));
107 break;
5d46c9d4
DB
108 case 2:
109 add_fetch_refspec(remote, xstrdup(s));
110 break;
5751f490
DB
111 }
112 }
113 fclose(f);
114}
115
116static void read_branches_file(struct remote *remote)
117{
118 const char *slash = strchr(remote->name, '/');
119 int n = slash ? slash - remote->name : 1000;
120 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
121 char *s, *p;
122 int len;
123
124 if (!f)
125 return;
126 s = fgets(buffer, BUF_SIZE, f);
127 fclose(f);
128 if (!s)
129 return;
130 while (isspace(*s))
131 s++;
132 if (!*s)
133 return;
134 p = s + strlen(s);
135 while (isspace(p[-1]))
136 *--p = 0;
137 len = p - s;
138 if (slash)
139 len += strlen(slash);
140 p = xmalloc(len + 1);
141 strcpy(p, s);
142 if (slash)
143 strcat(p, slash);
144 add_uri(remote, p);
145}
146
147static char *default_remote_name = NULL;
148static const char *current_branch = NULL;
149static int current_branch_len = 0;
150
151static int handle_config(const char *key, const char *value)
152{
153 const char *name;
154 const char *subkey;
155 struct remote *remote;
156 if (!prefixcmp(key, "branch.") && current_branch &&
157 !strncmp(key + 7, current_branch, current_branch_len) &&
158 !strcmp(key + 7 + current_branch_len, ".remote")) {
159 free(default_remote_name);
160 default_remote_name = xstrdup(value);
161 }
162 if (prefixcmp(key, "remote."))
163 return 0;
164 name = key + 7;
165 subkey = strrchr(name, '.');
166 if (!subkey)
167 return error("Config with no key for remote %s", name);
168 if (*subkey == '/') {
169 warning("Config remote shorthand cannot begin with '/': %s", name);
170 return 0;
171 }
172 remote = make_remote(name, subkey - name);
173 if (!value) {
174 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
175 * [remote "frotz"]
176 * disabled
177 * is a valid way to set it to true; we get NULL in value so
178 * we need to handle it here.
179 *
180 * if (!strcmp(subkey, ".disabled")) {
181 * val = git_config_bool(key, value);
182 * return 0;
183 * } else
184 *
185 */
186 return 0; /* ignore unknown booleans */
187 }
188 if (!strcmp(subkey, ".url")) {
189 add_uri(remote, xstrdup(value));
190 } else if (!strcmp(subkey, ".push")) {
191 add_push_refspec(remote, xstrdup(value));
5d46c9d4
DB
192 } else if (!strcmp(subkey, ".fetch")) {
193 add_fetch_refspec(remote, xstrdup(value));
5751f490
DB
194 } else if (!strcmp(subkey, ".receivepack")) {
195 if (!remote->receivepack)
196 remote->receivepack = xstrdup(value);
197 else
198 error("more than one receivepack given, using the first");
0012ba21
DB
199 } else if (!strcmp(subkey, ".uploadpack")) {
200 if (!remote->uploadpack)
201 remote->uploadpack = xstrdup(value);
202 else
203 error("more than one uploadpack given, using the first");
5751f490
DB
204 }
205 return 0;
206}
207
208static void read_config(void)
209{
210 unsigned char sha1[20];
211 const char *head_ref;
212 int flag;
213 if (default_remote_name) // did this already
214 return;
215 default_remote_name = xstrdup("origin");
216 current_branch = NULL;
217 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
218 if (head_ref && (flag & REF_ISSYMREF) &&
219 !prefixcmp(head_ref, "refs/heads/")) {
220 current_branch = head_ref + strlen("refs/heads/");
221 current_branch_len = strlen(current_branch);
222 }
223 git_config(handle_config);
224}
225
6b62816c
DB
226static struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
227{
228 int i;
229 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
230 for (i = 0; i < nr_refspec; i++) {
231 const char *sp, *ep, *gp;
232 sp = refspec[i];
233 if (*sp == '+') {
234 rs[i].force = 1;
235 sp++;
236 }
237 gp = strchr(sp, '*');
238 ep = strchr(sp, ':');
239 if (gp && ep && gp > ep)
240 gp = NULL;
241 if (ep) {
242 if (ep[1]) {
243 const char *glob = strchr(ep + 1, '*');
244 if (!glob)
245 gp = NULL;
246 if (gp)
247 rs[i].dst = xstrndup(ep + 1,
248 glob - ep - 1);
249 else
250 rs[i].dst = xstrdup(ep + 1);
251 }
252 } else {
253 ep = sp + strlen(sp);
254 }
255 if (gp) {
256 rs[i].pattern = 1;
257 ep = gp;
258 }
259 rs[i].src = xstrndup(sp, ep - sp);
260 }
261 return rs;
262}
263
5751f490
DB
264struct remote *remote_get(const char *name)
265{
266 struct remote *ret;
267
268 read_config();
269 if (!name)
270 name = default_remote_name;
271 ret = make_remote(name, 0);
272 if (name[0] != '/') {
273 if (!ret->uri)
274 read_remotes_file(ret);
275 if (!ret->uri)
276 read_branches_file(ret);
277 }
278 if (!ret->uri)
279 add_uri(ret, name);
280 if (!ret->uri)
281 return NULL;
5d46c9d4 282 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
6b62816c 283 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
5751f490
DB
284 return ret;
285}
6b62816c 286
b42f6927
JS
287int for_each_remote(each_remote_fn fn, void *priv)
288{
289 int i, result = 0;
290 read_config();
291 for (i = 0; i < allocated_remotes && !result; i++) {
292 struct remote *r = remotes[i];
293 if (!r)
294 continue;
295 if (!r->fetch)
296 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
297 r->fetch_refspec);
298 if (!r->push)
299 r->push = parse_ref_spec(r->push_refspec_nr,
300 r->push_refspec);
301 result = fn(r, priv);
302 }
303 return result;
304}
305
5d46c9d4
DB
306int remote_has_uri(struct remote *remote, const char *uri)
307{
308 int i;
309 for (i = 0; i < remote->uri_nr; i++) {
310 if (!strcmp(remote->uri[i], uri))
311 return 1;
312 }
313 return 0;
314}
315
316int remote_find_tracking(struct remote *remote, struct refspec *refspec)
317{
b42f6927
JS
318 int find_src = refspec->src == NULL;
319 char *needle, **result;
5d46c9d4 320 int i;
b42f6927
JS
321
322 if (find_src) {
323 if (refspec->dst == NULL)
324 return error("find_tracking: need either src or dst");
325 needle = refspec->dst;
326 result = &refspec->src;
327 } else {
328 needle = refspec->src;
329 result = &refspec->dst;
330 }
331
5d46c9d4
DB
332 for (i = 0; i < remote->fetch_refspec_nr; i++) {
333 struct refspec *fetch = &remote->fetch[i];
b42f6927
JS
334 const char *key = find_src ? fetch->dst : fetch->src;
335 const char *value = find_src ? fetch->src : fetch->dst;
5d46c9d4
DB
336 if (!fetch->dst)
337 continue;
338 if (fetch->pattern) {
b42f6927
JS
339 if (!prefixcmp(needle, key)) {
340 *result = xmalloc(strlen(value) +
341 strlen(needle) -
342 strlen(key) + 1);
343 strcpy(*result, value);
344 strcpy(*result + strlen(value),
345 needle + strlen(key));
5d46c9d4
DB
346 refspec->force = fetch->force;
347 return 0;
348 }
b42f6927
JS
349 } else if (!strcmp(needle, key)) {
350 *result = xstrdup(value);
351 refspec->force = fetch->force;
352 return 0;
5d46c9d4
DB
353 }
354 }
5d46c9d4
DB
355 return -1;
356}
357
dfd255dd
DB
358struct ref *alloc_ref(unsigned namelen)
359{
360 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
361 memset(ret, 0, sizeof(struct ref) + namelen);
362 return ret;
363}
364
365void free_refs(struct ref *ref)
366{
367 struct ref *next;
368 while (ref) {
369 next = ref->next;
370 if (ref->peer_ref)
371 free(ref->peer_ref);
372 free(ref);
373 ref = next;
374 }
375}
376
6b62816c
DB
377static int count_refspec_match(const char *pattern,
378 struct ref *refs,
379 struct ref **matched_ref)
380{
381 int patlen = strlen(pattern);
382 struct ref *matched_weak = NULL;
383 struct ref *matched = NULL;
384 int weak_match = 0;
385 int match = 0;
386
387 for (weak_match = match = 0; refs; refs = refs->next) {
388 char *name = refs->name;
389 int namelen = strlen(name);
6b62816c
DB
390
391 if (namelen < patlen ||
392 memcmp(name + namelen - patlen, pattern, patlen))
393 continue;
394 if (namelen != patlen && name[namelen - patlen - 1] != '/')
395 continue;
396
397 /* A match is "weak" if it is with refs outside
398 * heads or tags, and did not specify the pattern
399 * in full (e.g. "refs/remotes/origin/master") or at
400 * least from the toplevel (e.g. "remotes/origin/master");
401 * otherwise "git push $URL master" would result in
402 * ambiguity between remotes/origin/master and heads/master
403 * at the remote site.
404 */
405 if (namelen != patlen &&
406 patlen != namelen - 5 &&
407 prefixcmp(name, "refs/heads/") &&
408 prefixcmp(name, "refs/tags/")) {
409 /* We want to catch the case where only weak
410 * matches are found and there are multiple
411 * matches, and where more than one strong
412 * matches are found, as ambiguous. One
413 * strong match with zero or more weak matches
414 * are acceptable as a unique match.
415 */
416 matched_weak = refs;
417 weak_match++;
418 }
419 else {
420 matched = refs;
421 match++;
422 }
423 }
424 if (!matched) {
425 *matched_ref = matched_weak;
426 return weak_match;
427 }
428 else {
429 *matched_ref = matched;
430 return match;
431 }
432}
433
1d735267 434static void tail_link_ref(struct ref *ref, struct ref ***tail)
6b62816c
DB
435{
436 **tail = ref;
1d735267
DB
437 while (ref->next)
438 ref = ref->next;
6b62816c 439 *tail = &ref->next;
6b62816c
DB
440}
441
442static struct ref *try_explicit_object_name(const char *name)
443{
444 unsigned char sha1[20];
445 struct ref *ref;
446 int len;
447
448 if (!*name) {
dfd255dd 449 ref = alloc_ref(20);
6b62816c
DB
450 strcpy(ref->name, "(delete)");
451 hashclr(ref->new_sha1);
452 return ref;
453 }
454 if (get_sha1(name, sha1))
455 return NULL;
456 len = strlen(name) + 1;
dfd255dd 457 ref = alloc_ref(len);
6b62816c
DB
458 memcpy(ref->name, name, len);
459 hashcpy(ref->new_sha1, sha1);
460 return ref;
461}
462
1d735267 463static struct ref *make_linked_ref(const char *name, struct ref ***tail)
6b62816c 464{
1d735267 465 struct ref *ret;
163f0ee5 466 size_t len;
6b62816c 467
163f0ee5 468 len = strlen(name) + 1;
1d735267
DB
469 ret = alloc_ref(len);
470 memcpy(ret->name, name, len);
471 tail_link_ref(ret, tail);
472 return ret;
163f0ee5 473}
8558fd9e 474
54a8ad92
JH
475static int match_explicit(struct ref *src, struct ref *dst,
476 struct ref ***dst_tail,
477 struct refspec *rs,
478 int errs)
6b62816c 479{
54a8ad92 480 struct ref *matched_src, *matched_dst;
8558fd9e 481
54a8ad92 482 const char *dst_value = rs->dst;
6b62816c 483
54a8ad92
JH
484 if (rs->pattern)
485 return errs;
8558fd9e 486
54a8ad92
JH
487 matched_src = matched_dst = NULL;
488 switch (count_refspec_match(rs->src, src, &matched_src)) {
489 case 1:
490 break;
491 case 0:
492 /* The source could be in the get_sha1() format
493 * not a reference name. :refs/other is a
494 * way to delete 'other' ref at the remote end.
495 */
496 matched_src = try_explicit_object_name(rs->src);
497 if (matched_src)
6b62816c 498 break;
54a8ad92
JH
499 error("src refspec %s does not match any.",
500 rs->src);
501 break;
502 default:
3c8b7df1 503 matched_src = NULL;
54a8ad92
JH
504 error("src refspec %s matches more than one.",
505 rs->src);
506 break;
507 }
3c8b7df1
JH
508
509 if (!matched_src)
510 errs = 1;
511
512 if (dst_value == NULL)
1ed10b88 513 dst_value = matched_src->name;
3c8b7df1 514
54a8ad92
JH
515 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
516 case 1:
517 break;
518 case 0:
163f0ee5 519 if (!memcmp(dst_value, "refs/", 5))
1d735267 520 matched_dst = make_linked_ref(dst_value, dst_tail);
3c8b7df1 521 else
54a8ad92
JH
522 error("dst refspec %s does not match any "
523 "existing ref on the remote and does "
524 "not start with refs/.", dst_value);
54a8ad92
JH
525 break;
526 default:
3c8b7df1 527 matched_dst = NULL;
54a8ad92
JH
528 error("dst refspec %s matches more than one.",
529 dst_value);
530 break;
531 }
3c8b7df1
JH
532 if (errs || matched_dst == NULL)
533 return 1;
54a8ad92
JH
534 if (matched_dst->peer_ref) {
535 errs = 1;
536 error("dst ref %s receives from more than one src.",
537 matched_dst->name);
6b62816c 538 }
54a8ad92
JH
539 else {
540 matched_dst->peer_ref = matched_src;
541 matched_dst->force = rs->force;
6b62816c 542 }
54a8ad92
JH
543 return errs;
544}
545
546static int match_explicit_refs(struct ref *src, struct ref *dst,
547 struct ref ***dst_tail, struct refspec *rs,
548 int rs_nr)
549{
550 int i, errs;
551 for (i = errs = 0; i < rs_nr; i++)
552 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
6b62816c
DB
553 return -errs;
554}
555
556static struct ref *find_ref_by_name(struct ref *list, const char *name)
557{
558 for ( ; list; list = list->next)
559 if (!strcmp(list->name, name))
560 return list;
561 return NULL;
562}
563
6e66bf3c
AR
564static const struct refspec *check_pattern_match(const struct refspec *rs,
565 int rs_nr,
566 const struct ref *src)
8558fd9e
DB
567{
568 int i;
8558fd9e
DB
569 for (i = 0; i < rs_nr; i++) {
570 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
6e66bf3c 571 return rs + i;
8558fd9e 572 }
6e66bf3c 573 return NULL;
8558fd9e
DB
574}
575
54a8ad92
JH
576/*
577 * Note. This is used only by "push"; refspec matching rules for
578 * push and fetch are subtly different, so do not try to reuse it
579 * without thinking.
580 */
6b62816c
DB
581int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
582 int nr_refspec, char **refspec, int all)
583{
584 struct refspec *rs =
585 parse_ref_spec(nr_refspec, (const char **) refspec);
586
8558fd9e
DB
587 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
588 return -1;
6b62816c
DB
589
590 /* pick the remainder */
591 for ( ; src; src = src->next) {
592 struct ref *dst_peer;
6e66bf3c
AR
593 const struct refspec *pat = NULL;
594 char *dst_name;
6b62816c
DB
595 if (src->peer_ref)
596 continue;
6e66bf3c
AR
597 if (nr_refspec) {
598 pat = check_pattern_match(rs, nr_refspec, src);
599 if (!pat)
600 continue;
601 }
098e711e
JH
602 else if (prefixcmp(src->name, "refs/heads/"))
603 /*
604 * "matching refs"; traditionally we pushed everything
605 * including refs outside refs/heads/ hierarchy, but
606 * that does not make much sense these days.
607 */
608 continue;
8558fd9e 609
6e66bf3c 610 if (pat) {
efd8f793
DB
611 const char *dst_side = pat->dst ? pat->dst : pat->src;
612 dst_name = xmalloc(strlen(dst_side) +
6e66bf3c
AR
613 strlen(src->name) -
614 strlen(pat->src) + 2);
efd8f793 615 strcpy(dst_name, dst_side);
6e66bf3c
AR
616 strcat(dst_name, src->name + strlen(pat->src));
617 } else
aa32eedc 618 dst_name = xstrdup(src->name);
6e66bf3c 619 dst_peer = find_ref_by_name(dst, dst_name);
8558fd9e
DB
620 if (dst_peer && dst_peer->peer_ref)
621 /* We're already sending something to this ref. */
6e66bf3c 622 goto free_name;
8558fd9e
DB
623 if (!dst_peer && !nr_refspec && !all)
624 /* Remote doesn't have it, and we have no
625 * explicit pattern, and we don't have
626 * --all. */
6e66bf3c 627 goto free_name;
6b62816c
DB
628 if (!dst_peer) {
629 /* Create a new one and link it */
1d735267 630 dst_peer = make_linked_ref(dst_name, dst_tail);
6b62816c 631 hashcpy(dst_peer->new_sha1, src->new_sha1);
6b62816c
DB
632 }
633 dst_peer->peer_ref = src;
6e66bf3c
AR
634 free_name:
635 free(dst_name);
6b62816c
DB
636 }
637 return 0;
638}