5 struct counted_string
{
12 struct counted_string
*instead_of
;
17 static struct remote
**remotes
;
18 static int remotes_alloc
;
19 static int remotes_nr
;
21 static struct branch
**branches
;
22 static int branches_alloc
;
23 static int branches_nr
;
25 static struct branch
*current_branch
;
26 static const char *default_remote_name
;
28 static struct rewrite
**rewrite
;
29 static int rewrite_alloc
;
30 static int rewrite_nr
;
32 #define BUF_SIZE (2048)
33 static char buffer
[BUF_SIZE
];
35 static const char *alias_url(const char *url
)
39 struct counted_string
*longest
;
44 for (i
= 0; i
< rewrite_nr
; i
++) {
47 for (j
= 0; j
< rewrite
[i
]->instead_of_nr
; j
++) {
48 if (!prefixcmp(url
, rewrite
[i
]->instead_of
[j
].s
) &&
50 longest
->len
< rewrite
[i
]->instead_of
[j
].len
)) {
51 longest
= &(rewrite
[i
]->instead_of
[j
]);
59 ret
= malloc(rewrite
[longest_i
]->baselen
+
60 (strlen(url
) - longest
->len
) + 1);
61 strcpy(ret
, rewrite
[longest_i
]->base
);
62 strcpy(ret
+ rewrite
[longest_i
]->baselen
, url
+ longest
->len
);
66 static void add_push_refspec(struct remote
*remote
, const char *ref
)
68 ALLOC_GROW(remote
->push_refspec
,
69 remote
->push_refspec_nr
+ 1,
70 remote
->push_refspec_alloc
);
71 remote
->push_refspec
[remote
->push_refspec_nr
++] = ref
;
74 static void add_fetch_refspec(struct remote
*remote
, const char *ref
)
76 ALLOC_GROW(remote
->fetch_refspec
,
77 remote
->fetch_refspec_nr
+ 1,
78 remote
->fetch_refspec_alloc
);
79 remote
->fetch_refspec
[remote
->fetch_refspec_nr
++] = ref
;
82 static void add_url(struct remote
*remote
, const char *url
)
84 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
85 remote
->url
[remote
->url_nr
++] = url
;
88 static void add_url_alias(struct remote
*remote
, const char *url
)
90 add_url(remote
, alias_url(url
));
93 static struct remote
*make_remote(const char *name
, int len
)
98 for (i
= 0; i
< remotes_nr
; i
++) {
99 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
100 !remotes
[i
]->name
[len
]) :
101 !strcmp(name
, remotes
[i
]->name
))
105 ret
= xcalloc(1, sizeof(struct remote
));
106 ALLOC_GROW(remotes
, remotes_nr
+ 1, remotes_alloc
);
107 remotes
[remotes_nr
++] = ret
;
109 ret
->name
= xstrndup(name
, len
);
111 ret
->name
= xstrdup(name
);
115 static void add_merge(struct branch
*branch
, const char *name
)
117 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
118 branch
->merge_alloc
);
119 branch
->merge_name
[branch
->merge_nr
++] = name
;
122 static struct branch
*make_branch(const char *name
, int len
)
128 for (i
= 0; i
< branches_nr
; i
++) {
129 if (len
? (!strncmp(name
, branches
[i
]->name
, len
) &&
130 !branches
[i
]->name
[len
]) :
131 !strcmp(name
, branches
[i
]->name
))
135 ALLOC_GROW(branches
, branches_nr
+ 1, branches_alloc
);
136 ret
= xcalloc(1, sizeof(struct branch
));
137 branches
[branches_nr
++] = ret
;
139 ret
->name
= xstrndup(name
, len
);
141 ret
->name
= xstrdup(name
);
142 refname
= malloc(strlen(name
) + strlen("refs/heads/") + 1);
143 strcpy(refname
, "refs/heads/");
144 strcpy(refname
+ strlen("refs/heads/"), ret
->name
);
145 ret
->refname
= refname
;
150 static struct rewrite
*make_rewrite(const char *base
, int len
)
155 for (i
= 0; i
< rewrite_nr
; i
++) {
157 ? (len
== rewrite
[i
]->baselen
&&
158 !strncmp(base
, rewrite
[i
]->base
, len
))
159 : !strcmp(base
, rewrite
[i
]->base
))
163 ALLOC_GROW(rewrite
, rewrite_nr
+ 1, rewrite_alloc
);
164 ret
= xcalloc(1, sizeof(struct rewrite
));
165 rewrite
[rewrite_nr
++] = ret
;
167 ret
->base
= xstrndup(base
, len
);
171 ret
->base
= xstrdup(base
);
172 ret
->baselen
= strlen(base
);
177 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
179 ALLOC_GROW(rewrite
->instead_of
, rewrite
->instead_of_nr
+ 1, rewrite
->instead_of_alloc
);
180 rewrite
->instead_of
[rewrite
->instead_of_nr
].s
= instead_of
;
181 rewrite
->instead_of
[rewrite
->instead_of_nr
].len
= strlen(instead_of
);
182 rewrite
->instead_of_nr
++;
185 static void read_remotes_file(struct remote
*remote
)
187 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
191 while (fgets(buffer
, BUF_SIZE
, f
)) {
195 if (!prefixcmp(buffer
, "URL:")) {
198 } else if (!prefixcmp(buffer
, "Push:")) {
201 } else if (!prefixcmp(buffer
, "Pull:")) {
213 while (isspace(p
[-1]))
216 switch (value_list
) {
218 add_url_alias(remote
, xstrdup(s
));
221 add_push_refspec(remote
, xstrdup(s
));
224 add_fetch_refspec(remote
, xstrdup(s
));
231 static void read_branches_file(struct remote
*remote
)
233 const char *slash
= strchr(remote
->name
, '/');
235 struct strbuf branch
;
236 int n
= slash
? slash
- remote
->name
: 1000;
237 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
243 s
= fgets(buffer
, BUF_SIZE
, f
);
252 while (isspace(p
[-1]))
256 len
+= strlen(slash
);
257 p
= xmalloc(len
+ 1);
263 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
264 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
265 * the partial URL obtained from the branches file plus
266 * "/netdev-2.6" and does not store it in any tracking ref.
267 * #branch specifier in the file is ignored.
269 * Otherwise, the branches file would have URL and optionally
270 * #branch specified. The "master" (or specified) branch is
271 * fetched and stored in the local branch of the same name.
273 strbuf_init(&branch
, 0);
274 frag
= strchr(p
, '#');
277 strbuf_addf(&branch
, "refs/heads/%s", frag
);
279 strbuf_addstr(&branch
, "refs/heads/master");
281 strbuf_addf(&branch
, ":refs/heads/%s", remote
->name
);
283 strbuf_reset(&branch
);
284 strbuf_addstr(&branch
, "HEAD:");
286 add_url_alias(remote
, p
);
287 add_fetch_refspec(remote
, strbuf_detach(&branch
, 0));
288 remote
->fetch_tags
= 1; /* always auto-follow */
291 static int handle_config(const char *key
, const char *value
)
295 struct remote
*remote
;
296 struct branch
*branch
;
297 if (!prefixcmp(key
, "branch.")) {
299 subkey
= strrchr(name
, '.');
302 branch
= make_branch(name
, subkey
- name
);
303 if (!strcmp(subkey
, ".remote")) {
305 return config_error_nonbool(key
);
306 branch
->remote_name
= xstrdup(value
);
307 if (branch
== current_branch
)
308 default_remote_name
= branch
->remote_name
;
309 } else if (!strcmp(subkey
, ".merge")) {
311 return config_error_nonbool(key
);
312 add_merge(branch
, xstrdup(value
));
316 if (!prefixcmp(key
, "url.")) {
317 struct rewrite
*rewrite
;
319 subkey
= strrchr(name
, '.');
322 rewrite
= make_rewrite(name
, subkey
- name
);
323 if (!strcmp(subkey
, ".insteadof")) {
325 return config_error_nonbool(key
);
326 add_instead_of(rewrite
, xstrdup(value
));
329 if (prefixcmp(key
, "remote."))
332 subkey
= strrchr(name
, '.');
334 return error("Config with no key for remote %s", name
);
335 if (*subkey
== '/') {
336 warning("Config remote shorthand cannot begin with '/': %s", name
);
339 remote
= make_remote(name
, subkey
- name
);
341 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
344 * is a valid way to set it to true; we get NULL in value so
345 * we need to handle it here.
347 * if (!strcmp(subkey, ".disabled")) {
348 * val = git_config_bool(key, value);
353 return 0; /* ignore unknown booleans */
355 if (!strcmp(subkey
, ".url")) {
356 add_url(remote
, xstrdup(value
));
357 } else if (!strcmp(subkey
, ".push")) {
358 add_push_refspec(remote
, xstrdup(value
));
359 } else if (!strcmp(subkey
, ".fetch")) {
360 add_fetch_refspec(remote
, xstrdup(value
));
361 } else if (!strcmp(subkey
, ".receivepack")) {
362 if (!remote
->receivepack
)
363 remote
->receivepack
= xstrdup(value
);
365 error("more than one receivepack given, using the first");
366 } else if (!strcmp(subkey
, ".uploadpack")) {
367 if (!remote
->uploadpack
)
368 remote
->uploadpack
= xstrdup(value
);
370 error("more than one uploadpack given, using the first");
371 } else if (!strcmp(subkey
, ".tagopt")) {
372 if (!strcmp(value
, "--no-tags"))
373 remote
->fetch_tags
= -1;
374 } else if (!strcmp(subkey
, ".proxy")) {
375 remote
->http_proxy
= xstrdup(value
);
376 } else if (!strcmp(subkey
, ".skipdefaultupdate"))
377 remote
->skip_default_update
= 1;
381 static void alias_all_urls(void)
384 for (i
= 0; i
< remotes_nr
; i
++) {
387 for (j
= 0; j
< remotes
[i
]->url_nr
; j
++) {
388 remotes
[i
]->url
[j
] = alias_url(remotes
[i
]->url
[j
]);
393 static void read_config(void)
395 unsigned char sha1
[20];
396 const char *head_ref
;
398 if (default_remote_name
) // did this already
400 default_remote_name
= xstrdup("origin");
401 current_branch
= NULL
;
402 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
403 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
404 !prefixcmp(head_ref
, "refs/heads/")) {
406 make_branch(head_ref
+ strlen("refs/heads/"), 0);
408 git_config(handle_config
);
412 static struct refspec
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
416 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
418 for (i
= 0; i
< nr_refspec
; i
++) {
421 const char *lhs
, *rhs
;
423 llen
= rlen
= is_glob
= 0;
431 rhs
= strrchr(lhs
, ':');
435 is_glob
= (2 <= rlen
&& !strcmp(rhs
+ rlen
- 2, "/*"));
438 rs
[i
].dst
= xstrndup(rhs
, rlen
);
441 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
442 if (2 <= llen
&& !memcmp(lhs
+ llen
- 2, "/*", 2)) {
443 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
447 } else if (rhs
&& is_glob
) {
451 rs
[i
].pattern
= is_glob
;
452 rs
[i
].src
= xstrndup(lhs
, llen
);
457 * - empty is allowed; it means HEAD.
458 * - otherwise it must be a valid looking ref.
463 st
= check_ref_format(rs
[i
].src
);
464 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
469 * - missing is ok, and is same as empty.
470 * - empty is ok; it means not to store.
471 * - otherwise it must be a valid looking ref.
475 } else if (!*rs
[i
].dst
) {
478 st
= check_ref_format(rs
[i
].dst
);
479 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
485 * - empty is allowed; it means delete.
486 * - when wildcarded, it must be a valid looking ref.
487 * - otherwise, it must be an extended SHA-1, but
488 * there is no existing way to validate this.
493 st
= check_ref_format(rs
[i
].src
);
494 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
498 ; /* anything goes, for now */
501 * - missing is allowed, but LHS then must be a
503 * - empty is not allowed.
504 * - otherwise it must be a valid looking ref.
507 st
= check_ref_format(rs
[i
].src
);
508 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
510 } else if (!*rs
[i
].dst
) {
513 st
= check_ref_format(rs
[i
].dst
);
514 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
526 die("Invalid refspec '%s'", refspec
[i
]);
529 int valid_fetch_refspec(const char *fetch_refspec_str
)
531 const char *fetch_refspec
[] = { fetch_refspec_str
};
532 struct refspec
*refspec
;
534 refspec
= parse_refspec_internal(1, fetch_refspec
, 1, 1);
540 struct refspec
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
542 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
545 struct refspec
*parse_push_refspec(int nr_refspec
, const char **refspec
)
547 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
550 static int valid_remote_nick(const char *name
)
552 if (!name
[0] || /* not empty */
553 (name
[0] == '.' && /* not "." */
554 (!name
[1] || /* not ".." */
555 (name
[1] == '.' && !name
[2]))))
557 return !strchr(name
, '/'); /* no slash */
560 struct remote
*remote_get(const char *name
)
566 name
= default_remote_name
;
567 ret
= make_remote(name
, 0);
568 if (valid_remote_nick(name
)) {
570 read_remotes_file(ret
);
572 read_branches_file(ret
);
575 add_url_alias(ret
, name
);
578 ret
->fetch
= parse_fetch_refspec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
579 ret
->push
= parse_push_refspec(ret
->push_refspec_nr
, ret
->push_refspec
);
583 int for_each_remote(each_remote_fn fn
, void *priv
)
587 for (i
= 0; i
< remotes_nr
&& !result
; i
++) {
588 struct remote
*r
= remotes
[i
];
592 r
->fetch
= parse_fetch_refspec(r
->fetch_refspec_nr
,
595 r
->push
= parse_push_refspec(r
->push_refspec_nr
,
597 result
= fn(r
, priv
);
602 void ref_remove_duplicates(struct ref
*ref_map
)
606 for (; ref_map
; ref_map
= ref_map
->next
) {
607 if (!ref_map
->peer_ref
)
609 posn
= &ref_map
->next
;
611 if ((*posn
)->peer_ref
&&
612 !strcmp((*posn
)->peer_ref
->name
,
613 ref_map
->peer_ref
->name
)) {
614 if (strcmp((*posn
)->name
, ref_map
->name
))
615 die("%s tracks both %s and %s",
616 ref_map
->peer_ref
->name
,
617 (*posn
)->name
, ref_map
->name
);
618 next
= (*posn
)->next
;
619 free((*posn
)->peer_ref
);
623 posn
= &(*posn
)->next
;
629 int remote_has_url(struct remote
*remote
, const char *url
)
632 for (i
= 0; i
< remote
->url_nr
; i
++) {
633 if (!strcmp(remote
->url
[i
], url
))
639 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
641 int find_src
= refspec
->src
== NULL
;
642 char *needle
, **result
;
647 return error("find_tracking: need either src or dst");
648 needle
= refspec
->dst
;
649 result
= &refspec
->src
;
651 needle
= refspec
->src
;
652 result
= &refspec
->dst
;
655 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
656 struct refspec
*fetch
= &remote
->fetch
[i
];
657 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
658 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
661 if (fetch
->pattern
) {
662 if (!prefixcmp(needle
, key
) &&
663 needle
[strlen(key
)] == '/') {
664 *result
= xmalloc(strlen(value
) +
667 strcpy(*result
, value
);
668 strcpy(*result
+ strlen(value
),
669 needle
+ strlen(key
));
670 refspec
->force
= fetch
->force
;
673 } else if (!strcmp(needle
, key
)) {
674 *result
= xstrdup(value
);
675 refspec
->force
= fetch
->force
;
682 struct ref
*alloc_ref(unsigned namelen
)
684 struct ref
*ret
= xmalloc(sizeof(struct ref
) + namelen
);
685 memset(ret
, 0, sizeof(struct ref
) + namelen
);
689 static struct ref
*copy_ref(const struct ref
*ref
)
691 struct ref
*ret
= xmalloc(sizeof(struct ref
) + strlen(ref
->name
) + 1);
692 memcpy(ret
, ref
, sizeof(struct ref
) + strlen(ref
->name
) + 1);
697 struct ref
*copy_ref_list(const struct ref
*ref
)
699 struct ref
*ret
= NULL
;
700 struct ref
**tail
= &ret
;
702 *tail
= copy_ref(ref
);
704 tail
= &((*tail
)->next
);
709 void free_ref(struct ref
*ref
)
713 free(ref
->remote_status
);
718 void free_refs(struct ref
*ref
)
729 static int count_refspec_match(const char *pattern
,
731 struct ref
**matched_ref
)
733 int patlen
= strlen(pattern
);
734 struct ref
*matched_weak
= NULL
;
735 struct ref
*matched
= NULL
;
739 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
740 char *name
= refs
->name
;
741 int namelen
= strlen(name
);
743 if (!refname_match(pattern
, name
, ref_rev_parse_rules
))
746 /* A match is "weak" if it is with refs outside
747 * heads or tags, and did not specify the pattern
748 * in full (e.g. "refs/remotes/origin/master") or at
749 * least from the toplevel (e.g. "remotes/origin/master");
750 * otherwise "git push $URL master" would result in
751 * ambiguity between remotes/origin/master and heads/master
752 * at the remote site.
754 if (namelen
!= patlen
&&
755 patlen
!= namelen
- 5 &&
756 prefixcmp(name
, "refs/heads/") &&
757 prefixcmp(name
, "refs/tags/")) {
758 /* We want to catch the case where only weak
759 * matches are found and there are multiple
760 * matches, and where more than one strong
761 * matches are found, as ambiguous. One
762 * strong match with zero or more weak matches
763 * are acceptable as a unique match.
774 *matched_ref
= matched_weak
;
778 *matched_ref
= matched
;
783 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
791 static struct ref
*try_explicit_object_name(const char *name
)
793 unsigned char sha1
[20];
799 strcpy(ref
->name
, "(delete)");
800 hashclr(ref
->new_sha1
);
803 if (get_sha1(name
, sha1
))
805 len
= strlen(name
) + 1;
806 ref
= alloc_ref(len
);
807 memcpy(ref
->name
, name
, len
);
808 hashcpy(ref
->new_sha1
, sha1
);
812 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
817 len
= strlen(name
) + 1;
818 ret
= alloc_ref(len
);
819 memcpy(ret
->name
, name
, len
);
820 tail_link_ref(ret
, tail
);
824 static char *guess_ref(const char *name
, struct ref
*peer
)
826 struct strbuf buf
= STRBUF_INIT
;
827 unsigned char sha1
[20];
829 const char *r
= resolve_ref(peer
->name
, sha1
, 1, NULL
);
833 if (!prefixcmp(r
, "refs/heads/"))
834 strbuf_addstr(&buf
, "refs/heads/");
835 else if (!prefixcmp(r
, "refs/tags/"))
836 strbuf_addstr(&buf
, "refs/tags/");
840 strbuf_addstr(&buf
, name
);
841 return strbuf_detach(&buf
, NULL
);
844 static int match_explicit(struct ref
*src
, struct ref
*dst
,
845 struct ref
***dst_tail
,
849 struct ref
*matched_src
, *matched_dst
;
851 const char *dst_value
= rs
->dst
;
857 matched_src
= matched_dst
= NULL
;
858 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
862 /* The source could be in the get_sha1() format
863 * not a reference name. :refs/other is a
864 * way to delete 'other' ref at the remote end.
866 matched_src
= try_explicit_object_name(rs
->src
);
868 error("src refspec %s does not match any.", rs
->src
);
872 error("src refspec %s matches more than one.", rs
->src
);
880 unsigned char sha1
[20];
885 dst_value
= resolve_ref(matched_src
->name
, sha1
, 1, &flag
);
887 ((flag
& REF_ISSYMREF
) &&
888 prefixcmp(dst_value
, "refs/heads/")))
889 die("%s cannot be resolved to branch.",
893 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
897 if (!memcmp(dst_value
, "refs/", 5))
898 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
899 else if((dst_guess
= guess_ref(dst_value
, matched_src
)))
900 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
902 error("unable to push to unqualified destination: %s\n"
903 "The destination refspec neither matches an "
904 "existing ref on the remote nor\n"
905 "begins with refs/, and we are unable to "
906 "guess a prefix based on the source ref.",
911 error("dst refspec %s matches more than one.",
915 if (errs
|| !matched_dst
)
917 if (matched_dst
->peer_ref
) {
919 error("dst ref %s receives from more than one src.",
923 matched_dst
->peer_ref
= matched_src
;
924 matched_dst
->force
= rs
->force
;
929 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
930 struct ref
***dst_tail
, struct refspec
*rs
,
934 for (i
= errs
= 0; i
< rs_nr
; i
++)
935 errs
|= match_explicit(src
, dst
, dst_tail
, &rs
[i
], errs
);
939 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
941 const struct ref
*src
)
944 for (i
= 0; i
< rs_nr
; i
++) {
946 !prefixcmp(src
->name
, rs
[i
].src
) &&
947 src
->name
[strlen(rs
[i
].src
)] == '/')
954 * Note. This is used only by "push"; refspec matching rules for
955 * push and fetch are subtly different, so do not try to reuse it
958 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
959 int nr_refspec
, const char **refspec
, int flags
)
962 parse_push_refspec(nr_refspec
, (const char **) refspec
);
963 int send_all
= flags
& MATCH_REFS_ALL
;
964 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
966 if (match_explicit_refs(src
, dst
, dst_tail
, rs
, nr_refspec
))
969 /* pick the remainder */
970 for ( ; src
; src
= src
->next
) {
971 struct ref
*dst_peer
;
972 const struct refspec
*pat
= NULL
;
977 pat
= check_pattern_match(rs
, nr_refspec
, src
);
981 else if (!send_mirror
&& prefixcmp(src
->name
, "refs/heads/"))
983 * "matching refs"; traditionally we pushed everything
984 * including refs outside refs/heads/ hierarchy, but
985 * that does not make much sense these days.
990 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
991 dst_name
= xmalloc(strlen(dst_side
) +
993 strlen(pat
->src
) + 2);
994 strcpy(dst_name
, dst_side
);
995 strcat(dst_name
, src
->name
+ strlen(pat
->src
));
997 dst_name
= xstrdup(src
->name
);
998 dst_peer
= find_ref_by_name(dst
, dst_name
);
999 if (dst_peer
&& dst_peer
->peer_ref
)
1000 /* We're already sending something to this ref. */
1003 if (!dst_peer
&& !nr_refspec
&& !(send_all
|| send_mirror
))
1005 * Remote doesn't have it, and we have no
1006 * explicit pattern, and we don't have
1007 * --all nor --mirror.
1011 /* Create a new one and link it */
1012 dst_peer
= make_linked_ref(dst_name
, dst_tail
);
1013 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
1015 dst_peer
->peer_ref
= src
;
1017 dst_peer
->force
= pat
->force
;
1024 struct branch
*branch_get(const char *name
)
1029 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1030 ret
= current_branch
;
1032 ret
= make_branch(name
, 0);
1033 if (ret
&& ret
->remote_name
) {
1034 ret
->remote
= remote_get(ret
->remote_name
);
1035 if (ret
->merge_nr
) {
1037 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
1039 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1040 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1041 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1042 remote_find_tracking(ret
->remote
,
1050 int branch_has_merge_config(struct branch
*branch
)
1052 return branch
&& !!branch
->merge
;
1055 int branch_merge_matches(struct branch
*branch
,
1057 const char *refname
)
1059 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1061 return refname_match(branch
->merge
[i
]->src
, refname
, ref_fetch_rules
);
1064 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
1065 const struct refspec
*refspec
)
1067 const struct ref
*ref
;
1068 struct ref
*ret
= NULL
;
1069 struct ref
**tail
= &ret
;
1071 int remote_prefix_len
= strlen(refspec
->src
);
1072 int local_prefix_len
= strlen(refspec
->dst
);
1074 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1075 if (strchr(ref
->name
, '^'))
1076 continue; /* a dereference item */
1077 if (!prefixcmp(ref
->name
, refspec
->src
)) {
1079 struct ref
*cpy
= copy_ref(ref
);
1080 match
= ref
->name
+ remote_prefix_len
;
1082 cpy
->peer_ref
= alloc_ref(local_prefix_len
+
1084 sprintf(cpy
->peer_ref
->name
, "%s%s",
1085 refspec
->dst
, match
);
1087 cpy
->peer_ref
->force
= 1;
1096 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
1098 const struct ref
*ref
;
1099 for (ref
= refs
; ref
; ref
= ref
->next
) {
1100 if (refname_match(name
, ref
->name
, ref_fetch_rules
))
1106 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
1108 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
1113 return copy_ref(ref
);
1116 static struct ref
*get_local_ref(const char *name
)
1122 if (!prefixcmp(name
, "refs/")) {
1123 ret
= alloc_ref(strlen(name
) + 1);
1124 strcpy(ret
->name
, name
);
1128 if (!prefixcmp(name
, "heads/") ||
1129 !prefixcmp(name
, "tags/") ||
1130 !prefixcmp(name
, "remotes/")) {
1131 ret
= alloc_ref(strlen(name
) + 6);
1132 sprintf(ret
->name
, "refs/%s", name
);
1136 ret
= alloc_ref(strlen(name
) + 12);
1137 sprintf(ret
->name
, "refs/heads/%s", name
);
1141 int get_fetch_map(const struct ref
*remote_refs
,
1142 const struct refspec
*refspec
,
1146 struct ref
*ref_map
, **rmp
;
1148 if (refspec
->pattern
) {
1149 ref_map
= get_expanded_map(remote_refs
, refspec
);
1151 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
1153 ref_map
= get_remote_ref(remote_refs
, name
);
1154 if (!missing_ok
&& !ref_map
)
1155 die("Couldn't find remote ref %s", name
);
1157 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
1158 if (ref_map
->peer_ref
&& refspec
->force
)
1159 ref_map
->peer_ref
->force
= 1;
1163 for (rmp
= &ref_map
; *rmp
; ) {
1164 if ((*rmp
)->peer_ref
) {
1165 int st
= check_ref_format((*rmp
)->peer_ref
->name
+ 5);
1166 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
) {
1167 struct ref
*ignore
= *rmp
;
1168 error("* Ignoring funny ref '%s' locally",
1169 (*rmp
)->peer_ref
->name
);
1170 *rmp
= (*rmp
)->next
;
1171 free(ignore
->peer_ref
);
1176 rmp
= &((*rmp
)->next
);
1180 tail_link_ref(ref_map
, tail
);
1185 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
1189 for (; list
; list
= list
->next
)
1190 if (!strcmp(ref
->symref
, list
->name
)) {
1191 hashcpy(ref
->old_sha1
, list
->old_sha1
);