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