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);
400 else if (!strcmp(subkey, ".url")) {
402 if (git_config_string(&v, key, value))
405 } else if (!strcmp(subkey, ".pushurl")) {
407 if (git_config_string(&v, key, value))
409 add_pushurl(remote, v);
410 } else if (!strcmp(subkey, ".push")) {
412 if (git_config_string(&v, key, value))
414 add_push_refspec(remote, v);
415 } else if (!strcmp(subkey, ".fetch")) {
417 if (git_config_string(&v, key, value))
419 add_fetch_refspec(remote, v);
420 } else if (!strcmp(subkey, ".receivepack")) {
422 if (git_config_string(&v, key, value))
424 if (!remote->receivepack)
425 remote->receivepack = v;
427 error("more than one receivepack given, using the first");
428 } else if (!strcmp(subkey, ".uploadpack")) {
430 if (git_config_string(&v, key, value))
432 if (!remote->uploadpack)
433 remote->uploadpack = v;
435 error("more than one uploadpack given, using the first");
436 } else if (!strcmp(subkey, ".tagopt")) {
437 if (!strcmp(value, "--no-tags"))
438 remote->fetch_tags = -1;
439 } else if (!strcmp(subkey, ".proxy")) {
440 return git_config_string((const char **)&remote->http_proxy,
446 static void alias_all_urls(void)
449 for (i = 0; i < remotes_nr; i++) {
450 int add_pushurl_aliases;
453 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
454 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
456 add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
457 for (j = 0; j < remotes[i]->url_nr; j++) {
458 if (add_pushurl_aliases)
459 add_pushurl_alias(remotes[i], remotes[i]->url[j]);
460 remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
465 static void read_config(void)
467 unsigned char sha1[20];
468 const char *head_ref;
470 if (default_remote_name) // did this already
472 default_remote_name = xstrdup("origin");
473 current_branch = NULL;
474 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
475 if (head_ref && (flag & REF_ISSYMREF) &&
476 !prefixcmp(head_ref, "refs/heads/")) {
478 make_branch(head_ref + strlen("refs/heads/"), 0);
480 git_config(handle_config, NULL);
485 * We need to make sure the tracking branches are well formed, but a
486 * wildcard refspec in "struct refspec" must have a trailing slash. We
487 * temporarily drop the trailing '/' while calling check_ref_format(),
488 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
489 * error return is Ok for a wildcard refspec.
491 static int verify_refname(char *name, int is_glob)
495 result = check_ref_format(name);
496 if (is_glob && result == CHECK_REF_FORMAT_WILDCARD)
497 result = CHECK_REF_FORMAT_OK;
502 * This function frees a refspec array.
503 * Warning: code paths should be checked to ensure that the src
504 * and dst pointers are always freeable pointers as well
505 * as the refspec pointer itself.
507 static void free_refspecs(struct refspec *refspec, int nr_refspec)
514 for (i = 0; i < nr_refspec; i++) {
515 free(refspec[i].src);
516 free(refspec[i].dst);
521 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
525 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
527 for (i = 0; i < nr_refspec; i++) {
530 const char *lhs, *rhs;
540 rhs = strrchr(lhs, ':');
543 * Before going on, special case ":" (or "+:") as a refspec
546 if (!fetch && rhs == lhs && rhs[1] == '\0') {
552 size_t rlen = strlen(++rhs);
553 is_glob = (1 <= rlen && strchr(rhs, '*'));
554 rs[i].dst = xstrndup(rhs, rlen);
557 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
558 if (1 <= llen && memchr(lhs, '*', llen)) {
559 if ((rhs && !is_glob) || (!rhs && fetch))
562 } else if (rhs && is_glob) {
566 rs[i].pattern = is_glob;
567 rs[i].src = xstrndup(lhs, llen);
572 * - empty is allowed; it means HEAD.
573 * - otherwise it must be a valid looking ref.
578 st = verify_refname(rs[i].src, is_glob);
579 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
584 * - missing is ok, and is same as empty.
585 * - empty is ok; it means not to store.
586 * - otherwise it must be a valid looking ref.
590 } else if (!*rs[i].dst) {
593 st = verify_refname(rs[i].dst, is_glob);
594 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
600 * - empty is allowed; it means delete.
601 * - when wildcarded, it must be a valid looking ref.
602 * - otherwise, it must be an extended SHA-1, but
603 * there is no existing way to validate this.
608 st = verify_refname(rs[i].src, is_glob);
609 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
613 ; /* anything goes, for now */
616 * - missing is allowed, but LHS then must be a
618 * - empty is not allowed.
619 * - otherwise it must be a valid looking ref.
622 st = verify_refname(rs[i].src, is_glob);
623 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
625 } else if (!*rs[i].dst) {
628 st = verify_refname(rs[i].dst, is_glob);
629 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
639 * nr_refspec must be greater than zero and i must be valid
640 * since it is only possible to reach this point from within
641 * the for loop above.
643 free_refspecs(rs, i+1);
646 die("Invalid refspec '%s'", refspec[i]);
649 int valid_fetch_refspec(const char *fetch_refspec_str)
651 const char *fetch_refspec[] = { fetch_refspec_str };
652 struct refspec *refspec;
654 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
655 free_refspecs(refspec, 1);
659 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
661 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
664 static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
666 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
669 static int valid_remote_nick(const char *name)
671 if (!name[0] || is_dot_or_dotdot(name))
673 return !strchr(name, '/'); /* no slash */
676 struct remote *remote_get(const char *name)
685 name = default_remote_name;
686 name_given = explicit_default_remote_name;
689 ret = make_remote(name, 0);
690 if (valid_remote_nick(name)) {
692 read_remotes_file(ret);
694 read_branches_file(ret);
696 if (name_given && !ret->url)
697 add_url_alias(ret, name);
700 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
701 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
705 int remote_is_configured(const char *name)
710 for (i = 0; i < remotes_nr; i++)
711 if (!strcmp(name, remotes[i]->name))
716 int for_each_remote(each_remote_fn fn, void *priv)
720 for (i = 0; i < remotes_nr && !result; i++) {
721 struct remote *r = remotes[i];
725 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
728 r->push = parse_push_refspec(r->push_refspec_nr,
730 result = fn(r, priv);
735 void ref_remove_duplicates(struct ref *ref_map)
739 for (; ref_map; ref_map = ref_map->next) {
740 if (!ref_map->peer_ref)
742 posn = &ref_map->next;
744 if ((*posn)->peer_ref &&
745 !strcmp((*posn)->peer_ref->name,
746 ref_map->peer_ref->name)) {
747 if (strcmp((*posn)->name, ref_map->name))
748 die("%s tracks both %s and %s",
749 ref_map->peer_ref->name,
750 (*posn)->name, ref_map->name);
751 next = (*posn)->next;
752 free((*posn)->peer_ref);
756 posn = &(*posn)->next;
762 int remote_has_url(struct remote *remote, const char *url)
765 for (i = 0; i < remote->url_nr; i++) {
766 if (!strcmp(remote->url[i], url))
772 static int match_name_with_pattern(const char *key, const char *name,
773 const char *value, char **result)
775 const char *kstar = strchr(key, '*');
781 die("Key '%s' of pattern had no '*'", key);
783 ksuffixlen = strlen(kstar + 1);
784 namelen = strlen(name);
785 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
786 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
788 const char *vstar = strchr(value, '*');
792 die("Value '%s' of pattern has no '*'", value);
793 vlen = vstar - value;
794 vsuffixlen = strlen(vstar + 1);
795 *result = xmalloc(vlen + vsuffixlen +
797 klen - ksuffixlen + 1);
798 strncpy(*result, value, vlen);
799 strncpy(*result + vlen,
800 name + klen, namelen - klen - ksuffixlen);
801 strcpy(*result + vlen + namelen - klen - ksuffixlen,
807 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
809 int find_src = refspec->src == NULL;
810 char *needle, **result;
815 return error("find_tracking: need either src or dst");
816 needle = refspec->dst;
817 result = &refspec->src;
819 needle = refspec->src;
820 result = &refspec->dst;
823 for (i = 0; i < remote->fetch_refspec_nr; i++) {
824 struct refspec *fetch = &remote->fetch[i];
825 const char *key = find_src ? fetch->dst : fetch->src;
826 const char *value = find_src ? fetch->src : fetch->dst;
829 if (fetch->pattern) {
830 if (match_name_with_pattern(key, needle, value, result)) {
831 refspec->force = fetch->force;
834 } else if (!strcmp(needle, key)) {
835 *result = xstrdup(value);
836 refspec->force = fetch->force;
843 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
846 size_t len = strlen(name);
847 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
848 memcpy(ref->name, prefix, prefixlen);
849 memcpy(ref->name + prefixlen, name, len);
853 struct ref *alloc_ref(const char *name)
855 return alloc_ref_with_prefix("", 0, name);
858 static struct ref *copy_ref(const struct ref *ref)
864 len = strlen(ref->name);
865 cpy = xmalloc(sizeof(struct ref) + len + 1);
866 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
868 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
869 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
870 cpy->peer_ref = copy_ref(ref->peer_ref);
874 struct ref *copy_ref_list(const struct ref *ref)
876 struct ref *ret = NULL;
877 struct ref **tail = &ret;
879 *tail = copy_ref(ref);
881 tail = &((*tail)->next);
886 static void free_ref(struct ref *ref)
890 free_ref(ref->peer_ref);
891 free(ref->remote_status);
896 void free_refs(struct ref *ref)
906 static int count_refspec_match(const char *pattern,
908 struct ref **matched_ref)
910 int patlen = strlen(pattern);
911 struct ref *matched_weak = NULL;
912 struct ref *matched = NULL;
916 for (weak_match = match = 0; refs; refs = refs->next) {
917 char *name = refs->name;
918 int namelen = strlen(name);
920 if (!refname_match(pattern, name, ref_rev_parse_rules))
923 /* A match is "weak" if it is with refs outside
924 * heads or tags, and did not specify the pattern
925 * in full (e.g. "refs/remotes/origin/master") or at
926 * least from the toplevel (e.g. "remotes/origin/master");
927 * otherwise "git push $URL master" would result in
928 * ambiguity between remotes/origin/master and heads/master
929 * at the remote site.
931 if (namelen != patlen &&
932 patlen != namelen - 5 &&
933 prefixcmp(name, "refs/heads/") &&
934 prefixcmp(name, "refs/tags/")) {
935 /* We want to catch the case where only weak
936 * matches are found and there are multiple
937 * matches, and where more than one strong
938 * matches are found, as ambiguous. One
939 * strong match with zero or more weak matches
940 * are acceptable as a unique match.
951 *matched_ref = matched_weak;
955 *matched_ref = matched;
960 static void tail_link_ref(struct ref *ref, struct ref ***tail)
968 static struct ref *try_explicit_object_name(const char *name)
970 unsigned char sha1[20];
974 ref = alloc_ref("(delete)");
975 hashclr(ref->new_sha1);
978 if (get_sha1(name, sha1))
980 ref = alloc_ref(name);
981 hashcpy(ref->new_sha1, sha1);
985 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
987 struct ref *ret = alloc_ref(name);
988 tail_link_ref(ret, tail);
992 static char *guess_ref(const char *name, struct ref *peer)
994 struct strbuf buf = STRBUF_INIT;
995 unsigned char sha1[20];
997 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
1001 if (!prefixcmp(r, "refs/heads/"))
1002 strbuf_addstr(&buf, "refs/heads/");
1003 else if (!prefixcmp(r, "refs/tags/"))
1004 strbuf_addstr(&buf, "refs/tags/");
1008 strbuf_addstr(&buf, name);
1009 return strbuf_detach(&buf, NULL);
1012 static int match_explicit(struct ref *src, struct ref *dst,
1013 struct ref ***dst_tail,
1016 struct ref *matched_src, *matched_dst;
1019 const char *dst_value = rs->dst;
1022 if (rs->pattern || rs->matching)
1025 matched_src = matched_dst = NULL;
1026 switch (count_refspec_match(rs->src, src, &matched_src)) {
1031 /* The source could be in the get_sha1() format
1032 * not a reference name. :refs/other is a
1033 * way to delete 'other' ref at the remote end.
1035 matched_src = try_explicit_object_name(rs->src);
1037 return error("src refspec %s does not match any.", rs->src);
1041 return error("src refspec %s matches more than one.", rs->src);
1045 unsigned char sha1[20];
1048 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
1050 ((flag & REF_ISSYMREF) &&
1051 prefixcmp(dst_value, "refs/heads/")))
1052 die("%s cannot be resolved to branch.",
1056 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1060 if (!memcmp(dst_value, "refs/", 5))
1061 matched_dst = make_linked_ref(dst_value, dst_tail);
1062 else if ((dst_guess = guess_ref(dst_value, matched_src)))
1063 matched_dst = make_linked_ref(dst_guess, dst_tail);
1065 error("unable to push to unqualified destination: %s\n"
1066 "The destination refspec neither matches an "
1067 "existing ref on the remote nor\n"
1068 "begins with refs/, and we are unable to "
1069 "guess a prefix based on the source ref.",
1074 error("dst refspec %s matches more than one.",
1080 if (matched_dst->peer_ref)
1081 return error("dst ref %s receives from more than one src.",
1084 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
1085 matched_dst->force = rs->force;
1090 static int match_explicit_refs(struct ref *src, struct ref *dst,
1091 struct ref ***dst_tail, struct refspec *rs,
1095 for (i = errs = 0; i < rs_nr; i++)
1096 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1100 static const struct refspec *check_pattern_match(const struct refspec *rs,
1102 const struct ref *src)
1105 int matching_refs = -1;
1106 for (i = 0; i < rs_nr; i++) {
1107 if (rs[i].matching &&
1108 (matching_refs == -1 || rs[i].force)) {
1113 if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,
1117 if (matching_refs != -1)
1118 return rs + matching_refs;
1123 static struct ref **tail_ref(struct ref **head)
1125 struct ref **tail = head;
1127 tail = &((*tail)->next);
1132 * Note. This is used only by "push"; refspec matching rules for
1133 * push and fetch are subtly different, so do not try to reuse it
1136 int match_refs(struct ref *src, struct ref **dst,
1137 int nr_refspec, const char **refspec, int flags)
1140 int send_all = flags & MATCH_REFS_ALL;
1141 int send_mirror = flags & MATCH_REFS_MIRROR;
1143 static const char *default_refspec[] = { ":", NULL };
1144 struct ref **dst_tail = tail_ref(dst);
1148 refspec = default_refspec;
1150 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1151 errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec);
1153 /* pick the remainder */
1154 for ( ; src; src = src->next) {
1155 struct ref *dst_peer;
1156 const struct refspec *pat = NULL;
1161 pat = check_pattern_match(rs, nr_refspec, src);
1165 if (pat->matching) {
1167 * "matching refs"; traditionally we pushed everything
1168 * including refs outside refs/heads/ hierarchy, but
1169 * that does not make much sense these days.
1171 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1173 dst_name = xstrdup(src->name);
1176 const char *dst_side = pat->dst ? pat->dst : pat->src;
1177 if (!match_name_with_pattern(pat->src, src->name,
1178 dst_side, &dst_name))
1179 die("Didn't think it matches any more");
1181 dst_peer = find_ref_by_name(*dst, dst_name);
1183 if (dst_peer->peer_ref)
1184 /* We're already sending something to this ref. */
1188 if (pat->matching && !(send_all || send_mirror))
1190 * Remote doesn't have it, and we have no
1191 * explicit pattern, and we don't have
1192 * --all nor --mirror.
1196 /* Create a new one and link it */
1197 dst_peer = make_linked_ref(dst_name, &dst_tail);
1198 hashcpy(dst_peer->new_sha1, src->new_sha1);
1200 dst_peer->peer_ref = copy_ref(src);
1201 dst_peer->force = pat->force;
1210 struct branch *branch_get(const char *name)
1215 if (!name || !*name || !strcmp(name, "HEAD"))
1216 ret = current_branch;
1218 ret = make_branch(name, 0);
1219 if (ret && ret->remote_name) {
1220 ret->remote = remote_get(ret->remote_name);
1221 if (ret->merge_nr) {
1223 ret->merge = xcalloc(sizeof(*ret->merge),
1225 for (i = 0; i < ret->merge_nr; i++) {
1226 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1227 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1228 if (remote_find_tracking(ret->remote, ret->merge[i])
1229 && !strcmp(ret->remote_name, "."))
1230 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1237 int branch_has_merge_config(struct branch *branch)
1239 return branch && !!branch->merge;
1242 int branch_merge_matches(struct branch *branch,
1244 const char *refname)
1246 if (!branch || i < 0 || i >= branch->merge_nr)
1248 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1251 static struct ref *get_expanded_map(const struct ref *remote_refs,
1252 const struct refspec *refspec)
1254 const struct ref *ref;
1255 struct ref *ret = NULL;
1256 struct ref **tail = &ret;
1260 for (ref = remote_refs; ref; ref = ref->next) {
1261 if (strchr(ref->name, '^'))
1262 continue; /* a dereference item */
1263 if (match_name_with_pattern(refspec->src, ref->name,
1264 refspec->dst, &expn_name)) {
1265 struct ref *cpy = copy_ref(ref);
1267 cpy->peer_ref = alloc_ref(expn_name);
1270 cpy->peer_ref->force = 1;
1279 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1281 const struct ref *ref;
1282 for (ref = refs; ref; ref = ref->next) {
1283 if (refname_match(name, ref->name, ref_fetch_rules))
1289 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1291 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1296 return copy_ref(ref);
1299 static struct ref *get_local_ref(const char *name)
1301 if (!name || name[0] == '\0')
1304 if (!prefixcmp(name, "refs/"))
1305 return alloc_ref(name);
1307 if (!prefixcmp(name, "heads/") ||
1308 !prefixcmp(name, "tags/") ||
1309 !prefixcmp(name, "remotes/"))
1310 return alloc_ref_with_prefix("refs/", 5, name);
1312 return alloc_ref_with_prefix("refs/heads/", 11, name);
1315 int get_fetch_map(const struct ref *remote_refs,
1316 const struct refspec *refspec,
1320 struct ref *ref_map, **rmp;
1322 if (refspec->pattern) {
1323 ref_map = get_expanded_map(remote_refs, refspec);
1325 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1327 ref_map = get_remote_ref(remote_refs, name);
1328 if (!missing_ok && !ref_map)
1329 die("Couldn't find remote ref %s", name);
1331 ref_map->peer_ref = get_local_ref(refspec->dst);
1332 if (ref_map->peer_ref && refspec->force)
1333 ref_map->peer_ref->force = 1;
1337 for (rmp = &ref_map; *rmp; ) {
1338 if ((*rmp)->peer_ref) {
1339 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1340 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1341 struct ref *ignore = *rmp;
1342 error("* Ignoring funny ref '%s' locally",
1343 (*rmp)->peer_ref->name);
1344 *rmp = (*rmp)->next;
1345 free(ignore->peer_ref);
1350 rmp = &((*rmp)->next);
1354 tail_link_ref(ref_map, tail);
1359 int resolve_remote_symref(struct ref *ref, struct ref *list)
1363 for (; list; list = list->next)
1364 if (!strcmp(ref->symref, list->name)) {
1365 hashcpy(ref->old_sha1, list->old_sha1);
1371 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1374 struct commit_list *temp = list;
1375 temp->item->object.flags &= ~mark;
1381 int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1384 struct commit *old, *new;
1385 struct commit_list *list, *used;
1388 /* Both new and old must be commit-ish and new is descendant of
1389 * old. Otherwise we require --force.
1391 o = deref_tag(parse_object(old_sha1), NULL, 0);
1392 if (!o || o->type != OBJ_COMMIT)
1394 old = (struct commit *) o;
1396 o = deref_tag(parse_object(new_sha1), NULL, 0);
1397 if (!o || o->type != OBJ_COMMIT)
1399 new = (struct commit *) o;
1401 if (parse_commit(new) < 0)
1405 commit_list_insert(new, &list);
1407 new = pop_most_recent_commit(&list, TMP_MARK);
1408 commit_list_insert(new, &used);
1414 unmark_and_free(list, TMP_MARK);
1415 unmark_and_free(used, TMP_MARK);
1420 * Return true if there is anything to report, otherwise false.
1422 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1424 unsigned char sha1[20];
1425 struct commit *ours, *theirs;
1427 struct rev_info revs;
1428 const char *rev_argv[10], *base;
1432 * Nothing to report unless we are marked to build on top of
1436 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1440 * If what we used to build on no longer exists, there is
1441 * nothing to report.
1443 base = branch->merge[0]->dst;
1444 if (!resolve_ref(base, sha1, 1, NULL))
1446 theirs = lookup_commit_reference(sha1);
1450 if (!resolve_ref(branch->refname, sha1, 1, NULL))
1452 ours = lookup_commit_reference(sha1);
1456 /* are we the same? */
1460 /* Run "rev-list --left-right ours...theirs" internally... */
1462 rev_argv[rev_argc++] = NULL;
1463 rev_argv[rev_argc++] = "--left-right";
1464 rev_argv[rev_argc++] = symmetric;
1465 rev_argv[rev_argc++] = "--";
1466 rev_argv[rev_argc] = NULL;
1468 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1469 strcpy(symmetric + 40, "...");
1470 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1472 init_revisions(&revs, NULL);
1473 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1474 prepare_revision_walk(&revs);
1476 /* ... and count the commits on each side. */
1480 struct commit *c = get_revision(&revs);
1483 if (c->object.flags & SYMMETRIC_LEFT)
1489 /* clear object flags smudged by the above traversal */
1490 clear_commit_marks(ours, ALL_REV_FLAGS);
1491 clear_commit_marks(theirs, ALL_REV_FLAGS);
1496 * Return true when there is anything to report, otherwise false.
1498 int format_tracking_info(struct branch *branch, struct strbuf *sb)
1500 int num_ours, num_theirs;
1503 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1506 base = branch->merge[0]->dst;
1507 base = shorten_unambiguous_ref(base, 0);
1509 strbuf_addf(sb, "Your branch is ahead of '%s' "
1510 "by %d commit%s.\n",
1511 base, num_ours, (num_ours == 1) ? "" : "s");
1513 strbuf_addf(sb, "Your branch is behind '%s' "
1515 "and can be fast-forwarded.\n",
1516 base, num_theirs, (num_theirs == 1) ? "" : "s");
1518 strbuf_addf(sb, "Your branch and '%s' have diverged,\n"
1519 "and have %d and %d different commit(s) each, "
1521 base, num_ours, num_theirs);
1525 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1527 struct ref ***local_tail = cb_data;
1531 /* we already know it starts with refs/ to get here */
1532 if (check_ref_format(refname + 5))
1535 len = strlen(refname) + 1;
1536 ref = xcalloc(1, sizeof(*ref) + len);
1537 hashcpy(ref->new_sha1, sha1);
1538 memcpy(ref->name, refname, len);
1540 *local_tail = &ref->next;
1544 struct ref *get_local_heads(void)
1546 struct ref *local_refs = NULL, **local_tail = &local_refs;
1547 for_each_ref(one_local_ref, &local_tail);
1551 struct ref *guess_remote_head(const struct ref *head,
1552 const struct ref *refs,
1555 const struct ref *r;
1556 struct ref *list = NULL;
1557 struct ref **tail = &list;
1563 * Some transports support directly peeking at
1564 * where HEAD points; if that is the case, then
1565 * we don't have to guess.
1568 return copy_ref(find_ref_by_name(refs, head->symref));
1570 /* If refs/heads/master could be right, it is. */
1572 r = find_ref_by_name(refs, "refs/heads/master");
1573 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1577 /* Look for another ref that points there */
1578 for (r = refs; r; r = r->next) {
1579 if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
1580 *tail = copy_ref(r);
1581 tail = &((*tail)->next);