10 static struct refspec s_tag_refspec
= {
18 const struct refspec
*tag_refspec
= &s_tag_refspec
;
20 struct counted_string
{
27 struct counted_string
*instead_of
;
32 struct rewrite
**rewrite
;
37 static struct remote
**remotes
;
38 static int remotes_alloc
;
39 static int remotes_nr
;
41 static struct branch
**branches
;
42 static int branches_alloc
;
43 static int branches_nr
;
45 static struct branch
*current_branch
;
46 static const char *default_remote_name
;
47 static int explicit_default_remote_name
;
49 static struct rewrites rewrites
;
51 #define BUF_SIZE (2048)
52 static char buffer
[BUF_SIZE
];
54 static const char *alias_url(const char *url
, struct rewrites
*r
)
58 struct counted_string
*longest
;
63 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
66 for (j
= 0; j
< r
->rewrite
[i
]->instead_of_nr
; j
++) {
67 if (!prefixcmp(url
, r
->rewrite
[i
]->instead_of
[j
].s
) &&
69 longest
->len
< r
->rewrite
[i
]->instead_of
[j
].len
)) {
70 longest
= &(r
->rewrite
[i
]->instead_of
[j
]);
78 ret
= xmalloc(r
->rewrite
[longest_i
]->baselen
+
79 (strlen(url
) - longest
->len
) + 1);
80 strcpy(ret
, r
->rewrite
[longest_i
]->base
);
81 strcpy(ret
+ r
->rewrite
[longest_i
]->baselen
, url
+ longest
->len
);
85 static void add_push_refspec(struct remote
*remote
, const char *ref
)
87 ALLOC_GROW(remote
->push_refspec
,
88 remote
->push_refspec_nr
+ 1,
89 remote
->push_refspec_alloc
);
90 remote
->push_refspec
[remote
->push_refspec_nr
++] = ref
;
93 static void add_fetch_refspec(struct remote
*remote
, const char *ref
)
95 ALLOC_GROW(remote
->fetch_refspec
,
96 remote
->fetch_refspec_nr
+ 1,
97 remote
->fetch_refspec_alloc
);
98 remote
->fetch_refspec
[remote
->fetch_refspec_nr
++] = ref
;
101 static void add_url(struct remote
*remote
, const char *url
)
103 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
104 remote
->url
[remote
->url_nr
++] = url
;
107 static void add_url_alias(struct remote
*remote
, const char *url
)
109 add_url(remote
, alias_url(url
, &rewrites
));
112 static void add_pushurl(struct remote
*remote
, const char *pushurl
)
114 ALLOC_GROW(remote
->pushurl
, remote
->pushurl_nr
+ 1, remote
->pushurl_alloc
);
115 remote
->pushurl
[remote
->pushurl_nr
++] = pushurl
;
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(struct rewrites
*r
, const char *base
, int len
)
180 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
182 ? (len
== r
->rewrite
[i
]->baselen
&&
183 !strncmp(base
, r
->rewrite
[i
]->base
, len
))
184 : !strcmp(base
, r
->rewrite
[i
]->base
))
185 return r
->rewrite
[i
];
188 ALLOC_GROW(r
->rewrite
, r
->rewrite_nr
+ 1, r
->rewrite_alloc
);
189 ret
= xcalloc(1, sizeof(struct rewrite
));
190 r
->rewrite
[r
->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
, NULL
));
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
, NULL
));
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(&rewrites
, 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
, '.');
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
, ".pushurl")) {
393 if (git_config_string(&v
, key
, value
))
395 add_pushurl(remote
, v
);
396 } else if (!strcmp(subkey
, ".push")) {
398 if (git_config_string(&v
, key
, value
))
400 add_push_refspec(remote
, v
);
401 } else if (!strcmp(subkey
, ".fetch")) {
403 if (git_config_string(&v
, key
, value
))
405 add_fetch_refspec(remote
, v
);
406 } else if (!strcmp(subkey
, ".receivepack")) {
408 if (git_config_string(&v
, key
, value
))
410 if (!remote
->receivepack
)
411 remote
->receivepack
= v
;
413 error("more than one receivepack given, using the first");
414 } else if (!strcmp(subkey
, ".uploadpack")) {
416 if (git_config_string(&v
, key
, value
))
418 if (!remote
->uploadpack
)
419 remote
->uploadpack
= v
;
421 error("more than one uploadpack given, using the first");
422 } else if (!strcmp(subkey
, ".tagopt")) {
423 if (!strcmp(value
, "--no-tags"))
424 remote
->fetch_tags
= -1;
425 } else if (!strcmp(subkey
, ".proxy")) {
426 return git_config_string((const char **)&remote
->http_proxy
,
432 static void alias_all_urls(void)
435 for (i
= 0; i
< remotes_nr
; i
++) {
438 for (j
= 0; j
< remotes
[i
]->url_nr
; j
++) {
439 remotes
[i
]->url
[j
] = alias_url(remotes
[i
]->url
[j
], &rewrites
);
441 for (j
= 0; j
< remotes
[i
]->pushurl_nr
; j
++) {
442 remotes
[i
]->pushurl
[j
] = alias_url(remotes
[i
]->pushurl
[j
], &rewrites
);
447 static void read_config(void)
449 unsigned char sha1
[20];
450 const char *head_ref
;
452 if (default_remote_name
) // did this already
454 default_remote_name
= xstrdup("origin");
455 current_branch
= NULL
;
456 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
457 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
458 !prefixcmp(head_ref
, "refs/heads/")) {
460 make_branch(head_ref
+ strlen("refs/heads/"), 0);
462 git_config(handle_config
, NULL
);
467 * We need to make sure the tracking branches are well formed, but a
468 * wildcard refspec in "struct refspec" must have a trailing slash. We
469 * temporarily drop the trailing '/' while calling check_ref_format(),
470 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
471 * error return is Ok for a wildcard refspec.
473 static int verify_refname(char *name
, int is_glob
)
477 result
= check_ref_format(name
);
478 if (is_glob
&& result
== CHECK_REF_FORMAT_WILDCARD
)
479 result
= CHECK_REF_FORMAT_OK
;
484 * This function frees a refspec array.
485 * Warning: code paths should be checked to ensure that the src
486 * and dst pointers are always freeable pointers as well
487 * as the refspec pointer itself.
489 static void free_refspecs(struct refspec
*refspec
, int nr_refspec
)
496 for (i
= 0; i
< nr_refspec
; i
++) {
497 free(refspec
[i
].src
);
498 free(refspec
[i
].dst
);
503 static struct refspec
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
507 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
509 for (i
= 0; i
< nr_refspec
; i
++) {
512 const char *lhs
, *rhs
;
522 rhs
= strrchr(lhs
, ':');
525 * Before going on, special case ":" (or "+:") as a refspec
528 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
534 size_t rlen
= strlen(++rhs
);
535 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
536 rs
[i
].dst
= xstrndup(rhs
, rlen
);
539 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
540 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
541 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
544 } else if (rhs
&& is_glob
) {
548 rs
[i
].pattern
= is_glob
;
549 rs
[i
].src
= xstrndup(lhs
, llen
);
554 * - empty is allowed; it means HEAD.
555 * - otherwise it must be a valid looking ref.
560 st
= verify_refname(rs
[i
].src
, is_glob
);
561 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
566 * - missing is ok, and is same as empty.
567 * - empty is ok; it means not to store.
568 * - otherwise it must be a valid looking ref.
572 } else if (!*rs
[i
].dst
) {
575 st
= verify_refname(rs
[i
].dst
, is_glob
);
576 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
582 * - empty is allowed; it means delete.
583 * - when wildcarded, it must be a valid looking ref.
584 * - otherwise, it must be an extended SHA-1, but
585 * there is no existing way to validate this.
590 st
= verify_refname(rs
[i
].src
, is_glob
);
591 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
595 ; /* anything goes, for now */
598 * - missing is allowed, but LHS then must be a
600 * - empty is not allowed.
601 * - otherwise it must be a valid looking ref.
604 st
= verify_refname(rs
[i
].src
, is_glob
);
605 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
607 } else if (!*rs
[i
].dst
) {
610 st
= verify_refname(rs
[i
].dst
, is_glob
);
611 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
621 * nr_refspec must be greater than zero and i must be valid
622 * since it is only possible to reach this point from within
623 * the for loop above.
625 free_refspecs(rs
, i
+1);
628 die("Invalid refspec '%s'", refspec
[i
]);
631 int valid_fetch_refspec(const char *fetch_refspec_str
)
633 const char *fetch_refspec
[] = { fetch_refspec_str
};
634 struct refspec
*refspec
;
636 refspec
= parse_refspec_internal(1, fetch_refspec
, 1, 1);
637 free_refspecs(refspec
, 1);
641 struct refspec
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
643 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
646 static struct refspec
*parse_push_refspec(int nr_refspec
, const char **refspec
)
648 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
651 static int valid_remote_nick(const char *name
)
653 if (!name
[0] || is_dot_or_dotdot(name
))
655 return !strchr(name
, '/'); /* no slash */
658 struct remote
*remote_get(const char *name
)
667 name
= default_remote_name
;
668 name_given
= explicit_default_remote_name
;
671 ret
= make_remote(name
, 0);
672 if (valid_remote_nick(name
)) {
674 read_remotes_file(ret
);
676 read_branches_file(ret
);
678 if (name_given
&& !ret
->url
)
679 add_url_alias(ret
, name
);
682 ret
->fetch
= parse_fetch_refspec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
683 ret
->push
= parse_push_refspec(ret
->push_refspec_nr
, ret
->push_refspec
);
687 int remote_is_configured(const char *name
)
692 for (i
= 0; i
< remotes_nr
; i
++)
693 if (!strcmp(name
, remotes
[i
]->name
))
698 int for_each_remote(each_remote_fn fn
, void *priv
)
702 for (i
= 0; i
< remotes_nr
&& !result
; i
++) {
703 struct remote
*r
= remotes
[i
];
707 r
->fetch
= parse_fetch_refspec(r
->fetch_refspec_nr
,
710 r
->push
= parse_push_refspec(r
->push_refspec_nr
,
712 result
= fn(r
, priv
);
717 void ref_remove_duplicates(struct ref
*ref_map
)
721 for (; ref_map
; ref_map
= ref_map
->next
) {
722 if (!ref_map
->peer_ref
)
724 posn
= &ref_map
->next
;
726 if ((*posn
)->peer_ref
&&
727 !strcmp((*posn
)->peer_ref
->name
,
728 ref_map
->peer_ref
->name
)) {
729 if (strcmp((*posn
)->name
, ref_map
->name
))
730 die("%s tracks both %s and %s",
731 ref_map
->peer_ref
->name
,
732 (*posn
)->name
, ref_map
->name
);
733 next
= (*posn
)->next
;
734 free((*posn
)->peer_ref
);
738 posn
= &(*posn
)->next
;
744 int remote_has_url(struct remote
*remote
, const char *url
)
747 for (i
= 0; i
< remote
->url_nr
; i
++) {
748 if (!strcmp(remote
->url
[i
], url
))
754 static int match_name_with_pattern(const char *key
, const char *name
,
755 const char *value
, char **result
)
757 const char *kstar
= strchr(key
, '*');
763 die("Key '%s' of pattern had no '*'", key
);
765 ksuffixlen
= strlen(kstar
+ 1);
766 namelen
= strlen(name
);
767 ret
= !strncmp(name
, key
, klen
) && namelen
>= klen
+ ksuffixlen
&&
768 !memcmp(name
+ namelen
- ksuffixlen
, kstar
+ 1, ksuffixlen
);
770 const char *vstar
= strchr(value
, '*');
774 die("Value '%s' of pattern has no '*'", value
);
775 vlen
= vstar
- value
;
776 vsuffixlen
= strlen(vstar
+ 1);
777 *result
= xmalloc(vlen
+ vsuffixlen
+
779 klen
- ksuffixlen
+ 1);
780 strncpy(*result
, value
, vlen
);
781 strncpy(*result
+ vlen
,
782 name
+ klen
, namelen
- klen
- ksuffixlen
);
783 strcpy(*result
+ vlen
+ namelen
- klen
- ksuffixlen
,
789 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
791 int find_src
= refspec
->src
== NULL
;
792 char *needle
, **result
;
797 return error("find_tracking: need either src or dst");
798 needle
= refspec
->dst
;
799 result
= &refspec
->src
;
801 needle
= refspec
->src
;
802 result
= &refspec
->dst
;
805 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
806 struct refspec
*fetch
= &remote
->fetch
[i
];
807 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
808 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
811 if (fetch
->pattern
) {
812 if (match_name_with_pattern(key
, needle
, value
, result
)) {
813 refspec
->force
= fetch
->force
;
816 } else if (!strcmp(needle
, key
)) {
817 *result
= xstrdup(value
);
818 refspec
->force
= fetch
->force
;
825 static struct ref
*alloc_ref_with_prefix(const char *prefix
, size_t prefixlen
,
828 size_t len
= strlen(name
);
829 struct ref
*ref
= xcalloc(1, sizeof(struct ref
) + prefixlen
+ len
+ 1);
830 memcpy(ref
->name
, prefix
, prefixlen
);
831 memcpy(ref
->name
+ prefixlen
, name
, len
);
835 struct ref
*alloc_ref(const char *name
)
837 return alloc_ref_with_prefix("", 0, name
);
840 static struct ref
*copy_ref(const struct ref
*ref
)
846 len
= strlen(ref
->name
);
847 cpy
= xmalloc(sizeof(struct ref
) + len
+ 1);
848 memcpy(cpy
, ref
, sizeof(struct ref
) + len
+ 1);
850 cpy
->symref
= ref
->symref
? xstrdup(ref
->symref
) : NULL
;
851 cpy
->remote_status
= ref
->remote_status
? xstrdup(ref
->remote_status
) : NULL
;
852 cpy
->peer_ref
= copy_ref(ref
->peer_ref
);
856 struct ref
*copy_ref_list(const struct ref
*ref
)
858 struct ref
*ret
= NULL
;
859 struct ref
**tail
= &ret
;
861 *tail
= copy_ref(ref
);
863 tail
= &((*tail
)->next
);
868 static void free_ref(struct ref
*ref
)
872 free_ref(ref
->peer_ref
);
873 free(ref
->remote_status
);
878 void free_refs(struct ref
*ref
)
888 static int count_refspec_match(const char *pattern
,
890 struct ref
**matched_ref
)
892 int patlen
= strlen(pattern
);
893 struct ref
*matched_weak
= NULL
;
894 struct ref
*matched
= NULL
;
898 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
899 char *name
= refs
->name
;
900 int namelen
= strlen(name
);
902 if (!refname_match(pattern
, name
, ref_rev_parse_rules
))
905 /* A match is "weak" if it is with refs outside
906 * heads or tags, and did not specify the pattern
907 * in full (e.g. "refs/remotes/origin/master") or at
908 * least from the toplevel (e.g. "remotes/origin/master");
909 * otherwise "git push $URL master" would result in
910 * ambiguity between remotes/origin/master and heads/master
911 * at the remote site.
913 if (namelen
!= patlen
&&
914 patlen
!= namelen
- 5 &&
915 prefixcmp(name
, "refs/heads/") &&
916 prefixcmp(name
, "refs/tags/")) {
917 /* We want to catch the case where only weak
918 * matches are found and there are multiple
919 * matches, and where more than one strong
920 * matches are found, as ambiguous. One
921 * strong match with zero or more weak matches
922 * are acceptable as a unique match.
933 *matched_ref
= matched_weak
;
937 *matched_ref
= matched
;
942 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
950 static struct ref
*try_explicit_object_name(const char *name
)
952 unsigned char sha1
[20];
956 ref
= alloc_ref("(delete)");
957 hashclr(ref
->new_sha1
);
960 if (get_sha1(name
, sha1
))
962 ref
= alloc_ref(name
);
963 hashcpy(ref
->new_sha1
, sha1
);
967 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
969 struct ref
*ret
= alloc_ref(name
);
970 tail_link_ref(ret
, tail
);
974 static char *guess_ref(const char *name
, struct ref
*peer
)
976 struct strbuf buf
= STRBUF_INIT
;
977 unsigned char sha1
[20];
979 const char *r
= resolve_ref(peer
->name
, sha1
, 1, NULL
);
983 if (!prefixcmp(r
, "refs/heads/"))
984 strbuf_addstr(&buf
, "refs/heads/");
985 else if (!prefixcmp(r
, "refs/tags/"))
986 strbuf_addstr(&buf
, "refs/tags/");
990 strbuf_addstr(&buf
, name
);
991 return strbuf_detach(&buf
, NULL
);
994 static int match_explicit(struct ref
*src
, struct ref
*dst
,
995 struct ref
***dst_tail
,
998 struct ref
*matched_src
, *matched_dst
;
1001 const char *dst_value
= rs
->dst
;
1004 if (rs
->pattern
|| rs
->matching
)
1007 matched_src
= matched_dst
= NULL
;
1008 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
1013 /* The source could be in the get_sha1() format
1014 * not a reference name. :refs/other is a
1015 * way to delete 'other' ref at the remote end.
1017 matched_src
= try_explicit_object_name(rs
->src
);
1019 return error("src refspec %s does not match any.", rs
->src
);
1023 return error("src refspec %s matches more than one.", rs
->src
);
1027 unsigned char sha1
[20];
1030 dst_value
= resolve_ref(matched_src
->name
, sha1
, 1, &flag
);
1032 ((flag
& REF_ISSYMREF
) &&
1033 prefixcmp(dst_value
, "refs/heads/")))
1034 die("%s cannot be resolved to branch.",
1038 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
1042 if (!memcmp(dst_value
, "refs/", 5))
1043 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
1044 else if ((dst_guess
= guess_ref(dst_value
, matched_src
)))
1045 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
1047 error("unable to push to unqualified destination: %s\n"
1048 "The destination refspec neither matches an "
1049 "existing ref on the remote nor\n"
1050 "begins with refs/, and we are unable to "
1051 "guess a prefix based on the source ref.",
1056 error("dst refspec %s matches more than one.",
1062 if (matched_dst
->peer_ref
)
1063 return error("dst ref %s receives from more than one src.",
1066 matched_dst
->peer_ref
= copy_src
? copy_ref(matched_src
) : matched_src
;
1067 matched_dst
->force
= rs
->force
;
1072 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
1073 struct ref
***dst_tail
, struct refspec
*rs
,
1077 for (i
= errs
= 0; i
< rs_nr
; i
++)
1078 errs
+= match_explicit(src
, dst
, dst_tail
, &rs
[i
]);
1082 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
1084 const struct ref
*src
)
1087 int matching_refs
= -1;
1088 for (i
= 0; i
< rs_nr
; i
++) {
1089 if (rs
[i
].matching
&&
1090 (matching_refs
== -1 || rs
[i
].force
)) {
1095 if (rs
[i
].pattern
&& match_name_with_pattern(rs
[i
].src
, src
->name
,
1099 if (matching_refs
!= -1)
1100 return rs
+ matching_refs
;
1105 static struct ref
**tail_ref(struct ref
**head
)
1107 struct ref
**tail
= head
;
1109 tail
= &((*tail
)->next
);
1114 * Note. This is used only by "push"; refspec matching rules for
1115 * push and fetch are subtly different, so do not try to reuse it
1118 int match_refs(struct ref
*src
, struct ref
**dst
,
1119 int nr_refspec
, const char **refspec
, int flags
)
1122 int send_all
= flags
& MATCH_REFS_ALL
;
1123 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
1125 static const char *default_refspec
[] = { ":", NULL
};
1126 struct ref
**dst_tail
= tail_ref(dst
);
1130 refspec
= default_refspec
;
1132 rs
= parse_push_refspec(nr_refspec
, (const char **) refspec
);
1133 errs
= match_explicit_refs(src
, *dst
, &dst_tail
, rs
, nr_refspec
);
1135 /* pick the remainder */
1136 for ( ; src
; src
= src
->next
) {
1137 struct ref
*dst_peer
;
1138 const struct refspec
*pat
= NULL
;
1143 pat
= check_pattern_match(rs
, nr_refspec
, src
);
1147 if (pat
->matching
) {
1149 * "matching refs"; traditionally we pushed everything
1150 * including refs outside refs/heads/ hierarchy, but
1151 * that does not make much sense these days.
1153 if (!send_mirror
&& prefixcmp(src
->name
, "refs/heads/"))
1155 dst_name
= xstrdup(src
->name
);
1158 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
1159 if (!match_name_with_pattern(pat
->src
, src
->name
,
1160 dst_side
, &dst_name
))
1161 die("Didn't think it matches any more");
1163 dst_peer
= find_ref_by_name(*dst
, dst_name
);
1165 if (dst_peer
->peer_ref
)
1166 /* We're already sending something to this ref. */
1170 if (pat
->matching
&& !(send_all
|| send_mirror
))
1172 * Remote doesn't have it, and we have no
1173 * explicit pattern, and we don't have
1174 * --all nor --mirror.
1178 /* Create a new one and link it */
1179 dst_peer
= make_linked_ref(dst_name
, &dst_tail
);
1180 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
1182 dst_peer
->peer_ref
= copy_ref(src
);
1183 dst_peer
->force
= pat
->force
;
1192 struct branch
*branch_get(const char *name
)
1197 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1198 ret
= current_branch
;
1200 ret
= make_branch(name
, 0);
1201 if (ret
&& ret
->remote_name
) {
1202 ret
->remote
= remote_get(ret
->remote_name
);
1203 if (ret
->merge_nr
) {
1205 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
1207 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1208 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1209 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1210 if (remote_find_tracking(ret
->remote
, ret
->merge
[i
])
1211 && !strcmp(ret
->remote_name
, "."))
1212 ret
->merge
[i
]->dst
= xstrdup(ret
->merge_name
[i
]);
1219 int branch_has_merge_config(struct branch
*branch
)
1221 return branch
&& !!branch
->merge
;
1224 int branch_merge_matches(struct branch
*branch
,
1226 const char *refname
)
1228 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1230 return refname_match(branch
->merge
[i
]->src
, refname
, ref_fetch_rules
);
1233 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
1234 const struct refspec
*refspec
)
1236 const struct ref
*ref
;
1237 struct ref
*ret
= NULL
;
1238 struct ref
**tail
= &ret
;
1242 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1243 if (strchr(ref
->name
, '^'))
1244 continue; /* a dereference item */
1245 if (match_name_with_pattern(refspec
->src
, ref
->name
,
1246 refspec
->dst
, &expn_name
)) {
1247 struct ref
*cpy
= copy_ref(ref
);
1249 cpy
->peer_ref
= alloc_ref(expn_name
);
1252 cpy
->peer_ref
->force
= 1;
1261 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
1263 const struct ref
*ref
;
1264 for (ref
= refs
; ref
; ref
= ref
->next
) {
1265 if (refname_match(name
, ref
->name
, ref_fetch_rules
))
1271 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
1273 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
1278 return copy_ref(ref
);
1281 static struct ref
*get_local_ref(const char *name
)
1283 if (!name
|| name
[0] == '\0')
1286 if (!prefixcmp(name
, "refs/"))
1287 return alloc_ref(name
);
1289 if (!prefixcmp(name
, "heads/") ||
1290 !prefixcmp(name
, "tags/") ||
1291 !prefixcmp(name
, "remotes/"))
1292 return alloc_ref_with_prefix("refs/", 5, name
);
1294 return alloc_ref_with_prefix("refs/heads/", 11, name
);
1297 int get_fetch_map(const struct ref
*remote_refs
,
1298 const struct refspec
*refspec
,
1302 struct ref
*ref_map
, **rmp
;
1304 if (refspec
->pattern
) {
1305 ref_map
= get_expanded_map(remote_refs
, refspec
);
1307 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
1309 ref_map
= get_remote_ref(remote_refs
, name
);
1310 if (!missing_ok
&& !ref_map
)
1311 die("Couldn't find remote ref %s", name
);
1313 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
1314 if (ref_map
->peer_ref
&& refspec
->force
)
1315 ref_map
->peer_ref
->force
= 1;
1319 for (rmp
= &ref_map
; *rmp
; ) {
1320 if ((*rmp
)->peer_ref
) {
1321 int st
= check_ref_format((*rmp
)->peer_ref
->name
+ 5);
1322 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
) {
1323 struct ref
*ignore
= *rmp
;
1324 error("* Ignoring funny ref '%s' locally",
1325 (*rmp
)->peer_ref
->name
);
1326 *rmp
= (*rmp
)->next
;
1327 free(ignore
->peer_ref
);
1332 rmp
= &((*rmp
)->next
);
1336 tail_link_ref(ref_map
, tail
);
1341 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
1345 for (; list
; list
= list
->next
)
1346 if (!strcmp(ref
->symref
, list
->name
)) {
1347 hashcpy(ref
->old_sha1
, list
->old_sha1
);
1353 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
1356 struct commit_list
*temp
= list
;
1357 temp
->item
->object
.flags
&= ~mark
;
1363 int ref_newer(const unsigned char *new_sha1
, const unsigned char *old_sha1
)
1366 struct commit
*old
, *new;
1367 struct commit_list
*list
, *used
;
1370 /* Both new and old must be commit-ish and new is descendant of
1371 * old. Otherwise we require --force.
1373 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
1374 if (!o
|| o
->type
!= OBJ_COMMIT
)
1376 old
= (struct commit
*) o
;
1378 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
1379 if (!o
|| o
->type
!= OBJ_COMMIT
)
1381 new = (struct commit
*) o
;
1383 if (parse_commit(new) < 0)
1387 commit_list_insert(new, &list
);
1389 new = pop_most_recent_commit(&list
, TMP_MARK
);
1390 commit_list_insert(new, &used
);
1396 unmark_and_free(list
, TMP_MARK
);
1397 unmark_and_free(used
, TMP_MARK
);
1402 * Return true if there is anything to report, otherwise false.
1404 int stat_tracking_info(struct branch
*branch
, int *num_ours
, int *num_theirs
)
1406 unsigned char sha1
[20];
1407 struct commit
*ours
, *theirs
;
1409 struct rev_info revs
;
1410 const char *rev_argv
[10], *base
;
1414 * Nothing to report unless we are marked to build on top of
1418 !branch
->merge
|| !branch
->merge
[0] || !branch
->merge
[0]->dst
)
1422 * If what we used to build on no longer exists, there is
1423 * nothing to report.
1425 base
= branch
->merge
[0]->dst
;
1426 if (!resolve_ref(base
, sha1
, 1, NULL
))
1428 theirs
= lookup_commit_reference(sha1
);
1432 if (!resolve_ref(branch
->refname
, sha1
, 1, NULL
))
1434 ours
= lookup_commit_reference(sha1
);
1438 /* are we the same? */
1442 /* Run "rev-list --left-right ours...theirs" internally... */
1444 rev_argv
[rev_argc
++] = NULL
;
1445 rev_argv
[rev_argc
++] = "--left-right";
1446 rev_argv
[rev_argc
++] = symmetric
;
1447 rev_argv
[rev_argc
++] = "--";
1448 rev_argv
[rev_argc
] = NULL
;
1450 strcpy(symmetric
, sha1_to_hex(ours
->object
.sha1
));
1451 strcpy(symmetric
+ 40, "...");
1452 strcpy(symmetric
+ 43, sha1_to_hex(theirs
->object
.sha1
));
1454 init_revisions(&revs
, NULL
);
1455 setup_revisions(rev_argc
, rev_argv
, &revs
, NULL
);
1456 prepare_revision_walk(&revs
);
1458 /* ... and count the commits on each side. */
1462 struct commit
*c
= get_revision(&revs
);
1465 if (c
->object
.flags
& SYMMETRIC_LEFT
)
1471 /* clear object flags smudged by the above traversal */
1472 clear_commit_marks(ours
, ALL_REV_FLAGS
);
1473 clear_commit_marks(theirs
, ALL_REV_FLAGS
);
1478 * Return true when there is anything to report, otherwise false.
1480 int format_tracking_info(struct branch
*branch
, struct strbuf
*sb
)
1482 int num_ours
, num_theirs
;
1485 if (!stat_tracking_info(branch
, &num_ours
, &num_theirs
))
1488 base
= branch
->merge
[0]->dst
;
1489 base
= shorten_unambiguous_ref(base
, 0);
1491 strbuf_addf(sb
, "Your branch is ahead of '%s' "
1492 "by %d commit%s.\n",
1493 base
, num_ours
, (num_ours
== 1) ? "" : "s");
1495 strbuf_addf(sb
, "Your branch is behind '%s' "
1497 "and can be fast-forwarded.\n",
1498 base
, num_theirs
, (num_theirs
== 1) ? "" : "s");
1500 strbuf_addf(sb
, "Your branch and '%s' have diverged,\n"
1501 "and have %d and %d different commit(s) each, "
1503 base
, num_ours
, num_theirs
);
1507 static int one_local_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
1509 struct ref
***local_tail
= cb_data
;
1513 /* we already know it starts with refs/ to get here */
1514 if (check_ref_format(refname
+ 5))
1517 len
= strlen(refname
) + 1;
1518 ref
= xcalloc(1, sizeof(*ref
) + len
);
1519 hashcpy(ref
->new_sha1
, sha1
);
1520 memcpy(ref
->name
, refname
, len
);
1522 *local_tail
= &ref
->next
;
1526 struct ref
*get_local_heads(void)
1528 struct ref
*local_refs
= NULL
, **local_tail
= &local_refs
;
1529 for_each_ref(one_local_ref
, &local_tail
);
1533 struct ref
*guess_remote_head(const struct ref
*head
,
1534 const struct ref
*refs
,
1537 const struct ref
*r
;
1538 struct ref
*list
= NULL
;
1539 struct ref
**tail
= &list
;
1545 * Some transports support directly peeking at
1546 * where HEAD points; if that is the case, then
1547 * we don't have to guess.
1550 return copy_ref(find_ref_by_name(refs
, head
->symref
));
1552 /* If refs/heads/master could be right, it is. */
1554 r
= find_ref_by_name(refs
, "refs/heads/master");
1555 if (r
&& !hashcmp(r
->old_sha1
, head
->old_sha1
))
1559 /* Look for another ref that points there */
1560 for (r
= refs
; r
; r
= r
->next
) {
1561 if (r
!= head
&& !hashcmp(r
->old_sha1
, head
->old_sha1
)) {
1562 *tail
= copy_ref(r
);
1563 tail
= &((*tail
)->next
);