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