10 static struct refspec s_tag_refspec = {
18 const struct refspec *tag_refspec = &s_tag_refspec;
20 struct counted_string {
27 struct counted_string *instead_of;
32 struct rewrite **rewrite;
37 static struct remote **remotes;
38 static int remotes_alloc;
39 static int remotes_nr;
41 static struct branch **branches;
42 static int branches_alloc;
43 static int branches_nr;
45 static struct branch *current_branch;
46 static const char *default_remote_name;
47 static int explicit_default_remote_name;
49 static struct rewrites rewrites;
50 static struct rewrites rewrites_push;
52 #define BUF_SIZE (2048)
53 static char buffer[BUF_SIZE];
55 static const char *alias_url(const char *url, struct rewrites *r)
59 struct counted_string *longest;
64 for (i = 0; i < r->rewrite_nr; i++) {
67 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
68 if (!prefixcmp(url, r->rewrite[i]->instead_of[j].s) &&
70 longest->len < r->rewrite[i]->instead_of[j].len)) {
71 longest = &(r->rewrite[i]->instead_of[j]);
79 ret = xmalloc(r->rewrite[longest_i]->baselen +
80 (strlen(url) - longest->len) + 1);
81 strcpy(ret, r->rewrite[longest_i]->base);
82 strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
86 static void add_push_refspec(struct remote *remote, const char *ref)
88 ALLOC_GROW(remote->push_refspec,
89 remote->push_refspec_nr + 1,
90 remote->push_refspec_alloc);
91 remote->push_refspec[remote->push_refspec_nr++] = ref;
94 static void add_fetch_refspec(struct remote *remote, const char *ref)
96 ALLOC_GROW(remote->fetch_refspec,
97 remote->fetch_refspec_nr + 1,
98 remote->fetch_refspec_alloc);
99 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
102 static void add_url(struct remote *remote, const char *url)
104 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
105 remote->url[remote->url_nr++] = url;
108 static void add_pushurl(struct remote *remote, const char *pushurl)
110 ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
111 remote->pushurl[remote->pushurl_nr++] = pushurl;
114 static void add_pushurl_alias(struct remote *remote, const char *url)
116 const char *pushurl = alias_url(url, &rewrites_push);
118 add_pushurl(remote, pushurl);
121 static void add_url_alias(struct remote *remote, const char *url)
123 add_url(remote, alias_url(url, &rewrites));
124 add_pushurl_alias(remote, url);
127 static struct remote *make_remote(const char *name, int len)
132 for (i = 0; i < remotes_nr; i++) {
133 if (len ? (!strncmp(name, remotes[i]->name, len) &&
134 !remotes[i]->name[len]) :
135 !strcmp(name, remotes[i]->name))
139 ret = xcalloc(1, sizeof(struct remote));
140 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
141 remotes[remotes_nr++] = ret;
143 ret->name = xstrndup(name, len);
145 ret->name = xstrdup(name);
149 static void add_merge(struct branch *branch, const char *name)
151 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
152 branch->merge_alloc);
153 branch->merge_name[branch->merge_nr++] = name;
156 static struct branch *make_branch(const char *name, int len)
162 for (i = 0; i < branches_nr; i++) {
163 if (len ? (!strncmp(name, branches[i]->name, len) &&
164 !branches[i]->name[len]) :
165 !strcmp(name, branches[i]->name))
169 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
170 ret = xcalloc(1, sizeof(struct branch));
171 branches[branches_nr++] = ret;
173 ret->name = xstrndup(name, len);
175 ret->name = xstrdup(name);
176 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
177 strcpy(refname, "refs/heads/");
178 strcpy(refname + strlen("refs/heads/"), ret->name);
179 ret->refname = refname;
184 static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
189 for (i = 0; i < r->rewrite_nr; i++) {
191 ? (len == r->rewrite[i]->baselen &&
192 !strncmp(base, r->rewrite[i]->base, len))
193 : !strcmp(base, r->rewrite[i]->base))
194 return r->rewrite[i];
197 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
198 ret = xcalloc(1, sizeof(struct rewrite));
199 r->rewrite[r->rewrite_nr++] = ret;
201 ret->base = xstrndup(base, len);
205 ret->base = xstrdup(base);
206 ret->baselen = strlen(base);
211 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
213 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
214 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
215 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
216 rewrite->instead_of_nr++;
219 static void read_remotes_file(struct remote *remote)
221 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
225 remote->origin = REMOTE_REMOTES;
226 while (fgets(buffer, BUF_SIZE, f)) {
230 if (!prefixcmp(buffer, "URL:")) {
233 } else if (!prefixcmp(buffer, "Push:")) {
236 } else if (!prefixcmp(buffer, "Pull:")) {
248 while (isspace(p[-1]))
251 switch (value_list) {
253 add_url_alias(remote, xstrdup(s));
256 add_push_refspec(remote, xstrdup(s));
259 add_fetch_refspec(remote, xstrdup(s));
266 static void read_branches_file(struct remote *remote)
268 const char *slash = strchr(remote->name, '/');
270 struct strbuf branch = STRBUF_INIT;
271 int n = slash ? slash - remote->name : 1000;
272 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
278 s = fgets(buffer, BUF_SIZE, f);
286 remote->origin = REMOTE_BRANCHES;
288 while (isspace(p[-1]))
292 len += strlen(slash);
293 p = xmalloc(len + 1);
299 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
300 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
301 * the partial URL obtained from the branches file plus
302 * "/netdev-2.6" and does not store it in any tracking ref.
303 * #branch specifier in the file is ignored.
305 * Otherwise, the branches file would have URL and optionally
306 * #branch specified. The "master" (or specified) branch is
307 * fetched and stored in the local branch of the same name.
309 frag = strchr(p, '#');
312 strbuf_addf(&branch, "refs/heads/%s", frag);
314 strbuf_addstr(&branch, "refs/heads/master");
316 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
318 strbuf_reset(&branch);
319 strbuf_addstr(&branch, "HEAD:");
321 add_url_alias(remote, p);
322 add_fetch_refspec(remote, strbuf_detach(&branch, NULL));
324 * Cogito compatible push: push current HEAD to remote #branch
325 * (master if missing)
327 strbuf_init(&branch, 0);
328 strbuf_addstr(&branch, "HEAD");
330 strbuf_addf(&branch, ":refs/heads/%s", frag);
332 strbuf_addstr(&branch, ":refs/heads/master");
333 add_push_refspec(remote, strbuf_detach(&branch, NULL));
334 remote->fetch_tags = 1; /* always auto-follow */
337 static int handle_config(const char *key, const char *value, void *cb)
341 struct remote *remote;
342 struct branch *branch;
343 if (!prefixcmp(key, "branch.")) {
345 subkey = strrchr(name, '.');
348 branch = make_branch(name, subkey - name);
349 if (!strcmp(subkey, ".remote")) {
351 return config_error_nonbool(key);
352 branch->remote_name = xstrdup(value);
353 if (branch == current_branch) {
354 default_remote_name = branch->remote_name;
355 explicit_default_remote_name = 1;
357 } else if (!strcmp(subkey, ".merge")) {
359 return config_error_nonbool(key);
360 add_merge(branch, xstrdup(value));
364 if (!prefixcmp(key, "url.")) {
365 struct rewrite *rewrite;
367 subkey = strrchr(name, '.');
370 if (!strcmp(subkey, ".insteadof")) {
371 rewrite = make_rewrite(&rewrites, name, subkey - name);
373 return config_error_nonbool(key);
374 add_instead_of(rewrite, xstrdup(value));
375 } else if (!strcmp(subkey, ".pushinsteadof")) {
376 rewrite = make_rewrite(&rewrites_push, name, subkey - name);
378 return config_error_nonbool(key);
379 add_instead_of(rewrite, xstrdup(value));
382 if (prefixcmp(key, "remote."))
386 warning("Config remote shorthand cannot begin with '/': %s",
390 subkey = strrchr(name, '.');
393 remote = make_remote(name, subkey - name);
394 remote->origin = REMOTE_CONFIG;
395 if (!strcmp(subkey, ".mirror"))
396 remote->mirror = git_config_bool(key, value);
397 else if (!strcmp(subkey, ".skipdefaultupdate"))
398 remote->skip_default_update = git_config_bool(key, value);
399 else if (!strcmp(subkey, ".skipfetchall"))
400 remote->skip_default_update = git_config_bool(key, value);
401 else if (!strcmp(subkey, ".url")) {
403 if (git_config_string(&v, key, value))
406 } else if (!strcmp(subkey, ".pushurl")) {
408 if (git_config_string(&v, key, value))
410 add_pushurl(remote, v);
411 } else if (!strcmp(subkey, ".push")) {
413 if (git_config_string(&v, key, value))
415 add_push_refspec(remote, v);
416 } else if (!strcmp(subkey, ".fetch")) {
418 if (git_config_string(&v, key, value))
420 add_fetch_refspec(remote, v);
421 } else if (!strcmp(subkey, ".receivepack")) {
423 if (git_config_string(&v, key, value))
425 if (!remote->receivepack)
426 remote->receivepack = v;
428 error("more than one receivepack given, using the first");
429 } else if (!strcmp(subkey, ".uploadpack")) {
431 if (git_config_string(&v, key, value))
433 if (!remote->uploadpack)
434 remote->uploadpack = v;
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,
447 static void alias_all_urls(void)
450 for (i = 0; i < remotes_nr; i++) {
451 int add_pushurl_aliases;
454 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
455 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
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);
466 static void read_config(void)
468 unsigned char sha1[20];
469 const char *head_ref;
471 if (default_remote_name) // did this already
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/")) {
479 make_branch(head_ref + strlen("refs/heads/"), 0);
481 git_config(handle_config, NULL);
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.
492 static int verify_refname(char *name, int is_glob)
496 result = check_ref_format(name);
497 if (is_glob && result == CHECK_REF_FORMAT_WILDCARD)
498 result = CHECK_REF_FORMAT_OK;
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.
508 static void free_refspecs(struct refspec *refspec, int nr_refspec)
515 for (i = 0; i < nr_refspec; i++) {
516 free(refspec[i].src);
517 free(refspec[i].dst);
522 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
526 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
528 for (i = 0; i < nr_refspec; i++) {
531 const char *lhs, *rhs;
541 rhs = strrchr(lhs, ':');
544 * Before going on, special case ":" (or "+:") as a refspec
547 if (!fetch && rhs == lhs && rhs[1] == '\0') {
553 size_t rlen = strlen(++rhs);
554 is_glob = (1 <= rlen && strchr(rhs, '*'));
555 rs[i].dst = xstrndup(rhs, rlen);
558 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
559 if (1 <= llen && memchr(lhs, '*', llen)) {
560 if ((rhs && !is_glob) || (!rhs && fetch))
563 } else if (rhs && is_glob) {
567 rs[i].pattern = is_glob;
568 rs[i].src = xstrndup(lhs, llen);
573 * - empty is allowed; it means HEAD.
574 * - otherwise it must be a valid looking ref.
579 st = verify_refname(rs[i].src, is_glob);
580 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
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.
591 } else if (!*rs[i].dst) {
594 st = verify_refname(rs[i].dst, is_glob);
595 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
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.
609 st = verify_refname(rs[i].src, is_glob);
610 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
614 ; /* anything goes, for now */
617 * - missing is allowed, but LHS then must be a
619 * - empty is not allowed.
620 * - otherwise it must be a valid looking ref.
623 st = verify_refname(rs[i].src, is_glob);
624 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
626 } else if (!*rs[i].dst) {
629 st = verify_refname(rs[i].dst, is_glob);
630 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
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.
644 free_refspecs(rs, i+1);
647 die("Invalid refspec '%s'", refspec[i]);
650 int valid_fetch_refspec(const char *fetch_refspec_str)
652 const char *fetch_refspec[] = { fetch_refspec_str };
653 struct refspec *refspec;
655 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
656 free_refspecs(refspec, 1);
660 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
662 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
665 static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
667 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
670 static int valid_remote_nick(const char *name)
672 if (!name[0] || is_dot_or_dotdot(name))
674 return !strchr(name, '/'); /* no slash */
677 struct remote *remote_get(const char *name)
686 name = default_remote_name;
687 name_given = explicit_default_remote_name;
690 ret = make_remote(name, 0);
691 if (valid_remote_nick(name)) {
693 read_remotes_file(ret);
695 read_branches_file(ret);
697 if (name_given && !ret->url)
698 add_url_alias(ret, name);
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);
706 int remote_is_configured(const char *name)
711 for (i = 0; i < remotes_nr; i++)
712 if (!strcmp(name, remotes[i]->name))
717 int for_each_remote(each_remote_fn fn, void *priv)
721 for (i = 0; i < remotes_nr && !result; i++) {
722 struct remote *r = remotes[i];
726 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
729 r->push = parse_push_refspec(r->push_refspec_nr,
731 result = fn(r, priv);
736 void ref_remove_duplicates(struct ref *ref_map)
740 for (; ref_map; ref_map = ref_map->next) {
741 if (!ref_map->peer_ref)
743 posn = &ref_map->next;
745 if ((*posn)->peer_ref &&
746 !strcmp((*posn)->peer_ref->name,
747 ref_map->peer_ref->name)) {
748 if (strcmp((*posn)->name, ref_map->name))
749 die("%s tracks both %s and %s",
750 ref_map->peer_ref->name,
751 (*posn)->name, ref_map->name);
752 next = (*posn)->next;
753 free((*posn)->peer_ref);
757 posn = &(*posn)->next;
763 int remote_has_url(struct remote *remote, const char *url)
766 for (i = 0; i < remote->url_nr; i++) {
767 if (!strcmp(remote->url[i], url))
773 static int match_name_with_pattern(const char *key, const char *name,
774 const char *value, char **result)
776 const char *kstar = strchr(key, '*');
782 die("Key '%s' of pattern had no '*'", key);
784 ksuffixlen = strlen(kstar + 1);
785 namelen = strlen(name);
786 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
787 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
789 const char *vstar = strchr(value, '*');
793 die("Value '%s' of pattern has no '*'", value);
794 vlen = vstar - value;
795 vsuffixlen = strlen(vstar + 1);
796 *result = xmalloc(vlen + vsuffixlen +
798 klen - ksuffixlen + 1);
799 strncpy(*result, value, vlen);
800 strncpy(*result + vlen,
801 name + klen, namelen - klen - ksuffixlen);
802 strcpy(*result + vlen + namelen - klen - ksuffixlen,
808 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
810 int find_src = refspec->src == NULL;
811 char *needle, **result;
816 return error("find_tracking: need either src or dst");
817 needle = refspec->dst;
818 result = &refspec->src;
820 needle = refspec->src;
821 result = &refspec->dst;
824 for (i = 0; i < remote->fetch_refspec_nr; i++) {
825 struct refspec *fetch = &remote->fetch[i];
826 const char *key = find_src ? fetch->dst : fetch->src;
827 const char *value = find_src ? fetch->src : fetch->dst;
830 if (fetch->pattern) {
831 if (match_name_with_pattern(key, needle, value, result)) {
832 refspec->force = fetch->force;
835 } else if (!strcmp(needle, key)) {
836 *result = xstrdup(value);
837 refspec->force = fetch->force;
844 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
847 size_t len = strlen(name);
848 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
849 memcpy(ref->name, prefix, prefixlen);
850 memcpy(ref->name + prefixlen, name, len);
854 struct ref *alloc_ref(const char *name)
856 return alloc_ref_with_prefix("", 0, name);
859 static struct ref *copy_ref(const struct ref *ref)
865 len = strlen(ref->name);
866 cpy = xmalloc(sizeof(struct ref) + len + 1);
867 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
869 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
870 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
871 cpy->peer_ref = copy_ref(ref->peer_ref);
875 struct ref *copy_ref_list(const struct ref *ref)
877 struct ref *ret = NULL;
878 struct ref **tail = &ret;
880 *tail = copy_ref(ref);
882 tail = &((*tail)->next);
887 static void free_ref(struct ref *ref)
891 free_ref(ref->peer_ref);
892 free(ref->remote_status);
897 void free_refs(struct ref *ref)
907 static int count_refspec_match(const char *pattern,
909 struct ref **matched_ref)
911 int patlen = strlen(pattern);
912 struct ref *matched_weak = NULL;
913 struct ref *matched = NULL;
917 for (weak_match = match = 0; refs; refs = refs->next) {
918 char *name = refs->name;
919 int namelen = strlen(name);
921 if (!refname_match(pattern, name, ref_rev_parse_rules))
924 /* A match is "weak" if it is with refs outside
925 * heads or tags, and did not specify the pattern
926 * in full (e.g. "refs/remotes/origin/master") or at
927 * least from the toplevel (e.g. "remotes/origin/master");
928 * otherwise "git push $URL master" would result in
929 * ambiguity between remotes/origin/master and heads/master
930 * at the remote site.
932 if (namelen != patlen &&
933 patlen != namelen - 5 &&
934 prefixcmp(name, "refs/heads/") &&
935 prefixcmp(name, "refs/tags/")) {
936 /* We want to catch the case where only weak
937 * matches are found and there are multiple
938 * matches, and where more than one strong
939 * matches are found, as ambiguous. One
940 * strong match with zero or more weak matches
941 * are acceptable as a unique match.
952 *matched_ref = matched_weak;
956 *matched_ref = matched;
961 static void tail_link_ref(struct ref *ref, struct ref ***tail)
969 static struct ref *try_explicit_object_name(const char *name)
971 unsigned char sha1[20];
975 ref = alloc_ref("(delete)");
976 hashclr(ref->new_sha1);
979 if (get_sha1(name, sha1))
981 ref = alloc_ref(name);
982 hashcpy(ref->new_sha1, sha1);
986 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
988 struct ref *ret = alloc_ref(name);
989 tail_link_ref(ret, tail);
993 static char *guess_ref(const char *name, struct ref *peer)
995 struct strbuf buf = STRBUF_INIT;
996 unsigned char sha1[20];
998 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
1002 if (!prefixcmp(r, "refs/heads/"))
1003 strbuf_addstr(&buf, "refs/heads/");
1004 else if (!prefixcmp(r, "refs/tags/"))
1005 strbuf_addstr(&buf, "refs/tags/");
1009 strbuf_addstr(&buf, name);
1010 return strbuf_detach(&buf, NULL);
1013 static int match_explicit(struct ref *src, struct ref *dst,
1014 struct ref ***dst_tail,
1017 struct ref *matched_src, *matched_dst;
1020 const char *dst_value = rs->dst;
1023 if (rs->pattern || rs->matching)
1026 matched_src = matched_dst = NULL;
1027 switch (count_refspec_match(rs->src, src, &matched_src)) {
1032 /* The source could be in the get_sha1() format
1033 * not a reference name. :refs/other is a
1034 * way to delete 'other' ref at the remote end.
1036 matched_src = try_explicit_object_name(rs->src);
1038 return error("src refspec %s does not match any.", rs->src);
1042 return error("src refspec %s matches more than one.", rs->src);
1046 unsigned char sha1[20];
1049 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
1051 ((flag & REF_ISSYMREF) &&
1052 prefixcmp(dst_value, "refs/heads/")))
1053 die("%s cannot be resolved to branch.",
1057 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1061 if (!memcmp(dst_value, "refs/", 5))
1062 matched_dst = make_linked_ref(dst_value, dst_tail);
1063 else if ((dst_guess = guess_ref(dst_value, matched_src)))
1064 matched_dst = make_linked_ref(dst_guess, dst_tail);
1066 error("unable to push to unqualified destination: %s\n"
1067 "The destination refspec neither matches an "
1068 "existing ref on the remote nor\n"
1069 "begins with refs/, and we are unable to "
1070 "guess a prefix based on the source ref.",
1075 error("dst refspec %s matches more than one.",
1081 if (matched_dst->peer_ref)
1082 return error("dst ref %s receives from more than one src.",
1085 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
1086 matched_dst->force = rs->force;
1091 static int match_explicit_refs(struct ref *src, struct ref *dst,
1092 struct ref ***dst_tail, struct refspec *rs,
1096 for (i = errs = 0; i < rs_nr; i++)
1097 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1101 static const struct refspec *check_pattern_match(const struct refspec *rs,
1103 const struct ref *src)
1106 int matching_refs = -1;
1107 for (i = 0; i < rs_nr; i++) {
1108 if (rs[i].matching &&
1109 (matching_refs == -1 || rs[i].force)) {
1114 if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,
1118 if (matching_refs != -1)
1119 return rs + matching_refs;
1124 static struct ref **tail_ref(struct ref **head)
1126 struct ref **tail = head;
1128 tail = &((*tail)->next);
1133 * Note. This is used only by "push"; refspec matching rules for
1134 * push and fetch are subtly different, so do not try to reuse it
1137 int match_refs(struct ref *src, struct ref **dst,
1138 int nr_refspec, const char **refspec, int flags)
1141 int send_all = flags & MATCH_REFS_ALL;
1142 int send_mirror = flags & MATCH_REFS_MIRROR;
1144 static const char *default_refspec[] = { ":", NULL };
1145 struct ref **dst_tail = tail_ref(dst);
1149 refspec = default_refspec;
1151 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1152 errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec);
1154 /* pick the remainder */
1155 for ( ; src; src = src->next) {
1156 struct ref *dst_peer;
1157 const struct refspec *pat = NULL;
1162 pat = check_pattern_match(rs, nr_refspec, src);
1166 if (pat->matching) {
1168 * "matching refs"; traditionally we pushed everything
1169 * including refs outside refs/heads/ hierarchy, but
1170 * that does not make much sense these days.
1172 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1174 dst_name = xstrdup(src->name);
1177 const char *dst_side = pat->dst ? pat->dst : pat->src;
1178 if (!match_name_with_pattern(pat->src, src->name,
1179 dst_side, &dst_name))
1180 die("Didn't think it matches any more");
1182 dst_peer = find_ref_by_name(*dst, dst_name);
1184 if (dst_peer->peer_ref)
1185 /* We're already sending something to this ref. */
1189 if (pat->matching && !(send_all || send_mirror))
1191 * Remote doesn't have it, and we have no
1192 * explicit pattern, and we don't have
1193 * --all nor --mirror.
1197 /* Create a new one and link it */
1198 dst_peer = make_linked_ref(dst_name, &dst_tail);
1199 hashcpy(dst_peer->new_sha1, src->new_sha1);
1201 dst_peer->peer_ref = copy_ref(src);
1202 dst_peer->force = pat->force;
1211 struct branch *branch_get(const char *name)
1216 if (!name || !*name || !strcmp(name, "HEAD"))
1217 ret = current_branch;
1219 ret = make_branch(name, 0);
1220 if (ret && ret->remote_name) {
1221 ret->remote = remote_get(ret->remote_name);
1222 if (ret->merge_nr) {
1224 ret->merge = xcalloc(sizeof(*ret->merge),
1226 for (i = 0; i < ret->merge_nr; i++) {
1227 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1228 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1229 if (remote_find_tracking(ret->remote, ret->merge[i])
1230 && !strcmp(ret->remote_name, "."))
1231 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1238 int branch_has_merge_config(struct branch *branch)
1240 return branch && !!branch->merge;
1243 int branch_merge_matches(struct branch *branch,
1245 const char *refname)
1247 if (!branch || i < 0 || i >= branch->merge_nr)
1249 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1252 static struct ref *get_expanded_map(const struct ref *remote_refs,
1253 const struct refspec *refspec)
1255 const struct ref *ref;
1256 struct ref *ret = NULL;
1257 struct ref **tail = &ret;
1261 for (ref = remote_refs; ref; ref = ref->next) {
1262 if (strchr(ref->name, '^'))
1263 continue; /* a dereference item */
1264 if (match_name_with_pattern(refspec->src, ref->name,
1265 refspec->dst, &expn_name)) {
1266 struct ref *cpy = copy_ref(ref);
1268 cpy->peer_ref = alloc_ref(expn_name);
1271 cpy->peer_ref->force = 1;
1280 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1282 const struct ref *ref;
1283 for (ref = refs; ref; ref = ref->next) {
1284 if (refname_match(name, ref->name, ref_fetch_rules))
1290 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1292 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1297 return copy_ref(ref);
1300 static struct ref *get_local_ref(const char *name)
1302 if (!name || name[0] == '\0')
1305 if (!prefixcmp(name, "refs/"))
1306 return alloc_ref(name);
1308 if (!prefixcmp(name, "heads/") ||
1309 !prefixcmp(name, "tags/") ||
1310 !prefixcmp(name, "remotes/"))
1311 return alloc_ref_with_prefix("refs/", 5, name);
1313 return alloc_ref_with_prefix("refs/heads/", 11, name);
1316 int get_fetch_map(const struct ref *remote_refs,
1317 const struct refspec *refspec,
1321 struct ref *ref_map, **rmp;
1323 if (refspec->pattern) {
1324 ref_map = get_expanded_map(remote_refs, refspec);
1326 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1328 ref_map = get_remote_ref(remote_refs, name);
1329 if (!missing_ok && !ref_map)
1330 die("Couldn't find remote ref %s", name);
1332 ref_map->peer_ref = get_local_ref(refspec->dst);
1333 if (ref_map->peer_ref && refspec->force)
1334 ref_map->peer_ref->force = 1;
1338 for (rmp = &ref_map; *rmp; ) {
1339 if ((*rmp)->peer_ref) {
1340 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1341 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1342 struct ref *ignore = *rmp;
1343 error("* Ignoring funny ref '%s' locally",
1344 (*rmp)->peer_ref->name);
1345 *rmp = (*rmp)->next;
1346 free(ignore->peer_ref);
1351 rmp = &((*rmp)->next);
1355 tail_link_ref(ref_map, tail);
1360 int resolve_remote_symref(struct ref *ref, struct ref *list)
1364 for (; list; list = list->next)
1365 if (!strcmp(ref->symref, list->name)) {
1366 hashcpy(ref->old_sha1, list->old_sha1);
1372 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1375 struct commit_list *temp = list;
1376 temp->item->object.flags &= ~mark;
1382 int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1385 struct commit *old, *new;
1386 struct commit_list *list, *used;
1389 /* Both new and old must be commit-ish and new is descendant of
1390 * old. Otherwise we require --force.
1392 o = deref_tag(parse_object(old_sha1), NULL, 0);
1393 if (!o || o->type != OBJ_COMMIT)
1395 old = (struct commit *) o;
1397 o = deref_tag(parse_object(new_sha1), NULL, 0);
1398 if (!o || o->type != OBJ_COMMIT)
1400 new = (struct commit *) o;
1402 if (parse_commit(new) < 0)
1406 commit_list_insert(new, &list);
1408 new = pop_most_recent_commit(&list, TMP_MARK);
1409 commit_list_insert(new, &used);
1415 unmark_and_free(list, TMP_MARK);
1416 unmark_and_free(used, TMP_MARK);
1421 * Return true if there is anything to report, otherwise false.
1423 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1425 unsigned char sha1[20];
1426 struct commit *ours, *theirs;
1428 struct rev_info revs;
1429 const char *rev_argv[10], *base;
1433 * Nothing to report unless we are marked to build on top of
1437 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1441 * If what we used to build on no longer exists, there is
1442 * nothing to report.
1444 base = branch->merge[0]->dst;
1445 if (!resolve_ref(base, sha1, 1, NULL))
1447 theirs = lookup_commit_reference(sha1);
1451 if (!resolve_ref(branch->refname, sha1, 1, NULL))
1453 ours = lookup_commit_reference(sha1);
1457 /* are we the same? */
1461 /* Run "rev-list --left-right ours...theirs" internally... */
1463 rev_argv[rev_argc++] = NULL;
1464 rev_argv[rev_argc++] = "--left-right";
1465 rev_argv[rev_argc++] = symmetric;
1466 rev_argv[rev_argc++] = "--";
1467 rev_argv[rev_argc] = NULL;
1469 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1470 strcpy(symmetric + 40, "...");
1471 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1473 init_revisions(&revs, NULL);
1474 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1475 prepare_revision_walk(&revs);
1477 /* ... and count the commits on each side. */
1481 struct commit *c = get_revision(&revs);
1484 if (c->object.flags & SYMMETRIC_LEFT)
1490 /* clear object flags smudged by the above traversal */
1491 clear_commit_marks(ours, ALL_REV_FLAGS);
1492 clear_commit_marks(theirs, ALL_REV_FLAGS);
1497 * Return true when there is anything to report, otherwise false.
1499 int format_tracking_info(struct branch *branch, struct strbuf *sb)
1501 int num_ours, num_theirs;
1504 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1507 base = branch->merge[0]->dst;
1508 base = shorten_unambiguous_ref(base, 0);
1510 strbuf_addf(sb, "Your branch is ahead of '%s' "
1511 "by %d commit%s.\n",
1512 base, num_ours, (num_ours == 1) ? "" : "s");
1514 strbuf_addf(sb, "Your branch is behind '%s' "
1516 "and can be fast-forwarded.\n",
1517 base, num_theirs, (num_theirs == 1) ? "" : "s");
1519 strbuf_addf(sb, "Your branch and '%s' have diverged,\n"
1520 "and have %d and %d different commit(s) each, "
1522 base, num_ours, num_theirs);
1526 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1528 struct ref ***local_tail = cb_data;
1532 /* we already know it starts with refs/ to get here */
1533 if (check_ref_format(refname + 5))
1536 len = strlen(refname) + 1;
1537 ref = xcalloc(1, sizeof(*ref) + len);
1538 hashcpy(ref->new_sha1, sha1);
1539 memcpy(ref->name, refname, len);
1541 *local_tail = &ref->next;
1545 struct ref *get_local_heads(void)
1547 struct ref *local_refs = NULL, **local_tail = &local_refs;
1548 for_each_ref(one_local_ref, &local_tail);
1552 struct ref *guess_remote_head(const struct ref *head,
1553 const struct ref *refs,
1556 const struct ref *r;
1557 struct ref *list = NULL;
1558 struct ref **tail = &list;
1564 * Some transports support directly peeking at
1565 * where HEAD points; if that is the case, then
1566 * we don't have to guess.
1569 return copy_ref(find_ref_by_name(refs, head->symref));
1571 /* If refs/heads/master could be right, it is. */
1573 r = find_ref_by_name(refs, "refs/heads/master");
1574 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1578 /* Look for another ref that points there */
1579 for (r = refs; r; r = r->next) {
1580 if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
1581 *tail = copy_ref(r);
1582 tail = &((*tail)->next);