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 int valid_remote(const struct remote *remote)
60 static const char *alias_url(const char *url, struct rewrites *r)
64 struct counted_string *longest;
69 for (i = 0; i < r->rewrite_nr; i++) {
72 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
73 if (!prefixcmp(url, r->rewrite[i]->instead_of[j].s) &&
75 longest->len < r->rewrite[i]->instead_of[j].len)) {
76 longest = &(r->rewrite[i]->instead_of[j]);
84 ret = xmalloc(r->rewrite[longest_i]->baselen +
85 (strlen(url) - longest->len) + 1);
86 strcpy(ret, r->rewrite[longest_i]->base);
87 strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
91 static void add_push_refspec(struct remote *remote, const char *ref)
93 ALLOC_GROW(remote->push_refspec,
94 remote->push_refspec_nr + 1,
95 remote->push_refspec_alloc);
96 remote->push_refspec[remote->push_refspec_nr++] = ref;
99 static void add_fetch_refspec(struct remote *remote, const char *ref)
101 ALLOC_GROW(remote->fetch_refspec,
102 remote->fetch_refspec_nr + 1,
103 remote->fetch_refspec_alloc);
104 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
107 static void add_url(struct remote *remote, const char *url)
109 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
110 remote->url[remote->url_nr++] = url;
113 static void add_pushurl(struct remote *remote, const char *pushurl)
115 ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
116 remote->pushurl[remote->pushurl_nr++] = pushurl;
119 static void add_pushurl_alias(struct remote *remote, const char *url)
121 const char *pushurl = alias_url(url, &rewrites_push);
123 add_pushurl(remote, pushurl);
126 static void add_url_alias(struct remote *remote, const char *url)
128 add_url(remote, alias_url(url, &rewrites));
129 add_pushurl_alias(remote, url);
132 static struct remote *make_remote(const char *name, int len)
137 for (i = 0; i < remotes_nr; i++) {
138 if (len ? (!strncmp(name, remotes[i]->name, len) &&
139 !remotes[i]->name[len]) :
140 !strcmp(name, remotes[i]->name))
144 ret = xcalloc(1, sizeof(struct remote));
145 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
146 remotes[remotes_nr++] = ret;
148 ret->name = xstrndup(name, len);
150 ret->name = xstrdup(name);
154 static void add_merge(struct branch *branch, const char *name)
156 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
157 branch->merge_alloc);
158 branch->merge_name[branch->merge_nr++] = name;
161 static struct branch *make_branch(const char *name, int len)
167 for (i = 0; i < branches_nr; i++) {
168 if (len ? (!strncmp(name, branches[i]->name, len) &&
169 !branches[i]->name[len]) :
170 !strcmp(name, branches[i]->name))
174 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
175 ret = xcalloc(1, sizeof(struct branch));
176 branches[branches_nr++] = ret;
178 ret->name = xstrndup(name, len);
180 ret->name = xstrdup(name);
181 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
182 strcpy(refname, "refs/heads/");
183 strcpy(refname + strlen("refs/heads/"), ret->name);
184 ret->refname = refname;
189 static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
194 for (i = 0; i < r->rewrite_nr; i++) {
196 ? (len == r->rewrite[i]->baselen &&
197 !strncmp(base, r->rewrite[i]->base, len))
198 : !strcmp(base, r->rewrite[i]->base))
199 return r->rewrite[i];
202 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
203 ret = xcalloc(1, sizeof(struct rewrite));
204 r->rewrite[r->rewrite_nr++] = ret;
206 ret->base = xstrndup(base, len);
210 ret->base = xstrdup(base);
211 ret->baselen = strlen(base);
216 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
218 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
219 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
220 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
221 rewrite->instead_of_nr++;
224 static void read_remotes_file(struct remote *remote)
226 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
230 remote->origin = REMOTE_REMOTES;
231 while (fgets(buffer, BUF_SIZE, f)) {
235 if (!prefixcmp(buffer, "URL:")) {
238 } else if (!prefixcmp(buffer, "Push:")) {
241 } else if (!prefixcmp(buffer, "Pull:")) {
253 while (isspace(p[-1]))
256 switch (value_list) {
258 add_url_alias(remote, xstrdup(s));
261 add_push_refspec(remote, xstrdup(s));
264 add_fetch_refspec(remote, xstrdup(s));
271 static void read_branches_file(struct remote *remote)
273 const char *slash = strchr(remote->name, '/');
275 struct strbuf branch = STRBUF_INIT;
276 int n = slash ? slash - remote->name : 1000;
277 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
283 s = fgets(buffer, BUF_SIZE, f);
291 remote->origin = REMOTE_BRANCHES;
293 while (isspace(p[-1]))
297 len += strlen(slash);
298 p = xmalloc(len + 1);
304 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
305 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
306 * the partial URL obtained from the branches file plus
307 * "/netdev-2.6" and does not store it in any tracking ref.
308 * #branch specifier in the file is ignored.
310 * Otherwise, the branches file would have URL and optionally
311 * #branch specified. The "master" (or specified) branch is
312 * fetched and stored in the local branch of the same name.
314 frag = strchr(p, '#');
317 strbuf_addf(&branch, "refs/heads/%s", frag);
319 strbuf_addstr(&branch, "refs/heads/master");
321 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
323 strbuf_reset(&branch);
324 strbuf_addstr(&branch, "HEAD:");
326 add_url_alias(remote, p);
327 add_fetch_refspec(remote, strbuf_detach(&branch, NULL));
329 * Cogito compatible push: push current HEAD to remote #branch
330 * (master if missing)
332 strbuf_init(&branch, 0);
333 strbuf_addstr(&branch, "HEAD");
335 strbuf_addf(&branch, ":refs/heads/%s", frag);
337 strbuf_addstr(&branch, ":refs/heads/master");
338 add_push_refspec(remote, strbuf_detach(&branch, NULL));
339 remote->fetch_tags = 1; /* always auto-follow */
342 static int handle_config(const char *key, const char *value, void *cb)
346 struct remote *remote;
347 struct branch *branch;
348 if (!prefixcmp(key, "branch.")) {
350 subkey = strrchr(name, '.');
353 branch = make_branch(name, subkey - name);
354 if (!strcmp(subkey, ".remote")) {
356 return config_error_nonbool(key);
357 branch->remote_name = xstrdup(value);
358 if (branch == current_branch) {
359 default_remote_name = branch->remote_name;
360 explicit_default_remote_name = 1;
362 } else if (!strcmp(subkey, ".merge")) {
364 return config_error_nonbool(key);
365 add_merge(branch, xstrdup(value));
369 if (!prefixcmp(key, "url.")) {
370 struct rewrite *rewrite;
372 subkey = strrchr(name, '.');
375 if (!strcmp(subkey, ".insteadof")) {
376 rewrite = make_rewrite(&rewrites, name, subkey - name);
378 return config_error_nonbool(key);
379 add_instead_of(rewrite, xstrdup(value));
380 } else if (!strcmp(subkey, ".pushinsteadof")) {
381 rewrite = make_rewrite(&rewrites_push, name, subkey - name);
383 return config_error_nonbool(key);
384 add_instead_of(rewrite, xstrdup(value));
387 if (prefixcmp(key, "remote."))
391 warning("Config remote shorthand cannot begin with '/': %s",
395 subkey = strrchr(name, '.');
398 remote = make_remote(name, subkey - name);
399 remote->origin = REMOTE_CONFIG;
400 if (!strcmp(subkey, ".mirror"))
401 remote->mirror = git_config_bool(key, value);
402 else if (!strcmp(subkey, ".skipdefaultupdate"))
403 remote->skip_default_update = git_config_bool(key, value);
405 else if (!strcmp(subkey, ".url")) {
407 if (git_config_string(&v, key, value))
410 } else if (!strcmp(subkey, ".pushurl")) {
412 if (git_config_string(&v, key, value))
414 add_pushurl(remote, v);
415 } else if (!strcmp(subkey, ".push")) {
417 if (git_config_string(&v, key, value))
419 add_push_refspec(remote, v);
420 } else if (!strcmp(subkey, ".fetch")) {
422 if (git_config_string(&v, key, value))
424 add_fetch_refspec(remote, v);
425 } else if (!strcmp(subkey, ".receivepack")) {
427 if (git_config_string(&v, key, value))
429 if (!remote->receivepack)
430 remote->receivepack = v;
432 error("more than one receivepack given, using the first");
433 } else if (!strcmp(subkey, ".uploadpack")) {
435 if (git_config_string(&v, key, value))
437 if (!remote->uploadpack)
438 remote->uploadpack = v;
440 error("more than one uploadpack given, using the first");
441 } else if (!strcmp(subkey, ".tagopt")) {
442 if (!strcmp(value, "--no-tags"))
443 remote->fetch_tags = -1;
444 } else if (!strcmp(subkey, ".proxy")) {
445 return git_config_string((const char **)&remote->http_proxy,
451 static void alias_all_urls(void)
454 for (i = 0; i < remotes_nr; i++) {
455 int add_pushurl_aliases;
458 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
459 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
461 add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
462 for (j = 0; j < remotes[i]->url_nr; j++) {
463 if (add_pushurl_aliases)
464 add_pushurl_alias(remotes[i], remotes[i]->url[j]);
465 remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
470 static void read_config(void)
472 unsigned char sha1[20];
473 const char *head_ref;
475 if (default_remote_name) // did this already
477 default_remote_name = xstrdup("origin");
478 current_branch = NULL;
479 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
480 if (head_ref && (flag & REF_ISSYMREF) &&
481 !prefixcmp(head_ref, "refs/heads/")) {
483 make_branch(head_ref + strlen("refs/heads/"), 0);
485 git_config(handle_config, NULL);
490 * We need to make sure the tracking branches are well formed, but a
491 * wildcard refspec in "struct refspec" must have a trailing slash. We
492 * temporarily drop the trailing '/' while calling check_ref_format(),
493 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
494 * error return is Ok for a wildcard refspec.
496 static int verify_refname(char *name, int is_glob)
500 result = check_ref_format(name);
501 if (is_glob && result == CHECK_REF_FORMAT_WILDCARD)
502 result = CHECK_REF_FORMAT_OK;
507 * This function frees a refspec array.
508 * Warning: code paths should be checked to ensure that the src
509 * and dst pointers are always freeable pointers as well
510 * as the refspec pointer itself.
512 static void free_refspecs(struct refspec *refspec, int nr_refspec)
519 for (i = 0; i < nr_refspec; i++) {
520 free(refspec[i].src);
521 free(refspec[i].dst);
526 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
530 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
532 for (i = 0; i < nr_refspec; i++) {
535 const char *lhs, *rhs;
545 rhs = strrchr(lhs, ':');
548 * Before going on, special case ":" (or "+:") as a refspec
551 if (!fetch && rhs == lhs && rhs[1] == '\0') {
557 size_t rlen = strlen(++rhs);
558 is_glob = (1 <= rlen && strchr(rhs, '*'));
559 rs[i].dst = xstrndup(rhs, rlen);
562 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
563 if (1 <= llen && memchr(lhs, '*', llen)) {
564 if ((rhs && !is_glob) || (!rhs && fetch))
567 } else if (rhs && is_glob) {
571 rs[i].pattern = is_glob;
572 rs[i].src = xstrndup(lhs, llen);
577 * - empty is allowed; it means HEAD.
578 * - otherwise it must be a valid looking ref.
583 st = verify_refname(rs[i].src, is_glob);
584 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
589 * - missing is ok, and is same as empty.
590 * - empty is ok; it means not to store.
591 * - otherwise it must be a valid looking ref.
595 } else if (!*rs[i].dst) {
598 st = verify_refname(rs[i].dst, is_glob);
599 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
605 * - empty is allowed; it means delete.
606 * - when wildcarded, it must be a valid looking ref.
607 * - otherwise, it must be an extended SHA-1, but
608 * there is no existing way to validate this.
613 st = verify_refname(rs[i].src, is_glob);
614 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
618 ; /* anything goes, for now */
621 * - missing is allowed, but LHS then must be a
623 * - empty is not allowed.
624 * - otherwise it must be a valid looking ref.
627 st = verify_refname(rs[i].src, is_glob);
628 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
630 } else if (!*rs[i].dst) {
633 st = verify_refname(rs[i].dst, is_glob);
634 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
644 * nr_refspec must be greater than zero and i must be valid
645 * since it is only possible to reach this point from within
646 * the for loop above.
648 free_refspecs(rs, i+1);
651 die("Invalid refspec '%s'", refspec[i]);
654 int valid_fetch_refspec(const char *fetch_refspec_str)
656 const char *fetch_refspec[] = { fetch_refspec_str };
657 struct refspec *refspec;
659 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
660 free_refspecs(refspec, 1);
664 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
666 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
669 static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
671 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
674 static int valid_remote_nick(const char *name)
676 if (!name[0] || is_dot_or_dotdot(name))
678 return !strchr(name, '/'); /* no slash */
681 struct remote *remote_get(const char *name)
690 name = default_remote_name;
691 name_given = explicit_default_remote_name;
694 ret = make_remote(name, 0);
695 if (valid_remote_nick(name)) {
696 if (!valid_remote(ret))
697 read_remotes_file(ret);
698 if (!valid_remote(ret))
699 read_branches_file(ret);
701 if (name_given && !valid_remote(ret))
702 add_url_alias(ret, name);
703 if (!valid_remote(ret))
705 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
706 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
710 int remote_is_configured(const char *name)
715 for (i = 0; i < remotes_nr; i++)
716 if (!strcmp(name, remotes[i]->name))
721 int for_each_remote(each_remote_fn fn, void *priv)
725 for (i = 0; i < remotes_nr && !result; i++) {
726 struct remote *r = remotes[i];
730 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
733 r->push = parse_push_refspec(r->push_refspec_nr,
735 result = fn(r, priv);
740 void ref_remove_duplicates(struct ref *ref_map)
744 for (; ref_map; ref_map = ref_map->next) {
745 if (!ref_map->peer_ref)
747 posn = &ref_map->next;
749 if ((*posn)->peer_ref &&
750 !strcmp((*posn)->peer_ref->name,
751 ref_map->peer_ref->name)) {
752 if (strcmp((*posn)->name, ref_map->name))
753 die("%s tracks both %s and %s",
754 ref_map->peer_ref->name,
755 (*posn)->name, ref_map->name);
756 next = (*posn)->next;
757 free((*posn)->peer_ref);
761 posn = &(*posn)->next;
767 int remote_has_url(struct remote *remote, const char *url)
770 for (i = 0; i < remote->url_nr; i++) {
771 if (!strcmp(remote->url[i], url))
777 static int match_name_with_pattern(const char *key, const char *name,
778 const char *value, char **result)
780 const char *kstar = strchr(key, '*');
786 die("Key '%s' of pattern had no '*'", key);
788 ksuffixlen = strlen(kstar + 1);
789 namelen = strlen(name);
790 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
791 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
793 const char *vstar = strchr(value, '*');
797 die("Value '%s' of pattern has no '*'", value);
798 vlen = vstar - value;
799 vsuffixlen = strlen(vstar + 1);
800 *result = xmalloc(vlen + vsuffixlen +
802 klen - ksuffixlen + 1);
803 strncpy(*result, value, vlen);
804 strncpy(*result + vlen,
805 name + klen, namelen - klen - ksuffixlen);
806 strcpy(*result + vlen + namelen - klen - ksuffixlen,
812 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
814 int find_src = refspec->src == NULL;
815 char *needle, **result;
820 return error("find_tracking: need either src or dst");
821 needle = refspec->dst;
822 result = &refspec->src;
824 needle = refspec->src;
825 result = &refspec->dst;
828 for (i = 0; i < remote->fetch_refspec_nr; i++) {
829 struct refspec *fetch = &remote->fetch[i];
830 const char *key = find_src ? fetch->dst : fetch->src;
831 const char *value = find_src ? fetch->src : fetch->dst;
834 if (fetch->pattern) {
835 if (match_name_with_pattern(key, needle, value, result)) {
836 refspec->force = fetch->force;
839 } else if (!strcmp(needle, key)) {
840 *result = xstrdup(value);
841 refspec->force = fetch->force;
848 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
851 size_t len = strlen(name);
852 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
853 memcpy(ref->name, prefix, prefixlen);
854 memcpy(ref->name + prefixlen, name, len);
858 struct ref *alloc_ref(const char *name)
860 return alloc_ref_with_prefix("", 0, name);
863 static struct ref *copy_ref(const struct ref *ref)
869 len = strlen(ref->name);
870 cpy = xmalloc(sizeof(struct ref) + len + 1);
871 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
873 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
874 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
875 cpy->peer_ref = copy_ref(ref->peer_ref);
879 struct ref *copy_ref_list(const struct ref *ref)
881 struct ref *ret = NULL;
882 struct ref **tail = &ret;
884 *tail = copy_ref(ref);
886 tail = &((*tail)->next);
891 static void free_ref(struct ref *ref)
895 free_ref(ref->peer_ref);
896 free(ref->remote_status);
901 void free_refs(struct ref *ref)
911 static int count_refspec_match(const char *pattern,
913 struct ref **matched_ref)
915 int patlen = strlen(pattern);
916 struct ref *matched_weak = NULL;
917 struct ref *matched = NULL;
921 for (weak_match = match = 0; refs; refs = refs->next) {
922 char *name = refs->name;
923 int namelen = strlen(name);
925 if (!refname_match(pattern, name, ref_rev_parse_rules))
928 /* A match is "weak" if it is with refs outside
929 * heads or tags, and did not specify the pattern
930 * in full (e.g. "refs/remotes/origin/master") or at
931 * least from the toplevel (e.g. "remotes/origin/master");
932 * otherwise "git push $URL master" would result in
933 * ambiguity between remotes/origin/master and heads/master
934 * at the remote site.
936 if (namelen != patlen &&
937 patlen != namelen - 5 &&
938 prefixcmp(name, "refs/heads/") &&
939 prefixcmp(name, "refs/tags/")) {
940 /* We want to catch the case where only weak
941 * matches are found and there are multiple
942 * matches, and where more than one strong
943 * matches are found, as ambiguous. One
944 * strong match with zero or more weak matches
945 * are acceptable as a unique match.
956 *matched_ref = matched_weak;
960 *matched_ref = matched;
965 static void tail_link_ref(struct ref *ref, struct ref ***tail)
973 static struct ref *try_explicit_object_name(const char *name)
975 unsigned char sha1[20];
979 ref = alloc_ref("(delete)");
980 hashclr(ref->new_sha1);
983 if (get_sha1(name, sha1))
985 ref = alloc_ref(name);
986 hashcpy(ref->new_sha1, sha1);
990 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
992 struct ref *ret = alloc_ref(name);
993 tail_link_ref(ret, tail);
997 static char *guess_ref(const char *name, struct ref *peer)
999 struct strbuf buf = STRBUF_INIT;
1000 unsigned char sha1[20];
1002 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
1006 if (!prefixcmp(r, "refs/heads/"))
1007 strbuf_addstr(&buf, "refs/heads/");
1008 else if (!prefixcmp(r, "refs/tags/"))
1009 strbuf_addstr(&buf, "refs/tags/");
1013 strbuf_addstr(&buf, name);
1014 return strbuf_detach(&buf, NULL);
1017 static int match_explicit(struct ref *src, struct ref *dst,
1018 struct ref ***dst_tail,
1021 struct ref *matched_src, *matched_dst;
1024 const char *dst_value = rs->dst;
1027 if (rs->pattern || rs->matching)
1030 matched_src = matched_dst = NULL;
1031 switch (count_refspec_match(rs->src, src, &matched_src)) {
1036 /* The source could be in the get_sha1() format
1037 * not a reference name. :refs/other is a
1038 * way to delete 'other' ref at the remote end.
1040 matched_src = try_explicit_object_name(rs->src);
1042 return error("src refspec %s does not match any.", rs->src);
1046 return error("src refspec %s matches more than one.", rs->src);
1050 unsigned char sha1[20];
1053 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
1055 ((flag & REF_ISSYMREF) &&
1056 prefixcmp(dst_value, "refs/heads/")))
1057 die("%s cannot be resolved to branch.",
1061 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1065 if (!memcmp(dst_value, "refs/", 5))
1066 matched_dst = make_linked_ref(dst_value, dst_tail);
1067 else if ((dst_guess = guess_ref(dst_value, matched_src)))
1068 matched_dst = make_linked_ref(dst_guess, dst_tail);
1070 error("unable to push to unqualified destination: %s\n"
1071 "The destination refspec neither matches an "
1072 "existing ref on the remote nor\n"
1073 "begins with refs/, and we are unable to "
1074 "guess a prefix based on the source ref.",
1079 error("dst refspec %s matches more than one.",
1085 if (matched_dst->peer_ref)
1086 return error("dst ref %s receives from more than one src.",
1089 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
1090 matched_dst->force = rs->force;
1095 static int match_explicit_refs(struct ref *src, struct ref *dst,
1096 struct ref ***dst_tail, struct refspec *rs,
1100 for (i = errs = 0; i < rs_nr; i++)
1101 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1105 static const struct refspec *check_pattern_match(const struct refspec *rs,
1107 const struct ref *src)
1110 int matching_refs = -1;
1111 for (i = 0; i < rs_nr; i++) {
1112 if (rs[i].matching &&
1113 (matching_refs == -1 || rs[i].force)) {
1118 if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,
1122 if (matching_refs != -1)
1123 return rs + matching_refs;
1128 static struct ref **tail_ref(struct ref **head)
1130 struct ref **tail = head;
1132 tail = &((*tail)->next);
1137 * Note. This is used only by "push"; refspec matching rules for
1138 * push and fetch are subtly different, so do not try to reuse it
1141 int match_refs(struct ref *src, struct ref **dst,
1142 int nr_refspec, const char **refspec, int flags)
1145 int send_all = flags & MATCH_REFS_ALL;
1146 int send_mirror = flags & MATCH_REFS_MIRROR;
1148 static const char *default_refspec[] = { ":", NULL };
1149 struct ref **dst_tail = tail_ref(dst);
1153 refspec = default_refspec;
1155 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1156 errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec);
1158 /* pick the remainder */
1159 for ( ; src; src = src->next) {
1160 struct ref *dst_peer;
1161 const struct refspec *pat = NULL;
1166 pat = check_pattern_match(rs, nr_refspec, src);
1170 if (pat->matching) {
1172 * "matching refs"; traditionally we pushed everything
1173 * including refs outside refs/heads/ hierarchy, but
1174 * that does not make much sense these days.
1176 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1178 dst_name = xstrdup(src->name);
1181 const char *dst_side = pat->dst ? pat->dst : pat->src;
1182 if (!match_name_with_pattern(pat->src, src->name,
1183 dst_side, &dst_name))
1184 die("Didn't think it matches any more");
1186 dst_peer = find_ref_by_name(*dst, dst_name);
1188 if (dst_peer->peer_ref)
1189 /* We're already sending something to this ref. */
1193 if (pat->matching && !(send_all || send_mirror))
1195 * Remote doesn't have it, and we have no
1196 * explicit pattern, and we don't have
1197 * --all nor --mirror.
1201 /* Create a new one and link it */
1202 dst_peer = make_linked_ref(dst_name, &dst_tail);
1203 hashcpy(dst_peer->new_sha1, src->new_sha1);
1205 dst_peer->peer_ref = copy_ref(src);
1206 dst_peer->force = pat->force;
1215 struct branch *branch_get(const char *name)
1220 if (!name || !*name || !strcmp(name, "HEAD"))
1221 ret = current_branch;
1223 ret = make_branch(name, 0);
1224 if (ret && ret->remote_name) {
1225 ret->remote = remote_get(ret->remote_name);
1226 if (ret->merge_nr) {
1228 ret->merge = xcalloc(sizeof(*ret->merge),
1230 for (i = 0; i < ret->merge_nr; i++) {
1231 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1232 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1233 if (remote_find_tracking(ret->remote, ret->merge[i])
1234 && !strcmp(ret->remote_name, "."))
1235 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1242 int branch_has_merge_config(struct branch *branch)
1244 return branch && !!branch->merge;
1247 int branch_merge_matches(struct branch *branch,
1249 const char *refname)
1251 if (!branch || i < 0 || i >= branch->merge_nr)
1253 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1256 static struct ref *get_expanded_map(const struct ref *remote_refs,
1257 const struct refspec *refspec)
1259 const struct ref *ref;
1260 struct ref *ret = NULL;
1261 struct ref **tail = &ret;
1265 for (ref = remote_refs; ref; ref = ref->next) {
1266 if (strchr(ref->name, '^'))
1267 continue; /* a dereference item */
1268 if (match_name_with_pattern(refspec->src, ref->name,
1269 refspec->dst, &expn_name)) {
1270 struct ref *cpy = copy_ref(ref);
1272 cpy->peer_ref = alloc_ref(expn_name);
1275 cpy->peer_ref->force = 1;
1284 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1286 const struct ref *ref;
1287 for (ref = refs; ref; ref = ref->next) {
1288 if (refname_match(name, ref->name, ref_fetch_rules))
1294 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1296 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1301 return copy_ref(ref);
1304 static struct ref *get_local_ref(const char *name)
1306 if (!name || name[0] == '\0')
1309 if (!prefixcmp(name, "refs/"))
1310 return alloc_ref(name);
1312 if (!prefixcmp(name, "heads/") ||
1313 !prefixcmp(name, "tags/") ||
1314 !prefixcmp(name, "remotes/"))
1315 return alloc_ref_with_prefix("refs/", 5, name);
1317 return alloc_ref_with_prefix("refs/heads/", 11, name);
1320 int get_fetch_map(const struct ref *remote_refs,
1321 const struct refspec *refspec,
1325 struct ref *ref_map, **rmp;
1327 if (refspec->pattern) {
1328 ref_map = get_expanded_map(remote_refs, refspec);
1330 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1332 ref_map = get_remote_ref(remote_refs, name);
1333 if (!missing_ok && !ref_map)
1334 die("Couldn't find remote ref %s", name);
1336 ref_map->peer_ref = get_local_ref(refspec->dst);
1337 if (ref_map->peer_ref && refspec->force)
1338 ref_map->peer_ref->force = 1;
1342 for (rmp = &ref_map; *rmp; ) {
1343 if ((*rmp)->peer_ref) {
1344 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1345 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1346 struct ref *ignore = *rmp;
1347 error("* Ignoring funny ref '%s' locally",
1348 (*rmp)->peer_ref->name);
1349 *rmp = (*rmp)->next;
1350 free(ignore->peer_ref);
1355 rmp = &((*rmp)->next);
1359 tail_link_ref(ref_map, tail);
1364 int resolve_remote_symref(struct ref *ref, struct ref *list)
1368 for (; list; list = list->next)
1369 if (!strcmp(ref->symref, list->name)) {
1370 hashcpy(ref->old_sha1, list->old_sha1);
1376 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1379 struct commit_list *temp = list;
1380 temp->item->object.flags &= ~mark;
1386 int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1389 struct commit *old, *new;
1390 struct commit_list *list, *used;
1393 /* Both new and old must be commit-ish and new is descendant of
1394 * old. Otherwise we require --force.
1396 o = deref_tag(parse_object(old_sha1), NULL, 0);
1397 if (!o || o->type != OBJ_COMMIT)
1399 old = (struct commit *) o;
1401 o = deref_tag(parse_object(new_sha1), NULL, 0);
1402 if (!o || o->type != OBJ_COMMIT)
1404 new = (struct commit *) o;
1406 if (parse_commit(new) < 0)
1410 commit_list_insert(new, &list);
1412 new = pop_most_recent_commit(&list, TMP_MARK);
1413 commit_list_insert(new, &used);
1419 unmark_and_free(list, TMP_MARK);
1420 unmark_and_free(used, TMP_MARK);
1425 * Return true if there is anything to report, otherwise false.
1427 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1429 unsigned char sha1[20];
1430 struct commit *ours, *theirs;
1432 struct rev_info revs;
1433 const char *rev_argv[10], *base;
1437 * Nothing to report unless we are marked to build on top of
1441 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1445 * If what we used to build on no longer exists, there is
1446 * nothing to report.
1448 base = branch->merge[0]->dst;
1449 if (!resolve_ref(base, sha1, 1, NULL))
1451 theirs = lookup_commit_reference(sha1);
1455 if (!resolve_ref(branch->refname, sha1, 1, NULL))
1457 ours = lookup_commit_reference(sha1);
1461 /* are we the same? */
1465 /* Run "rev-list --left-right ours...theirs" internally... */
1467 rev_argv[rev_argc++] = NULL;
1468 rev_argv[rev_argc++] = "--left-right";
1469 rev_argv[rev_argc++] = symmetric;
1470 rev_argv[rev_argc++] = "--";
1471 rev_argv[rev_argc] = NULL;
1473 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1474 strcpy(symmetric + 40, "...");
1475 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1477 init_revisions(&revs, NULL);
1478 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1479 prepare_revision_walk(&revs);
1481 /* ... and count the commits on each side. */
1485 struct commit *c = get_revision(&revs);
1488 if (c->object.flags & SYMMETRIC_LEFT)
1494 /* clear object flags smudged by the above traversal */
1495 clear_commit_marks(ours, ALL_REV_FLAGS);
1496 clear_commit_marks(theirs, ALL_REV_FLAGS);
1501 * Return true when there is anything to report, otherwise false.
1503 int format_tracking_info(struct branch *branch, struct strbuf *sb)
1505 int num_ours, num_theirs;
1508 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1511 base = branch->merge[0]->dst;
1512 base = shorten_unambiguous_ref(base, 0);
1514 strbuf_addf(sb, "Your branch is ahead of '%s' "
1515 "by %d commit%s.\n",
1516 base, num_ours, (num_ours == 1) ? "" : "s");
1518 strbuf_addf(sb, "Your branch is behind '%s' "
1520 "and can be fast-forwarded.\n",
1521 base, num_theirs, (num_theirs == 1) ? "" : "s");
1523 strbuf_addf(sb, "Your branch and '%s' have diverged,\n"
1524 "and have %d and %d different commit(s) each, "
1526 base, num_ours, num_theirs);
1530 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1532 struct ref ***local_tail = cb_data;
1536 /* we already know it starts with refs/ to get here */
1537 if (check_ref_format(refname + 5))
1540 len = strlen(refname) + 1;
1541 ref = xcalloc(1, sizeof(*ref) + len);
1542 hashcpy(ref->new_sha1, sha1);
1543 memcpy(ref->name, refname, len);
1545 *local_tail = &ref->next;
1549 struct ref *get_local_heads(void)
1551 struct ref *local_refs = NULL, **local_tail = &local_refs;
1552 for_each_ref(one_local_ref, &local_tail);
1556 struct ref *guess_remote_head(const struct ref *head,
1557 const struct ref *refs,
1560 const struct ref *r;
1561 struct ref *list = NULL;
1562 struct ref **tail = &list;
1568 * Some transports support directly peeking at
1569 * where HEAD points; if that is the case, then
1570 * we don't have to guess.
1573 return copy_ref(find_ref_by_name(refs, head->symref));
1575 /* If refs/heads/master could be right, it is. */
1577 r = find_ref_by_name(refs, "refs/heads/master");
1578 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1582 /* Look for another ref that points there */
1583 for (r = refs; r; r = r->next) {
1584 if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
1585 *tail = copy_ref(r);
1586 tail = &((*tail)->next);