]> git.ipfire.org Git - thirdparty/git.git/blame - remote.c
log --graph --left-right: show left/right information in place of '*'
[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);
84bb2dfd
PB
340 if (!strcmp(subkey, ".mirror"))
341 remote->mirror = git_config_bool(key, value);
342 else if (!strcmp(subkey, ".skipdefaultupdate"))
343 remote->skip_default_update = git_config_bool(key, value);
344
345 else if (!strcmp(subkey, ".url")) {
346 const char *v;
347 if (git_config_string(&v, key, value))
348 return -1;
349 add_url(remote, v);
5751f490 350 } else if (!strcmp(subkey, ".push")) {
84bb2dfd
PB
351 const char *v;
352 if (git_config_string(&v, key, value))
353 return -1;
354 add_push_refspec(remote, v);
5d46c9d4 355 } else if (!strcmp(subkey, ".fetch")) {
84bb2dfd
PB
356 const char *v;
357 if (git_config_string(&v, key, value))
358 return -1;
359 add_fetch_refspec(remote, v);
5751f490 360 } else if (!strcmp(subkey, ".receivepack")) {
84bb2dfd
PB
361 const char *v;
362 if (git_config_string(&v, key, value))
363 return -1;
5751f490 364 if (!remote->receivepack)
84bb2dfd 365 remote->receivepack = v;
5751f490
DB
366 else
367 error("more than one receivepack given, using the first");
0012ba21 368 } else if (!strcmp(subkey, ".uploadpack")) {
84bb2dfd
PB
369 const char *v;
370 if (git_config_string(&v, key, value))
371 return -1;
0012ba21 372 if (!remote->uploadpack)
84bb2dfd 373 remote->uploadpack = v;
0012ba21
DB
374 else
375 error("more than one uploadpack given, using the first");
d71ab174
DB
376 } else if (!strcmp(subkey, ".tagopt")) {
377 if (!strcmp(value, "--no-tags"))
378 remote->fetch_tags = -1;
14c98218 379 } else if (!strcmp(subkey, ".proxy")) {
84bb2dfd
PB
380 return git_config_string((const char **)&remote->http_proxy,
381 key, value);
382 }
5751f490
DB
383 return 0;
384}
385
55029ae4
DB
386static void alias_all_urls(void)
387{
388 int i, j;
389 for (i = 0; i < remotes_nr; i++) {
390 if (!remotes[i])
391 continue;
392 for (j = 0; j < remotes[i]->url_nr; j++) {
393 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
394 }
395 }
396}
397
5751f490
DB
398static void read_config(void)
399{
400 unsigned char sha1[20];
401 const char *head_ref;
402 int flag;
403 if (default_remote_name) // did this already
404 return;
405 default_remote_name = xstrdup("origin");
406 current_branch = NULL;
407 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
408 if (head_ref && (flag & REF_ISSYMREF) &&
409 !prefixcmp(head_ref, "refs/heads/")) {
cf818348
DB
410 current_branch =
411 make_branch(head_ref + strlen("refs/heads/"), 0);
5751f490
DB
412 }
413 git_config(handle_config);
55029ae4 414 alias_all_urls();
5751f490
DB
415}
416
24b6177e 417static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
6b62816c
DB
418{
419 int i;
ef00d150 420 int st;
6b62816c 421 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
46220ca1 422
6b62816c 423 for (i = 0; i < nr_refspec; i++) {
46220ca1
JH
424 size_t llen, rlen;
425 int is_glob;
426 const char *lhs, *rhs;
427
428 llen = rlen = is_glob = 0;
429
430 lhs = refspec[i];
431 if (*lhs == '+') {
6b62816c 432 rs[i].force = 1;
46220ca1 433 lhs++;
6b62816c 434 }
46220ca1
JH
435
436 rhs = strrchr(lhs, ':');
a83619d6
PB
437
438 /*
439 * Before going on, special case ":" (or "+:") as a refspec
440 * for matching refs.
441 */
442 if (!fetch && rhs == lhs && rhs[1] == '\0') {
443 rs[i].matching = 1;
444 continue;
445 }
446
46220ca1
JH
447 if (rhs) {
448 rhs++;
449 rlen = strlen(rhs);
450 is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
7d19da46
JH
451 if (is_glob)
452 rlen -= 2;
453 rs[i].dst = xstrndup(rhs, rlen);
6b62816c 454 }
ef00d150 455
46220ca1 456 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
7d19da46
JH
457 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
458 if ((rhs && !is_glob) || (!rhs && fetch))
459 goto invalid;
460 is_glob = 1;
46220ca1 461 llen -= 2;
7d19da46
JH
462 } else if (rhs && is_glob) {
463 goto invalid;
ef00d150 464 }
7d19da46 465
46220ca1
JH
466 rs[i].pattern = is_glob;
467 rs[i].src = xstrndup(lhs, llen);
468
469 if (fetch) {
470 /*
471 * LHS
472 * - empty is allowed; it means HEAD.
473 * - otherwise it must be a valid looking ref.
474 */
475 if (!*rs[i].src)
476 ; /* empty is ok */
477 else {
478 st = check_ref_format(rs[i].src);
479 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
480 goto invalid;
481 }
482 /*
483 * RHS
7d19da46 484 * - missing is ok, and is same as empty.
46220ca1
JH
485 * - empty is ok; it means not to store.
486 * - otherwise it must be a valid looking ref.
487 */
488 if (!rs[i].dst) {
489 ; /* ok */
490 } else if (!*rs[i].dst) {
491 ; /* ok */
492 } else {
493 st = check_ref_format(rs[i].dst);
494 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
495 goto invalid;
496 }
497 } else {
498 /*
499 * LHS
500 * - empty is allowed; it means delete.
501 * - when wildcarded, it must be a valid looking ref.
502 * - otherwise, it must be an extended SHA-1, but
503 * there is no existing way to validate this.
504 */
505 if (!*rs[i].src)
506 ; /* empty is ok */
507 else if (is_glob) {
508 st = check_ref_format(rs[i].src);
509 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
510 goto invalid;
511 }
512 else
513 ; /* anything goes, for now */
514 /*
515 * RHS
516 * - missing is allowed, but LHS then must be a
517 * valid looking ref.
518 * - empty is not allowed.
519 * - otherwise it must be a valid looking ref.
520 */
521 if (!rs[i].dst) {
522 st = check_ref_format(rs[i].src);
523 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
524 goto invalid;
525 } else if (!*rs[i].dst) {
526 goto invalid;
527 } else {
528 st = check_ref_format(rs[i].dst);
529 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
530 goto invalid;
531 }
ef00d150 532 }
6b62816c
DB
533 }
534 return rs;
46220ca1
JH
535
536 invalid:
24b6177e
JF
537 if (verify) {
538 free(rs);
539 return NULL;
540 }
46220ca1
JH
541 die("Invalid refspec '%s'", refspec[i]);
542}
543
24b6177e
JF
544int valid_fetch_refspec(const char *fetch_refspec_str)
545{
546 const char *fetch_refspec[] = { fetch_refspec_str };
547 struct refspec *refspec;
548
549 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
550 if (refspec)
551 free(refspec);
552 return !!refspec;
553}
554
46220ca1
JH
555struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
556{
24b6177e 557 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
46220ca1
JH
558}
559
560struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
561{
24b6177e 562 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
6b62816c
DB
563}
564
df93e33c
DB
565static int valid_remote_nick(const char *name)
566{
567 if (!name[0] || /* not empty */
568 (name[0] == '.' && /* not "." */
569 (!name[1] || /* not ".." */
570 (name[1] == '.' && !name[2]))))
571 return 0;
572 return !strchr(name, '/'); /* no slash */
573}
574
5751f490
DB
575struct remote *remote_get(const char *name)
576{
577 struct remote *ret;
578
579 read_config();
580 if (!name)
581 name = default_remote_name;
582 ret = make_remote(name, 0);
df93e33c 583 if (valid_remote_nick(name)) {
28b91f8a 584 if (!ret->url)
5751f490 585 read_remotes_file(ret);
28b91f8a 586 if (!ret->url)
5751f490
DB
587 read_branches_file(ret);
588 }
28b91f8a 589 if (!ret->url)
55029ae4 590 add_url_alias(ret, name);
28b91f8a 591 if (!ret->url)
5751f490 592 return NULL;
46220ca1
JH
593 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
594 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
5751f490
DB
595 return ret;
596}
6b62816c 597
b42f6927
JS
598int for_each_remote(each_remote_fn fn, void *priv)
599{
600 int i, result = 0;
601 read_config();
2d31347b 602 for (i = 0; i < remotes_nr && !result; i++) {
b42f6927
JS
603 struct remote *r = remotes[i];
604 if (!r)
605 continue;
606 if (!r->fetch)
46220ca1
JH
607 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
608 r->fetch_refspec);
b42f6927 609 if (!r->push)
46220ca1
JH
610 r->push = parse_push_refspec(r->push_refspec_nr,
611 r->push_refspec);
b42f6927
JS
612 result = fn(r, priv);
613 }
614 return result;
615}
616
2467a4fa
DB
617void ref_remove_duplicates(struct ref *ref_map)
618{
619 struct ref **posn;
620 struct ref *next;
621 for (; ref_map; ref_map = ref_map->next) {
622 if (!ref_map->peer_ref)
623 continue;
624 posn = &ref_map->next;
625 while (*posn) {
626 if ((*posn)->peer_ref &&
627 !strcmp((*posn)->peer_ref->name,
628 ref_map->peer_ref->name)) {
629 if (strcmp((*posn)->name, ref_map->name))
630 die("%s tracks both %s and %s",
631 ref_map->peer_ref->name,
632 (*posn)->name, ref_map->name);
633 next = (*posn)->next;
634 free((*posn)->peer_ref);
635 free(*posn);
636 *posn = next;
637 } else {
638 posn = &(*posn)->next;
639 }
640 }
641 }
642}
643
28b91f8a 644int remote_has_url(struct remote *remote, const char *url)
5d46c9d4
DB
645{
646 int i;
28b91f8a
SP
647 for (i = 0; i < remote->url_nr; i++) {
648 if (!strcmp(remote->url[i], url))
5d46c9d4
DB
649 return 1;
650 }
651 return 0;
652}
653
654int remote_find_tracking(struct remote *remote, struct refspec *refspec)
655{
b42f6927
JS
656 int find_src = refspec->src == NULL;
657 char *needle, **result;
5d46c9d4 658 int i;
b42f6927
JS
659
660 if (find_src) {
009c5bcd 661 if (!refspec->dst)
b42f6927
JS
662 return error("find_tracking: need either src or dst");
663 needle = refspec->dst;
664 result = &refspec->src;
665 } else {
666 needle = refspec->src;
667 result = &refspec->dst;
668 }
669
5d46c9d4
DB
670 for (i = 0; i < remote->fetch_refspec_nr; i++) {
671 struct refspec *fetch = &remote->fetch[i];
b42f6927
JS
672 const char *key = find_src ? fetch->dst : fetch->src;
673 const char *value = find_src ? fetch->src : fetch->dst;
5d46c9d4
DB
674 if (!fetch->dst)
675 continue;
676 if (fetch->pattern) {
ef00d150
DB
677 if (!prefixcmp(needle, key) &&
678 needle[strlen(key)] == '/') {
b42f6927
JS
679 *result = xmalloc(strlen(value) +
680 strlen(needle) -
681 strlen(key) + 1);
682 strcpy(*result, value);
683 strcpy(*result + strlen(value),
684 needle + strlen(key));
5d46c9d4
DB
685 refspec->force = fetch->force;
686 return 0;
687 }
b42f6927
JS
688 } else if (!strcmp(needle, key)) {
689 *result = xstrdup(value);
690 refspec->force = fetch->force;
691 return 0;
5d46c9d4
DB
692 }
693 }
5d46c9d4
DB
694 return -1;
695}
696
dfd255dd
DB
697struct ref *alloc_ref(unsigned namelen)
698{
699 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
700 memset(ret, 0, sizeof(struct ref) + namelen);
701 return ret;
702}
703
737922aa
KK
704struct ref *alloc_ref_from_str(const char* str)
705{
706 struct ref *ret = alloc_ref(strlen(str) + 1);
707 strcpy(ret->name, str);
708 return ret;
709}
710
4577370e 711static struct ref *copy_ref(const struct ref *ref)
d71ab174
DB
712{
713 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
714 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
715 ret->next = NULL;
716 return ret;
717}
718
4577370e
DB
719struct ref *copy_ref_list(const struct ref *ref)
720{
721 struct ref *ret = NULL;
722 struct ref **tail = &ret;
723 while (ref) {
724 *tail = copy_ref(ref);
725 ref = ref->next;
726 tail = &((*tail)->next);
727 }
728 return ret;
729}
730
be885d96
DB
731void free_ref(struct ref *ref)
732{
733 if (!ref)
734 return;
735 free(ref->remote_status);
736 free(ref->symref);
737 free(ref);
738}
739
dfd255dd
DB
740void free_refs(struct ref *ref)
741{
742 struct ref *next;
743 while (ref) {
744 next = ref->next;
8e0f7003 745 free(ref->peer_ref);
be885d96 746 free_ref(ref);
dfd255dd
DB
747 ref = next;
748 }
749}
750
6b62816c
DB
751static int count_refspec_match(const char *pattern,
752 struct ref *refs,
753 struct ref **matched_ref)
754{
755 int patlen = strlen(pattern);
756 struct ref *matched_weak = NULL;
757 struct ref *matched = NULL;
758 int weak_match = 0;
759 int match = 0;
760
761 for (weak_match = match = 0; refs; refs = refs->next) {
762 char *name = refs->name;
763 int namelen = strlen(name);
6b62816c 764
ae36bdcf 765 if (!refname_match(pattern, name, ref_rev_parse_rules))
6b62816c
DB
766 continue;
767
768 /* A match is "weak" if it is with refs outside
769 * heads or tags, and did not specify the pattern
770 * in full (e.g. "refs/remotes/origin/master") or at
771 * least from the toplevel (e.g. "remotes/origin/master");
772 * otherwise "git push $URL master" would result in
773 * ambiguity between remotes/origin/master and heads/master
774 * at the remote site.
775 */
776 if (namelen != patlen &&
777 patlen != namelen - 5 &&
778 prefixcmp(name, "refs/heads/") &&
779 prefixcmp(name, "refs/tags/")) {
780 /* We want to catch the case where only weak
781 * matches are found and there are multiple
782 * matches, and where more than one strong
783 * matches are found, as ambiguous. One
784 * strong match with zero or more weak matches
785 * are acceptable as a unique match.
786 */
787 matched_weak = refs;
788 weak_match++;
789 }
790 else {
791 matched = refs;
792 match++;
793 }
794 }
795 if (!matched) {
796 *matched_ref = matched_weak;
797 return weak_match;
798 }
799 else {
800 *matched_ref = matched;
801 return match;
802 }
803}
804
1d735267 805static void tail_link_ref(struct ref *ref, struct ref ***tail)
6b62816c
DB
806{
807 **tail = ref;
1d735267
DB
808 while (ref->next)
809 ref = ref->next;
6b62816c 810 *tail = &ref->next;
6b62816c
DB
811}
812
813static struct ref *try_explicit_object_name(const char *name)
814{
815 unsigned char sha1[20];
816 struct ref *ref;
6b62816c
DB
817
818 if (!*name) {
dfd255dd 819 ref = alloc_ref(20);
6b62816c
DB
820 strcpy(ref->name, "(delete)");
821 hashclr(ref->new_sha1);
822 return ref;
823 }
824 if (get_sha1(name, sha1))
825 return NULL;
737922aa 826 ref = alloc_ref_from_str(name);
6b62816c
DB
827 hashcpy(ref->new_sha1, sha1);
828 return ref;
829}
830
1d735267 831static struct ref *make_linked_ref(const char *name, struct ref ***tail)
6b62816c 832{
737922aa 833 struct ref *ret = alloc_ref_from_str(name);
1d735267
DB
834 tail_link_ref(ret, tail);
835 return ret;
163f0ee5 836}
8558fd9e 837
f8aae120
JK
838static char *guess_ref(const char *name, struct ref *peer)
839{
840 struct strbuf buf = STRBUF_INIT;
841 unsigned char sha1[20];
842
843 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
844 if (!r)
845 return NULL;
846
847 if (!prefixcmp(r, "refs/heads/"))
848 strbuf_addstr(&buf, "refs/heads/");
849 else if (!prefixcmp(r, "refs/tags/"))
850 strbuf_addstr(&buf, "refs/tags/");
851 else
852 return NULL;
853
854 strbuf_addstr(&buf, name);
855 return strbuf_detach(&buf, NULL);
856}
857
54a8ad92
JH
858static int match_explicit(struct ref *src, struct ref *dst,
859 struct ref ***dst_tail,
860 struct refspec *rs,
861 int errs)
6b62816c 862{
54a8ad92 863 struct ref *matched_src, *matched_dst;
8558fd9e 864
54a8ad92 865 const char *dst_value = rs->dst;
f8aae120 866 char *dst_guess;
6b62816c 867
a83619d6 868 if (rs->pattern || rs->matching)
54a8ad92 869 return errs;
8558fd9e 870
54a8ad92
JH
871 matched_src = matched_dst = NULL;
872 switch (count_refspec_match(rs->src, src, &matched_src)) {
873 case 1:
874 break;
875 case 0:
876 /* The source could be in the get_sha1() format
877 * not a reference name. :refs/other is a
878 * way to delete 'other' ref at the remote end.
879 */
880 matched_src = try_explicit_object_name(rs->src);
7dfee372
SP
881 if (!matched_src)
882 error("src refspec %s does not match any.", rs->src);
54a8ad92
JH
883 break;
884 default:
3c8b7df1 885 matched_src = NULL;
7dfee372 886 error("src refspec %s matches more than one.", rs->src);
54a8ad92
JH
887 break;
888 }
3c8b7df1
JH
889
890 if (!matched_src)
891 errs = 1;
892
4491e62a 893 if (!dst_value) {
9f0ea7e8
DB
894 unsigned char sha1[20];
895 int flag;
896
4491e62a
SP
897 if (!matched_src)
898 return errs;
9f0ea7e8
DB
899 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
900 if (!dst_value ||
901 ((flag & REF_ISSYMREF) &&
902 prefixcmp(dst_value, "refs/heads/")))
903 die("%s cannot be resolved to branch.",
904 matched_src->name);
4491e62a 905 }
3c8b7df1 906
54a8ad92
JH
907 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
908 case 1:
909 break;
910 case 0:
163f0ee5 911 if (!memcmp(dst_value, "refs/", 5))
1d735267 912 matched_dst = make_linked_ref(dst_value, dst_tail);
f8aae120
JK
913 else if((dst_guess = guess_ref(dst_value, matched_src)))
914 matched_dst = make_linked_ref(dst_guess, dst_tail);
3c8b7df1 915 else
f8aae120
JK
916 error("unable to push to unqualified destination: %s\n"
917 "The destination refspec neither matches an "
918 "existing ref on the remote nor\n"
919 "begins with refs/, and we are unable to "
920 "guess a prefix based on the source ref.",
921 dst_value);
54a8ad92
JH
922 break;
923 default:
3c8b7df1 924 matched_dst = NULL;
54a8ad92
JH
925 error("dst refspec %s matches more than one.",
926 dst_value);
927 break;
928 }
009c5bcd 929 if (errs || !matched_dst)
3c8b7df1 930 return 1;
54a8ad92
JH
931 if (matched_dst->peer_ref) {
932 errs = 1;
933 error("dst ref %s receives from more than one src.",
934 matched_dst->name);
6b62816c 935 }
54a8ad92
JH
936 else {
937 matched_dst->peer_ref = matched_src;
938 matched_dst->force = rs->force;
6b62816c 939 }
54a8ad92
JH
940 return errs;
941}
942
943static int match_explicit_refs(struct ref *src, struct ref *dst,
944 struct ref ***dst_tail, struct refspec *rs,
945 int rs_nr)
946{
947 int i, errs;
948 for (i = errs = 0; i < rs_nr; i++)
949 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
6b62816c
DB
950 return -errs;
951}
952
6e66bf3c
AR
953static const struct refspec *check_pattern_match(const struct refspec *rs,
954 int rs_nr,
955 const struct ref *src)
8558fd9e
DB
956{
957 int i;
a83619d6 958 int matching_refs = -1;
8558fd9e 959 for (i = 0; i < rs_nr; i++) {
a83619d6
PB
960 if (rs[i].matching &&
961 (matching_refs == -1 || rs[i].force)) {
962 matching_refs = i;
963 continue;
964 }
965
ef00d150
DB
966 if (rs[i].pattern &&
967 !prefixcmp(src->name, rs[i].src) &&
968 src->name[strlen(rs[i].src)] == '/')
6e66bf3c 969 return rs + i;
8558fd9e 970 }
a83619d6
PB
971 if (matching_refs != -1)
972 return rs + matching_refs;
973 else
974 return NULL;
8558fd9e
DB
975}
976
54a8ad92
JH
977/*
978 * Note. This is used only by "push"; refspec matching rules for
979 * push and fetch are subtly different, so do not try to reuse it
980 * without thinking.
981 */
6b62816c 982int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
28b9d6e5 983 int nr_refspec, const char **refspec, int flags)
6b62816c 984{
a83619d6 985 struct refspec *rs;
28b9d6e5
AW
986 int send_all = flags & MATCH_REFS_ALL;
987 int send_mirror = flags & MATCH_REFS_MIRROR;
a83619d6 988 static const char *default_refspec[] = { ":", 0 };
6b62816c 989
a83619d6
PB
990 if (!nr_refspec) {
991 nr_refspec = 1;
992 refspec = default_refspec;
993 }
994 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
8558fd9e
DB
995 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
996 return -1;
6b62816c
DB
997
998 /* pick the remainder */
999 for ( ; src; src = src->next) {
1000 struct ref *dst_peer;
6e66bf3c
AR
1001 const struct refspec *pat = NULL;
1002 char *dst_name;
6b62816c
DB
1003 if (src->peer_ref)
1004 continue;
a83619d6
PB
1005
1006 pat = check_pattern_match(rs, nr_refspec, src);
1007 if (!pat)
1008 continue;
1009
1010 if (pat->matching) {
098e711e
JH
1011 /*
1012 * "matching refs"; traditionally we pushed everything
1013 * including refs outside refs/heads/ hierarchy, but
1014 * that does not make much sense these days.
1015 */
a83619d6
PB
1016 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1017 continue;
1018 dst_name = xstrdup(src->name);
8558fd9e 1019
a83619d6 1020 } else {
efd8f793
DB
1021 const char *dst_side = pat->dst ? pat->dst : pat->src;
1022 dst_name = xmalloc(strlen(dst_side) +
6e66bf3c
AR
1023 strlen(src->name) -
1024 strlen(pat->src) + 2);
efd8f793 1025 strcpy(dst_name, dst_side);
6e66bf3c 1026 strcat(dst_name, src->name + strlen(pat->src));
a83619d6 1027 }
6e66bf3c 1028 dst_peer = find_ref_by_name(dst, dst_name);
a83619d6
PB
1029 if (dst_peer) {
1030 if (dst_peer->peer_ref)
1031 /* We're already sending something to this ref. */
1032 goto free_name;
1033
1034 } else {
1035 if (pat->matching && !(send_all || send_mirror))
1036 /*
1037 * Remote doesn't have it, and we have no
1038 * explicit pattern, and we don't have
1039 * --all nor --mirror.
1040 */
1041 goto free_name;
28b9d6e5 1042
6b62816c 1043 /* Create a new one and link it */
1d735267 1044 dst_peer = make_linked_ref(dst_name, dst_tail);
6b62816c 1045 hashcpy(dst_peer->new_sha1, src->new_sha1);
6b62816c
DB
1046 }
1047 dst_peer->peer_ref = src;
a83619d6 1048 dst_peer->force = pat->force;
6e66bf3c
AR
1049 free_name:
1050 free(dst_name);
6b62816c
DB
1051 }
1052 return 0;
1053}
cf818348
DB
1054
1055struct branch *branch_get(const char *name)
1056{
1057 struct branch *ret;
1058
1059 read_config();
1060 if (!name || !*name || !strcmp(name, "HEAD"))
1061 ret = current_branch;
1062 else
1063 ret = make_branch(name, 0);
1064 if (ret && ret->remote_name) {
1065 ret->remote = remote_get(ret->remote_name);
1066 if (ret->merge_nr) {
1067 int i;
1068 ret->merge = xcalloc(sizeof(*ret->merge),
1069 ret->merge_nr);
1070 for (i = 0; i < ret->merge_nr; i++) {
1071 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1072 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1073 remote_find_tracking(ret->remote,
1074 ret->merge[i]);
1075 }
1076 }
1077 }
1078 return ret;
1079}
1080
1081int branch_has_merge_config(struct branch *branch)
1082{
1083 return branch && !!branch->merge;
1084}
1085
85682c19
SP
1086int branch_merge_matches(struct branch *branch,
1087 int i,
1088 const char *refname)
cf818348 1089{
85682c19 1090 if (!branch || i < 0 || i >= branch->merge_nr)
cf818348 1091 return 0;
605b4978 1092 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
cf818348 1093}
d71ab174 1094
4577370e 1095static struct ref *get_expanded_map(const struct ref *remote_refs,
d71ab174
DB
1096 const struct refspec *refspec)
1097{
4577370e 1098 const struct ref *ref;
d71ab174
DB
1099 struct ref *ret = NULL;
1100 struct ref **tail = &ret;
1101
1102 int remote_prefix_len = strlen(refspec->src);
1103 int local_prefix_len = strlen(refspec->dst);
1104
1105 for (ref = remote_refs; ref; ref = ref->next) {
1106 if (strchr(ref->name, '^'))
1107 continue; /* a dereference item */
1108 if (!prefixcmp(ref->name, refspec->src)) {
4577370e 1109 const char *match;
d71ab174
DB
1110 struct ref *cpy = copy_ref(ref);
1111 match = ref->name + remote_prefix_len;
1112
1113 cpy->peer_ref = alloc_ref(local_prefix_len +
1114 strlen(match) + 1);
1115 sprintf(cpy->peer_ref->name, "%s%s",
1116 refspec->dst, match);
1117 if (refspec->force)
1118 cpy->peer_ref->force = 1;
1119 *tail = cpy;
1120 tail = &cpy->next;
1121 }
1122 }
1123
1124 return ret;
1125}
1126
4577370e 1127static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
d71ab174 1128{
4577370e 1129 const struct ref *ref;
d71ab174 1130 for (ref = refs; ref; ref = ref->next) {
605b4978 1131 if (refname_match(name, ref->name, ref_fetch_rules))
d71ab174
DB
1132 return ref;
1133 }
1134 return NULL;
1135}
1136
4577370e 1137struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
d71ab174 1138{
4577370e 1139 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
d71ab174
DB
1140
1141 if (!ref)
9ad7c5ae 1142 return NULL;
d71ab174
DB
1143
1144 return copy_ref(ref);
1145}
1146
1147static struct ref *get_local_ref(const char *name)
1148{
1149 struct ref *ret;
1150 if (!name)
1151 return NULL;
1152
1153 if (!prefixcmp(name, "refs/")) {
737922aa 1154 return alloc_ref_from_str(name);
d71ab174
DB
1155 }
1156
1157 if (!prefixcmp(name, "heads/") ||
1158 !prefixcmp(name, "tags/") ||
1159 !prefixcmp(name, "remotes/")) {
1160 ret = alloc_ref(strlen(name) + 6);
1161 sprintf(ret->name, "refs/%s", name);
1162 return ret;
1163 }
1164
1165 ret = alloc_ref(strlen(name) + 12);
1166 sprintf(ret->name, "refs/heads/%s", name);
1167 return ret;
1168}
1169
4577370e 1170int get_fetch_map(const struct ref *remote_refs,
d71ab174 1171 const struct refspec *refspec,
9ad7c5ae
JH
1172 struct ref ***tail,
1173 int missing_ok)
d71ab174 1174{
ef00d150 1175 struct ref *ref_map, **rmp;
d71ab174
DB
1176
1177 if (refspec->pattern) {
1178 ref_map = get_expanded_map(remote_refs, refspec);
1179 } else {
9ad7c5ae
JH
1180 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1181
1182 ref_map = get_remote_ref(remote_refs, name);
1183 if (!missing_ok && !ref_map)
1184 die("Couldn't find remote ref %s", name);
1185 if (ref_map) {
1186 ref_map->peer_ref = get_local_ref(refspec->dst);
1187 if (ref_map->peer_ref && refspec->force)
1188 ref_map->peer_ref->force = 1;
1189 }
d71ab174
DB
1190 }
1191
ef00d150
DB
1192 for (rmp = &ref_map; *rmp; ) {
1193 if ((*rmp)->peer_ref) {
1194 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1195 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1196 struct ref *ignore = *rmp;
1197 error("* Ignoring funny ref '%s' locally",
1198 (*rmp)->peer_ref->name);
1199 *rmp = (*rmp)->next;
1200 free(ignore->peer_ref);
1201 free(ignore);
1202 continue;
1203 }
1204 }
1205 rmp = &((*rmp)->next);
d71ab174
DB
1206 }
1207
8f70a765
AR
1208 if (ref_map)
1209 tail_link_ref(ref_map, tail);
d71ab174
DB
1210
1211 return 0;
1212}
be885d96
DB
1213
1214int resolve_remote_symref(struct ref *ref, struct ref *list)
1215{
1216 if (!ref->symref)
1217 return 0;
1218 for (; list; list = list->next)
1219 if (!strcmp(ref->symref, list->name)) {
1220 hashcpy(ref->old_sha1, list->old_sha1);
1221 return 0;
1222 }
1223 return 1;
1224}