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