]> git.ipfire.org Git - thirdparty/git.git/blame - remote.c
Cleanup xread() loops to use read_in_full()
[thirdparty/git.git] / remote.c
CommitLineData
5751f490
DB
1#include "cache.h"
2#include "remote.h"
3#include "refs.h"
4
844112ca
JH
5struct counted_string {
6 size_t len;
7 const char *s;
8};
55029ae4
DB
9struct rewrite {
10 const char *base;
844112ca
JH
11 size_t baselen;
12 struct counted_string *instead_of;
55029ae4
DB
13 int instead_of_nr;
14 int instead_of_alloc;
15};
16
5751f490 17static struct remote **remotes;
2d31347b
DB
18static int remotes_alloc;
19static int remotes_nr;
5751f490 20
cf818348 21static struct branch **branches;
2d31347b
DB
22static int branches_alloc;
23static int branches_nr;
cf818348
DB
24
25static struct branch *current_branch;
26static const char *default_remote_name;
27
55029ae4
DB
28static struct rewrite **rewrite;
29static int rewrite_alloc;
30static int rewrite_nr;
31
5751f490
DB
32#define BUF_SIZE (2048)
33static char buffer[BUF_SIZE];
34
55029ae4
DB
35static const char *alias_url(const char *url)
36{
37 int i, j;
844112ca
JH
38 char *ret;
39 struct counted_string *longest;
40 int longest_i;
41
42 longest = NULL;
43 longest_i = -1;
55029ae4
DB
44 for (i = 0; i < rewrite_nr; i++) {
45 if (!rewrite[i])
46 continue;
47 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
844112ca
JH
48 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
49 (!longest ||
50 longest->len < rewrite[i]->instead_of[j].len)) {
51 longest = &(rewrite[i]->instead_of[j]);
52 longest_i = i;
55029ae4
DB
53 }
54 }
55 }
844112ca
JH
56 if (!longest)
57 return url;
58
59 ret = malloc(rewrite[longest_i]->baselen +
60 (strlen(url) - longest->len) + 1);
61 strcpy(ret, rewrite[longest_i]->base);
62 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
63 return ret;
55029ae4
DB
64}
65
5751f490
DB
66static void add_push_refspec(struct remote *remote, const char *ref)
67{
2d31347b
DB
68 ALLOC_GROW(remote->push_refspec,
69 remote->push_refspec_nr + 1,
70 remote->push_refspec_alloc);
71 remote->push_refspec[remote->push_refspec_nr++] = ref;
5751f490
DB
72}
73
5d46c9d4
DB
74static void add_fetch_refspec(struct remote *remote, const char *ref)
75{
2d31347b
DB
76 ALLOC_GROW(remote->fetch_refspec,
77 remote->fetch_refspec_nr + 1,
78 remote->fetch_refspec_alloc);
79 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
5d46c9d4
DB
80}
81
28b91f8a 82static void add_url(struct remote *remote, const char *url)
5751f490 83{
2d31347b
DB
84 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
85 remote->url[remote->url_nr++] = url;
5751f490
DB
86}
87
55029ae4
DB
88static void add_url_alias(struct remote *remote, const char *url)
89{
90 add_url(remote, alias_url(url));
91}
92
5751f490
DB
93static struct remote *make_remote(const char *name, int len)
94{
2d31347b
DB
95 struct remote *ret;
96 int i;
5751f490 97
2d31347b
DB
98 for (i = 0; i < remotes_nr; i++) {
99 if (len ? (!strncmp(name, remotes[i]->name, len) &&
100 !remotes[i]->name[len]) :
101 !strcmp(name, remotes[i]->name))
102 return remotes[i];
5751f490
DB
103 }
104
2d31347b
DB
105 ret = xcalloc(1, sizeof(struct remote));
106 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
107 remotes[remotes_nr++] = ret;
5751f490 108 if (len)
2d31347b 109 ret->name = xstrndup(name, len);
5751f490 110 else
2d31347b
DB
111 ret->name = xstrdup(name);
112 return ret;
5751f490
DB
113}
114
cf818348
DB
115static void add_merge(struct branch *branch, const char *name)
116{
2d31347b
DB
117 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
118 branch->merge_alloc);
119 branch->merge_name[branch->merge_nr++] = name;
cf818348
DB
120}
121
122static struct branch *make_branch(const char *name, int len)
123{
2d31347b
DB
124 struct branch *ret;
125 int i;
cf818348
DB
126 char *refname;
127
2d31347b
DB
128 for (i = 0; i < branches_nr; i++) {
129 if (len ? (!strncmp(name, branches[i]->name, len) &&
130 !branches[i]->name[len]) :
131 !strcmp(name, branches[i]->name))
132 return branches[i];
cf818348
DB
133 }
134
2d31347b
DB
135 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
136 ret = xcalloc(1, sizeof(struct branch));
137 branches[branches_nr++] = ret;
cf818348 138 if (len)
2d31347b 139 ret->name = xstrndup(name, len);
cf818348 140 else
2d31347b 141 ret->name = xstrdup(name);
cf818348
DB
142 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
143 strcpy(refname, "refs/heads/");
2d31347b
DB
144 strcpy(refname + strlen("refs/heads/"), ret->name);
145 ret->refname = refname;
cf818348 146
2d31347b 147 return ret;
cf818348
DB
148}
149
55029ae4
DB
150static struct rewrite *make_rewrite(const char *base, int len)
151{
152 struct rewrite *ret;
153 int i;
154
155 for (i = 0; i < rewrite_nr; i++) {
844112ca
JH
156 if (len
157 ? (len == rewrite[i]->baselen &&
158 !strncmp(base, rewrite[i]->base, len))
159 : !strcmp(base, rewrite[i]->base))
55029ae4
DB
160 return rewrite[i];
161 }
162
163 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
164 ret = xcalloc(1, sizeof(struct rewrite));
165 rewrite[rewrite_nr++] = ret;
844112ca 166 if (len) {
55029ae4 167 ret->base = xstrndup(base, len);
844112ca
JH
168 ret->baselen = len;
169 }
170 else {
55029ae4 171 ret->base = xstrdup(base);
844112ca
JH
172 ret->baselen = strlen(base);
173 }
55029ae4
DB
174 return ret;
175}
176
177static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
178{
179 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
844112ca
JH
180 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
181 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
182 rewrite->instead_of_nr++;
55029ae4
DB
183}
184
5751f490
DB
185static void read_remotes_file(struct remote *remote)
186{
187 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
188
189 if (!f)
190 return;
191 while (fgets(buffer, BUF_SIZE, f)) {
192 int value_list;
193 char *s, *p;
194
195 if (!prefixcmp(buffer, "URL:")) {
196 value_list = 0;
197 s = buffer + 4;
198 } else if (!prefixcmp(buffer, "Push:")) {
199 value_list = 1;
200 s = buffer + 5;
5d46c9d4
DB
201 } else if (!prefixcmp(buffer, "Pull:")) {
202 value_list = 2;
203 s = buffer + 5;
5751f490
DB
204 } else
205 continue;
206
207 while (isspace(*s))
208 s++;
209 if (!*s)
210 continue;
211
212 p = s + strlen(s);
213 while (isspace(p[-1]))
214 *--p = 0;
215
216 switch (value_list) {
217 case 0:
55029ae4 218 add_url_alias(remote, xstrdup(s));
5751f490
DB
219 break;
220 case 1:
221 add_push_refspec(remote, xstrdup(s));
222 break;
5d46c9d4
DB
223 case 2:
224 add_fetch_refspec(remote, xstrdup(s));
225 break;
5751f490
DB
226 }
227 }
228 fclose(f);
229}
230
231static void read_branches_file(struct remote *remote)
232{
233 const char *slash = strchr(remote->name, '/');
cf818348 234 char *frag;
472fa4cd 235 struct strbuf branch;
5751f490
DB
236 int n = slash ? slash - remote->name : 1000;
237 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
238 char *s, *p;
239 int len;
240
241 if (!f)
242 return;
243 s = fgets(buffer, BUF_SIZE, f);
244 fclose(f);
245 if (!s)
246 return;
247 while (isspace(*s))
248 s++;
249 if (!*s)
250 return;
251 p = s + strlen(s);
252 while (isspace(p[-1]))
253 *--p = 0;
254 len = p - s;
255 if (slash)
256 len += strlen(slash);
257 p = xmalloc(len + 1);
258 strcpy(p, s);
259 if (slash)
260 strcat(p, slash);
472fa4cd
DB
261
262 /*
263 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
264 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
265 * the partial URL obtained from the branches file plus
266 * "/netdev-2.6" and does not store it in any tracking ref.
267 * #branch specifier in the file is ignored.
268 *
269 * Otherwise, the branches file would have URL and optionally
270 * #branch specified. The "master" (or specified) branch is
271 * fetched and stored in the local branch of the same name.
272 */
273 strbuf_init(&branch, 0);
cf818348
DB
274 frag = strchr(p, '#');
275 if (frag) {
276 *(frag++) = '\0';
472fa4cd
DB
277 strbuf_addf(&branch, "refs/heads/%s", frag);
278 } else
279 strbuf_addstr(&branch, "refs/heads/master");
280 if (!slash) {
281 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
cf818348 282 } else {
472fa4cd
DB
283 strbuf_reset(&branch);
284 strbuf_addstr(&branch, "HEAD:");
cf818348 285 }
55029ae4 286 add_url_alias(remote, p);
472fa4cd 287 add_fetch_refspec(remote, strbuf_detach(&branch, 0));
d71ab174 288 remote->fetch_tags = 1; /* always auto-follow */
5751f490
DB
289}
290
5751f490
DB
291static int handle_config(const char *key, const char *value)
292{
293 const char *name;
294 const char *subkey;
295 struct remote *remote;
cf818348
DB
296 struct branch *branch;
297 if (!prefixcmp(key, "branch.")) {
298 name = key + 7;
299 subkey = strrchr(name, '.');
cf818348
DB
300 if (!subkey)
301 return 0;
896c0535 302 branch = make_branch(name, subkey - name);
cf818348 303 if (!strcmp(subkey, ".remote")) {
d2370cc2
JH
304 if (!value)
305 return config_error_nonbool(key);
cf818348
DB
306 branch->remote_name = xstrdup(value);
307 if (branch == current_branch)
308 default_remote_name = branch->remote_name;
d2370cc2
JH
309 } else if (!strcmp(subkey, ".merge")) {
310 if (!value)
311 return config_error_nonbool(key);
cf818348 312 add_merge(branch, xstrdup(value));
d2370cc2 313 }
cf818348 314 return 0;
5751f490 315 }
55029ae4
DB
316 if (!prefixcmp(key, "url.")) {
317 struct rewrite *rewrite;
60e3aba9 318 name = key + 4;
55029ae4
DB
319 subkey = strrchr(name, '.');
320 if (!subkey)
321 return 0;
322 rewrite = make_rewrite(name, subkey - name);
323 if (!strcmp(subkey, ".insteadof")) {
324 if (!value)
325 return config_error_nonbool(key);
326 add_instead_of(rewrite, xstrdup(value));
327 }
328 }
5751f490
DB
329 if (prefixcmp(key, "remote."))
330 return 0;
331 name = key + 7;
332 subkey = strrchr(name, '.');
333 if (!subkey)
334 return error("Config with no key for remote %s", name);
335 if (*subkey == '/') {
336 warning("Config remote shorthand cannot begin with '/': %s", name);
337 return 0;
338 }
339 remote = make_remote(name, subkey - name);
340 if (!value) {
341 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
342 * [remote "frotz"]
343 * disabled
344 * is a valid way to set it to true; we get NULL in value so
345 * we need to handle it here.
346 *
347 * if (!strcmp(subkey, ".disabled")) {
348 * val = git_config_bool(key, value);
349 * return 0;
350 * } else
351 *
352 */
353 return 0; /* ignore unknown booleans */
354 }
355 if (!strcmp(subkey, ".url")) {
28b91f8a 356 add_url(remote, xstrdup(value));
5751f490
DB
357 } else if (!strcmp(subkey, ".push")) {
358 add_push_refspec(remote, xstrdup(value));
5d46c9d4
DB
359 } else if (!strcmp(subkey, ".fetch")) {
360 add_fetch_refspec(remote, xstrdup(value));
5751f490
DB
361 } else if (!strcmp(subkey, ".receivepack")) {
362 if (!remote->receivepack)
363 remote->receivepack = xstrdup(value);
364 else
365 error("more than one receivepack given, using the first");
0012ba21
DB
366 } else if (!strcmp(subkey, ".uploadpack")) {
367 if (!remote->uploadpack)
368 remote->uploadpack = xstrdup(value);
369 else
370 error("more than one uploadpack given, using the first");
d71ab174
DB
371 } else if (!strcmp(subkey, ".tagopt")) {
372 if (!strcmp(value, "--no-tags"))
373 remote->fetch_tags = -1;
14c98218
SV
374 } else if (!strcmp(subkey, ".proxy")) {
375 remote->http_proxy = xstrdup(value);
211c8968
JS
376 } else if (!strcmp(subkey, ".skipdefaultupdate"))
377 remote->skip_default_update = 1;
5751f490
DB
378 return 0;
379}
380
55029ae4
DB
381static void alias_all_urls(void)
382{
383 int i, j;
384 for (i = 0; i < remotes_nr; i++) {
385 if (!remotes[i])
386 continue;
387 for (j = 0; j < remotes[i]->url_nr; j++) {
388 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
389 }
390 }
391}
392
5751f490
DB
393static void read_config(void)
394{
395 unsigned char sha1[20];
396 const char *head_ref;
397 int flag;
398 if (default_remote_name) // did this already
399 return;
400 default_remote_name = xstrdup("origin");
401 current_branch = NULL;
402 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
403 if (head_ref && (flag & REF_ISSYMREF) &&
404 !prefixcmp(head_ref, "refs/heads/")) {
cf818348
DB
405 current_branch =
406 make_branch(head_ref + strlen("refs/heads/"), 0);
5751f490
DB
407 }
408 git_config(handle_config);
55029ae4 409 alias_all_urls();
5751f490
DB
410}
411
24b6177e 412static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
6b62816c
DB
413{
414 int i;
ef00d150 415 int st;
6b62816c 416 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
46220ca1 417
6b62816c 418 for (i = 0; i < nr_refspec; i++) {
46220ca1
JH
419 size_t llen, rlen;
420 int is_glob;
421 const char *lhs, *rhs;
422
423 llen = rlen = is_glob = 0;
424
425 lhs = refspec[i];
426 if (*lhs == '+') {
6b62816c 427 rs[i].force = 1;
46220ca1 428 lhs++;
6b62816c 429 }
46220ca1
JH
430
431 rhs = strrchr(lhs, ':');
432 if (rhs) {
433 rhs++;
434 rlen = strlen(rhs);
435 is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
7d19da46
JH
436 if (is_glob)
437 rlen -= 2;
438 rs[i].dst = xstrndup(rhs, rlen);
6b62816c 439 }
ef00d150 440
46220ca1 441 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
7d19da46
JH
442 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
443 if ((rhs && !is_glob) || (!rhs && fetch))
444 goto invalid;
445 is_glob = 1;
46220ca1 446 llen -= 2;
7d19da46
JH
447 } else if (rhs && is_glob) {
448 goto invalid;
ef00d150 449 }
7d19da46 450
46220ca1
JH
451 rs[i].pattern = is_glob;
452 rs[i].src = xstrndup(lhs, llen);
453
454 if (fetch) {
455 /*
456 * LHS
457 * - empty is allowed; it means HEAD.
458 * - otherwise it must be a valid looking ref.
459 */
460 if (!*rs[i].src)
461 ; /* empty is ok */
462 else {
463 st = check_ref_format(rs[i].src);
464 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
465 goto invalid;
466 }
467 /*
468 * RHS
7d19da46 469 * - missing is ok, and is same as empty.
46220ca1
JH
470 * - empty is ok; it means not to store.
471 * - otherwise it must be a valid looking ref.
472 */
473 if (!rs[i].dst) {
474 ; /* ok */
475 } else if (!*rs[i].dst) {
476 ; /* ok */
477 } else {
478 st = check_ref_format(rs[i].dst);
479 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
480 goto invalid;
481 }
482 } else {
483 /*
484 * LHS
485 * - empty is allowed; it means delete.
486 * - when wildcarded, it must be a valid looking ref.
487 * - otherwise, it must be an extended SHA-1, but
488 * there is no existing way to validate this.
489 */
490 if (!*rs[i].src)
491 ; /* empty is ok */
492 else if (is_glob) {
493 st = check_ref_format(rs[i].src);
494 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
495 goto invalid;
496 }
497 else
498 ; /* anything goes, for now */
499 /*
500 * RHS
501 * - missing is allowed, but LHS then must be a
502 * valid looking ref.
503 * - empty is not allowed.
504 * - otherwise it must be a valid looking ref.
505 */
506 if (!rs[i].dst) {
507 st = check_ref_format(rs[i].src);
508 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
509 goto invalid;
510 } else if (!*rs[i].dst) {
511 goto invalid;
512 } else {
513 st = check_ref_format(rs[i].dst);
514 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
515 goto invalid;
516 }
ef00d150 517 }
6b62816c
DB
518 }
519 return rs;
46220ca1
JH
520
521 invalid:
24b6177e
JF
522 if (verify) {
523 free(rs);
524 return NULL;
525 }
46220ca1
JH
526 die("Invalid refspec '%s'", refspec[i]);
527}
528
24b6177e
JF
529int valid_fetch_refspec(const char *fetch_refspec_str)
530{
531 const char *fetch_refspec[] = { fetch_refspec_str };
532 struct refspec *refspec;
533
534 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
535 if (refspec)
536 free(refspec);
537 return !!refspec;
538}
539
46220ca1
JH
540struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
541{
24b6177e 542 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
46220ca1
JH
543}
544
545struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
546{
24b6177e 547 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
6b62816c
DB
548}
549
df93e33c
DB
550static int valid_remote_nick(const char *name)
551{
552 if (!name[0] || /* not empty */
553 (name[0] == '.' && /* not "." */
554 (!name[1] || /* not ".." */
555 (name[1] == '.' && !name[2]))))
556 return 0;
557 return !strchr(name, '/'); /* no slash */
558}
559
5751f490
DB
560struct remote *remote_get(const char *name)
561{
562 struct remote *ret;
563
564 read_config();
565 if (!name)
566 name = default_remote_name;
567 ret = make_remote(name, 0);
df93e33c 568 if (valid_remote_nick(name)) {
28b91f8a 569 if (!ret->url)
5751f490 570 read_remotes_file(ret);
28b91f8a 571 if (!ret->url)
5751f490
DB
572 read_branches_file(ret);
573 }
28b91f8a 574 if (!ret->url)
55029ae4 575 add_url_alias(ret, name);
28b91f8a 576 if (!ret->url)
5751f490 577 return NULL;
46220ca1
JH
578 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
579 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
5751f490
DB
580 return ret;
581}
6b62816c 582
b42f6927
JS
583int for_each_remote(each_remote_fn fn, void *priv)
584{
585 int i, result = 0;
586 read_config();
2d31347b 587 for (i = 0; i < remotes_nr && !result; i++) {
b42f6927
JS
588 struct remote *r = remotes[i];
589 if (!r)
590 continue;
591 if (!r->fetch)
46220ca1
JH
592 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
593 r->fetch_refspec);
b42f6927 594 if (!r->push)
46220ca1
JH
595 r->push = parse_push_refspec(r->push_refspec_nr,
596 r->push_refspec);
b42f6927
JS
597 result = fn(r, priv);
598 }
599 return result;
600}
601
2467a4fa
DB
602void ref_remove_duplicates(struct ref *ref_map)
603{
604 struct ref **posn;
605 struct ref *next;
606 for (; ref_map; ref_map = ref_map->next) {
607 if (!ref_map->peer_ref)
608 continue;
609 posn = &ref_map->next;
610 while (*posn) {
611 if ((*posn)->peer_ref &&
612 !strcmp((*posn)->peer_ref->name,
613 ref_map->peer_ref->name)) {
614 if (strcmp((*posn)->name, ref_map->name))
615 die("%s tracks both %s and %s",
616 ref_map->peer_ref->name,
617 (*posn)->name, ref_map->name);
618 next = (*posn)->next;
619 free((*posn)->peer_ref);
620 free(*posn);
621 *posn = next;
622 } else {
623 posn = &(*posn)->next;
624 }
625 }
626 }
627}
628
28b91f8a 629int remote_has_url(struct remote *remote, const char *url)
5d46c9d4
DB
630{
631 int i;
28b91f8a
SP
632 for (i = 0; i < remote->url_nr; i++) {
633 if (!strcmp(remote->url[i], url))
5d46c9d4
DB
634 return 1;
635 }
636 return 0;
637}
638
639int remote_find_tracking(struct remote *remote, struct refspec *refspec)
640{
b42f6927
JS
641 int find_src = refspec->src == NULL;
642 char *needle, **result;
5d46c9d4 643 int i;
b42f6927
JS
644
645 if (find_src) {
009c5bcd 646 if (!refspec->dst)
b42f6927
JS
647 return error("find_tracking: need either src or dst");
648 needle = refspec->dst;
649 result = &refspec->src;
650 } else {
651 needle = refspec->src;
652 result = &refspec->dst;
653 }
654
5d46c9d4
DB
655 for (i = 0; i < remote->fetch_refspec_nr; i++) {
656 struct refspec *fetch = &remote->fetch[i];
b42f6927
JS
657 const char *key = find_src ? fetch->dst : fetch->src;
658 const char *value = find_src ? fetch->src : fetch->dst;
5d46c9d4
DB
659 if (!fetch->dst)
660 continue;
661 if (fetch->pattern) {
ef00d150
DB
662 if (!prefixcmp(needle, key) &&
663 needle[strlen(key)] == '/') {
b42f6927
JS
664 *result = xmalloc(strlen(value) +
665 strlen(needle) -
666 strlen(key) + 1);
667 strcpy(*result, value);
668 strcpy(*result + strlen(value),
669 needle + strlen(key));
5d46c9d4
DB
670 refspec->force = fetch->force;
671 return 0;
672 }
b42f6927
JS
673 } else if (!strcmp(needle, key)) {
674 *result = xstrdup(value);
675 refspec->force = fetch->force;
676 return 0;
5d46c9d4
DB
677 }
678 }
5d46c9d4
DB
679 return -1;
680}
681
dfd255dd
DB
682struct ref *alloc_ref(unsigned namelen)
683{
684 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
685 memset(ret, 0, sizeof(struct ref) + namelen);
686 return ret;
687}
688
4577370e 689static struct ref *copy_ref(const struct ref *ref)
d71ab174
DB
690{
691 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
692 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
693 ret->next = NULL;
694 return ret;
695}
696
4577370e
DB
697struct ref *copy_ref_list(const struct ref *ref)
698{
699 struct ref *ret = NULL;
700 struct ref **tail = &ret;
701 while (ref) {
702 *tail = copy_ref(ref);
703 ref = ref->next;
704 tail = &((*tail)->next);
705 }
706 return ret;
707}
708
dfd255dd
DB
709void free_refs(struct ref *ref)
710{
711 struct ref *next;
712 while (ref) {
713 next = ref->next;
8e0f7003 714 free(ref->peer_ref);
dfd255dd
DB
715 free(ref);
716 ref = next;
717 }
718}
719
6b62816c
DB
720static int count_refspec_match(const char *pattern,
721 struct ref *refs,
722 struct ref **matched_ref)
723{
724 int patlen = strlen(pattern);
725 struct ref *matched_weak = NULL;
726 struct ref *matched = NULL;
727 int weak_match = 0;
728 int match = 0;
729
730 for (weak_match = match = 0; refs; refs = refs->next) {
731 char *name = refs->name;
732 int namelen = strlen(name);
6b62816c 733
ae36bdcf 734 if (!refname_match(pattern, name, ref_rev_parse_rules))
6b62816c
DB
735 continue;
736
737 /* A match is "weak" if it is with refs outside
738 * heads or tags, and did not specify the pattern
739 * in full (e.g. "refs/remotes/origin/master") or at
740 * least from the toplevel (e.g. "remotes/origin/master");
741 * otherwise "git push $URL master" would result in
742 * ambiguity between remotes/origin/master and heads/master
743 * at the remote site.
744 */
745 if (namelen != patlen &&
746 patlen != namelen - 5 &&
747 prefixcmp(name, "refs/heads/") &&
748 prefixcmp(name, "refs/tags/")) {
749 /* We want to catch the case where only weak
750 * matches are found and there are multiple
751 * matches, and where more than one strong
752 * matches are found, as ambiguous. One
753 * strong match with zero or more weak matches
754 * are acceptable as a unique match.
755 */
756 matched_weak = refs;
757 weak_match++;
758 }
759 else {
760 matched = refs;
761 match++;
762 }
763 }
764 if (!matched) {
765 *matched_ref = matched_weak;
766 return weak_match;
767 }
768 else {
769 *matched_ref = matched;
770 return match;
771 }
772}
773
1d735267 774static void tail_link_ref(struct ref *ref, struct ref ***tail)
6b62816c
DB
775{
776 **tail = ref;
1d735267
DB
777 while (ref->next)
778 ref = ref->next;
6b62816c 779 *tail = &ref->next;
6b62816c
DB
780}
781
782static struct ref *try_explicit_object_name(const char *name)
783{
784 unsigned char sha1[20];
785 struct ref *ref;
786 int len;
787
788 if (!*name) {
dfd255dd 789 ref = alloc_ref(20);
6b62816c
DB
790 strcpy(ref->name, "(delete)");
791 hashclr(ref->new_sha1);
792 return ref;
793 }
794 if (get_sha1(name, sha1))
795 return NULL;
796 len = strlen(name) + 1;
dfd255dd 797 ref = alloc_ref(len);
6b62816c
DB
798 memcpy(ref->name, name, len);
799 hashcpy(ref->new_sha1, sha1);
800 return ref;
801}
802
1d735267 803static struct ref *make_linked_ref(const char *name, struct ref ***tail)
6b62816c 804{
1d735267 805 struct ref *ret;
163f0ee5 806 size_t len;
6b62816c 807
163f0ee5 808 len = strlen(name) + 1;
1d735267
DB
809 ret = alloc_ref(len);
810 memcpy(ret->name, name, len);
811 tail_link_ref(ret, tail);
812 return ret;
163f0ee5 813}
8558fd9e 814
f8aae120
JK
815static char *guess_ref(const char *name, struct ref *peer)
816{
817 struct strbuf buf = STRBUF_INIT;
818 unsigned char sha1[20];
819
820 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
821 if (!r)
822 return NULL;
823
824 if (!prefixcmp(r, "refs/heads/"))
825 strbuf_addstr(&buf, "refs/heads/");
826 else if (!prefixcmp(r, "refs/tags/"))
827 strbuf_addstr(&buf, "refs/tags/");
828 else
829 return NULL;
830
831 strbuf_addstr(&buf, name);
832 return strbuf_detach(&buf, NULL);
833}
834
54a8ad92
JH
835static int match_explicit(struct ref *src, struct ref *dst,
836 struct ref ***dst_tail,
837 struct refspec *rs,
838 int errs)
6b62816c 839{
54a8ad92 840 struct ref *matched_src, *matched_dst;
8558fd9e 841
54a8ad92 842 const char *dst_value = rs->dst;
f8aae120 843 char *dst_guess;
6b62816c 844
54a8ad92
JH
845 if (rs->pattern)
846 return errs;
8558fd9e 847
54a8ad92
JH
848 matched_src = matched_dst = NULL;
849 switch (count_refspec_match(rs->src, src, &matched_src)) {
850 case 1:
851 break;
852 case 0:
853 /* The source could be in the get_sha1() format
854 * not a reference name. :refs/other is a
855 * way to delete 'other' ref at the remote end.
856 */
857 matched_src = try_explicit_object_name(rs->src);
7dfee372
SP
858 if (!matched_src)
859 error("src refspec %s does not match any.", rs->src);
54a8ad92
JH
860 break;
861 default:
3c8b7df1 862 matched_src = NULL;
7dfee372 863 error("src refspec %s matches more than one.", rs->src);
54a8ad92
JH
864 break;
865 }
3c8b7df1
JH
866
867 if (!matched_src)
868 errs = 1;
869
4491e62a 870 if (!dst_value) {
9f0ea7e8
DB
871 unsigned char sha1[20];
872 int flag;
873
4491e62a
SP
874 if (!matched_src)
875 return errs;
9f0ea7e8
DB
876 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
877 if (!dst_value ||
878 ((flag & REF_ISSYMREF) &&
879 prefixcmp(dst_value, "refs/heads/")))
880 die("%s cannot be resolved to branch.",
881 matched_src->name);
4491e62a 882 }
3c8b7df1 883
54a8ad92
JH
884 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
885 case 1:
886 break;
887 case 0:
163f0ee5 888 if (!memcmp(dst_value, "refs/", 5))
1d735267 889 matched_dst = make_linked_ref(dst_value, dst_tail);
f8aae120
JK
890 else if((dst_guess = guess_ref(dst_value, matched_src)))
891 matched_dst = make_linked_ref(dst_guess, dst_tail);
3c8b7df1 892 else
f8aae120
JK
893 error("unable to push to unqualified destination: %s\n"
894 "The destination refspec neither matches an "
895 "existing ref on the remote nor\n"
896 "begins with refs/, and we are unable to "
897 "guess a prefix based on the source ref.",
898 dst_value);
54a8ad92
JH
899 break;
900 default:
3c8b7df1 901 matched_dst = NULL;
54a8ad92
JH
902 error("dst refspec %s matches more than one.",
903 dst_value);
904 break;
905 }
009c5bcd 906 if (errs || !matched_dst)
3c8b7df1 907 return 1;
54a8ad92
JH
908 if (matched_dst->peer_ref) {
909 errs = 1;
910 error("dst ref %s receives from more than one src.",
911 matched_dst->name);
6b62816c 912 }
54a8ad92
JH
913 else {
914 matched_dst->peer_ref = matched_src;
915 matched_dst->force = rs->force;
6b62816c 916 }
54a8ad92
JH
917 return errs;
918}
919
920static int match_explicit_refs(struct ref *src, struct ref *dst,
921 struct ref ***dst_tail, struct refspec *rs,
922 int rs_nr)
923{
924 int i, errs;
925 for (i = errs = 0; i < rs_nr; i++)
926 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
6b62816c
DB
927 return -errs;
928}
929
6e66bf3c
AR
930static const struct refspec *check_pattern_match(const struct refspec *rs,
931 int rs_nr,
932 const struct ref *src)
8558fd9e
DB
933{
934 int i;
8558fd9e 935 for (i = 0; i < rs_nr; i++) {
ef00d150
DB
936 if (rs[i].pattern &&
937 !prefixcmp(src->name, rs[i].src) &&
938 src->name[strlen(rs[i].src)] == '/')
6e66bf3c 939 return rs + i;
8558fd9e 940 }
6e66bf3c 941 return NULL;
8558fd9e
DB
942}
943
54a8ad92
JH
944/*
945 * Note. This is used only by "push"; refspec matching rules for
946 * push and fetch are subtly different, so do not try to reuse it
947 * without thinking.
948 */
6b62816c 949int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
28b9d6e5 950 int nr_refspec, const char **refspec, int flags)
6b62816c
DB
951{
952 struct refspec *rs =
46220ca1 953 parse_push_refspec(nr_refspec, (const char **) refspec);
28b9d6e5
AW
954 int send_all = flags & MATCH_REFS_ALL;
955 int send_mirror = flags & MATCH_REFS_MIRROR;
6b62816c 956
8558fd9e
DB
957 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
958 return -1;
6b62816c
DB
959
960 /* pick the remainder */
961 for ( ; src; src = src->next) {
962 struct ref *dst_peer;
6e66bf3c
AR
963 const struct refspec *pat = NULL;
964 char *dst_name;
6b62816c
DB
965 if (src->peer_ref)
966 continue;
6e66bf3c
AR
967 if (nr_refspec) {
968 pat = check_pattern_match(rs, nr_refspec, src);
969 if (!pat)
970 continue;
971 }
28b9d6e5 972 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
098e711e
JH
973 /*
974 * "matching refs"; traditionally we pushed everything
975 * including refs outside refs/heads/ hierarchy, but
976 * that does not make much sense these days.
977 */
978 continue;
8558fd9e 979
6e66bf3c 980 if (pat) {
efd8f793
DB
981 const char *dst_side = pat->dst ? pat->dst : pat->src;
982 dst_name = xmalloc(strlen(dst_side) +
6e66bf3c
AR
983 strlen(src->name) -
984 strlen(pat->src) + 2);
efd8f793 985 strcpy(dst_name, dst_side);
6e66bf3c
AR
986 strcat(dst_name, src->name + strlen(pat->src));
987 } else
aa32eedc 988 dst_name = xstrdup(src->name);
6e66bf3c 989 dst_peer = find_ref_by_name(dst, dst_name);
8558fd9e
DB
990 if (dst_peer && dst_peer->peer_ref)
991 /* We're already sending something to this ref. */
6e66bf3c 992 goto free_name;
28b9d6e5
AW
993
994 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
995 /*
996 * Remote doesn't have it, and we have no
8558fd9e 997 * explicit pattern, and we don't have
28b9d6e5
AW
998 * --all nor --mirror.
999 */
6e66bf3c 1000 goto free_name;
6b62816c
DB
1001 if (!dst_peer) {
1002 /* Create a new one and link it */
1d735267 1003 dst_peer = make_linked_ref(dst_name, dst_tail);
6b62816c 1004 hashcpy(dst_peer->new_sha1, src->new_sha1);
6b62816c
DB
1005 }
1006 dst_peer->peer_ref = src;
5eb73581
JK
1007 if (pat)
1008 dst_peer->force = pat->force;
6e66bf3c
AR
1009 free_name:
1010 free(dst_name);
6b62816c
DB
1011 }
1012 return 0;
1013}
cf818348
DB
1014
1015struct branch *branch_get(const char *name)
1016{
1017 struct branch *ret;
1018
1019 read_config();
1020 if (!name || !*name || !strcmp(name, "HEAD"))
1021 ret = current_branch;
1022 else
1023 ret = make_branch(name, 0);
1024 if (ret && ret->remote_name) {
1025 ret->remote = remote_get(ret->remote_name);
1026 if (ret->merge_nr) {
1027 int i;
1028 ret->merge = xcalloc(sizeof(*ret->merge),
1029 ret->merge_nr);
1030 for (i = 0; i < ret->merge_nr; i++) {
1031 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1032 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1033 remote_find_tracking(ret->remote,
1034 ret->merge[i]);
1035 }
1036 }
1037 }
1038 return ret;
1039}
1040
1041int branch_has_merge_config(struct branch *branch)
1042{
1043 return branch && !!branch->merge;
1044}
1045
85682c19
SP
1046int branch_merge_matches(struct branch *branch,
1047 int i,
1048 const char *refname)
cf818348 1049{
85682c19 1050 if (!branch || i < 0 || i >= branch->merge_nr)
cf818348 1051 return 0;
605b4978 1052 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
cf818348 1053}
d71ab174 1054
4577370e 1055static struct ref *get_expanded_map(const struct ref *remote_refs,
d71ab174
DB
1056 const struct refspec *refspec)
1057{
4577370e 1058 const struct ref *ref;
d71ab174
DB
1059 struct ref *ret = NULL;
1060 struct ref **tail = &ret;
1061
1062 int remote_prefix_len = strlen(refspec->src);
1063 int local_prefix_len = strlen(refspec->dst);
1064
1065 for (ref = remote_refs; ref; ref = ref->next) {
1066 if (strchr(ref->name, '^'))
1067 continue; /* a dereference item */
1068 if (!prefixcmp(ref->name, refspec->src)) {
4577370e 1069 const char *match;
d71ab174
DB
1070 struct ref *cpy = copy_ref(ref);
1071 match = ref->name + remote_prefix_len;
1072
1073 cpy->peer_ref = alloc_ref(local_prefix_len +
1074 strlen(match) + 1);
1075 sprintf(cpy->peer_ref->name, "%s%s",
1076 refspec->dst, match);
1077 if (refspec->force)
1078 cpy->peer_ref->force = 1;
1079 *tail = cpy;
1080 tail = &cpy->next;
1081 }
1082 }
1083
1084 return ret;
1085}
1086
4577370e 1087static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
d71ab174 1088{
4577370e 1089 const struct ref *ref;
d71ab174 1090 for (ref = refs; ref; ref = ref->next) {
605b4978 1091 if (refname_match(name, ref->name, ref_fetch_rules))
d71ab174
DB
1092 return ref;
1093 }
1094 return NULL;
1095}
1096
4577370e 1097struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
d71ab174 1098{
4577370e 1099 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
d71ab174
DB
1100
1101 if (!ref)
9ad7c5ae 1102 return NULL;
d71ab174
DB
1103
1104 return copy_ref(ref);
1105}
1106
1107static struct ref *get_local_ref(const char *name)
1108{
1109 struct ref *ret;
1110 if (!name)
1111 return NULL;
1112
1113 if (!prefixcmp(name, "refs/")) {
1114 ret = alloc_ref(strlen(name) + 1);
1115 strcpy(ret->name, name);
1116 return ret;
1117 }
1118
1119 if (!prefixcmp(name, "heads/") ||
1120 !prefixcmp(name, "tags/") ||
1121 !prefixcmp(name, "remotes/")) {
1122 ret = alloc_ref(strlen(name) + 6);
1123 sprintf(ret->name, "refs/%s", name);
1124 return ret;
1125 }
1126
1127 ret = alloc_ref(strlen(name) + 12);
1128 sprintf(ret->name, "refs/heads/%s", name);
1129 return ret;
1130}
1131
4577370e 1132int get_fetch_map(const struct ref *remote_refs,
d71ab174 1133 const struct refspec *refspec,
9ad7c5ae
JH
1134 struct ref ***tail,
1135 int missing_ok)
d71ab174 1136{
ef00d150 1137 struct ref *ref_map, **rmp;
d71ab174
DB
1138
1139 if (refspec->pattern) {
1140 ref_map = get_expanded_map(remote_refs, refspec);
1141 } else {
9ad7c5ae
JH
1142 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1143
1144 ref_map = get_remote_ref(remote_refs, name);
1145 if (!missing_ok && !ref_map)
1146 die("Couldn't find remote ref %s", name);
1147 if (ref_map) {
1148 ref_map->peer_ref = get_local_ref(refspec->dst);
1149 if (ref_map->peer_ref && refspec->force)
1150 ref_map->peer_ref->force = 1;
1151 }
d71ab174
DB
1152 }
1153
ef00d150
DB
1154 for (rmp = &ref_map; *rmp; ) {
1155 if ((*rmp)->peer_ref) {
1156 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1157 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1158 struct ref *ignore = *rmp;
1159 error("* Ignoring funny ref '%s' locally",
1160 (*rmp)->peer_ref->name);
1161 *rmp = (*rmp)->next;
1162 free(ignore->peer_ref);
1163 free(ignore);
1164 continue;
1165 }
1166 }
1167 rmp = &((*rmp)->next);
d71ab174
DB
1168 }
1169
8f70a765
AR
1170 if (ref_map)
1171 tail_link_ref(ref_map, tail);
d71ab174
DB
1172
1173 return 0;
1174}