]> git.ipfire.org Git - thirdparty/git.git/blame - remote.c
Merge branch 'maint'
[thirdparty/git.git] / remote.c
CommitLineData
5751f490
DB
1#include "cache.h"
2#include "remote.h"
3#include "refs.h"
6d21bf96
JH
4#include "commit.h"
5#include "diff.h"
6#include "revision.h"
8ca12c0d 7#include "dir.h"
ec8452d5 8#include "tag.h"
5751f490 9
e0aaa29f
DB
10static struct refspec s_tag_refspec = {
11 0,
12 1,
b84c343c 13 0,
08fbdb30
DB
14 "refs/tags/*",
15 "refs/tags/*"
e0aaa29f
DB
16};
17
18const struct refspec *tag_refspec = &s_tag_refspec;
19
844112ca
JH
20struct counted_string {
21 size_t len;
22 const char *s;
23};
55029ae4
DB
24struct rewrite {
25 const char *base;
844112ca
JH
26 size_t baselen;
27 struct counted_string *instead_of;
55029ae4
DB
28 int instead_of_nr;
29 int instead_of_alloc;
30};
31
5751f490 32static struct remote **remotes;
2d31347b
DB
33static int remotes_alloc;
34static int remotes_nr;
5751f490 35
cf818348 36static struct branch **branches;
2d31347b
DB
37static int branches_alloc;
38static int branches_nr;
cf818348
DB
39
40static struct branch *current_branch;
41static const char *default_remote_name;
fa685bdf 42static int explicit_default_remote_name;
cf818348 43
55029ae4
DB
44static struct rewrite **rewrite;
45static int rewrite_alloc;
46static int rewrite_nr;
47
5751f490
DB
48#define BUF_SIZE (2048)
49static char buffer[BUF_SIZE];
50
55029ae4
DB
51static const char *alias_url(const char *url)
52{
53 int i, j;
844112ca
JH
54 char *ret;
55 struct counted_string *longest;
56 int longest_i;
57
58 longest = NULL;
59 longest_i = -1;
55029ae4
DB
60 for (i = 0; i < rewrite_nr; i++) {
61 if (!rewrite[i])
62 continue;
63 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
844112ca
JH
64 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
65 (!longest ||
66 longest->len < rewrite[i]->instead_of[j].len)) {
67 longest = &(rewrite[i]->instead_of[j]);
68 longest_i = i;
55029ae4
DB
69 }
70 }
71 }
844112ca
JH
72 if (!longest)
73 return url;
74
e8eec71d 75 ret = xmalloc(rewrite[longest_i]->baselen +
844112ca
JH
76 (strlen(url) - longest->len) + 1);
77 strcpy(ret, rewrite[longest_i]->base);
78 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
79 return ret;
55029ae4
DB
80}
81
5751f490
DB
82static void add_push_refspec(struct remote *remote, const char *ref)
83{
2d31347b
DB
84 ALLOC_GROW(remote->push_refspec,
85 remote->push_refspec_nr + 1,
86 remote->push_refspec_alloc);
87 remote->push_refspec[remote->push_refspec_nr++] = ref;
5751f490
DB
88}
89
5d46c9d4
DB
90static void add_fetch_refspec(struct remote *remote, const char *ref)
91{
2d31347b
DB
92 ALLOC_GROW(remote->fetch_refspec,
93 remote->fetch_refspec_nr + 1,
94 remote->fetch_refspec_alloc);
95 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
5d46c9d4
DB
96}
97
28b91f8a 98static void add_url(struct remote *remote, const char *url)
5751f490 99{
2d31347b
DB
100 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
101 remote->url[remote->url_nr++] = url;
5751f490
DB
102}
103
55029ae4
DB
104static void add_url_alias(struct remote *remote, const char *url)
105{
106 add_url(remote, alias_url(url));
107}
108
20346234
MG
109static void add_pushurl(struct remote *remote, const char *pushurl)
110{
111 ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
112 remote->pushurl[remote->pushurl_nr++] = pushurl;
113}
114
5751f490
DB
115static struct remote *make_remote(const char *name, int len)
116{
2d31347b
DB
117 struct remote *ret;
118 int i;
5751f490 119
2d31347b
DB
120 for (i = 0; i < remotes_nr; i++) {
121 if (len ? (!strncmp(name, remotes[i]->name, len) &&
122 !remotes[i]->name[len]) :
123 !strcmp(name, remotes[i]->name))
124 return remotes[i];
5751f490
DB
125 }
126
2d31347b
DB
127 ret = xcalloc(1, sizeof(struct remote));
128 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
129 remotes[remotes_nr++] = ret;
5751f490 130 if (len)
2d31347b 131 ret->name = xstrndup(name, len);
5751f490 132 else
2d31347b
DB
133 ret->name = xstrdup(name);
134 return ret;
5751f490
DB
135}
136
cf818348
DB
137static void add_merge(struct branch *branch, const char *name)
138{
2d31347b
DB
139 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
140 branch->merge_alloc);
141 branch->merge_name[branch->merge_nr++] = name;
cf818348
DB
142}
143
144static struct branch *make_branch(const char *name, int len)
145{
2d31347b
DB
146 struct branch *ret;
147 int i;
cf818348
DB
148 char *refname;
149
2d31347b
DB
150 for (i = 0; i < branches_nr; i++) {
151 if (len ? (!strncmp(name, branches[i]->name, len) &&
152 !branches[i]->name[len]) :
153 !strcmp(name, branches[i]->name))
154 return branches[i];
cf818348
DB
155 }
156
2d31347b
DB
157 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
158 ret = xcalloc(1, sizeof(struct branch));
159 branches[branches_nr++] = ret;
cf818348 160 if (len)
2d31347b 161 ret->name = xstrndup(name, len);
cf818348 162 else
2d31347b 163 ret->name = xstrdup(name);
e8eec71d 164 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
cf818348 165 strcpy(refname, "refs/heads/");
2d31347b
DB
166 strcpy(refname + strlen("refs/heads/"), ret->name);
167 ret->refname = refname;
cf818348 168
2d31347b 169 return ret;
cf818348
DB
170}
171
55029ae4
DB
172static struct rewrite *make_rewrite(const char *base, int len)
173{
174 struct rewrite *ret;
175 int i;
176
177 for (i = 0; i < rewrite_nr; i++) {
844112ca
JH
178 if (len
179 ? (len == rewrite[i]->baselen &&
180 !strncmp(base, rewrite[i]->base, len))
181 : !strcmp(base, rewrite[i]->base))
55029ae4
DB
182 return rewrite[i];
183 }
184
185 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
186 ret = xcalloc(1, sizeof(struct rewrite));
187 rewrite[rewrite_nr++] = ret;
844112ca 188 if (len) {
55029ae4 189 ret->base = xstrndup(base, len);
844112ca
JH
190 ret->baselen = len;
191 }
192 else {
55029ae4 193 ret->base = xstrdup(base);
844112ca
JH
194 ret->baselen = strlen(base);
195 }
55029ae4
DB
196 return ret;
197}
198
199static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
200{
201 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
844112ca
JH
202 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
203 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
204 rewrite->instead_of_nr++;
55029ae4
DB
205}
206
5751f490
DB
207static void read_remotes_file(struct remote *remote)
208{
209 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
210
211 if (!f)
212 return;
89cf4c70 213 remote->origin = REMOTE_REMOTES;
5751f490
DB
214 while (fgets(buffer, BUF_SIZE, f)) {
215 int value_list;
216 char *s, *p;
217
218 if (!prefixcmp(buffer, "URL:")) {
219 value_list = 0;
220 s = buffer + 4;
221 } else if (!prefixcmp(buffer, "Push:")) {
222 value_list = 1;
223 s = buffer + 5;
5d46c9d4
DB
224 } else if (!prefixcmp(buffer, "Pull:")) {
225 value_list = 2;
226 s = buffer + 5;
5751f490
DB
227 } else
228 continue;
229
230 while (isspace(*s))
231 s++;
232 if (!*s)
233 continue;
234
235 p = s + strlen(s);
236 while (isspace(p[-1]))
237 *--p = 0;
238
239 switch (value_list) {
240 case 0:
55029ae4 241 add_url_alias(remote, xstrdup(s));
5751f490
DB
242 break;
243 case 1:
244 add_push_refspec(remote, xstrdup(s));
245 break;
5d46c9d4
DB
246 case 2:
247 add_fetch_refspec(remote, xstrdup(s));
248 break;
5751f490
DB
249 }
250 }
251 fclose(f);
252}
253
254static void read_branches_file(struct remote *remote)
255{
256 const char *slash = strchr(remote->name, '/');
cf818348 257 char *frag;
f285a2d7 258 struct strbuf branch = STRBUF_INIT;
5751f490
DB
259 int n = slash ? slash - remote->name : 1000;
260 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
261 char *s, *p;
262 int len;
263
264 if (!f)
265 return;
266 s = fgets(buffer, BUF_SIZE, f);
267 fclose(f);
268 if (!s)
269 return;
270 while (isspace(*s))
271 s++;
272 if (!*s)
273 return;
89cf4c70 274 remote->origin = REMOTE_BRANCHES;
5751f490
DB
275 p = s + strlen(s);
276 while (isspace(p[-1]))
277 *--p = 0;
278 len = p - s;
279 if (slash)
280 len += strlen(slash);
281 p = xmalloc(len + 1);
282 strcpy(p, s);
283 if (slash)
284 strcat(p, slash);
472fa4cd
DB
285
286 /*
287 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
288 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
289 * the partial URL obtained from the branches file plus
290 * "/netdev-2.6" and does not store it in any tracking ref.
291 * #branch specifier in the file is ignored.
292 *
293 * Otherwise, the branches file would have URL and optionally
294 * #branch specified. The "master" (or specified) branch is
295 * fetched and stored in the local branch of the same name.
296 */
cf818348
DB
297 frag = strchr(p, '#');
298 if (frag) {
299 *(frag++) = '\0';
472fa4cd
DB
300 strbuf_addf(&branch, "refs/heads/%s", frag);
301 } else
302 strbuf_addstr(&branch, "refs/heads/master");
303 if (!slash) {
304 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
cf818348 305 } else {
472fa4cd
DB
306 strbuf_reset(&branch);
307 strbuf_addstr(&branch, "HEAD:");
cf818348 308 }
55029ae4 309 add_url_alias(remote, p);
2af202be 310 add_fetch_refspec(remote, strbuf_detach(&branch, NULL));
18afe101
MK
311 /*
312 * Cogito compatible push: push current HEAD to remote #branch
313 * (master if missing)
314 */
315 strbuf_init(&branch, 0);
316 strbuf_addstr(&branch, "HEAD");
317 if (frag)
318 strbuf_addf(&branch, ":refs/heads/%s", frag);
319 else
320 strbuf_addstr(&branch, ":refs/heads/master");
2af202be 321 add_push_refspec(remote, strbuf_detach(&branch, NULL));
d71ab174 322 remote->fetch_tags = 1; /* always auto-follow */
5751f490
DB
323}
324
ef90d6d4 325static int handle_config(const char *key, const char *value, void *cb)
5751f490
DB
326{
327 const char *name;
328 const char *subkey;
329 struct remote *remote;
cf818348
DB
330 struct branch *branch;
331 if (!prefixcmp(key, "branch.")) {
332 name = key + 7;
333 subkey = strrchr(name, '.');
cf818348
DB
334 if (!subkey)
335 return 0;
896c0535 336 branch = make_branch(name, subkey - name);
cf818348 337 if (!strcmp(subkey, ".remote")) {
d2370cc2
JH
338 if (!value)
339 return config_error_nonbool(key);
cf818348 340 branch->remote_name = xstrdup(value);
fa685bdf 341 if (branch == current_branch) {
cf818348 342 default_remote_name = branch->remote_name;
fa685bdf
DB
343 explicit_default_remote_name = 1;
344 }
d2370cc2
JH
345 } else if (!strcmp(subkey, ".merge")) {
346 if (!value)
347 return config_error_nonbool(key);
cf818348 348 add_merge(branch, xstrdup(value));
d2370cc2 349 }
cf818348 350 return 0;
5751f490 351 }
55029ae4
DB
352 if (!prefixcmp(key, "url.")) {
353 struct rewrite *rewrite;
60e3aba9 354 name = key + 4;
55029ae4
DB
355 subkey = strrchr(name, '.');
356 if (!subkey)
357 return 0;
358 rewrite = make_rewrite(name, subkey - name);
359 if (!strcmp(subkey, ".insteadof")) {
360 if (!value)
361 return config_error_nonbool(key);
362 add_instead_of(rewrite, xstrdup(value));
363 }
364 }
5751f490
DB
365 if (prefixcmp(key, "remote."))
366 return 0;
367 name = key + 7;
c82efafc
BC
368 if (*name == '/') {
369 warning("Config remote shorthand cannot begin with '/': %s",
370 name);
371 return 0;
372 }
5751f490
DB
373 subkey = strrchr(name, '.');
374 if (!subkey)
cd294bc3 375 return 0;
5751f490 376 remote = make_remote(name, subkey - name);
89cf4c70 377 remote->origin = REMOTE_CONFIG;
84bb2dfd
PB
378 if (!strcmp(subkey, ".mirror"))
379 remote->mirror = git_config_bool(key, value);
380 else if (!strcmp(subkey, ".skipdefaultupdate"))
381 remote->skip_default_update = git_config_bool(key, value);
382
383 else if (!strcmp(subkey, ".url")) {
384 const char *v;
385 if (git_config_string(&v, key, value))
386 return -1;
387 add_url(remote, v);
20346234
MG
388 } else if (!strcmp(subkey, ".pushurl")) {
389 const char *v;
390 if (git_config_string(&v, key, value))
391 return -1;
392 add_pushurl(remote, v);
5751f490 393 } else if (!strcmp(subkey, ".push")) {
84bb2dfd
PB
394 const char *v;
395 if (git_config_string(&v, key, value))
396 return -1;
397 add_push_refspec(remote, v);
5d46c9d4 398 } else if (!strcmp(subkey, ".fetch")) {
84bb2dfd
PB
399 const char *v;
400 if (git_config_string(&v, key, value))
401 return -1;
402 add_fetch_refspec(remote, v);
5751f490 403 } else if (!strcmp(subkey, ".receivepack")) {
84bb2dfd
PB
404 const char *v;
405 if (git_config_string(&v, key, value))
406 return -1;
5751f490 407 if (!remote->receivepack)
84bb2dfd 408 remote->receivepack = v;
5751f490
DB
409 else
410 error("more than one receivepack given, using the first");
0012ba21 411 } else if (!strcmp(subkey, ".uploadpack")) {
84bb2dfd
PB
412 const char *v;
413 if (git_config_string(&v, key, value))
414 return -1;
0012ba21 415 if (!remote->uploadpack)
84bb2dfd 416 remote->uploadpack = v;
0012ba21
DB
417 else
418 error("more than one uploadpack given, using the first");
d71ab174
DB
419 } else if (!strcmp(subkey, ".tagopt")) {
420 if (!strcmp(value, "--no-tags"))
421 remote->fetch_tags = -1;
14c98218 422 } else if (!strcmp(subkey, ".proxy")) {
84bb2dfd
PB
423 return git_config_string((const char **)&remote->http_proxy,
424 key, value);
425 }
5751f490
DB
426 return 0;
427}
428
55029ae4
DB
429static void alias_all_urls(void)
430{
431 int i, j;
432 for (i = 0; i < remotes_nr; i++) {
433 if (!remotes[i])
434 continue;
435 for (j = 0; j < remotes[i]->url_nr; j++) {
436 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
437 }
20346234
MG
438 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
439 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j]);
440 }
55029ae4
DB
441 }
442}
443
5751f490
DB
444static void read_config(void)
445{
446 unsigned char sha1[20];
447 const char *head_ref;
448 int flag;
449 if (default_remote_name) // did this already
450 return;
451 default_remote_name = xstrdup("origin");
452 current_branch = NULL;
453 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
454 if (head_ref && (flag & REF_ISSYMREF) &&
455 !prefixcmp(head_ref, "refs/heads/")) {
cf818348
DB
456 current_branch =
457 make_branch(head_ref + strlen("refs/heads/"), 0);
5751f490 458 }
ef90d6d4 459 git_config(handle_config, NULL);
55029ae4 460 alias_all_urls();
5751f490
DB
461}
462
47c6ef1c
JH
463/*
464 * We need to make sure the tracking branches are well formed, but a
465 * wildcard refspec in "struct refspec" must have a trailing slash. We
466 * temporarily drop the trailing '/' while calling check_ref_format(),
467 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
468 * error return is Ok for a wildcard refspec.
469 */
470static int verify_refname(char *name, int is_glob)
471{
08fbdb30 472 int result;
47c6ef1c 473
47c6ef1c 474 result = check_ref_format(name);
08fbdb30
DB
475 if (is_glob && result == CHECK_REF_FORMAT_WILDCARD)
476 result = CHECK_REF_FORMAT_OK;
47c6ef1c
JH
477 return result;
478}
479
2cb1f36d
BC
480/*
481 * This function frees a refspec array.
482 * Warning: code paths should be checked to ensure that the src
483 * and dst pointers are always freeable pointers as well
484 * as the refspec pointer itself.
485 */
697d7f5d 486static void free_refspecs(struct refspec *refspec, int nr_refspec)
2cb1f36d
BC
487{
488 int i;
489
490 if (!refspec)
491 return;
492
493 for (i = 0; i < nr_refspec; i++) {
494 free(refspec[i].src);
495 free(refspec[i].dst);
496 }
497 free(refspec);
498}
499
24b6177e 500static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
6b62816c
DB
501{
502 int i;
ef00d150 503 int st;
6b62816c 504 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
46220ca1 505
6b62816c 506 for (i = 0; i < nr_refspec; i++) {
47c6ef1c 507 size_t llen;
46220ca1
JH
508 int is_glob;
509 const char *lhs, *rhs;
510
8e76bf3f 511 is_glob = 0;
46220ca1
JH
512
513 lhs = refspec[i];
514 if (*lhs == '+') {
6b62816c 515 rs[i].force = 1;
46220ca1 516 lhs++;
6b62816c 517 }
46220ca1
JH
518
519 rhs = strrchr(lhs, ':');
a83619d6
PB
520
521 /*
522 * Before going on, special case ":" (or "+:") as a refspec
523 * for matching refs.
524 */
525 if (!fetch && rhs == lhs && rhs[1] == '\0') {
526 rs[i].matching = 1;
527 continue;
528 }
529
46220ca1 530 if (rhs) {
47c6ef1c 531 size_t rlen = strlen(++rhs);
abd2bde7 532 is_glob = (1 <= rlen && strchr(rhs, '*'));
08fbdb30 533 rs[i].dst = xstrndup(rhs, rlen);
6b62816c 534 }
ef00d150 535
46220ca1 536 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
abd2bde7 537 if (1 <= llen && memchr(lhs, '*', llen)) {
7d19da46
JH
538 if ((rhs && !is_glob) || (!rhs && fetch))
539 goto invalid;
540 is_glob = 1;
7d19da46
JH
541 } else if (rhs && is_glob) {
542 goto invalid;
ef00d150 543 }
7d19da46 544
46220ca1
JH
545 rs[i].pattern = is_glob;
546 rs[i].src = xstrndup(lhs, llen);
547
548 if (fetch) {
549 /*
550 * LHS
551 * - empty is allowed; it means HEAD.
552 * - otherwise it must be a valid looking ref.
553 */
554 if (!*rs[i].src)
555 ; /* empty is ok */
556 else {
47c6ef1c 557 st = verify_refname(rs[i].src, is_glob);
46220ca1
JH
558 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
559 goto invalid;
560 }
561 /*
562 * RHS
7d19da46 563 * - missing is ok, and is same as empty.
46220ca1
JH
564 * - empty is ok; it means not to store.
565 * - otherwise it must be a valid looking ref.
566 */
567 if (!rs[i].dst) {
568 ; /* ok */
569 } else if (!*rs[i].dst) {
570 ; /* ok */
571 } else {
47c6ef1c 572 st = verify_refname(rs[i].dst, is_glob);
46220ca1
JH
573 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
574 goto invalid;
575 }
576 } else {
577 /*
578 * LHS
579 * - empty is allowed; it means delete.
580 * - when wildcarded, it must be a valid looking ref.
581 * - otherwise, it must be an extended SHA-1, but
582 * there is no existing way to validate this.
583 */
584 if (!*rs[i].src)
585 ; /* empty is ok */
586 else if (is_glob) {
47c6ef1c 587 st = verify_refname(rs[i].src, is_glob);
46220ca1
JH
588 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
589 goto invalid;
590 }
591 else
592 ; /* anything goes, for now */
593 /*
594 * RHS
595 * - missing is allowed, but LHS then must be a
596 * valid looking ref.
597 * - empty is not allowed.
598 * - otherwise it must be a valid looking ref.
599 */
600 if (!rs[i].dst) {
47c6ef1c 601 st = verify_refname(rs[i].src, is_glob);
46220ca1
JH
602 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
603 goto invalid;
604 } else if (!*rs[i].dst) {
605 goto invalid;
606 } else {
47c6ef1c 607 st = verify_refname(rs[i].dst, is_glob);
46220ca1
JH
608 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
609 goto invalid;
610 }
ef00d150 611 }
6b62816c
DB
612 }
613 return rs;
46220ca1
JH
614
615 invalid:
24b6177e 616 if (verify) {
2cb1f36d
BC
617 /*
618 * nr_refspec must be greater than zero and i must be valid
619 * since it is only possible to reach this point from within
620 * the for loop above.
621 */
622 free_refspecs(rs, i+1);
24b6177e
JF
623 return NULL;
624 }
46220ca1
JH
625 die("Invalid refspec '%s'", refspec[i]);
626}
627
24b6177e
JF
628int valid_fetch_refspec(const char *fetch_refspec_str)
629{
630 const char *fetch_refspec[] = { fetch_refspec_str };
631 struct refspec *refspec;
632
633 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
2cb1f36d 634 free_refspecs(refspec, 1);
24b6177e
JF
635 return !!refspec;
636}
637
46220ca1
JH
638struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
639{
24b6177e 640 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
46220ca1
JH
641}
642
697d7f5d 643static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
46220ca1 644{
24b6177e 645 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
6b62816c
DB
646}
647
df93e33c
DB
648static int valid_remote_nick(const char *name)
649{
8ca12c0d 650 if (!name[0] || is_dot_or_dotdot(name))
df93e33c
DB
651 return 0;
652 return !strchr(name, '/'); /* no slash */
653}
654
5751f490
DB
655struct remote *remote_get(const char *name)
656{
657 struct remote *ret;
fa685bdf 658 int name_given = 0;
5751f490
DB
659
660 read_config();
fa685bdf
DB
661 if (name)
662 name_given = 1;
663 else {
5751f490 664 name = default_remote_name;
fa685bdf
DB
665 name_given = explicit_default_remote_name;
666 }
9326d494 667
5751f490 668 ret = make_remote(name, 0);
df93e33c 669 if (valid_remote_nick(name)) {
28b91f8a 670 if (!ret->url)
5751f490 671 read_remotes_file(ret);
28b91f8a 672 if (!ret->url)
5751f490
DB
673 read_branches_file(ret);
674 }
9326d494 675 if (name_given && !ret->url)
55029ae4 676 add_url_alias(ret, name);
28b91f8a 677 if (!ret->url)
5751f490 678 return NULL;
46220ca1
JH
679 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
680 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
5751f490
DB
681 return ret;
682}
6b62816c 683
9a23ba33
FAG
684int remote_is_configured(const char *name)
685{
686 int i;
687 read_config();
688
689 for (i = 0; i < remotes_nr; i++)
690 if (!strcmp(name, remotes[i]->name))
691 return 1;
692 return 0;
693}
694
b42f6927
JS
695int for_each_remote(each_remote_fn fn, void *priv)
696{
697 int i, result = 0;
698 read_config();
2d31347b 699 for (i = 0; i < remotes_nr && !result; i++) {
b42f6927
JS
700 struct remote *r = remotes[i];
701 if (!r)
702 continue;
703 if (!r->fetch)
46220ca1
JH
704 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
705 r->fetch_refspec);
b42f6927 706 if (!r->push)
46220ca1
JH
707 r->push = parse_push_refspec(r->push_refspec_nr,
708 r->push_refspec);
b42f6927
JS
709 result = fn(r, priv);
710 }
711 return result;
712}
713
2467a4fa
DB
714void ref_remove_duplicates(struct ref *ref_map)
715{
716 struct ref **posn;
717 struct ref *next;
718 for (; ref_map; ref_map = ref_map->next) {
719 if (!ref_map->peer_ref)
720 continue;
721 posn = &ref_map->next;
722 while (*posn) {
723 if ((*posn)->peer_ref &&
724 !strcmp((*posn)->peer_ref->name,
725 ref_map->peer_ref->name)) {
726 if (strcmp((*posn)->name, ref_map->name))
727 die("%s tracks both %s and %s",
728 ref_map->peer_ref->name,
729 (*posn)->name, ref_map->name);
730 next = (*posn)->next;
731 free((*posn)->peer_ref);
732 free(*posn);
733 *posn = next;
734 } else {
735 posn = &(*posn)->next;
736 }
737 }
738 }
739}
740
28b91f8a 741int remote_has_url(struct remote *remote, const char *url)
5d46c9d4
DB
742{
743 int i;
28b91f8a
SP
744 for (i = 0; i < remote->url_nr; i++) {
745 if (!strcmp(remote->url[i], url))
5d46c9d4
DB
746 return 1;
747 }
748 return 0;
749}
750
e928213f
DB
751static int match_name_with_pattern(const char *key, const char *name,
752 const char *value, char **result)
a3c84239 753{
08fbdb30
DB
754 const char *kstar = strchr(key, '*');
755 size_t klen;
abd2bde7
DB
756 size_t ksuffixlen;
757 size_t namelen;
08fbdb30
DB
758 int ret;
759 if (!kstar)
760 die("Key '%s' of pattern had no '*'", key);
761 klen = kstar - key;
abd2bde7
DB
762 ksuffixlen = strlen(kstar + 1);
763 namelen = strlen(name);
764 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
765 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
e928213f 766 if (ret && value) {
08fbdb30
DB
767 const char *vstar = strchr(value, '*');
768 size_t vlen;
abd2bde7 769 size_t vsuffixlen;
08fbdb30
DB
770 if (!vstar)
771 die("Value '%s' of pattern has no '*'", value);
772 vlen = vstar - value;
abd2bde7
DB
773 vsuffixlen = strlen(vstar + 1);
774 *result = xmalloc(vlen + vsuffixlen +
e928213f 775 strlen(name) -
abd2bde7
DB
776 klen - ksuffixlen + 1);
777 strncpy(*result, value, vlen);
778 strncpy(*result + vlen,
779 name + klen, namelen - klen - ksuffixlen);
780 strcpy(*result + vlen + namelen - klen - ksuffixlen,
781 vstar + 1);
e928213f 782 }
a3c84239
DB
783 return ret;
784}
785
5d46c9d4
DB
786int remote_find_tracking(struct remote *remote, struct refspec *refspec)
787{
b42f6927
JS
788 int find_src = refspec->src == NULL;
789 char *needle, **result;
5d46c9d4 790 int i;
b42f6927
JS
791
792 if (find_src) {
009c5bcd 793 if (!refspec->dst)
b42f6927
JS
794 return error("find_tracking: need either src or dst");
795 needle = refspec->dst;
796 result = &refspec->src;
797 } else {
798 needle = refspec->src;
799 result = &refspec->dst;
800 }
801
5d46c9d4
DB
802 for (i = 0; i < remote->fetch_refspec_nr; i++) {
803 struct refspec *fetch = &remote->fetch[i];
b42f6927
JS
804 const char *key = find_src ? fetch->dst : fetch->src;
805 const char *value = find_src ? fetch->src : fetch->dst;
5d46c9d4
DB
806 if (!fetch->dst)
807 continue;
808 if (fetch->pattern) {
e928213f 809 if (match_name_with_pattern(key, needle, value, result)) {
5d46c9d4
DB
810 refspec->force = fetch->force;
811 return 0;
812 }
b42f6927
JS
813 } else if (!strcmp(needle, key)) {
814 *result = xstrdup(value);
815 refspec->force = fetch->force;
816 return 0;
5d46c9d4
DB
817 }
818 }
5d46c9d4
DB
819 return -1;
820}
821
8009768e
RS
822static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
823 const char *name)
824{
825 size_t len = strlen(name);
826 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
827 memcpy(ref->name, prefix, prefixlen);
828 memcpy(ref->name + prefixlen, name, len);
829 return ref;
830}
831
59c69c0c 832struct ref *alloc_ref(const char *name)
dfd255dd 833{
59c69c0c 834 return alloc_ref_with_prefix("", 0, name);
737922aa
KK
835}
836
4577370e 837static struct ref *copy_ref(const struct ref *ref)
d71ab174 838{
7b3db095
JS
839 struct ref *cpy;
840 size_t len;
841 if (!ref)
842 return NULL;
843 len = strlen(ref->name);
844 cpy = xmalloc(sizeof(struct ref) + len + 1);
845 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
846 cpy->next = NULL;
847 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
848 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
849 cpy->peer_ref = copy_ref(ref->peer_ref);
850 return cpy;
d71ab174
DB
851}
852
4577370e
DB
853struct ref *copy_ref_list(const struct ref *ref)
854{
855 struct ref *ret = NULL;
856 struct ref **tail = &ret;
857 while (ref) {
858 *tail = copy_ref(ref);
859 ref = ref->next;
860 tail = &((*tail)->next);
861 }
862 return ret;
863}
864
697d7f5d 865static void free_ref(struct ref *ref)
be885d96
DB
866{
867 if (!ref)
868 return;
7b3db095 869 free_ref(ref->peer_ref);
be885d96
DB
870 free(ref->remote_status);
871 free(ref->symref);
872 free(ref);
873}
874
dfd255dd
DB
875void free_refs(struct ref *ref)
876{
877 struct ref *next;
878 while (ref) {
879 next = ref->next;
be885d96 880 free_ref(ref);
dfd255dd
DB
881 ref = next;
882 }
883}
884
6b62816c
DB
885static int count_refspec_match(const char *pattern,
886 struct ref *refs,
887 struct ref **matched_ref)
888{
889 int patlen = strlen(pattern);
890 struct ref *matched_weak = NULL;
891 struct ref *matched = NULL;
892 int weak_match = 0;
893 int match = 0;
894
895 for (weak_match = match = 0; refs; refs = refs->next) {
896 char *name = refs->name;
897 int namelen = strlen(name);
6b62816c 898
ae36bdcf 899 if (!refname_match(pattern, name, ref_rev_parse_rules))
6b62816c
DB
900 continue;
901
902 /* A match is "weak" if it is with refs outside
903 * heads or tags, and did not specify the pattern
904 * in full (e.g. "refs/remotes/origin/master") or at
905 * least from the toplevel (e.g. "remotes/origin/master");
906 * otherwise "git push $URL master" would result in
907 * ambiguity between remotes/origin/master and heads/master
908 * at the remote site.
909 */
910 if (namelen != patlen &&
911 patlen != namelen - 5 &&
912 prefixcmp(name, "refs/heads/") &&
913 prefixcmp(name, "refs/tags/")) {
914 /* We want to catch the case where only weak
915 * matches are found and there are multiple
916 * matches, and where more than one strong
917 * matches are found, as ambiguous. One
918 * strong match with zero or more weak matches
919 * are acceptable as a unique match.
920 */
921 matched_weak = refs;
922 weak_match++;
923 }
924 else {
925 matched = refs;
926 match++;
927 }
928 }
929 if (!matched) {
930 *matched_ref = matched_weak;
931 return weak_match;
932 }
933 else {
934 *matched_ref = matched;
935 return match;
936 }
937}
938
1d735267 939static void tail_link_ref(struct ref *ref, struct ref ***tail)
6b62816c
DB
940{
941 **tail = ref;
1d735267
DB
942 while (ref->next)
943 ref = ref->next;
6b62816c 944 *tail = &ref->next;
6b62816c
DB
945}
946
947static struct ref *try_explicit_object_name(const char *name)
948{
949 unsigned char sha1[20];
950 struct ref *ref;
6b62816c
DB
951
952 if (!*name) {
59c69c0c 953 ref = alloc_ref("(delete)");
6b62816c
DB
954 hashclr(ref->new_sha1);
955 return ref;
956 }
957 if (get_sha1(name, sha1))
958 return NULL;
59c69c0c 959 ref = alloc_ref(name);
6b62816c
DB
960 hashcpy(ref->new_sha1, sha1);
961 return ref;
962}
963
1d735267 964static struct ref *make_linked_ref(const char *name, struct ref ***tail)
6b62816c 965{
59c69c0c 966 struct ref *ret = alloc_ref(name);
1d735267
DB
967 tail_link_ref(ret, tail);
968 return ret;
163f0ee5 969}
8558fd9e 970
f8aae120
JK
971static char *guess_ref(const char *name, struct ref *peer)
972{
973 struct strbuf buf = STRBUF_INIT;
974 unsigned char sha1[20];
975
976 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
977 if (!r)
978 return NULL;
979
980 if (!prefixcmp(r, "refs/heads/"))
981 strbuf_addstr(&buf, "refs/heads/");
982 else if (!prefixcmp(r, "refs/tags/"))
983 strbuf_addstr(&buf, "refs/tags/");
984 else
985 return NULL;
986
987 strbuf_addstr(&buf, name);
988 return strbuf_detach(&buf, NULL);
989}
990
54a8ad92
JH
991static int match_explicit(struct ref *src, struct ref *dst,
992 struct ref ***dst_tail,
9a7bbd1d 993 struct refspec *rs)
6b62816c 994{
54a8ad92 995 struct ref *matched_src, *matched_dst;
cdf690e5 996 int copy_src;
8558fd9e 997
54a8ad92 998 const char *dst_value = rs->dst;
f8aae120 999 char *dst_guess;
6b62816c 1000
a83619d6 1001 if (rs->pattern || rs->matching)
9a7bbd1d 1002 return 0;
8558fd9e 1003
54a8ad92
JH
1004 matched_src = matched_dst = NULL;
1005 switch (count_refspec_match(rs->src, src, &matched_src)) {
1006 case 1:
cdf690e5 1007 copy_src = 1;
54a8ad92
JH
1008 break;
1009 case 0:
1010 /* The source could be in the get_sha1() format
1011 * not a reference name. :refs/other is a
1012 * way to delete 'other' ref at the remote end.
1013 */
1014 matched_src = try_explicit_object_name(rs->src);
7dfee372 1015 if (!matched_src)
9a7bbd1d 1016 return error("src refspec %s does not match any.", rs->src);
cdf690e5 1017 copy_src = 0;
54a8ad92
JH
1018 break;
1019 default:
9a7bbd1d 1020 return error("src refspec %s matches more than one.", rs->src);
54a8ad92 1021 }
3c8b7df1 1022
4491e62a 1023 if (!dst_value) {
9f0ea7e8
DB
1024 unsigned char sha1[20];
1025 int flag;
1026
9f0ea7e8
DB
1027 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
1028 if (!dst_value ||
1029 ((flag & REF_ISSYMREF) &&
1030 prefixcmp(dst_value, "refs/heads/")))
1031 die("%s cannot be resolved to branch.",
1032 matched_src->name);
4491e62a 1033 }
3c8b7df1 1034
54a8ad92
JH
1035 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1036 case 1:
1037 break;
1038 case 0:
163f0ee5 1039 if (!memcmp(dst_value, "refs/", 5))
1d735267 1040 matched_dst = make_linked_ref(dst_value, dst_tail);
eeefa7c9 1041 else if ((dst_guess = guess_ref(dst_value, matched_src)))
f8aae120 1042 matched_dst = make_linked_ref(dst_guess, dst_tail);
3c8b7df1 1043 else
f8aae120
JK
1044 error("unable to push to unqualified destination: %s\n"
1045 "The destination refspec neither matches an "
1046 "existing ref on the remote nor\n"
1047 "begins with refs/, and we are unable to "
1048 "guess a prefix based on the source ref.",
1049 dst_value);
54a8ad92
JH
1050 break;
1051 default:
3c8b7df1 1052 matched_dst = NULL;
54a8ad92
JH
1053 error("dst refspec %s matches more than one.",
1054 dst_value);
1055 break;
1056 }
9a7bbd1d
JK
1057 if (!matched_dst)
1058 return -1;
1059 if (matched_dst->peer_ref)
1060 return error("dst ref %s receives from more than one src.",
54a8ad92 1061 matched_dst->name);
54a8ad92 1062 else {
cdf690e5 1063 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
54a8ad92 1064 matched_dst->force = rs->force;
6b62816c 1065 }
9a7bbd1d 1066 return 0;
54a8ad92
JH
1067}
1068
1069static int match_explicit_refs(struct ref *src, struct ref *dst,
1070 struct ref ***dst_tail, struct refspec *rs,
1071 int rs_nr)
1072{
1073 int i, errs;
1074 for (i = errs = 0; i < rs_nr; i++)
9a7bbd1d
JK
1075 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1076 return errs;
6b62816c
DB
1077}
1078
6e66bf3c
AR
1079static const struct refspec *check_pattern_match(const struct refspec *rs,
1080 int rs_nr,
1081 const struct ref *src)
8558fd9e
DB
1082{
1083 int i;
a83619d6 1084 int matching_refs = -1;
8558fd9e 1085 for (i = 0; i < rs_nr; i++) {
a83619d6
PB
1086 if (rs[i].matching &&
1087 (matching_refs == -1 || rs[i].force)) {
1088 matching_refs = i;
1089 continue;
1090 }
1091
e928213f
DB
1092 if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,
1093 NULL, NULL))
6e66bf3c 1094 return rs + i;
8558fd9e 1095 }
a83619d6
PB
1096 if (matching_refs != -1)
1097 return rs + matching_refs;
1098 else
1099 return NULL;
8558fd9e
DB
1100}
1101
6d2bf96e
CB
1102static struct ref **tail_ref(struct ref **head)
1103{
1104 struct ref **tail = head;
1105 while (*tail)
1106 tail = &((*tail)->next);
1107 return tail;
1108}
1109
54a8ad92
JH
1110/*
1111 * Note. This is used only by "push"; refspec matching rules for
1112 * push and fetch are subtly different, so do not try to reuse it
1113 * without thinking.
1114 */
6d2bf96e 1115int match_refs(struct ref *src, struct ref **dst,
28b9d6e5 1116 int nr_refspec, const char **refspec, int flags)
6b62816c 1117{
a83619d6 1118 struct refspec *rs;
28b9d6e5
AW
1119 int send_all = flags & MATCH_REFS_ALL;
1120 int send_mirror = flags & MATCH_REFS_MIRROR;
5f48cb95 1121 int errs;
2af202be 1122 static const char *default_refspec[] = { ":", NULL };
6d2bf96e 1123 struct ref **dst_tail = tail_ref(dst);
6b62816c 1124
a83619d6
PB
1125 if (!nr_refspec) {
1126 nr_refspec = 1;
1127 refspec = default_refspec;
1128 }
1129 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
6d2bf96e 1130 errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec);
6b62816c
DB
1131
1132 /* pick the remainder */
1133 for ( ; src; src = src->next) {
1134 struct ref *dst_peer;
6e66bf3c
AR
1135 const struct refspec *pat = NULL;
1136 char *dst_name;
6b62816c
DB
1137 if (src->peer_ref)
1138 continue;
a83619d6
PB
1139
1140 pat = check_pattern_match(rs, nr_refspec, src);
1141 if (!pat)
1142 continue;
1143
1144 if (pat->matching) {
098e711e
JH
1145 /*
1146 * "matching refs"; traditionally we pushed everything
1147 * including refs outside refs/heads/ hierarchy, but
1148 * that does not make much sense these days.
1149 */
a83619d6
PB
1150 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1151 continue;
1152 dst_name = xstrdup(src->name);
8558fd9e 1153
a83619d6 1154 } else {
efd8f793 1155 const char *dst_side = pat->dst ? pat->dst : pat->src;
e928213f
DB
1156 if (!match_name_with_pattern(pat->src, src->name,
1157 dst_side, &dst_name))
1158 die("Didn't think it matches any more");
a83619d6 1159 }
6d2bf96e 1160 dst_peer = find_ref_by_name(*dst, dst_name);
a83619d6
PB
1161 if (dst_peer) {
1162 if (dst_peer->peer_ref)
1163 /* We're already sending something to this ref. */
1164 goto free_name;
1165
1166 } else {
1167 if (pat->matching && !(send_all || send_mirror))
1168 /*
1169 * Remote doesn't have it, and we have no
1170 * explicit pattern, and we don't have
1171 * --all nor --mirror.
1172 */
1173 goto free_name;
28b9d6e5 1174
6b62816c 1175 /* Create a new one and link it */
6d2bf96e 1176 dst_peer = make_linked_ref(dst_name, &dst_tail);
6b62816c 1177 hashcpy(dst_peer->new_sha1, src->new_sha1);
6b62816c 1178 }
cdf690e5 1179 dst_peer->peer_ref = copy_ref(src);
a83619d6 1180 dst_peer->force = pat->force;
6e66bf3c
AR
1181 free_name:
1182 free(dst_name);
6b62816c 1183 }
5f48cb95
JS
1184 if (errs)
1185 return -1;
6b62816c
DB
1186 return 0;
1187}
cf818348
DB
1188
1189struct branch *branch_get(const char *name)
1190{
1191 struct branch *ret;
1192
1193 read_config();
1194 if (!name || !*name || !strcmp(name, "HEAD"))
1195 ret = current_branch;
1196 else
1197 ret = make_branch(name, 0);
1198 if (ret && ret->remote_name) {
1199 ret->remote = remote_get(ret->remote_name);
1200 if (ret->merge_nr) {
1201 int i;
1202 ret->merge = xcalloc(sizeof(*ret->merge),
1203 ret->merge_nr);
1204 for (i = 0; i < ret->merge_nr; i++) {
1205 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1206 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
5e6e2b48
MG
1207 if (remote_find_tracking(ret->remote, ret->merge[i])
1208 && !strcmp(ret->remote_name, "."))
1209 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
cf818348
DB
1210 }
1211 }
1212 }
1213 return ret;
1214}
1215
1216int branch_has_merge_config(struct branch *branch)
1217{
1218 return branch && !!branch->merge;
1219}
1220
85682c19
SP
1221int branch_merge_matches(struct branch *branch,
1222 int i,
1223 const char *refname)
cf818348 1224{
85682c19 1225 if (!branch || i < 0 || i >= branch->merge_nr)
cf818348 1226 return 0;
605b4978 1227 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
cf818348 1228}
d71ab174 1229
4577370e 1230static struct ref *get_expanded_map(const struct ref *remote_refs,
d71ab174
DB
1231 const struct refspec *refspec)
1232{
4577370e 1233 const struct ref *ref;
d71ab174
DB
1234 struct ref *ret = NULL;
1235 struct ref **tail = &ret;
1236
e928213f 1237 char *expn_name;
d71ab174
DB
1238
1239 for (ref = remote_refs; ref; ref = ref->next) {
1240 if (strchr(ref->name, '^'))
1241 continue; /* a dereference item */
e928213f
DB
1242 if (match_name_with_pattern(refspec->src, ref->name,
1243 refspec->dst, &expn_name)) {
d71ab174 1244 struct ref *cpy = copy_ref(ref);
d71ab174 1245
e928213f
DB
1246 cpy->peer_ref = alloc_ref(expn_name);
1247 free(expn_name);
d71ab174
DB
1248 if (refspec->force)
1249 cpy->peer_ref->force = 1;
1250 *tail = cpy;
1251 tail = &cpy->next;
1252 }
1253 }
1254
1255 return ret;
1256}
1257
4577370e 1258static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
d71ab174 1259{
4577370e 1260 const struct ref *ref;
d71ab174 1261 for (ref = refs; ref; ref = ref->next) {
605b4978 1262 if (refname_match(name, ref->name, ref_fetch_rules))
d71ab174
DB
1263 return ref;
1264 }
1265 return NULL;
1266}
1267
4577370e 1268struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
d71ab174 1269{
4577370e 1270 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
d71ab174
DB
1271
1272 if (!ref)
9ad7c5ae 1273 return NULL;
d71ab174
DB
1274
1275 return copy_ref(ref);
1276}
1277
1278static struct ref *get_local_ref(const char *name)
1279{
3eb96997 1280 if (!name || name[0] == '\0')
d71ab174
DB
1281 return NULL;
1282
59c69c0c
RS
1283 if (!prefixcmp(name, "refs/"))
1284 return alloc_ref(name);
d71ab174
DB
1285
1286 if (!prefixcmp(name, "heads/") ||
1287 !prefixcmp(name, "tags/") ||
8009768e
RS
1288 !prefixcmp(name, "remotes/"))
1289 return alloc_ref_with_prefix("refs/", 5, name);
d71ab174 1290
8009768e 1291 return alloc_ref_with_prefix("refs/heads/", 11, name);
d71ab174
DB
1292}
1293
4577370e 1294int get_fetch_map(const struct ref *remote_refs,
d71ab174 1295 const struct refspec *refspec,
9ad7c5ae
JH
1296 struct ref ***tail,
1297 int missing_ok)
d71ab174 1298{
ef00d150 1299 struct ref *ref_map, **rmp;
d71ab174
DB
1300
1301 if (refspec->pattern) {
1302 ref_map = get_expanded_map(remote_refs, refspec);
1303 } else {
9ad7c5ae
JH
1304 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1305
1306 ref_map = get_remote_ref(remote_refs, name);
1307 if (!missing_ok && !ref_map)
1308 die("Couldn't find remote ref %s", name);
1309 if (ref_map) {
1310 ref_map->peer_ref = get_local_ref(refspec->dst);
1311 if (ref_map->peer_ref && refspec->force)
1312 ref_map->peer_ref->force = 1;
1313 }
d71ab174
DB
1314 }
1315
ef00d150
DB
1316 for (rmp = &ref_map; *rmp; ) {
1317 if ((*rmp)->peer_ref) {
1318 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1319 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1320 struct ref *ignore = *rmp;
1321 error("* Ignoring funny ref '%s' locally",
1322 (*rmp)->peer_ref->name);
1323 *rmp = (*rmp)->next;
1324 free(ignore->peer_ref);
1325 free(ignore);
1326 continue;
1327 }
1328 }
1329 rmp = &((*rmp)->next);
d71ab174
DB
1330 }
1331
8f70a765
AR
1332 if (ref_map)
1333 tail_link_ref(ref_map, tail);
d71ab174
DB
1334
1335 return 0;
1336}
be885d96
DB
1337
1338int resolve_remote_symref(struct ref *ref, struct ref *list)
1339{
1340 if (!ref->symref)
1341 return 0;
1342 for (; list; list = list->next)
1343 if (!strcmp(ref->symref, list->name)) {
1344 hashcpy(ref->old_sha1, list->old_sha1);
1345 return 0;
1346 }
1347 return 1;
1348}
6d21bf96 1349
ec8452d5
JS
1350static void unmark_and_free(struct commit_list *list, unsigned int mark)
1351{
1352 while (list) {
1353 struct commit_list *temp = list;
1354 temp->item->object.flags &= ~mark;
1355 list = temp->next;
1356 free(temp);
1357 }
1358}
1359
1360int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1361{
1362 struct object *o;
1363 struct commit *old, *new;
1364 struct commit_list *list, *used;
1365 int found = 0;
1366
1367 /* Both new and old must be commit-ish and new is descendant of
1368 * old. Otherwise we require --force.
1369 */
1370 o = deref_tag(parse_object(old_sha1), NULL, 0);
1371 if (!o || o->type != OBJ_COMMIT)
1372 return 0;
1373 old = (struct commit *) o;
1374
1375 o = deref_tag(parse_object(new_sha1), NULL, 0);
1376 if (!o || o->type != OBJ_COMMIT)
1377 return 0;
1378 new = (struct commit *) o;
1379
1380 if (parse_commit(new) < 0)
1381 return 0;
1382
1383 used = list = NULL;
1384 commit_list_insert(new, &list);
1385 while (list) {
1386 new = pop_most_recent_commit(&list, TMP_MARK);
1387 commit_list_insert(new, &used);
1388 if (new == old) {
1389 found = 1;
1390 break;
1391 }
1392 }
1393 unmark_and_free(list, TMP_MARK);
1394 unmark_and_free(used, TMP_MARK);
1395 return found;
1396}
1397
6d21bf96
JH
1398/*
1399 * Return true if there is anything to report, otherwise false.
1400 */
1401int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1402{
1403 unsigned char sha1[20];
1404 struct commit *ours, *theirs;
1405 char symmetric[84];
1406 struct rev_info revs;
1407 const char *rev_argv[10], *base;
1408 int rev_argc;
1409
1410 /*
1411 * Nothing to report unless we are marked to build on top of
1412 * somebody else.
1413 */
1414 if (!branch ||
1415 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1416 return 0;
1417
1418 /*
1419 * If what we used to build on no longer exists, there is
1420 * nothing to report.
1421 */
1422 base = branch->merge[0]->dst;
1423 if (!resolve_ref(base, sha1, 1, NULL))
1424 return 0;
57ffc5f8 1425 theirs = lookup_commit_reference(sha1);
6d21bf96
JH
1426 if (!theirs)
1427 return 0;
1428
1429 if (!resolve_ref(branch->refname, sha1, 1, NULL))
1430 return 0;
57ffc5f8 1431 ours = lookup_commit_reference(sha1);
6d21bf96
JH
1432 if (!ours)
1433 return 0;
1434
1435 /* are we the same? */
1436 if (theirs == ours)
1437 return 0;
1438
8fbf879e 1439 /* Run "rev-list --left-right ours...theirs" internally... */
6d21bf96
JH
1440 rev_argc = 0;
1441 rev_argv[rev_argc++] = NULL;
1442 rev_argv[rev_argc++] = "--left-right";
1443 rev_argv[rev_argc++] = symmetric;
1444 rev_argv[rev_argc++] = "--";
1445 rev_argv[rev_argc] = NULL;
1446
1447 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1448 strcpy(symmetric + 40, "...");
1449 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1450
1451 init_revisions(&revs, NULL);
1452 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1453 prepare_revision_walk(&revs);
1454
1455 /* ... and count the commits on each side. */
1456 *num_ours = 0;
1457 *num_theirs = 0;
1458 while (1) {
1459 struct commit *c = get_revision(&revs);
1460 if (!c)
1461 break;
1462 if (c->object.flags & SYMMETRIC_LEFT)
1463 (*num_ours)++;
1464 else
1465 (*num_theirs)++;
1466 }
c0234b2e
JH
1467
1468 /* clear object flags smudged by the above traversal */
1469 clear_commit_marks(ours, ALL_REV_FLAGS);
1470 clear_commit_marks(theirs, ALL_REV_FLAGS);
6d21bf96
JH
1471 return 1;
1472}
1473
1474/*
1475 * Return true when there is anything to report, otherwise false.
1476 */
1477int format_tracking_info(struct branch *branch, struct strbuf *sb)
1478{
1479 int num_ours, num_theirs;
4de53ce0 1480 const char *base;
6d21bf96
JH
1481
1482 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1483 return 0;
1484
1485 base = branch->merge[0]->dst;
45972ffb 1486 base = shorten_unambiguous_ref(base, 0);
6d21bf96 1487 if (!num_theirs)
4de53ce0 1488 strbuf_addf(sb, "Your branch is ahead of '%s' "
6d21bf96 1489 "by %d commit%s.\n",
4de53ce0 1490 base, num_ours, (num_ours == 1) ? "" : "s");
6d21bf96 1491 else if (!num_ours)
4de53ce0
AP
1492 strbuf_addf(sb, "Your branch is behind '%s' "
1493 "by %d commit%s, "
6d21bf96 1494 "and can be fast-forwarded.\n",
4de53ce0 1495 base, num_theirs, (num_theirs == 1) ? "" : "s");
6d21bf96 1496 else
4de53ce0
AP
1497 strbuf_addf(sb, "Your branch and '%s' have diverged,\n"
1498 "and have %d and %d different commit(s) each, "
1499 "respectively.\n",
1500 base, num_ours, num_theirs);
6d21bf96
JH
1501 return 1;
1502}
454e2025
JS
1503
1504static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1505{
1506 struct ref ***local_tail = cb_data;
1507 struct ref *ref;
1508 int len;
1509
1510 /* we already know it starts with refs/ to get here */
1511 if (check_ref_format(refname + 5))
1512 return 0;
1513
1514 len = strlen(refname) + 1;
1515 ref = xcalloc(1, sizeof(*ref) + len);
1516 hashcpy(ref->new_sha1, sha1);
1517 memcpy(ref->name, refname, len);
1518 **local_tail = ref;
1519 *local_tail = &ref->next;
1520 return 0;
1521}
1522
1523struct ref *get_local_heads(void)
1524{
55f0566f 1525 struct ref *local_refs = NULL, **local_tail = &local_refs;
454e2025
JS
1526 for_each_ref(one_local_ref, &local_tail);
1527 return local_refs;
1528}
8ef51733 1529
4229f1fa
JS
1530struct ref *guess_remote_head(const struct ref *head,
1531 const struct ref *refs,
1532 int all)
8ef51733 1533{
8ef51733 1534 const struct ref *r;
4229f1fa
JS
1535 struct ref *list = NULL;
1536 struct ref **tail = &list;
8ef51733 1537
6cb4e6cc 1538 if (!head)
8ef51733
JS
1539 return NULL;
1540
fbb074c2
JK
1541 /*
1542 * Some transports support directly peeking at
1543 * where HEAD points; if that is the case, then
1544 * we don't have to guess.
1545 */
1546 if (head->symref)
1547 return copy_ref(find_ref_by_name(refs, head->symref));
1548
8ef51733 1549 /* If refs/heads/master could be right, it is. */
4229f1fa
JS
1550 if (!all) {
1551 r = find_ref_by_name(refs, "refs/heads/master");
1552 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1553 return copy_ref(r);
1554 }
8ef51733
JS
1555
1556 /* Look for another ref that points there */
4229f1fa
JS
1557 for (r = refs; r; r = r->next) {
1558 if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
1559 *tail = copy_ref(r);
1560 tail = &((*tail)->next);
1561 if (!all)
1562 break;
1563 }
1564 }
8ef51733 1565
4229f1fa 1566 return list;
8ef51733 1567}