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