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