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