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