9 static struct refspec s_tag_refspec
= {
17 const struct refspec
*tag_refspec
= &s_tag_refspec
;
19 struct counted_string
{
26 struct counted_string
*instead_of
;
31 static struct remote
**remotes
;
32 static int remotes_alloc
;
33 static int remotes_nr
;
35 static struct branch
**branches
;
36 static int branches_alloc
;
37 static int branches_nr
;
39 static struct branch
*current_branch
;
40 static const char *default_remote_name
;
41 static int explicit_default_remote_name
;
43 static struct rewrite
**rewrite
;
44 static int rewrite_alloc
;
45 static int rewrite_nr
;
47 #define BUF_SIZE (2048)
48 static char buffer
[BUF_SIZE
];
50 static const char *alias_url(const char *url
)
54 struct counted_string
*longest
;
59 for (i
= 0; i
< rewrite_nr
; i
++) {
62 for (j
= 0; j
< rewrite
[i
]->instead_of_nr
; j
++) {
63 if (!prefixcmp(url
, rewrite
[i
]->instead_of
[j
].s
) &&
65 longest
->len
< rewrite
[i
]->instead_of
[j
].len
)) {
66 longest
= &(rewrite
[i
]->instead_of
[j
]);
74 ret
= xmalloc(rewrite
[longest_i
]->baselen
+
75 (strlen(url
) - longest
->len
) + 1);
76 strcpy(ret
, rewrite
[longest_i
]->base
);
77 strcpy(ret
+ rewrite
[longest_i
]->baselen
, url
+ longest
->len
);
81 static void add_push_refspec(struct remote
*remote
, const char *ref
)
83 ALLOC_GROW(remote
->push_refspec
,
84 remote
->push_refspec_nr
+ 1,
85 remote
->push_refspec_alloc
);
86 remote
->push_refspec
[remote
->push_refspec_nr
++] = ref
;
89 static void add_fetch_refspec(struct remote
*remote
, const char *ref
)
91 ALLOC_GROW(remote
->fetch_refspec
,
92 remote
->fetch_refspec_nr
+ 1,
93 remote
->fetch_refspec_alloc
);
94 remote
->fetch_refspec
[remote
->fetch_refspec_nr
++] = ref
;
97 static void add_url(struct remote
*remote
, const char *url
)
99 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
100 remote
->url
[remote
->url_nr
++] = url
;
103 static void add_url_alias(struct remote
*remote
, const char *url
)
105 add_url(remote
, alias_url(url
));
108 static struct remote
*get_remote_by_name(const char *name
)
111 for (i
= 0; i
< remotes_nr
; i
++) {
112 if (!strcmp(name
, remotes
[i
]->name
))
118 static struct remote
*make_remote(const char *name
, int len
)
123 for (i
= 0; i
< remotes_nr
; i
++) {
124 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
125 !remotes
[i
]->name
[len
]) :
126 !strcmp(name
, remotes
[i
]->name
))
130 ret
= xcalloc(1, sizeof(struct remote
));
131 ALLOC_GROW(remotes
, remotes_nr
+ 1, remotes_alloc
);
132 remotes
[remotes_nr
++] = ret
;
134 ret
->name
= xstrndup(name
, len
);
136 ret
->name
= xstrdup(name
);
140 static void add_merge(struct branch
*branch
, const char *name
)
142 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
143 branch
->merge_alloc
);
144 branch
->merge_name
[branch
->merge_nr
++] = name
;
147 static struct branch
*make_branch(const char *name
, int len
)
153 for (i
= 0; i
< branches_nr
; i
++) {
154 if (len
? (!strncmp(name
, branches
[i
]->name
, len
) &&
155 !branches
[i
]->name
[len
]) :
156 !strcmp(name
, branches
[i
]->name
))
160 ALLOC_GROW(branches
, branches_nr
+ 1, branches_alloc
);
161 ret
= xcalloc(1, sizeof(struct branch
));
162 branches
[branches_nr
++] = ret
;
164 ret
->name
= xstrndup(name
, len
);
166 ret
->name
= xstrdup(name
);
167 refname
= xmalloc(strlen(name
) + strlen("refs/heads/") + 1);
168 strcpy(refname
, "refs/heads/");
169 strcpy(refname
+ strlen("refs/heads/"), ret
->name
);
170 ret
->refname
= refname
;
175 static struct rewrite
*make_rewrite(const char *base
, int len
)
180 for (i
= 0; i
< rewrite_nr
; i
++) {
182 ? (len
== rewrite
[i
]->baselen
&&
183 !strncmp(base
, rewrite
[i
]->base
, len
))
184 : !strcmp(base
, rewrite
[i
]->base
))
188 ALLOC_GROW(rewrite
, rewrite_nr
+ 1, rewrite_alloc
);
189 ret
= xcalloc(1, sizeof(struct rewrite
));
190 rewrite
[rewrite_nr
++] = ret
;
192 ret
->base
= xstrndup(base
, len
);
196 ret
->base
= xstrdup(base
);
197 ret
->baselen
= strlen(base
);
202 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
204 ALLOC_GROW(rewrite
->instead_of
, rewrite
->instead_of_nr
+ 1, rewrite
->instead_of_alloc
);
205 rewrite
->instead_of
[rewrite
->instead_of_nr
].s
= instead_of
;
206 rewrite
->instead_of
[rewrite
->instead_of_nr
].len
= strlen(instead_of
);
207 rewrite
->instead_of_nr
++;
210 static void read_remotes_file(struct remote
*remote
)
212 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
216 remote
->origin
= REMOTE_REMOTES
;
217 while (fgets(buffer
, BUF_SIZE
, f
)) {
221 if (!prefixcmp(buffer
, "URL:")) {
224 } else if (!prefixcmp(buffer
, "Push:")) {
227 } else if (!prefixcmp(buffer
, "Pull:")) {
239 while (isspace(p
[-1]))
242 switch (value_list
) {
244 add_url_alias(remote
, xstrdup(s
));
247 add_push_refspec(remote
, xstrdup(s
));
250 add_fetch_refspec(remote
, xstrdup(s
));
257 static void read_branches_file(struct remote
*remote
)
259 const char *slash
= strchr(remote
->name
, '/');
261 struct strbuf branch
= STRBUF_INIT
;
262 int n
= slash
? slash
- remote
->name
: 1000;
263 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
269 s
= fgets(buffer
, BUF_SIZE
, f
);
277 remote
->origin
= REMOTE_BRANCHES
;
279 while (isspace(p
[-1]))
283 len
+= strlen(slash
);
284 p
= xmalloc(len
+ 1);
290 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
291 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
292 * the partial URL obtained from the branches file plus
293 * "/netdev-2.6" and does not store it in any tracking ref.
294 * #branch specifier in the file is ignored.
296 * Otherwise, the branches file would have URL and optionally
297 * #branch specified. The "master" (or specified) branch is
298 * fetched and stored in the local branch of the same name.
300 frag
= strchr(p
, '#');
303 strbuf_addf(&branch
, "refs/heads/%s", frag
);
305 strbuf_addstr(&branch
, "refs/heads/master");
307 strbuf_addf(&branch
, ":refs/heads/%s", remote
->name
);
309 strbuf_reset(&branch
);
310 strbuf_addstr(&branch
, "HEAD:");
312 add_url_alias(remote
, p
);
313 add_fetch_refspec(remote
, strbuf_detach(&branch
, 0));
315 * Cogito compatible push: push current HEAD to remote #branch
316 * (master if missing)
318 strbuf_init(&branch
, 0);
319 strbuf_addstr(&branch
, "HEAD");
321 strbuf_addf(&branch
, ":refs/heads/%s", frag
);
323 strbuf_addstr(&branch
, ":refs/heads/master");
324 add_push_refspec(remote
, strbuf_detach(&branch
, 0));
325 remote
->fetch_tags
= 1; /* always auto-follow */
328 static int handle_config(const char *key
, const char *value
, void *cb
)
332 struct remote
*remote
;
333 struct branch
*branch
;
334 if (!prefixcmp(key
, "branch.")) {
336 subkey
= strrchr(name
, '.');
339 branch
= make_branch(name
, subkey
- name
);
340 if (!strcmp(subkey
, ".remote")) {
342 return config_error_nonbool(key
);
343 branch
->remote_name
= xstrdup(value
);
344 if (branch
== current_branch
) {
345 default_remote_name
= branch
->remote_name
;
346 explicit_default_remote_name
= 1;
348 } else if (!strcmp(subkey
, ".merge")) {
350 return config_error_nonbool(key
);
351 add_merge(branch
, xstrdup(value
));
355 if (!prefixcmp(key
, "url.")) {
356 struct rewrite
*rewrite
;
358 subkey
= strrchr(name
, '.');
361 rewrite
= make_rewrite(name
, subkey
- name
);
362 if (!strcmp(subkey
, ".insteadof")) {
364 return config_error_nonbool(key
);
365 add_instead_of(rewrite
, xstrdup(value
));
368 if (prefixcmp(key
, "remote."))
372 warning("Config remote shorthand cannot begin with '/': %s",
376 subkey
= strrchr(name
, '.');
378 return error("Config with no key for remote %s", name
);
379 remote
= make_remote(name
, subkey
- name
);
380 remote
->origin
= REMOTE_CONFIG
;
381 if (!strcmp(subkey
, ".mirror"))
382 remote
->mirror
= git_config_bool(key
, value
);
383 else if (!strcmp(subkey
, ".skipdefaultupdate"))
384 remote
->skip_default_update
= git_config_bool(key
, value
);
386 else if (!strcmp(subkey
, ".url")) {
388 if (git_config_string(&v
, key
, value
))
391 } else if (!strcmp(subkey
, ".push")) {
393 if (git_config_string(&v
, key
, value
))
395 add_push_refspec(remote
, v
);
396 } else if (!strcmp(subkey
, ".fetch")) {
398 if (git_config_string(&v
, key
, value
))
400 add_fetch_refspec(remote
, v
);
401 } else if (!strcmp(subkey
, ".receivepack")) {
403 if (git_config_string(&v
, key
, value
))
405 if (!remote
->receivepack
)
406 remote
->receivepack
= v
;
408 error("more than one receivepack given, using the first");
409 } else if (!strcmp(subkey
, ".uploadpack")) {
411 if (git_config_string(&v
, key
, value
))
413 if (!remote
->uploadpack
)
414 remote
->uploadpack
= v
;
416 error("more than one uploadpack given, using the first");
417 } else if (!strcmp(subkey
, ".tagopt")) {
418 if (!strcmp(value
, "--no-tags"))
419 remote
->fetch_tags
= -1;
420 } else if (!strcmp(subkey
, ".proxy")) {
421 return git_config_string((const char **)&remote
->http_proxy
,
427 static void alias_all_urls(void)
430 for (i
= 0; i
< remotes_nr
; i
++) {
433 for (j
= 0; j
< remotes
[i
]->url_nr
; j
++) {
434 remotes
[i
]->url
[j
] = alias_url(remotes
[i
]->url
[j
]);
439 static void read_config(void)
441 unsigned char sha1
[20];
442 const char *head_ref
;
444 if (default_remote_name
) // did this already
446 default_remote_name
= xstrdup("origin");
447 current_branch
= NULL
;
448 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
449 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
450 !prefixcmp(head_ref
, "refs/heads/")) {
452 make_branch(head_ref
+ strlen("refs/heads/"), 0);
454 git_config(handle_config
, NULL
);
459 * We need to make sure the tracking branches are well formed, but a
460 * wildcard refspec in "struct refspec" must have a trailing slash. We
461 * temporarily drop the trailing '/' while calling check_ref_format(),
462 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
463 * error return is Ok for a wildcard refspec.
465 static int verify_refname(char *name
, int is_glob
)
467 int result
, len
= -1;
471 assert(name
[len
- 1] == '/');
472 name
[len
- 1] = '\0';
474 result
= check_ref_format(name
);
481 * This function frees a refspec array.
482 * Warning: code paths should be checked to ensure that the src
483 * and dst pointers are always freeable pointers as well
484 * as the refspec pointer itself.
486 static void free_refspecs(struct refspec
*refspec
, int nr_refspec
)
493 for (i
= 0; i
< nr_refspec
; i
++) {
494 free(refspec
[i
].src
);
495 free(refspec
[i
].dst
);
500 static struct refspec
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
504 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
506 for (i
= 0; i
< nr_refspec
; i
++) {
509 const char *lhs
, *rhs
;
519 rhs
= strrchr(lhs
, ':');
522 * Before going on, special case ":" (or "+:") as a refspec
525 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
531 size_t rlen
= strlen(++rhs
);
532 is_glob
= (2 <= rlen
&& !strcmp(rhs
+ rlen
- 2, "/*"));
533 rs
[i
].dst
= xstrndup(rhs
, rlen
- is_glob
);
536 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
537 if (2 <= llen
&& !memcmp(lhs
+ llen
- 2, "/*", 2)) {
538 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
542 } else if (rhs
&& is_glob
) {
546 rs
[i
].pattern
= is_glob
;
547 rs
[i
].src
= xstrndup(lhs
, llen
);
552 * - empty is allowed; it means HEAD.
553 * - otherwise it must be a valid looking ref.
558 st
= verify_refname(rs
[i
].src
, is_glob
);
559 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
564 * - missing is ok, and is same as empty.
565 * - empty is ok; it means not to store.
566 * - otherwise it must be a valid looking ref.
570 } else if (!*rs
[i
].dst
) {
573 st
= verify_refname(rs
[i
].dst
, is_glob
);
574 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
580 * - empty is allowed; it means delete.
581 * - when wildcarded, it must be a valid looking ref.
582 * - otherwise, it must be an extended SHA-1, but
583 * there is no existing way to validate this.
588 st
= verify_refname(rs
[i
].src
, is_glob
);
589 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
593 ; /* anything goes, for now */
596 * - missing is allowed, but LHS then must be a
598 * - empty is not allowed.
599 * - otherwise it must be a valid looking ref.
602 st
= verify_refname(rs
[i
].src
, is_glob
);
603 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
605 } else if (!*rs
[i
].dst
) {
608 st
= verify_refname(rs
[i
].dst
, is_glob
);
609 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
619 * nr_refspec must be greater than zero and i must be valid
620 * since it is only possible to reach this point from within
621 * the for loop above.
623 free_refspecs(rs
, i
+1);
626 die("Invalid refspec '%s'", refspec
[i
]);
629 int valid_fetch_refspec(const char *fetch_refspec_str
)
631 const char *fetch_refspec
[] = { fetch_refspec_str
};
632 struct refspec
*refspec
;
634 refspec
= parse_refspec_internal(1, fetch_refspec
, 1, 1);
635 free_refspecs(refspec
, 1);
639 struct refspec
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
641 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
644 static struct refspec
*parse_push_refspec(int nr_refspec
, const char **refspec
)
646 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
649 static int valid_remote_nick(const char *name
)
651 if (!name
[0] || is_dot_or_dotdot(name
))
653 return !strchr(name
, '/'); /* no slash */
656 struct remote
*remote_get(const char *name
)
665 name
= default_remote_name
;
666 name_given
= explicit_default_remote_name
;
669 ret
= make_remote(name
, 0);
671 ret
= get_remote_by_name(name
);
675 if (valid_remote_nick(name
)) {
677 read_remotes_file(ret
);
679 read_branches_file(ret
);
682 add_url_alias(ret
, name
);
685 ret
->fetch
= parse_fetch_refspec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
686 ret
->push
= parse_push_refspec(ret
->push_refspec_nr
, ret
->push_refspec
);
690 int for_each_remote(each_remote_fn fn
, void *priv
)
694 for (i
= 0; i
< remotes_nr
&& !result
; i
++) {
695 struct remote
*r
= remotes
[i
];
699 r
->fetch
= parse_fetch_refspec(r
->fetch_refspec_nr
,
702 r
->push
= parse_push_refspec(r
->push_refspec_nr
,
704 result
= fn(r
, priv
);
709 void ref_remove_duplicates(struct ref
*ref_map
)
713 for (; ref_map
; ref_map
= ref_map
->next
) {
714 if (!ref_map
->peer_ref
)
716 posn
= &ref_map
->next
;
718 if ((*posn
)->peer_ref
&&
719 !strcmp((*posn
)->peer_ref
->name
,
720 ref_map
->peer_ref
->name
)) {
721 if (strcmp((*posn
)->name
, ref_map
->name
))
722 die("%s tracks both %s and %s",
723 ref_map
->peer_ref
->name
,
724 (*posn
)->name
, ref_map
->name
);
725 next
= (*posn
)->next
;
726 free((*posn
)->peer_ref
);
730 posn
= &(*posn
)->next
;
736 int remote_has_url(struct remote
*remote
, const char *url
)
739 for (i
= 0; i
< remote
->url_nr
; i
++) {
740 if (!strcmp(remote
->url
[i
], url
))
746 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
748 int find_src
= refspec
->src
== NULL
;
749 char *needle
, **result
;
754 return error("find_tracking: need either src or dst");
755 needle
= refspec
->dst
;
756 result
= &refspec
->src
;
758 needle
= refspec
->src
;
759 result
= &refspec
->dst
;
762 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
763 struct refspec
*fetch
= &remote
->fetch
[i
];
764 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
765 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
768 if (fetch
->pattern
) {
769 if (!prefixcmp(needle
, key
)) {
770 *result
= xmalloc(strlen(value
) +
773 strcpy(*result
, value
);
774 strcpy(*result
+ strlen(value
),
775 needle
+ strlen(key
));
776 refspec
->force
= fetch
->force
;
779 } else if (!strcmp(needle
, key
)) {
780 *result
= xstrdup(value
);
781 refspec
->force
= fetch
->force
;
788 static struct ref
*alloc_ref_with_prefix(const char *prefix
, size_t prefixlen
,
791 size_t len
= strlen(name
);
792 struct ref
*ref
= xcalloc(1, sizeof(struct ref
) + prefixlen
+ len
+ 1);
793 memcpy(ref
->name
, prefix
, prefixlen
);
794 memcpy(ref
->name
+ prefixlen
, name
, len
);
798 struct ref
*alloc_ref(const char *name
)
800 return alloc_ref_with_prefix("", 0, name
);
803 static struct ref
*copy_ref(const struct ref
*ref
)
805 struct ref
*ret
= xmalloc(sizeof(struct ref
) + strlen(ref
->name
) + 1);
806 memcpy(ret
, ref
, sizeof(struct ref
) + strlen(ref
->name
) + 1);
811 struct ref
*copy_ref_list(const struct ref
*ref
)
813 struct ref
*ret
= NULL
;
814 struct ref
**tail
= &ret
;
816 *tail
= copy_ref(ref
);
818 tail
= &((*tail
)->next
);
823 static void free_ref(struct ref
*ref
)
827 free(ref
->remote_status
);
832 void free_refs(struct ref
*ref
)
843 static int count_refspec_match(const char *pattern
,
845 struct ref
**matched_ref
)
847 int patlen
= strlen(pattern
);
848 struct ref
*matched_weak
= NULL
;
849 struct ref
*matched
= NULL
;
853 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
854 char *name
= refs
->name
;
855 int namelen
= strlen(name
);
857 if (!refname_match(pattern
, name
, ref_rev_parse_rules
))
860 /* A match is "weak" if it is with refs outside
861 * heads or tags, and did not specify the pattern
862 * in full (e.g. "refs/remotes/origin/master") or at
863 * least from the toplevel (e.g. "remotes/origin/master");
864 * otherwise "git push $URL master" would result in
865 * ambiguity between remotes/origin/master and heads/master
866 * at the remote site.
868 if (namelen
!= patlen
&&
869 patlen
!= namelen
- 5 &&
870 prefixcmp(name
, "refs/heads/") &&
871 prefixcmp(name
, "refs/tags/")) {
872 /* We want to catch the case where only weak
873 * matches are found and there are multiple
874 * matches, and where more than one strong
875 * matches are found, as ambiguous. One
876 * strong match with zero or more weak matches
877 * are acceptable as a unique match.
888 *matched_ref
= matched_weak
;
892 *matched_ref
= matched
;
897 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
905 static struct ref
*try_explicit_object_name(const char *name
)
907 unsigned char sha1
[20];
911 ref
= alloc_ref("(delete)");
912 hashclr(ref
->new_sha1
);
915 if (get_sha1(name
, sha1
))
917 ref
= alloc_ref(name
);
918 hashcpy(ref
->new_sha1
, sha1
);
922 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
924 struct ref
*ret
= alloc_ref(name
);
925 tail_link_ref(ret
, tail
);
929 static char *guess_ref(const char *name
, struct ref
*peer
)
931 struct strbuf buf
= STRBUF_INIT
;
932 unsigned char sha1
[20];
934 const char *r
= resolve_ref(peer
->name
, sha1
, 1, NULL
);
938 if (!prefixcmp(r
, "refs/heads/"))
939 strbuf_addstr(&buf
, "refs/heads/");
940 else if (!prefixcmp(r
, "refs/tags/"))
941 strbuf_addstr(&buf
, "refs/tags/");
945 strbuf_addstr(&buf
, name
);
946 return strbuf_detach(&buf
, NULL
);
949 static int match_explicit(struct ref
*src
, struct ref
*dst
,
950 struct ref
***dst_tail
,
953 struct ref
*matched_src
, *matched_dst
;
955 const char *dst_value
= rs
->dst
;
958 if (rs
->pattern
|| rs
->matching
)
961 matched_src
= matched_dst
= NULL
;
962 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
966 /* The source could be in the get_sha1() format
967 * not a reference name. :refs/other is a
968 * way to delete 'other' ref at the remote end.
970 matched_src
= try_explicit_object_name(rs
->src
);
972 return error("src refspec %s does not match any.", rs
->src
);
975 return error("src refspec %s matches more than one.", rs
->src
);
979 unsigned char sha1
[20];
982 dst_value
= resolve_ref(matched_src
->name
, sha1
, 1, &flag
);
984 ((flag
& REF_ISSYMREF
) &&
985 prefixcmp(dst_value
, "refs/heads/")))
986 die("%s cannot be resolved to branch.",
990 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
994 if (!memcmp(dst_value
, "refs/", 5))
995 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
996 else if((dst_guess
= guess_ref(dst_value
, matched_src
)))
997 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
999 error("unable to push to unqualified destination: %s\n"
1000 "The destination refspec neither matches an "
1001 "existing ref on the remote nor\n"
1002 "begins with refs/, and we are unable to "
1003 "guess a prefix based on the source ref.",
1008 error("dst refspec %s matches more than one.",
1014 if (matched_dst
->peer_ref
)
1015 return error("dst ref %s receives from more than one src.",
1018 matched_dst
->peer_ref
= matched_src
;
1019 matched_dst
->force
= rs
->force
;
1024 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
1025 struct ref
***dst_tail
, struct refspec
*rs
,
1029 for (i
= errs
= 0; i
< rs_nr
; i
++)
1030 errs
+= match_explicit(src
, dst
, dst_tail
, &rs
[i
]);
1034 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
1036 const struct ref
*src
)
1039 int matching_refs
= -1;
1040 for (i
= 0; i
< rs_nr
; i
++) {
1041 if (rs
[i
].matching
&&
1042 (matching_refs
== -1 || rs
[i
].force
)) {
1047 if (rs
[i
].pattern
&& !prefixcmp(src
->name
, rs
[i
].src
))
1050 if (matching_refs
!= -1)
1051 return rs
+ matching_refs
;
1057 * Note. This is used only by "push"; refspec matching rules for
1058 * push and fetch are subtly different, so do not try to reuse it
1061 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
1062 int nr_refspec
, const char **refspec
, int flags
)
1065 int send_all
= flags
& MATCH_REFS_ALL
;
1066 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
1067 static const char *default_refspec
[] = { ":", 0 };
1071 refspec
= default_refspec
;
1073 rs
= parse_push_refspec(nr_refspec
, (const char **) refspec
);
1074 if (match_explicit_refs(src
, dst
, dst_tail
, rs
, nr_refspec
))
1077 /* pick the remainder */
1078 for ( ; src
; src
= src
->next
) {
1079 struct ref
*dst_peer
;
1080 const struct refspec
*pat
= NULL
;
1085 pat
= check_pattern_match(rs
, nr_refspec
, src
);
1089 if (pat
->matching
) {
1091 * "matching refs"; traditionally we pushed everything
1092 * including refs outside refs/heads/ hierarchy, but
1093 * that does not make much sense these days.
1095 if (!send_mirror
&& prefixcmp(src
->name
, "refs/heads/"))
1097 dst_name
= xstrdup(src
->name
);
1100 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
1101 dst_name
= xmalloc(strlen(dst_side
) +
1103 strlen(pat
->src
) + 2);
1104 strcpy(dst_name
, dst_side
);
1105 strcat(dst_name
, src
->name
+ strlen(pat
->src
));
1107 dst_peer
= find_ref_by_name(dst
, dst_name
);
1109 if (dst_peer
->peer_ref
)
1110 /* We're already sending something to this ref. */
1114 if (pat
->matching
&& !(send_all
|| send_mirror
))
1116 * Remote doesn't have it, and we have no
1117 * explicit pattern, and we don't have
1118 * --all nor --mirror.
1122 /* Create a new one and link it */
1123 dst_peer
= make_linked_ref(dst_name
, dst_tail
);
1124 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
1126 dst_peer
->peer_ref
= src
;
1127 dst_peer
->force
= pat
->force
;
1134 struct branch
*branch_get(const char *name
)
1139 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1140 ret
= current_branch
;
1142 ret
= make_branch(name
, 0);
1143 if (ret
&& ret
->remote_name
) {
1144 ret
->remote
= remote_get(ret
->remote_name
);
1145 if (ret
->merge_nr
) {
1147 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
1149 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1150 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1151 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1152 remote_find_tracking(ret
->remote
,
1160 int branch_has_merge_config(struct branch
*branch
)
1162 return branch
&& !!branch
->merge
;
1165 int branch_merge_matches(struct branch
*branch
,
1167 const char *refname
)
1169 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1171 return refname_match(branch
->merge
[i
]->src
, refname
, ref_fetch_rules
);
1174 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
1175 const struct refspec
*refspec
)
1177 const struct ref
*ref
;
1178 struct ref
*ret
= NULL
;
1179 struct ref
**tail
= &ret
;
1181 int remote_prefix_len
= strlen(refspec
->src
);
1182 int local_prefix_len
= strlen(refspec
->dst
);
1184 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1185 if (strchr(ref
->name
, '^'))
1186 continue; /* a dereference item */
1187 if (!prefixcmp(ref
->name
, refspec
->src
)) {
1189 struct ref
*cpy
= copy_ref(ref
);
1190 match
= ref
->name
+ remote_prefix_len
;
1192 cpy
->peer_ref
= alloc_ref_with_prefix(refspec
->dst
,
1193 local_prefix_len
, match
);
1195 cpy
->peer_ref
->force
= 1;
1204 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
1206 const struct ref
*ref
;
1207 for (ref
= refs
; ref
; ref
= ref
->next
) {
1208 if (refname_match(name
, ref
->name
, ref_fetch_rules
))
1214 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
1216 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
1221 return copy_ref(ref
);
1224 static struct ref
*get_local_ref(const char *name
)
1229 if (!prefixcmp(name
, "refs/"))
1230 return alloc_ref(name
);
1232 if (!prefixcmp(name
, "heads/") ||
1233 !prefixcmp(name
, "tags/") ||
1234 !prefixcmp(name
, "remotes/"))
1235 return alloc_ref_with_prefix("refs/", 5, name
);
1237 return alloc_ref_with_prefix("refs/heads/", 11, name
);
1240 int get_fetch_map(const struct ref
*remote_refs
,
1241 const struct refspec
*refspec
,
1245 struct ref
*ref_map
, **rmp
;
1247 if (refspec
->pattern
) {
1248 ref_map
= get_expanded_map(remote_refs
, refspec
);
1250 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
1252 ref_map
= get_remote_ref(remote_refs
, name
);
1253 if (!missing_ok
&& !ref_map
)
1254 die("Couldn't find remote ref %s", name
);
1256 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
1257 if (ref_map
->peer_ref
&& refspec
->force
)
1258 ref_map
->peer_ref
->force
= 1;
1262 for (rmp
= &ref_map
; *rmp
; ) {
1263 if ((*rmp
)->peer_ref
) {
1264 int st
= check_ref_format((*rmp
)->peer_ref
->name
+ 5);
1265 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
) {
1266 struct ref
*ignore
= *rmp
;
1267 error("* Ignoring funny ref '%s' locally",
1268 (*rmp
)->peer_ref
->name
);
1269 *rmp
= (*rmp
)->next
;
1270 free(ignore
->peer_ref
);
1275 rmp
= &((*rmp
)->next
);
1279 tail_link_ref(ref_map
, tail
);
1284 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
1288 for (; list
; list
= list
->next
)
1289 if (!strcmp(ref
->symref
, list
->name
)) {
1290 hashcpy(ref
->old_sha1
, list
->old_sha1
);
1297 * Return true if there is anything to report, otherwise false.
1299 int stat_tracking_info(struct branch
*branch
, int *num_ours
, int *num_theirs
)
1301 unsigned char sha1
[20];
1302 struct commit
*ours
, *theirs
;
1304 struct rev_info revs
;
1305 const char *rev_argv
[10], *base
;
1309 * Nothing to report unless we are marked to build on top of
1313 !branch
->merge
|| !branch
->merge
[0] || !branch
->merge
[0]->dst
)
1317 * If what we used to build on no longer exists, there is
1318 * nothing to report.
1320 base
= branch
->merge
[0]->dst
;
1321 if (!resolve_ref(base
, sha1
, 1, NULL
))
1323 theirs
= lookup_commit(sha1
);
1327 if (!resolve_ref(branch
->refname
, sha1
, 1, NULL
))
1329 ours
= lookup_commit(sha1
);
1333 /* are we the same? */
1337 /* Run "rev-list --left-right ours...theirs" internally... */
1339 rev_argv
[rev_argc
++] = NULL
;
1340 rev_argv
[rev_argc
++] = "--left-right";
1341 rev_argv
[rev_argc
++] = symmetric
;
1342 rev_argv
[rev_argc
++] = "--";
1343 rev_argv
[rev_argc
] = NULL
;
1345 strcpy(symmetric
, sha1_to_hex(ours
->object
.sha1
));
1346 strcpy(symmetric
+ 40, "...");
1347 strcpy(symmetric
+ 43, sha1_to_hex(theirs
->object
.sha1
));
1349 init_revisions(&revs
, NULL
);
1350 setup_revisions(rev_argc
, rev_argv
, &revs
, NULL
);
1351 prepare_revision_walk(&revs
);
1353 /* ... and count the commits on each side. */
1357 struct commit
*c
= get_revision(&revs
);
1360 if (c
->object
.flags
& SYMMETRIC_LEFT
)
1366 /* clear object flags smudged by the above traversal */
1367 clear_commit_marks(ours
, ALL_REV_FLAGS
);
1368 clear_commit_marks(theirs
, ALL_REV_FLAGS
);
1373 * Return true when there is anything to report, otherwise false.
1375 int format_tracking_info(struct branch
*branch
, struct strbuf
*sb
)
1377 int num_ours
, num_theirs
;
1380 if (!stat_tracking_info(branch
, &num_ours
, &num_theirs
))
1383 base
= branch
->merge
[0]->dst
;
1384 if (!prefixcmp(base
, "refs/remotes/")) {
1385 base
+= strlen("refs/remotes/");
1388 strbuf_addf(sb
, "Your branch is ahead of '%s' "
1389 "by %d commit%s.\n",
1390 base
, num_ours
, (num_ours
== 1) ? "" : "s");
1392 strbuf_addf(sb
, "Your branch is behind '%s' "
1394 "and can be fast-forwarded.\n",
1395 base
, num_theirs
, (num_theirs
== 1) ? "" : "s");
1397 strbuf_addf(sb
, "Your branch and '%s' have diverged,\n"
1398 "and have %d and %d different commit(s) each, "
1400 base
, num_ours
, num_theirs
);