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
);
349 if (!strcmp(subkey
, ".mirror"))
350 remote
->mirror
= git_config_bool(key
, value
);
351 else if (!strcmp(subkey
, ".skipdefaultupdate"))
352 remote
->skip_default_update
= git_config_bool(key
, value
);
354 else if (!strcmp(subkey
, ".url")) {
356 if (git_config_string(&v
, key
, value
))
359 } else if (!strcmp(subkey
, ".push")) {
361 if (git_config_string(&v
, key
, value
))
363 add_push_refspec(remote
, v
);
364 } else if (!strcmp(subkey
, ".fetch")) {
366 if (git_config_string(&v
, key
, value
))
368 add_fetch_refspec(remote
, v
);
369 } else if (!strcmp(subkey
, ".receivepack")) {
371 if (git_config_string(&v
, key
, value
))
373 if (!remote
->receivepack
)
374 remote
->receivepack
= v
;
376 error("more than one receivepack given, using the first");
377 } else if (!strcmp(subkey
, ".uploadpack")) {
379 if (git_config_string(&v
, key
, value
))
381 if (!remote
->uploadpack
)
382 remote
->uploadpack
= v
;
384 error("more than one uploadpack given, using the first");
385 } else if (!strcmp(subkey
, ".tagopt")) {
386 if (!strcmp(value
, "--no-tags"))
387 remote
->fetch_tags
= -1;
388 } else if (!strcmp(subkey
, ".proxy")) {
389 return git_config_string((const char **)&remote
->http_proxy
,
395 static void alias_all_urls(void)
398 for (i
= 0; i
< remotes_nr
; i
++) {
401 for (j
= 0; j
< remotes
[i
]->url_nr
; j
++) {
402 remotes
[i
]->url
[j
] = alias_url(remotes
[i
]->url
[j
]);
407 static void read_config(void)
409 unsigned char sha1
[20];
410 const char *head_ref
;
412 if (default_remote_name
) // did this already
414 default_remote_name
= xstrdup("origin");
415 current_branch
= NULL
;
416 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
417 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
418 !prefixcmp(head_ref
, "refs/heads/")) {
420 make_branch(head_ref
+ strlen("refs/heads/"), 0);
422 git_config(handle_config
);
426 static struct refspec
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
430 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
432 for (i
= 0; i
< nr_refspec
; i
++) {
435 const char *lhs
, *rhs
;
437 llen
= rlen
= is_glob
= 0;
445 rhs
= strrchr(lhs
, ':');
449 is_glob
= (2 <= rlen
&& !strcmp(rhs
+ rlen
- 2, "/*"));
452 rs
[i
].dst
= xstrndup(rhs
, rlen
);
455 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
456 if (2 <= llen
&& !memcmp(lhs
+ llen
- 2, "/*", 2)) {
457 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
461 } else if (rhs
&& is_glob
) {
465 rs
[i
].pattern
= is_glob
;
466 rs
[i
].src
= xstrndup(lhs
, llen
);
471 * - empty is allowed; it means HEAD.
472 * - otherwise it must be a valid looking ref.
477 st
= check_ref_format(rs
[i
].src
);
478 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
483 * - missing is ok, and is same as empty.
484 * - empty is ok; it means not to store.
485 * - otherwise it must be a valid looking ref.
489 } else if (!*rs
[i
].dst
) {
492 st
= check_ref_format(rs
[i
].dst
);
493 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
499 * - empty is allowed; it means delete.
500 * - when wildcarded, it must be a valid looking ref.
501 * - otherwise, it must be an extended SHA-1, but
502 * there is no existing way to validate this.
507 st
= check_ref_format(rs
[i
].src
);
508 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
512 ; /* anything goes, for now */
515 * - missing is allowed, but LHS then must be a
517 * - empty is not allowed.
518 * - otherwise it must be a valid looking ref.
521 st
= check_ref_format(rs
[i
].src
);
522 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
524 } else if (!*rs
[i
].dst
) {
527 st
= check_ref_format(rs
[i
].dst
);
528 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
540 die("Invalid refspec '%s'", refspec
[i
]);
543 int valid_fetch_refspec(const char *fetch_refspec_str
)
545 const char *fetch_refspec
[] = { fetch_refspec_str
};
546 struct refspec
*refspec
;
548 refspec
= parse_refspec_internal(1, fetch_refspec
, 1, 1);
554 struct refspec
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
556 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
559 struct refspec
*parse_push_refspec(int nr_refspec
, const char **refspec
)
561 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
564 static int valid_remote_nick(const char *name
)
566 if (!name
[0] || /* not empty */
567 (name
[0] == '.' && /* not "." */
568 (!name
[1] || /* not ".." */
569 (name
[1] == '.' && !name
[2]))))
571 return !strchr(name
, '/'); /* no slash */
574 struct remote
*remote_get(const char *name
)
580 name
= default_remote_name
;
581 ret
= make_remote(name
, 0);
582 if (valid_remote_nick(name
)) {
584 read_remotes_file(ret
);
586 read_branches_file(ret
);
589 add_url_alias(ret
, name
);
592 ret
->fetch
= parse_fetch_refspec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
593 ret
->push
= parse_push_refspec(ret
->push_refspec_nr
, ret
->push_refspec
);
597 int for_each_remote(each_remote_fn fn
, void *priv
)
601 for (i
= 0; i
< remotes_nr
&& !result
; i
++) {
602 struct remote
*r
= remotes
[i
];
606 r
->fetch
= parse_fetch_refspec(r
->fetch_refspec_nr
,
609 r
->push
= parse_push_refspec(r
->push_refspec_nr
,
611 result
= fn(r
, priv
);
616 void ref_remove_duplicates(struct ref
*ref_map
)
620 for (; ref_map
; ref_map
= ref_map
->next
) {
621 if (!ref_map
->peer_ref
)
623 posn
= &ref_map
->next
;
625 if ((*posn
)->peer_ref
&&
626 !strcmp((*posn
)->peer_ref
->name
,
627 ref_map
->peer_ref
->name
)) {
628 if (strcmp((*posn
)->name
, ref_map
->name
))
629 die("%s tracks both %s and %s",
630 ref_map
->peer_ref
->name
,
631 (*posn
)->name
, ref_map
->name
);
632 next
= (*posn
)->next
;
633 free((*posn
)->peer_ref
);
637 posn
= &(*posn
)->next
;
643 int remote_has_url(struct remote
*remote
, const char *url
)
646 for (i
= 0; i
< remote
->url_nr
; i
++) {
647 if (!strcmp(remote
->url
[i
], url
))
653 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
655 int find_src
= refspec
->src
== NULL
;
656 char *needle
, **result
;
661 return error("find_tracking: need either src or dst");
662 needle
= refspec
->dst
;
663 result
= &refspec
->src
;
665 needle
= refspec
->src
;
666 result
= &refspec
->dst
;
669 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
670 struct refspec
*fetch
= &remote
->fetch
[i
];
671 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
672 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
675 if (fetch
->pattern
) {
676 if (!prefixcmp(needle
, key
) &&
677 needle
[strlen(key
)] == '/') {
678 *result
= xmalloc(strlen(value
) +
681 strcpy(*result
, value
);
682 strcpy(*result
+ strlen(value
),
683 needle
+ strlen(key
));
684 refspec
->force
= fetch
->force
;
687 } else if (!strcmp(needle
, key
)) {
688 *result
= xstrdup(value
);
689 refspec
->force
= fetch
->force
;
696 struct ref
*alloc_ref(unsigned namelen
)
698 struct ref
*ret
= xmalloc(sizeof(struct ref
) + namelen
);
699 memset(ret
, 0, sizeof(struct ref
) + namelen
);
703 struct ref
*alloc_ref_from_str(const char* str
)
705 struct ref
*ret
= alloc_ref(strlen(str
) + 1);
706 strcpy(ret
->name
, str
);
710 static struct ref
*copy_ref(const struct ref
*ref
)
712 struct ref
*ret
= xmalloc(sizeof(struct ref
) + strlen(ref
->name
) + 1);
713 memcpy(ret
, ref
, sizeof(struct ref
) + strlen(ref
->name
) + 1);
718 struct ref
*copy_ref_list(const struct ref
*ref
)
720 struct ref
*ret
= NULL
;
721 struct ref
**tail
= &ret
;
723 *tail
= copy_ref(ref
);
725 tail
= &((*tail
)->next
);
730 void free_ref(struct ref
*ref
)
734 free(ref
->remote_status
);
739 void free_refs(struct ref
*ref
)
750 static int count_refspec_match(const char *pattern
,
752 struct ref
**matched_ref
)
754 int patlen
= strlen(pattern
);
755 struct ref
*matched_weak
= NULL
;
756 struct ref
*matched
= NULL
;
760 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
761 char *name
= refs
->name
;
762 int namelen
= strlen(name
);
764 if (!refname_match(pattern
, name
, ref_rev_parse_rules
))
767 /* A match is "weak" if it is with refs outside
768 * heads or tags, and did not specify the pattern
769 * in full (e.g. "refs/remotes/origin/master") or at
770 * least from the toplevel (e.g. "remotes/origin/master");
771 * otherwise "git push $URL master" would result in
772 * ambiguity between remotes/origin/master and heads/master
773 * at the remote site.
775 if (namelen
!= patlen
&&
776 patlen
!= namelen
- 5 &&
777 prefixcmp(name
, "refs/heads/") &&
778 prefixcmp(name
, "refs/tags/")) {
779 /* We want to catch the case where only weak
780 * matches are found and there are multiple
781 * matches, and where more than one strong
782 * matches are found, as ambiguous. One
783 * strong match with zero or more weak matches
784 * are acceptable as a unique match.
795 *matched_ref
= matched_weak
;
799 *matched_ref
= matched
;
804 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
812 static struct ref
*try_explicit_object_name(const char *name
)
814 unsigned char sha1
[20];
819 strcpy(ref
->name
, "(delete)");
820 hashclr(ref
->new_sha1
);
823 if (get_sha1(name
, sha1
))
825 ref
= alloc_ref_from_str(name
);
826 hashcpy(ref
->new_sha1
, sha1
);
830 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
832 struct ref
*ret
= alloc_ref_from_str(name
);
833 tail_link_ref(ret
, tail
);
837 static char *guess_ref(const char *name
, struct ref
*peer
)
839 struct strbuf buf
= STRBUF_INIT
;
840 unsigned char sha1
[20];
842 const char *r
= resolve_ref(peer
->name
, sha1
, 1, NULL
);
846 if (!prefixcmp(r
, "refs/heads/"))
847 strbuf_addstr(&buf
, "refs/heads/");
848 else if (!prefixcmp(r
, "refs/tags/"))
849 strbuf_addstr(&buf
, "refs/tags/");
853 strbuf_addstr(&buf
, name
);
854 return strbuf_detach(&buf
, NULL
);
857 static int match_explicit(struct ref
*src
, struct ref
*dst
,
858 struct ref
***dst_tail
,
862 struct ref
*matched_src
, *matched_dst
;
864 const char *dst_value
= rs
->dst
;
870 matched_src
= matched_dst
= NULL
;
871 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
875 /* The source could be in the get_sha1() format
876 * not a reference name. :refs/other is a
877 * way to delete 'other' ref at the remote end.
879 matched_src
= try_explicit_object_name(rs
->src
);
881 error("src refspec %s does not match any.", rs
->src
);
885 error("src refspec %s matches more than one.", rs
->src
);
893 unsigned char sha1
[20];
898 dst_value
= resolve_ref(matched_src
->name
, sha1
, 1, &flag
);
900 ((flag
& REF_ISSYMREF
) &&
901 prefixcmp(dst_value
, "refs/heads/")))
902 die("%s cannot be resolved to branch.",
906 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
910 if (!memcmp(dst_value
, "refs/", 5))
911 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
912 else if((dst_guess
= guess_ref(dst_value
, matched_src
)))
913 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
915 error("unable to push to unqualified destination: %s\n"
916 "The destination refspec neither matches an "
917 "existing ref on the remote nor\n"
918 "begins with refs/, and we are unable to "
919 "guess a prefix based on the source ref.",
924 error("dst refspec %s matches more than one.",
928 if (errs
|| !matched_dst
)
930 if (matched_dst
->peer_ref
) {
932 error("dst ref %s receives from more than one src.",
936 matched_dst
->peer_ref
= matched_src
;
937 matched_dst
->force
= rs
->force
;
942 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
943 struct ref
***dst_tail
, struct refspec
*rs
,
947 for (i
= errs
= 0; i
< rs_nr
; i
++)
948 errs
|= match_explicit(src
, dst
, dst_tail
, &rs
[i
], errs
);
952 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
954 const struct ref
*src
)
957 for (i
= 0; i
< rs_nr
; i
++) {
959 !prefixcmp(src
->name
, rs
[i
].src
) &&
960 src
->name
[strlen(rs
[i
].src
)] == '/')
967 * Note. This is used only by "push"; refspec matching rules for
968 * push and fetch are subtly different, so do not try to reuse it
971 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
972 int nr_refspec
, const char **refspec
, int flags
)
975 parse_push_refspec(nr_refspec
, (const char **) refspec
);
976 int send_all
= flags
& MATCH_REFS_ALL
;
977 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
979 if (match_explicit_refs(src
, dst
, dst_tail
, rs
, nr_refspec
))
982 /* pick the remainder */
983 for ( ; src
; src
= src
->next
) {
984 struct ref
*dst_peer
;
985 const struct refspec
*pat
= NULL
;
990 pat
= check_pattern_match(rs
, nr_refspec
, src
);
994 else if (!send_mirror
&& prefixcmp(src
->name
, "refs/heads/"))
996 * "matching refs"; traditionally we pushed everything
997 * including refs outside refs/heads/ hierarchy, but
998 * that does not make much sense these days.
1003 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
1004 dst_name
= xmalloc(strlen(dst_side
) +
1006 strlen(pat
->src
) + 2);
1007 strcpy(dst_name
, dst_side
);
1008 strcat(dst_name
, src
->name
+ strlen(pat
->src
));
1010 dst_name
= xstrdup(src
->name
);
1011 dst_peer
= find_ref_by_name(dst
, dst_name
);
1012 if (dst_peer
&& dst_peer
->peer_ref
)
1013 /* We're already sending something to this ref. */
1016 if (!dst_peer
&& !nr_refspec
&& !(send_all
|| send_mirror
))
1018 * Remote doesn't have it, and we have no
1019 * explicit pattern, and we don't have
1020 * --all nor --mirror.
1024 /* Create a new one and link it */
1025 dst_peer
= make_linked_ref(dst_name
, dst_tail
);
1026 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
1028 dst_peer
->peer_ref
= src
;
1030 dst_peer
->force
= pat
->force
;
1037 struct branch
*branch_get(const char *name
)
1042 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1043 ret
= current_branch
;
1045 ret
= make_branch(name
, 0);
1046 if (ret
&& ret
->remote_name
) {
1047 ret
->remote
= remote_get(ret
->remote_name
);
1048 if (ret
->merge_nr
) {
1050 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
1052 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1053 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1054 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1055 remote_find_tracking(ret
->remote
,
1063 int branch_has_merge_config(struct branch
*branch
)
1065 return branch
&& !!branch
->merge
;
1068 int branch_merge_matches(struct branch
*branch
,
1070 const char *refname
)
1072 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1074 return refname_match(branch
->merge
[i
]->src
, refname
, ref_fetch_rules
);
1077 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
1078 const struct refspec
*refspec
)
1080 const struct ref
*ref
;
1081 struct ref
*ret
= NULL
;
1082 struct ref
**tail
= &ret
;
1084 int remote_prefix_len
= strlen(refspec
->src
);
1085 int local_prefix_len
= strlen(refspec
->dst
);
1087 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1088 if (strchr(ref
->name
, '^'))
1089 continue; /* a dereference item */
1090 if (!prefixcmp(ref
->name
, refspec
->src
)) {
1092 struct ref
*cpy
= copy_ref(ref
);
1093 match
= ref
->name
+ remote_prefix_len
;
1095 cpy
->peer_ref
= alloc_ref(local_prefix_len
+
1097 sprintf(cpy
->peer_ref
->name
, "%s%s",
1098 refspec
->dst
, match
);
1100 cpy
->peer_ref
->force
= 1;
1109 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
1111 const struct ref
*ref
;
1112 for (ref
= refs
; ref
; ref
= ref
->next
) {
1113 if (refname_match(name
, ref
->name
, ref_fetch_rules
))
1119 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
1121 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
1126 return copy_ref(ref
);
1129 static struct ref
*get_local_ref(const char *name
)
1135 if (!prefixcmp(name
, "refs/")) {
1136 return alloc_ref_from_str(name
);
1139 if (!prefixcmp(name
, "heads/") ||
1140 !prefixcmp(name
, "tags/") ||
1141 !prefixcmp(name
, "remotes/")) {
1142 ret
= alloc_ref(strlen(name
) + 6);
1143 sprintf(ret
->name
, "refs/%s", name
);
1147 ret
= alloc_ref(strlen(name
) + 12);
1148 sprintf(ret
->name
, "refs/heads/%s", name
);
1152 int get_fetch_map(const struct ref
*remote_refs
,
1153 const struct refspec
*refspec
,
1157 struct ref
*ref_map
, **rmp
;
1159 if (refspec
->pattern
) {
1160 ref_map
= get_expanded_map(remote_refs
, refspec
);
1162 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
1164 ref_map
= get_remote_ref(remote_refs
, name
);
1165 if (!missing_ok
&& !ref_map
)
1166 die("Couldn't find remote ref %s", name
);
1168 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
1169 if (ref_map
->peer_ref
&& refspec
->force
)
1170 ref_map
->peer_ref
->force
= 1;
1174 for (rmp
= &ref_map
; *rmp
; ) {
1175 if ((*rmp
)->peer_ref
) {
1176 int st
= check_ref_format((*rmp
)->peer_ref
->name
+ 5);
1177 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
) {
1178 struct ref
*ignore
= *rmp
;
1179 error("* Ignoring funny ref '%s' locally",
1180 (*rmp
)->peer_ref
->name
);
1181 *rmp
= (*rmp
)->next
;
1182 free(ignore
->peer_ref
);
1187 rmp
= &((*rmp
)->next
);
1191 tail_link_ref(ref_map
, tail
);
1196 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
1200 for (; list
; list
= list
->next
)
1201 if (!strcmp(ref
->symref
, list
->name
)) {
1202 hashcpy(ref
->old_sha1
, list
->old_sha1
);