5 static struct refspec s_tag_refspec
= {
12 const struct refspec
*tag_refspec
= &s_tag_refspec
;
14 struct counted_string
{
21 struct counted_string
*instead_of
;
26 static struct remote
**remotes
;
27 static int remotes_alloc
;
28 static int remotes_nr
;
30 static struct branch
**branches
;
31 static int branches_alloc
;
32 static int branches_nr
;
34 static struct branch
*current_branch
;
35 static const char *default_remote_name
;
37 static struct rewrite
**rewrite
;
38 static int rewrite_alloc
;
39 static int rewrite_nr
;
41 #define BUF_SIZE (2048)
42 static char buffer
[BUF_SIZE
];
44 static const char *alias_url(const char *url
)
48 struct counted_string
*longest
;
53 for (i
= 0; i
< rewrite_nr
; i
++) {
56 for (j
= 0; j
< rewrite
[i
]->instead_of_nr
; j
++) {
57 if (!prefixcmp(url
, rewrite
[i
]->instead_of
[j
].s
) &&
59 longest
->len
< rewrite
[i
]->instead_of
[j
].len
)) {
60 longest
= &(rewrite
[i
]->instead_of
[j
]);
68 ret
= malloc(rewrite
[longest_i
]->baselen
+
69 (strlen(url
) - longest
->len
) + 1);
70 strcpy(ret
, rewrite
[longest_i
]->base
);
71 strcpy(ret
+ rewrite
[longest_i
]->baselen
, url
+ longest
->len
);
75 static void add_push_refspec(struct remote
*remote
, const char *ref
)
77 ALLOC_GROW(remote
->push_refspec
,
78 remote
->push_refspec_nr
+ 1,
79 remote
->push_refspec_alloc
);
80 remote
->push_refspec
[remote
->push_refspec_nr
++] = ref
;
83 static void add_fetch_refspec(struct remote
*remote
, const char *ref
)
85 ALLOC_GROW(remote
->fetch_refspec
,
86 remote
->fetch_refspec_nr
+ 1,
87 remote
->fetch_refspec_alloc
);
88 remote
->fetch_refspec
[remote
->fetch_refspec_nr
++] = ref
;
91 static void add_url(struct remote
*remote
, const char *url
)
93 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
94 remote
->url
[remote
->url_nr
++] = url
;
97 static void add_url_alias(struct remote
*remote
, const char *url
)
99 add_url(remote
, alias_url(url
));
102 static struct remote
*make_remote(const char *name
, int len
)
107 for (i
= 0; i
< remotes_nr
; i
++) {
108 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
109 !remotes
[i
]->name
[len
]) :
110 !strcmp(name
, remotes
[i
]->name
))
114 ret
= xcalloc(1, sizeof(struct remote
));
115 ALLOC_GROW(remotes
, remotes_nr
+ 1, remotes_alloc
);
116 remotes
[remotes_nr
++] = ret
;
118 ret
->name
= xstrndup(name
, len
);
120 ret
->name
= xstrdup(name
);
124 static void add_merge(struct branch
*branch
, const char *name
)
126 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
127 branch
->merge_alloc
);
128 branch
->merge_name
[branch
->merge_nr
++] = name
;
131 static struct branch
*make_branch(const char *name
, int len
)
137 for (i
= 0; i
< branches_nr
; i
++) {
138 if (len
? (!strncmp(name
, branches
[i
]->name
, len
) &&
139 !branches
[i
]->name
[len
]) :
140 !strcmp(name
, branches
[i
]->name
))
144 ALLOC_GROW(branches
, branches_nr
+ 1, branches_alloc
);
145 ret
= xcalloc(1, sizeof(struct branch
));
146 branches
[branches_nr
++] = ret
;
148 ret
->name
= xstrndup(name
, len
);
150 ret
->name
= xstrdup(name
);
151 refname
= malloc(strlen(name
) + strlen("refs/heads/") + 1);
152 strcpy(refname
, "refs/heads/");
153 strcpy(refname
+ strlen("refs/heads/"), ret
->name
);
154 ret
->refname
= refname
;
159 static struct rewrite
*make_rewrite(const char *base
, int len
)
164 for (i
= 0; i
< rewrite_nr
; i
++) {
166 ? (len
== rewrite
[i
]->baselen
&&
167 !strncmp(base
, rewrite
[i
]->base
, len
))
168 : !strcmp(base
, rewrite
[i
]->base
))
172 ALLOC_GROW(rewrite
, rewrite_nr
+ 1, rewrite_alloc
);
173 ret
= xcalloc(1, sizeof(struct rewrite
));
174 rewrite
[rewrite_nr
++] = ret
;
176 ret
->base
= xstrndup(base
, len
);
180 ret
->base
= xstrdup(base
);
181 ret
->baselen
= strlen(base
);
186 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
188 ALLOC_GROW(rewrite
->instead_of
, rewrite
->instead_of_nr
+ 1, rewrite
->instead_of_alloc
);
189 rewrite
->instead_of
[rewrite
->instead_of_nr
].s
= instead_of
;
190 rewrite
->instead_of
[rewrite
->instead_of_nr
].len
= strlen(instead_of
);
191 rewrite
->instead_of_nr
++;
194 static void read_remotes_file(struct remote
*remote
)
196 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
200 while (fgets(buffer
, BUF_SIZE
, f
)) {
204 if (!prefixcmp(buffer
, "URL:")) {
207 } else if (!prefixcmp(buffer
, "Push:")) {
210 } else if (!prefixcmp(buffer
, "Pull:")) {
222 while (isspace(p
[-1]))
225 switch (value_list
) {
227 add_url_alias(remote
, xstrdup(s
));
230 add_push_refspec(remote
, xstrdup(s
));
233 add_fetch_refspec(remote
, xstrdup(s
));
240 static void read_branches_file(struct remote
*remote
)
242 const char *slash
= strchr(remote
->name
, '/');
244 struct strbuf branch
;
245 int n
= slash
? slash
- remote
->name
: 1000;
246 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
252 s
= fgets(buffer
, BUF_SIZE
, f
);
261 while (isspace(p
[-1]))
265 len
+= strlen(slash
);
266 p
= xmalloc(len
+ 1);
272 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
273 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
274 * the partial URL obtained from the branches file plus
275 * "/netdev-2.6" and does not store it in any tracking ref.
276 * #branch specifier in the file is ignored.
278 * Otherwise, the branches file would have URL and optionally
279 * #branch specified. The "master" (or specified) branch is
280 * fetched and stored in the local branch of the same name.
282 strbuf_init(&branch
, 0);
283 frag
= strchr(p
, '#');
286 strbuf_addf(&branch
, "refs/heads/%s", frag
);
288 strbuf_addstr(&branch
, "refs/heads/master");
290 strbuf_addf(&branch
, ":refs/heads/%s", remote
->name
);
292 strbuf_reset(&branch
);
293 strbuf_addstr(&branch
, "HEAD:");
295 add_url_alias(remote
, p
);
296 add_fetch_refspec(remote
, strbuf_detach(&branch
, 0));
297 remote
->fetch_tags
= 1; /* always auto-follow */
300 static int handle_config(const char *key
, const char *value
)
304 struct remote
*remote
;
305 struct branch
*branch
;
306 if (!prefixcmp(key
, "branch.")) {
308 subkey
= strrchr(name
, '.');
311 branch
= make_branch(name
, subkey
- name
);
312 if (!strcmp(subkey
, ".remote")) {
314 return config_error_nonbool(key
);
315 branch
->remote_name
= xstrdup(value
);
316 if (branch
== current_branch
)
317 default_remote_name
= branch
->remote_name
;
318 } else if (!strcmp(subkey
, ".merge")) {
320 return config_error_nonbool(key
);
321 add_merge(branch
, xstrdup(value
));
325 if (!prefixcmp(key
, "url.")) {
326 struct rewrite
*rewrite
;
328 subkey
= strrchr(name
, '.');
331 rewrite
= make_rewrite(name
, subkey
- name
);
332 if (!strcmp(subkey
, ".insteadof")) {
334 return config_error_nonbool(key
);
335 add_instead_of(rewrite
, xstrdup(value
));
338 if (prefixcmp(key
, "remote."))
341 subkey
= strrchr(name
, '.');
343 return error("Config with no key for remote %s", name
);
344 if (*subkey
== '/') {
345 warning("Config remote shorthand cannot begin with '/': %s", name
);
348 remote
= make_remote(name
, subkey
- name
);
350 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
353 * is a valid way to set it to true; we get NULL in value so
354 * we need to handle it here.
356 * if (!strcmp(subkey, ".disabled")) {
357 * val = git_config_bool(key, value);
362 return 0; /* ignore unknown booleans */
364 if (!strcmp(subkey
, ".url")) {
365 add_url(remote
, xstrdup(value
));
366 } else if (!strcmp(subkey
, ".push")) {
367 add_push_refspec(remote
, xstrdup(value
));
368 } else if (!strcmp(subkey
, ".fetch")) {
369 add_fetch_refspec(remote
, xstrdup(value
));
370 } else if (!strcmp(subkey
, ".receivepack")) {
371 if (!remote
->receivepack
)
372 remote
->receivepack
= xstrdup(value
);
374 error("more than one receivepack given, using the first");
375 } else if (!strcmp(subkey
, ".uploadpack")) {
376 if (!remote
->uploadpack
)
377 remote
->uploadpack
= xstrdup(value
);
379 error("more than one uploadpack given, using the first");
380 } else if (!strcmp(subkey
, ".tagopt")) {
381 if (!strcmp(value
, "--no-tags"))
382 remote
->fetch_tags
= -1;
383 } else if (!strcmp(subkey
, ".proxy")) {
384 remote
->http_proxy
= xstrdup(value
);
385 } else if (!strcmp(subkey
, ".skipdefaultupdate"))
386 remote
->skip_default_update
= 1;
390 static void alias_all_urls(void)
393 for (i
= 0; i
< remotes_nr
; i
++) {
396 for (j
= 0; j
< remotes
[i
]->url_nr
; j
++) {
397 remotes
[i
]->url
[j
] = alias_url(remotes
[i
]->url
[j
]);
402 static void read_config(void)
404 unsigned char sha1
[20];
405 const char *head_ref
;
407 if (default_remote_name
) // did this already
409 default_remote_name
= xstrdup("origin");
410 current_branch
= NULL
;
411 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
412 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
413 !prefixcmp(head_ref
, "refs/heads/")) {
415 make_branch(head_ref
+ strlen("refs/heads/"), 0);
417 git_config(handle_config
);
421 static struct refspec
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
425 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
427 for (i
= 0; i
< nr_refspec
; i
++) {
430 const char *lhs
, *rhs
;
432 llen
= rlen
= is_glob
= 0;
440 rhs
= strrchr(lhs
, ':');
444 is_glob
= (2 <= rlen
&& !strcmp(rhs
+ rlen
- 2, "/*"));
447 rs
[i
].dst
= xstrndup(rhs
, rlen
);
450 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
451 if (2 <= llen
&& !memcmp(lhs
+ llen
- 2, "/*", 2)) {
452 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
456 } else if (rhs
&& is_glob
) {
460 rs
[i
].pattern
= is_glob
;
461 rs
[i
].src
= xstrndup(lhs
, llen
);
466 * - empty is allowed; it means HEAD.
467 * - otherwise it must be a valid looking ref.
472 st
= check_ref_format(rs
[i
].src
);
473 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
478 * - missing is ok, and is same as empty.
479 * - empty is ok; it means not to store.
480 * - otherwise it must be a valid looking ref.
484 } else if (!*rs
[i
].dst
) {
487 st
= check_ref_format(rs
[i
].dst
);
488 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
494 * - empty is allowed; it means delete.
495 * - when wildcarded, it must be a valid looking ref.
496 * - otherwise, it must be an extended SHA-1, but
497 * there is no existing way to validate this.
502 st
= check_ref_format(rs
[i
].src
);
503 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
507 ; /* anything goes, for now */
510 * - missing is allowed, but LHS then must be a
512 * - empty is not allowed.
513 * - otherwise it must be a valid looking ref.
516 st
= check_ref_format(rs
[i
].src
);
517 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
519 } else if (!*rs
[i
].dst
) {
522 st
= check_ref_format(rs
[i
].dst
);
523 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
535 die("Invalid refspec '%s'", refspec
[i
]);
538 int valid_fetch_refspec(const char *fetch_refspec_str
)
540 const char *fetch_refspec
[] = { fetch_refspec_str
};
541 struct refspec
*refspec
;
543 refspec
= parse_refspec_internal(1, fetch_refspec
, 1, 1);
549 struct refspec
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
551 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
554 struct refspec
*parse_push_refspec(int nr_refspec
, const char **refspec
)
556 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
559 static int valid_remote_nick(const char *name
)
561 if (!name
[0] || /* not empty */
562 (name
[0] == '.' && /* not "." */
563 (!name
[1] || /* not ".." */
564 (name
[1] == '.' && !name
[2]))))
566 return !strchr(name
, '/'); /* no slash */
569 struct remote
*remote_get(const char *name
)
575 name
= default_remote_name
;
576 ret
= make_remote(name
, 0);
577 if (valid_remote_nick(name
)) {
579 read_remotes_file(ret
);
581 read_branches_file(ret
);
584 add_url_alias(ret
, name
);
587 ret
->fetch
= parse_fetch_refspec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
588 ret
->push
= parse_push_refspec(ret
->push_refspec_nr
, ret
->push_refspec
);
592 int for_each_remote(each_remote_fn fn
, void *priv
)
596 for (i
= 0; i
< remotes_nr
&& !result
; i
++) {
597 struct remote
*r
= remotes
[i
];
601 r
->fetch
= parse_fetch_refspec(r
->fetch_refspec_nr
,
604 r
->push
= parse_push_refspec(r
->push_refspec_nr
,
606 result
= fn(r
, priv
);
611 void ref_remove_duplicates(struct ref
*ref_map
)
615 for (; ref_map
; ref_map
= ref_map
->next
) {
616 if (!ref_map
->peer_ref
)
618 posn
= &ref_map
->next
;
620 if ((*posn
)->peer_ref
&&
621 !strcmp((*posn
)->peer_ref
->name
,
622 ref_map
->peer_ref
->name
)) {
623 if (strcmp((*posn
)->name
, ref_map
->name
))
624 die("%s tracks both %s and %s",
625 ref_map
->peer_ref
->name
,
626 (*posn
)->name
, ref_map
->name
);
627 next
= (*posn
)->next
;
628 free((*posn
)->peer_ref
);
632 posn
= &(*posn
)->next
;
638 int remote_has_url(struct remote
*remote
, const char *url
)
641 for (i
= 0; i
< remote
->url_nr
; i
++) {
642 if (!strcmp(remote
->url
[i
], url
))
648 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
650 int find_src
= refspec
->src
== NULL
;
651 char *needle
, **result
;
656 return error("find_tracking: need either src or dst");
657 needle
= refspec
->dst
;
658 result
= &refspec
->src
;
660 needle
= refspec
->src
;
661 result
= &refspec
->dst
;
664 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
665 struct refspec
*fetch
= &remote
->fetch
[i
];
666 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
667 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
670 if (fetch
->pattern
) {
671 if (!prefixcmp(needle
, key
) &&
672 needle
[strlen(key
)] == '/') {
673 *result
= xmalloc(strlen(value
) +
676 strcpy(*result
, value
);
677 strcpy(*result
+ strlen(value
),
678 needle
+ strlen(key
));
679 refspec
->force
= fetch
->force
;
682 } else if (!strcmp(needle
, key
)) {
683 *result
= xstrdup(value
);
684 refspec
->force
= fetch
->force
;
691 struct ref
*alloc_ref(unsigned namelen
)
693 struct ref
*ret
= xmalloc(sizeof(struct ref
) + namelen
);
694 memset(ret
, 0, sizeof(struct ref
) + namelen
);
698 static struct ref
*copy_ref(const struct ref
*ref
)
700 struct ref
*ret
= xmalloc(sizeof(struct ref
) + strlen(ref
->name
) + 1);
701 memcpy(ret
, ref
, sizeof(struct ref
) + strlen(ref
->name
) + 1);
706 struct ref
*copy_ref_list(const struct ref
*ref
)
708 struct ref
*ret
= NULL
;
709 struct ref
**tail
= &ret
;
711 *tail
= copy_ref(ref
);
713 tail
= &((*tail
)->next
);
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
);