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