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