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
;
42 static struct rewrite
**rewrite
;
43 static int rewrite_alloc
;
44 static int rewrite_nr
;
46 #define BUF_SIZE (2048)
47 static char buffer
[BUF_SIZE
];
49 static const char *alias_url(const char *url
)
53 struct counted_string
*longest
;
58 for (i
= 0; i
< rewrite_nr
; i
++) {
61 for (j
= 0; j
< rewrite
[i
]->instead_of_nr
; j
++) {
62 if (!prefixcmp(url
, rewrite
[i
]->instead_of
[j
].s
) &&
64 longest
->len
< rewrite
[i
]->instead_of
[j
].len
)) {
65 longest
= &(rewrite
[i
]->instead_of
[j
]);
73 ret
= xmalloc(rewrite
[longest_i
]->baselen
+
74 (strlen(url
) - longest
->len
) + 1);
75 strcpy(ret
, rewrite
[longest_i
]->base
);
76 strcpy(ret
+ rewrite
[longest_i
]->baselen
, url
+ longest
->len
);
80 static void add_push_refspec(struct remote
*remote
, const char *ref
)
82 ALLOC_GROW(remote
->push_refspec
,
83 remote
->push_refspec_nr
+ 1,
84 remote
->push_refspec_alloc
);
85 remote
->push_refspec
[remote
->push_refspec_nr
++] = ref
;
88 static void add_fetch_refspec(struct remote
*remote
, const char *ref
)
90 ALLOC_GROW(remote
->fetch_refspec
,
91 remote
->fetch_refspec_nr
+ 1,
92 remote
->fetch_refspec_alloc
);
93 remote
->fetch_refspec
[remote
->fetch_refspec_nr
++] = ref
;
96 static void add_url(struct remote
*remote
, const char *url
)
98 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
99 remote
->url
[remote
->url_nr
++] = url
;
102 static void add_url_alias(struct remote
*remote
, const char *url
)
104 add_url(remote
, alias_url(url
));
107 static struct remote
*make_remote(const char *name
, int len
)
112 for (i
= 0; i
< remotes_nr
; i
++) {
113 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
114 !remotes
[i
]->name
[len
]) :
115 !strcmp(name
, remotes
[i
]->name
))
119 ret
= xcalloc(1, sizeof(struct remote
));
120 ALLOC_GROW(remotes
, remotes_nr
+ 1, remotes_alloc
);
121 remotes
[remotes_nr
++] = ret
;
123 ret
->name
= xstrndup(name
, len
);
125 ret
->name
= xstrdup(name
);
129 static void add_merge(struct branch
*branch
, const char *name
)
131 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
132 branch
->merge_alloc
);
133 branch
->merge_name
[branch
->merge_nr
++] = name
;
136 static struct branch
*make_branch(const char *name
, int len
)
142 for (i
= 0; i
< branches_nr
; i
++) {
143 if (len
? (!strncmp(name
, branches
[i
]->name
, len
) &&
144 !branches
[i
]->name
[len
]) :
145 !strcmp(name
, branches
[i
]->name
))
149 ALLOC_GROW(branches
, branches_nr
+ 1, branches_alloc
);
150 ret
= xcalloc(1, sizeof(struct branch
));
151 branches
[branches_nr
++] = ret
;
153 ret
->name
= xstrndup(name
, len
);
155 ret
->name
= xstrdup(name
);
156 refname
= xmalloc(strlen(name
) + strlen("refs/heads/") + 1);
157 strcpy(refname
, "refs/heads/");
158 strcpy(refname
+ strlen("refs/heads/"), ret
->name
);
159 ret
->refname
= refname
;
164 static struct rewrite
*make_rewrite(const char *base
, int len
)
169 for (i
= 0; i
< rewrite_nr
; i
++) {
171 ? (len
== rewrite
[i
]->baselen
&&
172 !strncmp(base
, rewrite
[i
]->base
, len
))
173 : !strcmp(base
, rewrite
[i
]->base
))
177 ALLOC_GROW(rewrite
, rewrite_nr
+ 1, rewrite_alloc
);
178 ret
= xcalloc(1, sizeof(struct rewrite
));
179 rewrite
[rewrite_nr
++] = ret
;
181 ret
->base
= xstrndup(base
, len
);
185 ret
->base
= xstrdup(base
);
186 ret
->baselen
= strlen(base
);
191 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
193 ALLOC_GROW(rewrite
->instead_of
, rewrite
->instead_of_nr
+ 1, rewrite
->instead_of_alloc
);
194 rewrite
->instead_of
[rewrite
->instead_of_nr
].s
= instead_of
;
195 rewrite
->instead_of
[rewrite
->instead_of_nr
].len
= strlen(instead_of
);
196 rewrite
->instead_of_nr
++;
199 static void read_remotes_file(struct remote
*remote
)
201 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
205 remote
->origin
= REMOTE_REMOTES
;
206 while (fgets(buffer
, BUF_SIZE
, f
)) {
210 if (!prefixcmp(buffer
, "URL:")) {
213 } else if (!prefixcmp(buffer
, "Push:")) {
216 } else if (!prefixcmp(buffer
, "Pull:")) {
228 while (isspace(p
[-1]))
231 switch (value_list
) {
233 add_url_alias(remote
, xstrdup(s
));
236 add_push_refspec(remote
, xstrdup(s
));
239 add_fetch_refspec(remote
, xstrdup(s
));
246 static void read_branches_file(struct remote
*remote
)
248 const char *slash
= strchr(remote
->name
, '/');
250 struct strbuf branch
= STRBUF_INIT
;
251 int n
= slash
? slash
- remote
->name
: 1000;
252 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
258 s
= fgets(buffer
, BUF_SIZE
, f
);
266 remote
->origin
= REMOTE_BRANCHES
;
268 while (isspace(p
[-1]))
272 len
+= strlen(slash
);
273 p
= xmalloc(len
+ 1);
279 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
280 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
281 * the partial URL obtained from the branches file plus
282 * "/netdev-2.6" and does not store it in any tracking ref.
283 * #branch specifier in the file is ignored.
285 * Otherwise, the branches file would have URL and optionally
286 * #branch specified. The "master" (or specified) branch is
287 * fetched and stored in the local branch of the same name.
289 frag
= strchr(p
, '#');
292 strbuf_addf(&branch
, "refs/heads/%s", frag
);
294 strbuf_addstr(&branch
, "refs/heads/master");
296 strbuf_addf(&branch
, ":refs/heads/%s", remote
->name
);
298 strbuf_reset(&branch
);
299 strbuf_addstr(&branch
, "HEAD:");
301 add_url_alias(remote
, p
);
302 add_fetch_refspec(remote
, strbuf_detach(&branch
, 0));
304 * Cogito compatible push: push current HEAD to remote #branch
305 * (master if missing)
307 strbuf_init(&branch
, 0);
308 strbuf_addstr(&branch
, "HEAD");
310 strbuf_addf(&branch
, ":refs/heads/%s", frag
);
312 strbuf_addstr(&branch
, ":refs/heads/master");
313 add_push_refspec(remote
, strbuf_detach(&branch
, 0));
314 remote
->fetch_tags
= 1; /* always auto-follow */
317 static int handle_config(const char *key
, const char *value
, void *cb
)
321 struct remote
*remote
;
322 struct branch
*branch
;
323 if (!prefixcmp(key
, "branch.")) {
325 subkey
= strrchr(name
, '.');
328 branch
= make_branch(name
, subkey
- name
);
329 if (!strcmp(subkey
, ".remote")) {
331 return config_error_nonbool(key
);
332 branch
->remote_name
= xstrdup(value
);
333 if (branch
== current_branch
)
334 default_remote_name
= branch
->remote_name
;
335 } else if (!strcmp(subkey
, ".merge")) {
337 return config_error_nonbool(key
);
338 add_merge(branch
, xstrdup(value
));
342 if (!prefixcmp(key
, "url.")) {
343 struct rewrite
*rewrite
;
345 subkey
= strrchr(name
, '.');
348 rewrite
= make_rewrite(name
, subkey
- name
);
349 if (!strcmp(subkey
, ".insteadof")) {
351 return config_error_nonbool(key
);
352 add_instead_of(rewrite
, xstrdup(value
));
355 if (prefixcmp(key
, "remote."))
359 warning("Config remote shorthand cannot begin with '/': %s",
363 subkey
= strrchr(name
, '.');
365 return error("Config with no key for remote %s", name
);
366 remote
= make_remote(name
, subkey
- name
);
367 remote
->origin
= REMOTE_CONFIG
;
368 if (!strcmp(subkey
, ".mirror"))
369 remote
->mirror
= git_config_bool(key
, value
);
370 else if (!strcmp(subkey
, ".skipdefaultupdate"))
371 remote
->skip_default_update
= git_config_bool(key
, value
);
373 else if (!strcmp(subkey
, ".url")) {
375 if (git_config_string(&v
, key
, value
))
378 } else if (!strcmp(subkey
, ".push")) {
380 if (git_config_string(&v
, key
, value
))
382 add_push_refspec(remote
, v
);
383 } else if (!strcmp(subkey
, ".fetch")) {
385 if (git_config_string(&v
, key
, value
))
387 add_fetch_refspec(remote
, v
);
388 } else if (!strcmp(subkey
, ".receivepack")) {
390 if (git_config_string(&v
, key
, value
))
392 if (!remote
->receivepack
)
393 remote
->receivepack
= v
;
395 error("more than one receivepack given, using the first");
396 } else if (!strcmp(subkey
, ".uploadpack")) {
398 if (git_config_string(&v
, key
, value
))
400 if (!remote
->uploadpack
)
401 remote
->uploadpack
= v
;
403 error("more than one uploadpack given, using the first");
404 } else if (!strcmp(subkey
, ".tagopt")) {
405 if (!strcmp(value
, "--no-tags"))
406 remote
->fetch_tags
= -1;
407 } else if (!strcmp(subkey
, ".proxy")) {
408 return git_config_string((const char **)&remote
->http_proxy
,
414 static void alias_all_urls(void)
417 for (i
= 0; i
< remotes_nr
; i
++) {
420 for (j
= 0; j
< remotes
[i
]->url_nr
; j
++) {
421 remotes
[i
]->url
[j
] = alias_url(remotes
[i
]->url
[j
]);
426 static void read_config(void)
428 unsigned char sha1
[20];
429 const char *head_ref
;
431 if (default_remote_name
) // did this already
433 default_remote_name
= xstrdup("origin");
434 current_branch
= NULL
;
435 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
436 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
437 !prefixcmp(head_ref
, "refs/heads/")) {
439 make_branch(head_ref
+ strlen("refs/heads/"), 0);
441 git_config(handle_config
, NULL
);
446 * We need to make sure the tracking branches are well formed, but a
447 * wildcard refspec in "struct refspec" must have a trailing slash. We
448 * temporarily drop the trailing '/' while calling check_ref_format(),
449 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
450 * error return is Ok for a wildcard refspec.
452 static int verify_refname(char *name
, int is_glob
)
456 result
= check_ref_format(name
);
457 if (is_glob
&& result
== CHECK_REF_FORMAT_WILDCARD
)
458 result
= CHECK_REF_FORMAT_OK
;
463 * This function frees a refspec array.
464 * Warning: code paths should be checked to ensure that the src
465 * and dst pointers are always freeable pointers as well
466 * as the refspec pointer itself.
468 static void free_refspecs(struct refspec
*refspec
, int nr_refspec
)
475 for (i
= 0; i
< nr_refspec
; i
++) {
476 free(refspec
[i
].src
);
477 free(refspec
[i
].dst
);
482 static struct refspec
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
486 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
488 for (i
= 0; i
< nr_refspec
; i
++) {
491 const char *lhs
, *rhs
;
501 rhs
= strrchr(lhs
, ':');
504 * Before going on, special case ":" (or "+:") as a refspec
507 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
513 size_t rlen
= strlen(++rhs
);
514 is_glob
= (2 <= rlen
&& !strcmp(rhs
+ rlen
- 2, "/*"));
515 rs
[i
].dst
= xstrndup(rhs
, rlen
);
518 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
519 if (2 <= llen
&& !memcmp(lhs
+ llen
- 2, "/*", 2)) {
520 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
523 } else if (rhs
&& is_glob
) {
527 rs
[i
].pattern
= is_glob
;
528 rs
[i
].src
= xstrndup(lhs
, llen
);
533 * - empty is allowed; it means HEAD.
534 * - otherwise it must be a valid looking ref.
539 st
= verify_refname(rs
[i
].src
, is_glob
);
540 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
545 * - missing is ok, and is same as empty.
546 * - empty is ok; it means not to store.
547 * - otherwise it must be a valid looking ref.
551 } else if (!*rs
[i
].dst
) {
554 st
= verify_refname(rs
[i
].dst
, is_glob
);
555 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
561 * - empty is allowed; it means delete.
562 * - when wildcarded, it must be a valid looking ref.
563 * - otherwise, it must be an extended SHA-1, but
564 * there is no existing way to validate this.
569 st
= verify_refname(rs
[i
].src
, is_glob
);
570 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
574 ; /* anything goes, for now */
577 * - missing is allowed, but LHS then must be a
579 * - empty is not allowed.
580 * - otherwise it must be a valid looking ref.
583 st
= verify_refname(rs
[i
].src
, is_glob
);
584 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
586 } else if (!*rs
[i
].dst
) {
589 st
= verify_refname(rs
[i
].dst
, is_glob
);
590 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
600 * nr_refspec must be greater than zero and i must be valid
601 * since it is only possible to reach this point from within
602 * the for loop above.
604 free_refspecs(rs
, i
+1);
607 die("Invalid refspec '%s'", refspec
[i
]);
610 int valid_fetch_refspec(const char *fetch_refspec_str
)
612 const char *fetch_refspec
[] = { fetch_refspec_str
};
613 struct refspec
*refspec
;
615 refspec
= parse_refspec_internal(1, fetch_refspec
, 1, 1);
616 free_refspecs(refspec
, 1);
620 struct refspec
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
622 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
625 static struct refspec
*parse_push_refspec(int nr_refspec
, const char **refspec
)
627 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
630 static int valid_remote_nick(const char *name
)
632 if (!name
[0] || is_dot_or_dotdot(name
))
634 return !strchr(name
, '/'); /* no slash */
637 struct remote
*remote_get(const char *name
)
643 name
= default_remote_name
;
644 ret
= make_remote(name
, 0);
645 if (valid_remote_nick(name
)) {
647 read_remotes_file(ret
);
649 read_branches_file(ret
);
652 add_url_alias(ret
, name
);
655 ret
->fetch
= parse_fetch_refspec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
656 ret
->push
= parse_push_refspec(ret
->push_refspec_nr
, ret
->push_refspec
);
660 int for_each_remote(each_remote_fn fn
, void *priv
)
664 for (i
= 0; i
< remotes_nr
&& !result
; i
++) {
665 struct remote
*r
= remotes
[i
];
669 r
->fetch
= parse_fetch_refspec(r
->fetch_refspec_nr
,
672 r
->push
= parse_push_refspec(r
->push_refspec_nr
,
674 result
= fn(r
, priv
);
679 void ref_remove_duplicates(struct ref
*ref_map
)
683 for (; ref_map
; ref_map
= ref_map
->next
) {
684 if (!ref_map
->peer_ref
)
686 posn
= &ref_map
->next
;
688 if ((*posn
)->peer_ref
&&
689 !strcmp((*posn
)->peer_ref
->name
,
690 ref_map
->peer_ref
->name
)) {
691 if (strcmp((*posn
)->name
, ref_map
->name
))
692 die("%s tracks both %s and %s",
693 ref_map
->peer_ref
->name
,
694 (*posn
)->name
, ref_map
->name
);
695 next
= (*posn
)->next
;
696 free((*posn
)->peer_ref
);
700 posn
= &(*posn
)->next
;
706 int remote_has_url(struct remote
*remote
, const char *url
)
709 for (i
= 0; i
< remote
->url_nr
; i
++) {
710 if (!strcmp(remote
->url
[i
], url
))
716 static int match_name_with_pattern(const char *key
, const char *name
,
717 const char *value
, char **result
)
719 const char *kstar
= strchr(key
, '*');
723 die("Key '%s' of pattern had no '*'", key
);
725 ret
= !strncmp(key
, name
, klen
);
727 const char *vstar
= strchr(value
, '*');
730 die("Value '%s' of pattern has no '*'", value
);
731 vlen
= vstar
- value
;
732 *result
= xmalloc(vlen
+
735 strcpy(*result
, value
);
736 strcpy(*result
+ vlen
, name
+ klen
);
741 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
743 int find_src
= refspec
->src
== NULL
;
744 char *needle
, **result
;
749 return error("find_tracking: need either src or dst");
750 needle
= refspec
->dst
;
751 result
= &refspec
->src
;
753 needle
= refspec
->src
;
754 result
= &refspec
->dst
;
757 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
758 struct refspec
*fetch
= &remote
->fetch
[i
];
759 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
760 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
763 if (fetch
->pattern
) {
764 if (match_name_with_pattern(key
, needle
, value
, result
)) {
765 refspec
->force
= fetch
->force
;
768 } else if (!strcmp(needle
, key
)) {
769 *result
= xstrdup(value
);
770 refspec
->force
= fetch
->force
;
777 static struct ref
*alloc_ref_with_prefix(const char *prefix
, size_t prefixlen
,
780 size_t len
= strlen(name
);
781 struct ref
*ref
= xcalloc(1, sizeof(struct ref
) + prefixlen
+ len
+ 1);
782 memcpy(ref
->name
, prefix
, prefixlen
);
783 memcpy(ref
->name
+ prefixlen
, name
, len
);
787 struct ref
*alloc_ref(const char *name
)
789 return alloc_ref_with_prefix("", 0, name
);
792 static struct ref
*copy_ref(const struct ref
*ref
)
794 struct ref
*ret
= xmalloc(sizeof(struct ref
) + strlen(ref
->name
) + 1);
795 memcpy(ret
, ref
, sizeof(struct ref
) + strlen(ref
->name
) + 1);
800 struct ref
*copy_ref_list(const struct ref
*ref
)
802 struct ref
*ret
= NULL
;
803 struct ref
**tail
= &ret
;
805 *tail
= copy_ref(ref
);
807 tail
= &((*tail
)->next
);
812 static void free_ref(struct ref
*ref
)
816 free(ref
->remote_status
);
821 void free_refs(struct ref
*ref
)
832 static int count_refspec_match(const char *pattern
,
834 struct ref
**matched_ref
)
836 int patlen
= strlen(pattern
);
837 struct ref
*matched_weak
= NULL
;
838 struct ref
*matched
= NULL
;
842 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
843 char *name
= refs
->name
;
844 int namelen
= strlen(name
);
846 if (!refname_match(pattern
, name
, ref_rev_parse_rules
))
849 /* A match is "weak" if it is with refs outside
850 * heads or tags, and did not specify the pattern
851 * in full (e.g. "refs/remotes/origin/master") or at
852 * least from the toplevel (e.g. "remotes/origin/master");
853 * otherwise "git push $URL master" would result in
854 * ambiguity between remotes/origin/master and heads/master
855 * at the remote site.
857 if (namelen
!= patlen
&&
858 patlen
!= namelen
- 5 &&
859 prefixcmp(name
, "refs/heads/") &&
860 prefixcmp(name
, "refs/tags/")) {
861 /* We want to catch the case where only weak
862 * matches are found and there are multiple
863 * matches, and where more than one strong
864 * matches are found, as ambiguous. One
865 * strong match with zero or more weak matches
866 * are acceptable as a unique match.
877 *matched_ref
= matched_weak
;
881 *matched_ref
= matched
;
886 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
894 static struct ref
*try_explicit_object_name(const char *name
)
896 unsigned char sha1
[20];
900 ref
= alloc_ref("(delete)");
901 hashclr(ref
->new_sha1
);
904 if (get_sha1(name
, sha1
))
906 ref
= alloc_ref(name
);
907 hashcpy(ref
->new_sha1
, sha1
);
911 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
913 struct ref
*ret
= alloc_ref(name
);
914 tail_link_ref(ret
, tail
);
918 static char *guess_ref(const char *name
, struct ref
*peer
)
920 struct strbuf buf
= STRBUF_INIT
;
921 unsigned char sha1
[20];
923 const char *r
= resolve_ref(peer
->name
, sha1
, 1, NULL
);
927 if (!prefixcmp(r
, "refs/heads/"))
928 strbuf_addstr(&buf
, "refs/heads/");
929 else if (!prefixcmp(r
, "refs/tags/"))
930 strbuf_addstr(&buf
, "refs/tags/");
934 strbuf_addstr(&buf
, name
);
935 return strbuf_detach(&buf
, NULL
);
938 static int match_explicit(struct ref
*src
, struct ref
*dst
,
939 struct ref
***dst_tail
,
942 struct ref
*matched_src
, *matched_dst
;
944 const char *dst_value
= rs
->dst
;
947 if (rs
->pattern
|| rs
->matching
)
950 matched_src
= matched_dst
= NULL
;
951 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
955 /* The source could be in the get_sha1() format
956 * not a reference name. :refs/other is a
957 * way to delete 'other' ref at the remote end.
959 matched_src
= try_explicit_object_name(rs
->src
);
961 return error("src refspec %s does not match any.", rs
->src
);
964 return error("src refspec %s matches more than one.", rs
->src
);
968 unsigned char sha1
[20];
971 dst_value
= resolve_ref(matched_src
->name
, sha1
, 1, &flag
);
973 ((flag
& REF_ISSYMREF
) &&
974 prefixcmp(dst_value
, "refs/heads/")))
975 die("%s cannot be resolved to branch.",
979 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
983 if (!memcmp(dst_value
, "refs/", 5))
984 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
985 else if((dst_guess
= guess_ref(dst_value
, matched_src
)))
986 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
988 error("unable to push to unqualified destination: %s\n"
989 "The destination refspec neither matches an "
990 "existing ref on the remote nor\n"
991 "begins with refs/, and we are unable to "
992 "guess a prefix based on the source ref.",
997 error("dst refspec %s matches more than one.",
1003 if (matched_dst
->peer_ref
)
1004 return error("dst ref %s receives from more than one src.",
1007 matched_dst
->peer_ref
= matched_src
;
1008 matched_dst
->force
= rs
->force
;
1013 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
1014 struct ref
***dst_tail
, struct refspec
*rs
,
1018 for (i
= errs
= 0; i
< rs_nr
; i
++)
1019 errs
+= match_explicit(src
, dst
, dst_tail
, &rs
[i
]);
1023 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
1025 const struct ref
*src
)
1028 int matching_refs
= -1;
1029 for (i
= 0; i
< rs_nr
; i
++) {
1030 if (rs
[i
].matching
&&
1031 (matching_refs
== -1 || rs
[i
].force
)) {
1036 if (rs
[i
].pattern
&& match_name_with_pattern(rs
[i
].src
, src
->name
,
1040 if (matching_refs
!= -1)
1041 return rs
+ matching_refs
;
1047 * Note. This is used only by "push"; refspec matching rules for
1048 * push and fetch are subtly different, so do not try to reuse it
1051 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
1052 int nr_refspec
, const char **refspec
, int flags
)
1055 int send_all
= flags
& MATCH_REFS_ALL
;
1056 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
1057 static const char *default_refspec
[] = { ":", 0 };
1061 refspec
= default_refspec
;
1063 rs
= parse_push_refspec(nr_refspec
, (const char **) refspec
);
1064 if (match_explicit_refs(src
, dst
, dst_tail
, rs
, nr_refspec
))
1067 /* pick the remainder */
1068 for ( ; src
; src
= src
->next
) {
1069 struct ref
*dst_peer
;
1070 const struct refspec
*pat
= NULL
;
1075 pat
= check_pattern_match(rs
, nr_refspec
, src
);
1079 if (pat
->matching
) {
1081 * "matching refs"; traditionally we pushed everything
1082 * including refs outside refs/heads/ hierarchy, but
1083 * that does not make much sense these days.
1085 if (!send_mirror
&& prefixcmp(src
->name
, "refs/heads/"))
1087 dst_name
= xstrdup(src
->name
);
1090 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
1091 if (!match_name_with_pattern(pat
->src
, src
->name
,
1092 dst_side
, &dst_name
))
1093 die("Didn't think it matches any more");
1095 dst_peer
= find_ref_by_name(dst
, dst_name
);
1097 if (dst_peer
->peer_ref
)
1098 /* We're already sending something to this ref. */
1102 if (pat
->matching
&& !(send_all
|| send_mirror
))
1104 * Remote doesn't have it, and we have no
1105 * explicit pattern, and we don't have
1106 * --all nor --mirror.
1110 /* Create a new one and link it */
1111 dst_peer
= make_linked_ref(dst_name
, dst_tail
);
1112 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
1114 dst_peer
->peer_ref
= src
;
1115 dst_peer
->force
= pat
->force
;
1122 struct branch
*branch_get(const char *name
)
1127 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1128 ret
= current_branch
;
1130 ret
= make_branch(name
, 0);
1131 if (ret
&& ret
->remote_name
) {
1132 ret
->remote
= remote_get(ret
->remote_name
);
1133 if (ret
->merge_nr
) {
1135 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
1137 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1138 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1139 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1140 remote_find_tracking(ret
->remote
,
1148 int branch_has_merge_config(struct branch
*branch
)
1150 return branch
&& !!branch
->merge
;
1153 int branch_merge_matches(struct branch
*branch
,
1155 const char *refname
)
1157 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1159 return refname_match(branch
->merge
[i
]->src
, refname
, ref_fetch_rules
);
1162 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
1163 const struct refspec
*refspec
)
1165 const struct ref
*ref
;
1166 struct ref
*ret
= NULL
;
1167 struct ref
**tail
= &ret
;
1171 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1172 if (strchr(ref
->name
, '^'))
1173 continue; /* a dereference item */
1174 if (match_name_with_pattern(refspec
->src
, ref
->name
,
1175 refspec
->dst
, &expn_name
)) {
1176 struct ref
*cpy
= copy_ref(ref
);
1178 cpy
->peer_ref
= alloc_ref(expn_name
);
1181 cpy
->peer_ref
->force
= 1;
1190 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
1192 const struct ref
*ref
;
1193 for (ref
= refs
; ref
; ref
= ref
->next
) {
1194 if (refname_match(name
, ref
->name
, ref_fetch_rules
))
1200 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
1202 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
1207 return copy_ref(ref
);
1210 static struct ref
*get_local_ref(const char *name
)
1215 if (!prefixcmp(name
, "refs/"))
1216 return alloc_ref(name
);
1218 if (!prefixcmp(name
, "heads/") ||
1219 !prefixcmp(name
, "tags/") ||
1220 !prefixcmp(name
, "remotes/"))
1221 return alloc_ref_with_prefix("refs/", 5, name
);
1223 return alloc_ref_with_prefix("refs/heads/", 11, name
);
1226 int get_fetch_map(const struct ref
*remote_refs
,
1227 const struct refspec
*refspec
,
1231 struct ref
*ref_map
, **rmp
;
1233 if (refspec
->pattern
) {
1234 ref_map
= get_expanded_map(remote_refs
, refspec
);
1236 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
1238 ref_map
= get_remote_ref(remote_refs
, name
);
1239 if (!missing_ok
&& !ref_map
)
1240 die("Couldn't find remote ref %s", name
);
1242 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
1243 if (ref_map
->peer_ref
&& refspec
->force
)
1244 ref_map
->peer_ref
->force
= 1;
1248 for (rmp
= &ref_map
; *rmp
; ) {
1249 if ((*rmp
)->peer_ref
) {
1250 int st
= check_ref_format((*rmp
)->peer_ref
->name
+ 5);
1251 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
) {
1252 struct ref
*ignore
= *rmp
;
1253 error("* Ignoring funny ref '%s' locally",
1254 (*rmp
)->peer_ref
->name
);
1255 *rmp
= (*rmp
)->next
;
1256 free(ignore
->peer_ref
);
1261 rmp
= &((*rmp
)->next
);
1265 tail_link_ref(ref_map
, tail
);
1270 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
1274 for (; list
; list
= list
->next
)
1275 if (!strcmp(ref
->symref
, list
->name
)) {
1276 hashcpy(ref
->old_sha1
, list
->old_sha1
);
1283 * Return true if there is anything to report, otherwise false.
1285 int stat_tracking_info(struct branch
*branch
, int *num_ours
, int *num_theirs
)
1287 unsigned char sha1
[20];
1288 struct commit
*ours
, *theirs
;
1290 struct rev_info revs
;
1291 const char *rev_argv
[10], *base
;
1295 * Nothing to report unless we are marked to build on top of
1299 !branch
->merge
|| !branch
->merge
[0] || !branch
->merge
[0]->dst
)
1303 * If what we used to build on no longer exists, there is
1304 * nothing to report.
1306 base
= branch
->merge
[0]->dst
;
1307 if (!resolve_ref(base
, sha1
, 1, NULL
))
1309 theirs
= lookup_commit(sha1
);
1313 if (!resolve_ref(branch
->refname
, sha1
, 1, NULL
))
1315 ours
= lookup_commit(sha1
);
1319 /* are we the same? */
1323 /* Run "rev-list --left-right ours...theirs" internally... */
1325 rev_argv
[rev_argc
++] = NULL
;
1326 rev_argv
[rev_argc
++] = "--left-right";
1327 rev_argv
[rev_argc
++] = symmetric
;
1328 rev_argv
[rev_argc
++] = "--";
1329 rev_argv
[rev_argc
] = NULL
;
1331 strcpy(symmetric
, sha1_to_hex(ours
->object
.sha1
));
1332 strcpy(symmetric
+ 40, "...");
1333 strcpy(symmetric
+ 43, sha1_to_hex(theirs
->object
.sha1
));
1335 init_revisions(&revs
, NULL
);
1336 setup_revisions(rev_argc
, rev_argv
, &revs
, NULL
);
1337 prepare_revision_walk(&revs
);
1339 /* ... and count the commits on each side. */
1343 struct commit
*c
= get_revision(&revs
);
1346 if (c
->object
.flags
& SYMMETRIC_LEFT
)
1352 /* clear object flags smudged by the above traversal */
1353 clear_commit_marks(ours
, ALL_REV_FLAGS
);
1354 clear_commit_marks(theirs
, ALL_REV_FLAGS
);
1359 * Return true when there is anything to report, otherwise false.
1361 int format_tracking_info(struct branch
*branch
, struct strbuf
*sb
)
1363 int num_ours
, num_theirs
;
1366 if (!stat_tracking_info(branch
, &num_ours
, &num_theirs
))
1369 base
= branch
->merge
[0]->dst
;
1370 if (!prefixcmp(base
, "refs/remotes/")) {
1371 base
+= strlen("refs/remotes/");
1374 strbuf_addf(sb
, "Your branch is ahead of '%s' "
1375 "by %d commit%s.\n",
1376 base
, num_ours
, (num_ours
== 1) ? "" : "s");
1378 strbuf_addf(sb
, "Your branch is behind '%s' "
1380 "and can be fast-forwarded.\n",
1381 base
, num_theirs
, (num_theirs
== 1) ? "" : "s");
1383 strbuf_addf(sb
, "Your branch and '%s' have diverged,\n"
1384 "and have %d and %d different commit(s) each, "
1386 base
, num_ours
, num_theirs
);