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