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