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