5 static struct refspec s_tag_refspec = {
13 const struct refspec *tag_refspec = &s_tag_refspec;
15 struct counted_string {
22 struct counted_string *instead_of;
27 static struct remote **remotes;
28 static int remotes_alloc;
29 static int remotes_nr;
31 static struct branch **branches;
32 static int branches_alloc;
33 static int branches_nr;
35 static struct branch *current_branch;
36 static const char *default_remote_name;
38 static struct rewrite **rewrite;
39 static int rewrite_alloc;
40 static int rewrite_nr;
42 #define BUF_SIZE (2048)
43 static char buffer[BUF_SIZE];
45 static const char *alias_url(const char *url)
49 struct counted_string *longest;
54 for (i = 0; i < rewrite_nr; i++) {
57 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
58 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
60 longest->len < rewrite[i]->instead_of[j].len)) {
61 longest = &(rewrite[i]->instead_of[j]);
69 ret = malloc(rewrite[longest_i]->baselen +
70 (strlen(url) - longest->len) + 1);
71 strcpy(ret, rewrite[longest_i]->base);
72 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
76 static void add_push_refspec(struct remote *remote, const char *ref)
78 ALLOC_GROW(remote->push_refspec,
79 remote->push_refspec_nr + 1,
80 remote->push_refspec_alloc);
81 remote->push_refspec[remote->push_refspec_nr++] = ref;
84 static void add_fetch_refspec(struct remote *remote, const char *ref)
86 ALLOC_GROW(remote->fetch_refspec,
87 remote->fetch_refspec_nr + 1,
88 remote->fetch_refspec_alloc);
89 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
92 static void add_url(struct remote *remote, const char *url)
94 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
95 remote->url[remote->url_nr++] = url;
98 static void add_url_alias(struct remote *remote, const char *url)
100 add_url(remote, alias_url(url));
103 static struct remote *make_remote(const char *name, int len)
108 for (i = 0; i < remotes_nr; i++) {
109 if (len ? (!strncmp(name, remotes[i]->name, len) &&
110 !remotes[i]->name[len]) :
111 !strcmp(name, remotes[i]->name))
115 ret = xcalloc(1, sizeof(struct remote));
116 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
117 remotes[remotes_nr++] = ret;
119 ret->name = xstrndup(name, len);
121 ret->name = xstrdup(name);
125 static void add_merge(struct branch *branch, const char *name)
127 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
128 branch->merge_alloc);
129 branch->merge_name[branch->merge_nr++] = name;
132 static struct branch *make_branch(const char *name, int len)
138 for (i = 0; i < branches_nr; i++) {
139 if (len ? (!strncmp(name, branches[i]->name, len) &&
140 !branches[i]->name[len]) :
141 !strcmp(name, branches[i]->name))
145 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
146 ret = xcalloc(1, sizeof(struct branch));
147 branches[branches_nr++] = ret;
149 ret->name = xstrndup(name, len);
151 ret->name = xstrdup(name);
152 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
153 strcpy(refname, "refs/heads/");
154 strcpy(refname + strlen("refs/heads/"), ret->name);
155 ret->refname = refname;
160 static struct rewrite *make_rewrite(const char *base, int len)
165 for (i = 0; i < rewrite_nr; i++) {
167 ? (len == rewrite[i]->baselen &&
168 !strncmp(base, rewrite[i]->base, len))
169 : !strcmp(base, rewrite[i]->base))
173 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
174 ret = xcalloc(1, sizeof(struct rewrite));
175 rewrite[rewrite_nr++] = ret;
177 ret->base = xstrndup(base, len);
181 ret->base = xstrdup(base);
182 ret->baselen = strlen(base);
187 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
189 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
190 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
191 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
192 rewrite->instead_of_nr++;
195 static void read_remotes_file(struct remote *remote)
197 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
201 while (fgets(buffer, BUF_SIZE, f)) {
205 if (!prefixcmp(buffer, "URL:")) {
208 } else if (!prefixcmp(buffer, "Push:")) {
211 } else if (!prefixcmp(buffer, "Pull:")) {
223 while (isspace(p[-1]))
226 switch (value_list) {
228 add_url_alias(remote, xstrdup(s));
231 add_push_refspec(remote, xstrdup(s));
234 add_fetch_refspec(remote, xstrdup(s));
241 static void read_branches_file(struct remote *remote)
243 const char *slash = strchr(remote->name, '/');
245 struct strbuf branch;
246 int n = slash ? slash - remote->name : 1000;
247 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
253 s = fgets(buffer, BUF_SIZE, f);
262 while (isspace(p[-1]))
266 len += strlen(slash);
267 p = xmalloc(len + 1);
273 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
274 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
275 * the partial URL obtained from the branches file plus
276 * "/netdev-2.6" and does not store it in any tracking ref.
277 * #branch specifier in the file is ignored.
279 * Otherwise, the branches file would have URL and optionally
280 * #branch specified. The "master" (or specified) branch is
281 * fetched and stored in the local branch of the same name.
283 strbuf_init(&branch, 0);
284 frag = strchr(p, '#');
287 strbuf_addf(&branch, "refs/heads/%s", frag);
289 strbuf_addstr(&branch, "refs/heads/master");
291 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
293 strbuf_reset(&branch);
294 strbuf_addstr(&branch, "HEAD:");
296 add_url_alias(remote, p);
297 add_fetch_refspec(remote, strbuf_detach(&branch, 0));
298 remote->fetch_tags = 1; /* always auto-follow */
301 static int handle_config(const char *key, const char *value, void *cb)
305 struct remote *remote;
306 struct branch *branch;
307 if (!prefixcmp(key, "branch.")) {
309 subkey = strrchr(name, '.');
312 branch = make_branch(name, subkey - name);
313 if (!strcmp(subkey, ".remote")) {
315 return config_error_nonbool(key);
316 branch->remote_name = xstrdup(value);
317 if (branch == current_branch)
318 default_remote_name = branch->remote_name;
319 } else if (!strcmp(subkey, ".merge")) {
321 return config_error_nonbool(key);
322 add_merge(branch, xstrdup(value));
326 if (!prefixcmp(key, "url.")) {
327 struct rewrite *rewrite;
329 subkey = strrchr(name, '.');
332 rewrite = make_rewrite(name, subkey - name);
333 if (!strcmp(subkey, ".insteadof")) {
335 return config_error_nonbool(key);
336 add_instead_of(rewrite, xstrdup(value));
339 if (prefixcmp(key, "remote."))
342 subkey = strrchr(name, '.');
344 return error("Config with no key for remote %s", name);
345 if (*subkey == '/') {
346 warning("Config remote shorthand cannot begin with '/': %s", name);
349 remote = make_remote(name, subkey - name);
350 if (!strcmp(subkey, ".mirror"))
351 remote->mirror = git_config_bool(key, value);
352 else if (!strcmp(subkey, ".skipdefaultupdate"))
353 remote->skip_default_update = git_config_bool(key, value);
355 else if (!strcmp(subkey, ".url")) {
357 if (git_config_string(&v, key, value))
360 } else if (!strcmp(subkey, ".push")) {
362 if (git_config_string(&v, key, value))
364 add_push_refspec(remote, v);
365 } else if (!strcmp(subkey, ".fetch")) {
367 if (git_config_string(&v, key, value))
369 add_fetch_refspec(remote, v);
370 } else if (!strcmp(subkey, ".receivepack")) {
372 if (git_config_string(&v, key, value))
374 if (!remote->receivepack)
375 remote->receivepack = v;
377 error("more than one receivepack given, using the first");
378 } else if (!strcmp(subkey, ".uploadpack")) {
380 if (git_config_string(&v, key, value))
382 if (!remote->uploadpack)
383 remote->uploadpack = v;
385 error("more than one uploadpack given, using the first");
386 } else if (!strcmp(subkey, ".tagopt")) {
387 if (!strcmp(value, "--no-tags"))
388 remote->fetch_tags = -1;
389 } else if (!strcmp(subkey, ".proxy")) {
390 return git_config_string((const char **)&remote->http_proxy,
396 static void alias_all_urls(void)
399 for (i = 0; i < remotes_nr; i++) {
402 for (j = 0; j < remotes[i]->url_nr; j++) {
403 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
408 static void read_config(void)
410 unsigned char sha1[20];
411 const char *head_ref;
413 if (default_remote_name) // did this already
415 default_remote_name = xstrdup("origin");
416 current_branch = NULL;
417 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
418 if (head_ref && (flag & REF_ISSYMREF) &&
419 !prefixcmp(head_ref, "refs/heads/")) {
421 make_branch(head_ref + strlen("refs/heads/"), 0);
423 git_config(handle_config, NULL);
428 * We need to make sure the tracking branches are well formed, but a
429 * wildcard refspec in "struct refspec" must have a trailing slash. We
430 * temporarily drop the trailing '/' while calling check_ref_format(),
431 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
432 * error return is Ok for a wildcard refspec.
434 static int verify_refname(char *name, int is_glob)
436 int result, len = -1;
440 assert(name[len - 1] == '/');
441 name[len - 1] = '\0';
443 result = check_ref_format(name);
449 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
453 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
455 for (i = 0; i < nr_refspec; i++) {
458 const char *lhs, *rhs;
468 rhs = strrchr(lhs, ':');
471 * Before going on, special case ":" (or "+:") as a refspec
474 if (!fetch && rhs == lhs && rhs[1] == '\0') {
480 size_t rlen = strlen(++rhs);
481 is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
482 rs[i].dst = xstrndup(rhs, rlen - is_glob);
485 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
486 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
487 if ((rhs && !is_glob) || (!rhs && fetch))
491 } else if (rhs && is_glob) {
495 rs[i].pattern = is_glob;
496 rs[i].src = xstrndup(lhs, llen);
501 * - empty is allowed; it means HEAD.
502 * - otherwise it must be a valid looking ref.
507 st = verify_refname(rs[i].src, is_glob);
508 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
513 * - missing is ok, and is same as empty.
514 * - empty is ok; it means not to store.
515 * - otherwise it must be a valid looking ref.
519 } else if (!*rs[i].dst) {
522 st = verify_refname(rs[i].dst, is_glob);
523 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
529 * - empty is allowed; it means delete.
530 * - when wildcarded, it must be a valid looking ref.
531 * - otherwise, it must be an extended SHA-1, but
532 * there is no existing way to validate this.
537 st = verify_refname(rs[i].src, is_glob);
538 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
542 ; /* anything goes, for now */
545 * - missing is allowed, but LHS then must be a
547 * - empty is not allowed.
548 * - otherwise it must be a valid looking ref.
551 st = verify_refname(rs[i].src, is_glob);
552 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
554 } else if (!*rs[i].dst) {
557 st = verify_refname(rs[i].dst, is_glob);
558 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
570 die("Invalid refspec '%s'", refspec[i]);
573 int valid_fetch_refspec(const char *fetch_refspec_str)
575 const char *fetch_refspec[] = { fetch_refspec_str };
576 struct refspec *refspec;
578 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
584 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
586 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
589 struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
591 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
594 static int valid_remote_nick(const char *name)
596 if (!name[0] || /* not empty */
597 (name[0] == '.' && /* not "." */
598 (!name[1] || /* not ".." */
599 (name[1] == '.' && !name[2]))))
601 return !strchr(name, '/'); /* no slash */
604 struct remote *remote_get(const char *name)
610 name = default_remote_name;
611 ret = make_remote(name, 0);
612 if (valid_remote_nick(name)) {
614 read_remotes_file(ret);
616 read_branches_file(ret);
619 add_url_alias(ret, name);
622 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
623 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
627 int for_each_remote(each_remote_fn fn, void *priv)
631 for (i = 0; i < remotes_nr && !result; i++) {
632 struct remote *r = remotes[i];
636 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
639 r->push = parse_push_refspec(r->push_refspec_nr,
641 result = fn(r, priv);
646 void ref_remove_duplicates(struct ref *ref_map)
650 for (; ref_map; ref_map = ref_map->next) {
651 if (!ref_map->peer_ref)
653 posn = &ref_map->next;
655 if ((*posn)->peer_ref &&
656 !strcmp((*posn)->peer_ref->name,
657 ref_map->peer_ref->name)) {
658 if (strcmp((*posn)->name, ref_map->name))
659 die("%s tracks both %s and %s",
660 ref_map->peer_ref->name,
661 (*posn)->name, ref_map->name);
662 next = (*posn)->next;
663 free((*posn)->peer_ref);
667 posn = &(*posn)->next;
673 int remote_has_url(struct remote *remote, const char *url)
676 for (i = 0; i < remote->url_nr; i++) {
677 if (!strcmp(remote->url[i], url))
683 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
685 int find_src = refspec->src == NULL;
686 char *needle, **result;
691 return error("find_tracking: need either src or dst");
692 needle = refspec->dst;
693 result = &refspec->src;
695 needle = refspec->src;
696 result = &refspec->dst;
699 for (i = 0; i < remote->fetch_refspec_nr; i++) {
700 struct refspec *fetch = &remote->fetch[i];
701 const char *key = find_src ? fetch->dst : fetch->src;
702 const char *value = find_src ? fetch->src : fetch->dst;
705 if (fetch->pattern) {
706 if (!prefixcmp(needle, key)) {
707 *result = xmalloc(strlen(value) +
710 strcpy(*result, value);
711 strcpy(*result + strlen(value),
712 needle + strlen(key));
713 refspec->force = fetch->force;
716 } else if (!strcmp(needle, key)) {
717 *result = xstrdup(value);
718 refspec->force = fetch->force;
725 struct ref *alloc_ref(unsigned namelen)
727 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
728 memset(ret, 0, sizeof(struct ref) + namelen);
732 struct ref *alloc_ref_from_str(const char* str)
734 struct ref *ret = alloc_ref(strlen(str) + 1);
735 strcpy(ret->name, str);
739 static struct ref *copy_ref(const struct ref *ref)
741 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
742 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
747 struct ref *copy_ref_list(const struct ref *ref)
749 struct ref *ret = NULL;
750 struct ref **tail = &ret;
752 *tail = copy_ref(ref);
754 tail = &((*tail)->next);
759 void free_ref(struct ref *ref)
763 free(ref->remote_status);
768 void free_refs(struct ref *ref)
779 static int count_refspec_match(const char *pattern,
781 struct ref **matched_ref)
783 int patlen = strlen(pattern);
784 struct ref *matched_weak = NULL;
785 struct ref *matched = NULL;
789 for (weak_match = match = 0; refs; refs = refs->next) {
790 char *name = refs->name;
791 int namelen = strlen(name);
793 if (!refname_match(pattern, name, ref_rev_parse_rules))
796 /* A match is "weak" if it is with refs outside
797 * heads or tags, and did not specify the pattern
798 * in full (e.g. "refs/remotes/origin/master") or at
799 * least from the toplevel (e.g. "remotes/origin/master");
800 * otherwise "git push $URL master" would result in
801 * ambiguity between remotes/origin/master and heads/master
802 * at the remote site.
804 if (namelen != patlen &&
805 patlen != namelen - 5 &&
806 prefixcmp(name, "refs/heads/") &&
807 prefixcmp(name, "refs/tags/")) {
808 /* We want to catch the case where only weak
809 * matches are found and there are multiple
810 * matches, and where more than one strong
811 * matches are found, as ambiguous. One
812 * strong match with zero or more weak matches
813 * are acceptable as a unique match.
824 *matched_ref = matched_weak;
828 *matched_ref = matched;
833 static void tail_link_ref(struct ref *ref, struct ref ***tail)
841 static struct ref *try_explicit_object_name(const char *name)
843 unsigned char sha1[20];
848 strcpy(ref->name, "(delete)");
849 hashclr(ref->new_sha1);
852 if (get_sha1(name, sha1))
854 ref = alloc_ref_from_str(name);
855 hashcpy(ref->new_sha1, sha1);
859 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
861 struct ref *ret = alloc_ref_from_str(name);
862 tail_link_ref(ret, tail);
866 static char *guess_ref(const char *name, struct ref *peer)
868 struct strbuf buf = STRBUF_INIT;
869 unsigned char sha1[20];
871 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
875 if (!prefixcmp(r, "refs/heads/"))
876 strbuf_addstr(&buf, "refs/heads/");
877 else if (!prefixcmp(r, "refs/tags/"))
878 strbuf_addstr(&buf, "refs/tags/");
882 strbuf_addstr(&buf, name);
883 return strbuf_detach(&buf, NULL);
886 static int match_explicit(struct ref *src, struct ref *dst,
887 struct ref ***dst_tail,
890 struct ref *matched_src, *matched_dst;
892 const char *dst_value = rs->dst;
895 if (rs->pattern || rs->matching)
898 matched_src = matched_dst = NULL;
899 switch (count_refspec_match(rs->src, src, &matched_src)) {
903 /* The source could be in the get_sha1() format
904 * not a reference name. :refs/other is a
905 * way to delete 'other' ref at the remote end.
907 matched_src = try_explicit_object_name(rs->src);
909 return error("src refspec %s does not match any.", rs->src);
912 return error("src refspec %s matches more than one.", rs->src);
916 unsigned char sha1[20];
919 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
921 ((flag & REF_ISSYMREF) &&
922 prefixcmp(dst_value, "refs/heads/")))
923 die("%s cannot be resolved to branch.",
927 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
931 if (!memcmp(dst_value, "refs/", 5))
932 matched_dst = make_linked_ref(dst_value, dst_tail);
933 else if((dst_guess = guess_ref(dst_value, matched_src)))
934 matched_dst = make_linked_ref(dst_guess, dst_tail);
936 error("unable to push to unqualified destination: %s\n"
937 "The destination refspec neither matches an "
938 "existing ref on the remote nor\n"
939 "begins with refs/, and we are unable to "
940 "guess a prefix based on the source ref.",
945 error("dst refspec %s matches more than one.",
951 if (matched_dst->peer_ref)
952 return error("dst ref %s receives from more than one src.",
955 matched_dst->peer_ref = matched_src;
956 matched_dst->force = rs->force;
961 static int match_explicit_refs(struct ref *src, struct ref *dst,
962 struct ref ***dst_tail, struct refspec *rs,
966 for (i = errs = 0; i < rs_nr; i++)
967 errs += match_explicit(src, dst, dst_tail, &rs[i]);
971 static const struct refspec *check_pattern_match(const struct refspec *rs,
973 const struct ref *src)
976 int matching_refs = -1;
977 for (i = 0; i < rs_nr; i++) {
978 if (rs[i].matching &&
979 (matching_refs == -1 || rs[i].force)) {
984 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
987 if (matching_refs != -1)
988 return rs + matching_refs;
994 * Note. This is used only by "push"; refspec matching rules for
995 * push and fetch are subtly different, so do not try to reuse it
998 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
999 int nr_refspec, const char **refspec, int flags)
1002 int send_all = flags & MATCH_REFS_ALL;
1003 int send_mirror = flags & MATCH_REFS_MIRROR;
1004 static const char *default_refspec[] = { ":", 0 };
1008 refspec = default_refspec;
1010 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1011 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
1014 /* pick the remainder */
1015 for ( ; src; src = src->next) {
1016 struct ref *dst_peer;
1017 const struct refspec *pat = NULL;
1022 pat = check_pattern_match(rs, nr_refspec, src);
1026 if (pat->matching) {
1028 * "matching refs"; traditionally we pushed everything
1029 * including refs outside refs/heads/ hierarchy, but
1030 * that does not make much sense these days.
1032 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1034 dst_name = xstrdup(src->name);
1037 const char *dst_side = pat->dst ? pat->dst : pat->src;
1038 dst_name = xmalloc(strlen(dst_side) +
1040 strlen(pat->src) + 2);
1041 strcpy(dst_name, dst_side);
1042 strcat(dst_name, src->name + strlen(pat->src));
1044 dst_peer = find_ref_by_name(dst, dst_name);
1046 if (dst_peer->peer_ref)
1047 /* We're already sending something to this ref. */
1051 if (pat->matching && !(send_all || send_mirror))
1053 * Remote doesn't have it, and we have no
1054 * explicit pattern, and we don't have
1055 * --all nor --mirror.
1059 /* Create a new one and link it */
1060 dst_peer = make_linked_ref(dst_name, dst_tail);
1061 hashcpy(dst_peer->new_sha1, src->new_sha1);
1063 dst_peer->peer_ref = src;
1064 dst_peer->force = pat->force;
1071 struct branch *branch_get(const char *name)
1076 if (!name || !*name || !strcmp(name, "HEAD"))
1077 ret = current_branch;
1079 ret = make_branch(name, 0);
1080 if (ret && ret->remote_name) {
1081 ret->remote = remote_get(ret->remote_name);
1082 if (ret->merge_nr) {
1084 ret->merge = xcalloc(sizeof(*ret->merge),
1086 for (i = 0; i < ret->merge_nr; i++) {
1087 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1088 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1089 remote_find_tracking(ret->remote,
1097 int branch_has_merge_config(struct branch *branch)
1099 return branch && !!branch->merge;
1102 int branch_merge_matches(struct branch *branch,
1104 const char *refname)
1106 if (!branch || i < 0 || i >= branch->merge_nr)
1108 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1111 static struct ref *get_expanded_map(const struct ref *remote_refs,
1112 const struct refspec *refspec)
1114 const struct ref *ref;
1115 struct ref *ret = NULL;
1116 struct ref **tail = &ret;
1118 int remote_prefix_len = strlen(refspec->src);
1119 int local_prefix_len = strlen(refspec->dst);
1121 for (ref = remote_refs; ref; ref = ref->next) {
1122 if (strchr(ref->name, '^'))
1123 continue; /* a dereference item */
1124 if (!prefixcmp(ref->name, refspec->src)) {
1126 struct ref *cpy = copy_ref(ref);
1127 match = ref->name + remote_prefix_len;
1129 cpy->peer_ref = alloc_ref(local_prefix_len +
1131 sprintf(cpy->peer_ref->name, "%s%s",
1132 refspec->dst, match);
1134 cpy->peer_ref->force = 1;
1143 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1145 const struct ref *ref;
1146 for (ref = refs; ref; ref = ref->next) {
1147 if (refname_match(name, ref->name, ref_fetch_rules))
1153 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1155 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1160 return copy_ref(ref);
1163 static struct ref *get_local_ref(const char *name)
1169 if (!prefixcmp(name, "refs/")) {
1170 return alloc_ref_from_str(name);
1173 if (!prefixcmp(name, "heads/") ||
1174 !prefixcmp(name, "tags/") ||
1175 !prefixcmp(name, "remotes/")) {
1176 ret = alloc_ref(strlen(name) + 6);
1177 sprintf(ret->name, "refs/%s", name);
1181 ret = alloc_ref(strlen(name) + 12);
1182 sprintf(ret->name, "refs/heads/%s", name);
1186 int get_fetch_map(const struct ref *remote_refs,
1187 const struct refspec *refspec,
1191 struct ref *ref_map, **rmp;
1193 if (refspec->pattern) {
1194 ref_map = get_expanded_map(remote_refs, refspec);
1196 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1198 ref_map = get_remote_ref(remote_refs, name);
1199 if (!missing_ok && !ref_map)
1200 die("Couldn't find remote ref %s", name);
1202 ref_map->peer_ref = get_local_ref(refspec->dst);
1203 if (ref_map->peer_ref && refspec->force)
1204 ref_map->peer_ref->force = 1;
1208 for (rmp = &ref_map; *rmp; ) {
1209 if ((*rmp)->peer_ref) {
1210 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1211 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1212 struct ref *ignore = *rmp;
1213 error("* Ignoring funny ref '%s' locally",
1214 (*rmp)->peer_ref->name);
1215 *rmp = (*rmp)->next;
1216 free(ignore->peer_ref);
1221 rmp = &((*rmp)->next);
1225 tail_link_ref(ref_map, tail);
1230 int resolve_remote_symref(struct ref *ref, struct ref *list)
1234 for (; list; list = list->next)
1235 if (!strcmp(ref->symref, list->name)) {
1236 hashcpy(ref->old_sha1, list->old_sha1);