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