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