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 static struct remote
**remotes
;
33 static int remotes_alloc
;
34 static int remotes_nr
;
36 static struct branch
**branches
;
37 static int branches_alloc
;
38 static int branches_nr
;
40 static struct branch
*current_branch
;
41 static const char *default_remote_name
;
42 static int explicit_default_remote_name
;
44 static struct rewrite
**rewrite
;
45 static int rewrite_alloc
;
46 static int rewrite_nr
;
48 #define BUF_SIZE (2048)
49 static char buffer
[BUF_SIZE
];
51 static const char *alias_url(const char *url
)
55 struct counted_string
*longest
;
60 for (i
= 0; i
< rewrite_nr
; i
++) {
63 for (j
= 0; j
< rewrite
[i
]->instead_of_nr
; j
++) {
64 if (!prefixcmp(url
, rewrite
[i
]->instead_of
[j
].s
) &&
66 longest
->len
< rewrite
[i
]->instead_of
[j
].len
)) {
67 longest
= &(rewrite
[i
]->instead_of
[j
]);
75 ret
= xmalloc(rewrite
[longest_i
]->baselen
+
76 (strlen(url
) - longest
->len
) + 1);
77 strcpy(ret
, rewrite
[longest_i
]->base
);
78 strcpy(ret
+ rewrite
[longest_i
]->baselen
, url
+ longest
->len
);
82 static void add_push_refspec(struct remote
*remote
, const char *ref
)
84 ALLOC_GROW(remote
->push_refspec
,
85 remote
->push_refspec_nr
+ 1,
86 remote
->push_refspec_alloc
);
87 remote
->push_refspec
[remote
->push_refspec_nr
++] = ref
;
90 static void add_fetch_refspec(struct remote
*remote
, const char *ref
)
92 ALLOC_GROW(remote
->fetch_refspec
,
93 remote
->fetch_refspec_nr
+ 1,
94 remote
->fetch_refspec_alloc
);
95 remote
->fetch_refspec
[remote
->fetch_refspec_nr
++] = ref
;
98 static void add_url(struct remote
*remote
, const char *url
)
100 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
101 remote
->url
[remote
->url_nr
++] = url
;
104 static void add_url_alias(struct remote
*remote
, const char *url
)
106 add_url(remote
, alias_url(url
));
109 static struct remote
*make_remote(const char *name
, int len
)
114 for (i
= 0; i
< remotes_nr
; i
++) {
115 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
116 !remotes
[i
]->name
[len
]) :
117 !strcmp(name
, remotes
[i
]->name
))
121 ret
= xcalloc(1, sizeof(struct remote
));
122 ALLOC_GROW(remotes
, remotes_nr
+ 1, remotes_alloc
);
123 remotes
[remotes_nr
++] = ret
;
125 ret
->name
= xstrndup(name
, len
);
127 ret
->name
= xstrdup(name
);
131 static void add_merge(struct branch
*branch
, const char *name
)
133 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
134 branch
->merge_alloc
);
135 branch
->merge_name
[branch
->merge_nr
++] = name
;
138 static struct branch
*make_branch(const char *name
, int len
)
144 for (i
= 0; i
< branches_nr
; i
++) {
145 if (len
? (!strncmp(name
, branches
[i
]->name
, len
) &&
146 !branches
[i
]->name
[len
]) :
147 !strcmp(name
, branches
[i
]->name
))
151 ALLOC_GROW(branches
, branches_nr
+ 1, branches_alloc
);
152 ret
= xcalloc(1, sizeof(struct branch
));
153 branches
[branches_nr
++] = ret
;
155 ret
->name
= xstrndup(name
, len
);
157 ret
->name
= xstrdup(name
);
158 refname
= xmalloc(strlen(name
) + strlen("refs/heads/") + 1);
159 strcpy(refname
, "refs/heads/");
160 strcpy(refname
+ strlen("refs/heads/"), ret
->name
);
161 ret
->refname
= refname
;
166 static struct rewrite
*make_rewrite(const char *base
, int len
)
171 for (i
= 0; i
< rewrite_nr
; i
++) {
173 ? (len
== rewrite
[i
]->baselen
&&
174 !strncmp(base
, rewrite
[i
]->base
, len
))
175 : !strcmp(base
, rewrite
[i
]->base
))
179 ALLOC_GROW(rewrite
, rewrite_nr
+ 1, rewrite_alloc
);
180 ret
= xcalloc(1, sizeof(struct rewrite
));
181 rewrite
[rewrite_nr
++] = ret
;
183 ret
->base
= xstrndup(base
, len
);
187 ret
->base
= xstrdup(base
);
188 ret
->baselen
= strlen(base
);
193 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
195 ALLOC_GROW(rewrite
->instead_of
, rewrite
->instead_of_nr
+ 1, rewrite
->instead_of_alloc
);
196 rewrite
->instead_of
[rewrite
->instead_of_nr
].s
= instead_of
;
197 rewrite
->instead_of
[rewrite
->instead_of_nr
].len
= strlen(instead_of
);
198 rewrite
->instead_of_nr
++;
201 static void read_remotes_file(struct remote
*remote
)
203 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
207 remote
->origin
= REMOTE_REMOTES
;
208 while (fgets(buffer
, BUF_SIZE
, f
)) {
212 if (!prefixcmp(buffer
, "URL:")) {
215 } else if (!prefixcmp(buffer
, "Push:")) {
218 } else if (!prefixcmp(buffer
, "Pull:")) {
230 while (isspace(p
[-1]))
233 switch (value_list
) {
235 add_url_alias(remote
, xstrdup(s
));
238 add_push_refspec(remote
, xstrdup(s
));
241 add_fetch_refspec(remote
, xstrdup(s
));
248 static void read_branches_file(struct remote
*remote
)
250 const char *slash
= strchr(remote
->name
, '/');
252 struct strbuf branch
= STRBUF_INIT
;
253 int n
= slash
? slash
- remote
->name
: 1000;
254 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
260 s
= fgets(buffer
, BUF_SIZE
, f
);
268 remote
->origin
= REMOTE_BRANCHES
;
270 while (isspace(p
[-1]))
274 len
+= strlen(slash
);
275 p
= xmalloc(len
+ 1);
281 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
282 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
283 * the partial URL obtained from the branches file plus
284 * "/netdev-2.6" and does not store it in any tracking ref.
285 * #branch specifier in the file is ignored.
287 * Otherwise, the branches file would have URL and optionally
288 * #branch specified. The "master" (or specified) branch is
289 * fetched and stored in the local branch of the same name.
291 frag
= strchr(p
, '#');
294 strbuf_addf(&branch
, "refs/heads/%s", frag
);
296 strbuf_addstr(&branch
, "refs/heads/master");
298 strbuf_addf(&branch
, ":refs/heads/%s", remote
->name
);
300 strbuf_reset(&branch
);
301 strbuf_addstr(&branch
, "HEAD:");
303 add_url_alias(remote
, p
);
304 add_fetch_refspec(remote
, strbuf_detach(&branch
, 0));
306 * Cogito compatible push: push current HEAD to remote #branch
307 * (master if missing)
309 strbuf_init(&branch
, 0);
310 strbuf_addstr(&branch
, "HEAD");
312 strbuf_addf(&branch
, ":refs/heads/%s", frag
);
314 strbuf_addstr(&branch
, ":refs/heads/master");
315 add_push_refspec(remote
, strbuf_detach(&branch
, 0));
316 remote
->fetch_tags
= 1; /* always auto-follow */
319 static int handle_config(const char *key
, const char *value
, void *cb
)
323 struct remote
*remote
;
324 struct branch
*branch
;
325 if (!prefixcmp(key
, "branch.")) {
327 subkey
= strrchr(name
, '.');
330 branch
= make_branch(name
, subkey
- name
);
331 if (!strcmp(subkey
, ".remote")) {
333 return config_error_nonbool(key
);
334 branch
->remote_name
= xstrdup(value
);
335 if (branch
== current_branch
) {
336 default_remote_name
= branch
->remote_name
;
337 explicit_default_remote_name
= 1;
339 } else if (!strcmp(subkey
, ".merge")) {
341 return config_error_nonbool(key
);
342 add_merge(branch
, xstrdup(value
));
346 if (!prefixcmp(key
, "url.")) {
347 struct rewrite
*rewrite
;
349 subkey
= strrchr(name
, '.');
352 rewrite
= make_rewrite(name
, subkey
- name
);
353 if (!strcmp(subkey
, ".insteadof")) {
355 return config_error_nonbool(key
);
356 add_instead_of(rewrite
, xstrdup(value
));
359 if (prefixcmp(key
, "remote."))
363 warning("Config remote shorthand cannot begin with '/': %s",
367 subkey
= strrchr(name
, '.');
369 return error("Config with no key for remote %s", name
);
370 remote
= make_remote(name
, subkey
- name
);
371 remote
->origin
= REMOTE_CONFIG
;
372 if (!strcmp(subkey
, ".mirror"))
373 remote
->mirror
= git_config_bool(key
, value
);
374 else if (!strcmp(subkey
, ".skipdefaultupdate"))
375 remote
->skip_default_update
= git_config_bool(key
, value
);
377 else if (!strcmp(subkey
, ".url")) {
379 if (git_config_string(&v
, key
, value
))
382 } else if (!strcmp(subkey
, ".push")) {
384 if (git_config_string(&v
, key
, value
))
386 add_push_refspec(remote
, v
);
387 } else if (!strcmp(subkey
, ".fetch")) {
389 if (git_config_string(&v
, key
, value
))
391 add_fetch_refspec(remote
, v
);
392 } else if (!strcmp(subkey
, ".receivepack")) {
394 if (git_config_string(&v
, key
, value
))
396 if (!remote
->receivepack
)
397 remote
->receivepack
= v
;
399 error("more than one receivepack given, using the first");
400 } else if (!strcmp(subkey
, ".uploadpack")) {
402 if (git_config_string(&v
, key
, value
))
404 if (!remote
->uploadpack
)
405 remote
->uploadpack
= v
;
407 error("more than one uploadpack given, using the first");
408 } else if (!strcmp(subkey
, ".tagopt")) {
409 if (!strcmp(value
, "--no-tags"))
410 remote
->fetch_tags
= -1;
411 } else if (!strcmp(subkey
, ".proxy")) {
412 return git_config_string((const char **)&remote
->http_proxy
,
418 static void alias_all_urls(void)
421 for (i
= 0; i
< remotes_nr
; i
++) {
424 for (j
= 0; j
< remotes
[i
]->url_nr
; j
++) {
425 remotes
[i
]->url
[j
] = alias_url(remotes
[i
]->url
[j
]);
430 static void read_config(void)
432 unsigned char sha1
[20];
433 const char *head_ref
;
435 if (default_remote_name
) // did this already
437 default_remote_name
= xstrdup("origin");
438 current_branch
= NULL
;
439 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
440 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
441 !prefixcmp(head_ref
, "refs/heads/")) {
443 make_branch(head_ref
+ strlen("refs/heads/"), 0);
445 git_config(handle_config
, NULL
);
450 * We need to make sure the tracking branches are well formed, but a
451 * wildcard refspec in "struct refspec" must have a trailing slash. We
452 * temporarily drop the trailing '/' while calling check_ref_format(),
453 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
454 * error return is Ok for a wildcard refspec.
456 static int verify_refname(char *name
, int is_glob
)
460 result
= check_ref_format(name
);
461 if (is_glob
&& result
== CHECK_REF_FORMAT_WILDCARD
)
462 result
= CHECK_REF_FORMAT_OK
;
467 * This function frees a refspec array.
468 * Warning: code paths should be checked to ensure that the src
469 * and dst pointers are always freeable pointers as well
470 * as the refspec pointer itself.
472 static void free_refspecs(struct refspec
*refspec
, int nr_refspec
)
479 for (i
= 0; i
< nr_refspec
; i
++) {
480 free(refspec
[i
].src
);
481 free(refspec
[i
].dst
);
486 static struct refspec
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
490 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
492 for (i
= 0; i
< nr_refspec
; i
++) {
495 const char *lhs
, *rhs
;
505 rhs
= strrchr(lhs
, ':');
508 * Before going on, special case ":" (or "+:") as a refspec
511 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
517 size_t rlen
= strlen(++rhs
);
518 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
519 rs
[i
].dst
= xstrndup(rhs
, rlen
);
522 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
523 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
524 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
527 } else if (rhs
&& is_glob
) {
531 rs
[i
].pattern
= is_glob
;
532 rs
[i
].src
= xstrndup(lhs
, llen
);
537 * - empty is allowed; it means HEAD.
538 * - otherwise it must be a valid looking ref.
543 st
= verify_refname(rs
[i
].src
, is_glob
);
544 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
549 * - missing is ok, and is same as empty.
550 * - empty is ok; it means not to store.
551 * - otherwise it must be a valid looking ref.
555 } else if (!*rs
[i
].dst
) {
558 st
= verify_refname(rs
[i
].dst
, is_glob
);
559 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
565 * - empty is allowed; it means delete.
566 * - when wildcarded, it must be a valid looking ref.
567 * - otherwise, it must be an extended SHA-1, but
568 * there is no existing way to validate this.
573 st
= verify_refname(rs
[i
].src
, is_glob
);
574 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
578 ; /* anything goes, for now */
581 * - missing is allowed, but LHS then must be a
583 * - empty is not allowed.
584 * - otherwise it must be a valid looking ref.
587 st
= verify_refname(rs
[i
].src
, is_glob
);
588 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
590 } else if (!*rs
[i
].dst
) {
593 st
= verify_refname(rs
[i
].dst
, is_glob
);
594 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
604 * nr_refspec must be greater than zero and i must be valid
605 * since it is only possible to reach this point from within
606 * the for loop above.
608 free_refspecs(rs
, i
+1);
611 die("Invalid refspec '%s'", refspec
[i
]);
614 int valid_fetch_refspec(const char *fetch_refspec_str
)
616 const char *fetch_refspec
[] = { fetch_refspec_str
};
617 struct refspec
*refspec
;
619 refspec
= parse_refspec_internal(1, fetch_refspec
, 1, 1);
620 free_refspecs(refspec
, 1);
624 struct refspec
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
626 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
629 static struct refspec
*parse_push_refspec(int nr_refspec
, const char **refspec
)
631 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
634 static int valid_remote_nick(const char *name
)
636 if (!name
[0] || is_dot_or_dotdot(name
))
638 return !strchr(name
, '/'); /* no slash */
641 struct remote
*remote_get(const char *name
)
650 name
= default_remote_name
;
651 name_given
= explicit_default_remote_name
;
654 ret
= make_remote(name
, 0);
655 if (valid_remote_nick(name
)) {
657 read_remotes_file(ret
);
659 read_branches_file(ret
);
661 if (name_given
&& !ret
->url
)
662 add_url_alias(ret
, name
);
665 ret
->fetch
= parse_fetch_refspec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
666 ret
->push
= parse_push_refspec(ret
->push_refspec_nr
, ret
->push_refspec
);
670 int for_each_remote(each_remote_fn fn
, void *priv
)
674 for (i
= 0; i
< remotes_nr
&& !result
; i
++) {
675 struct remote
*r
= remotes
[i
];
679 r
->fetch
= parse_fetch_refspec(r
->fetch_refspec_nr
,
682 r
->push
= parse_push_refspec(r
->push_refspec_nr
,
684 result
= fn(r
, priv
);
689 void ref_remove_duplicates(struct ref
*ref_map
)
693 for (; ref_map
; ref_map
= ref_map
->next
) {
694 if (!ref_map
->peer_ref
)
696 posn
= &ref_map
->next
;
698 if ((*posn
)->peer_ref
&&
699 !strcmp((*posn
)->peer_ref
->name
,
700 ref_map
->peer_ref
->name
)) {
701 if (strcmp((*posn
)->name
, ref_map
->name
))
702 die("%s tracks both %s and %s",
703 ref_map
->peer_ref
->name
,
704 (*posn
)->name
, ref_map
->name
);
705 next
= (*posn
)->next
;
706 free((*posn
)->peer_ref
);
710 posn
= &(*posn
)->next
;
716 int remote_has_url(struct remote
*remote
, const char *url
)
719 for (i
= 0; i
< remote
->url_nr
; i
++) {
720 if (!strcmp(remote
->url
[i
], url
))
726 static int match_name_with_pattern(const char *key
, const char *name
,
727 const char *value
, char **result
)
729 const char *kstar
= strchr(key
, '*');
735 die("Key '%s' of pattern had no '*'", key
);
737 ksuffixlen
= strlen(kstar
+ 1);
738 namelen
= strlen(name
);
739 ret
= !strncmp(name
, key
, klen
) && namelen
>= klen
+ ksuffixlen
&&
740 !memcmp(name
+ namelen
- ksuffixlen
, kstar
+ 1, ksuffixlen
);
742 const char *vstar
= strchr(value
, '*');
746 die("Value '%s' of pattern has no '*'", value
);
747 vlen
= vstar
- value
;
748 vsuffixlen
= strlen(vstar
+ 1);
749 *result
= xmalloc(vlen
+ vsuffixlen
+
751 klen
- ksuffixlen
+ 1);
752 strncpy(*result
, value
, vlen
);
753 strncpy(*result
+ vlen
,
754 name
+ klen
, namelen
- klen
- ksuffixlen
);
755 strcpy(*result
+ vlen
+ namelen
- klen
- ksuffixlen
,
761 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
763 int find_src
= refspec
->src
== NULL
;
764 char *needle
, **result
;
769 return error("find_tracking: need either src or dst");
770 needle
= refspec
->dst
;
771 result
= &refspec
->src
;
773 needle
= refspec
->src
;
774 result
= &refspec
->dst
;
777 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
778 struct refspec
*fetch
= &remote
->fetch
[i
];
779 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
780 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
783 if (fetch
->pattern
) {
784 if (match_name_with_pattern(key
, needle
, value
, result
)) {
785 refspec
->force
= fetch
->force
;
788 } else if (!strcmp(needle
, key
)) {
789 *result
= xstrdup(value
);
790 refspec
->force
= fetch
->force
;
797 static struct ref
*alloc_ref_with_prefix(const char *prefix
, size_t prefixlen
,
800 size_t len
= strlen(name
);
801 struct ref
*ref
= xcalloc(1, sizeof(struct ref
) + prefixlen
+ len
+ 1);
802 memcpy(ref
->name
, prefix
, prefixlen
);
803 memcpy(ref
->name
+ prefixlen
, name
, len
);
807 struct ref
*alloc_ref(const char *name
)
809 return alloc_ref_with_prefix("", 0, name
);
812 static struct ref
*copy_ref(const struct ref
*ref
)
818 len
= strlen(ref
->name
);
819 cpy
= xmalloc(sizeof(struct ref
) + len
+ 1);
820 memcpy(cpy
, ref
, sizeof(struct ref
) + len
+ 1);
822 cpy
->symref
= ref
->symref
? xstrdup(ref
->symref
) : NULL
;
823 cpy
->remote_status
= ref
->remote_status
? xstrdup(ref
->remote_status
) : NULL
;
824 cpy
->peer_ref
= copy_ref(ref
->peer_ref
);
828 struct ref
*copy_ref_list(const struct ref
*ref
)
830 struct ref
*ret
= NULL
;
831 struct ref
**tail
= &ret
;
833 *tail
= copy_ref(ref
);
835 tail
= &((*tail
)->next
);
840 static void free_ref(struct ref
*ref
)
844 free_ref(ref
->peer_ref
);
845 free(ref
->remote_status
);
850 void free_refs(struct ref
*ref
)
860 static int count_refspec_match(const char *pattern
,
862 struct ref
**matched_ref
)
864 int patlen
= strlen(pattern
);
865 struct ref
*matched_weak
= NULL
;
866 struct ref
*matched
= NULL
;
870 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
871 char *name
= refs
->name
;
872 int namelen
= strlen(name
);
874 if (!refname_match(pattern
, name
, ref_rev_parse_rules
))
877 /* A match is "weak" if it is with refs outside
878 * heads or tags, and did not specify the pattern
879 * in full (e.g. "refs/remotes/origin/master") or at
880 * least from the toplevel (e.g. "remotes/origin/master");
881 * otherwise "git push $URL master" would result in
882 * ambiguity between remotes/origin/master and heads/master
883 * at the remote site.
885 if (namelen
!= patlen
&&
886 patlen
!= namelen
- 5 &&
887 prefixcmp(name
, "refs/heads/") &&
888 prefixcmp(name
, "refs/tags/")) {
889 /* We want to catch the case where only weak
890 * matches are found and there are multiple
891 * matches, and where more than one strong
892 * matches are found, as ambiguous. One
893 * strong match with zero or more weak matches
894 * are acceptable as a unique match.
905 *matched_ref
= matched_weak
;
909 *matched_ref
= matched
;
914 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
922 static struct ref
*try_explicit_object_name(const char *name
)
924 unsigned char sha1
[20];
928 ref
= alloc_ref("(delete)");
929 hashclr(ref
->new_sha1
);
932 if (get_sha1(name
, sha1
))
934 ref
= alloc_ref(name
);
935 hashcpy(ref
->new_sha1
, sha1
);
939 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
941 struct ref
*ret
= alloc_ref(name
);
942 tail_link_ref(ret
, tail
);
946 static char *guess_ref(const char *name
, struct ref
*peer
)
948 struct strbuf buf
= STRBUF_INIT
;
949 unsigned char sha1
[20];
951 const char *r
= resolve_ref(peer
->name
, sha1
, 1, NULL
);
955 if (!prefixcmp(r
, "refs/heads/"))
956 strbuf_addstr(&buf
, "refs/heads/");
957 else if (!prefixcmp(r
, "refs/tags/"))
958 strbuf_addstr(&buf
, "refs/tags/");
962 strbuf_addstr(&buf
, name
);
963 return strbuf_detach(&buf
, NULL
);
966 static int match_explicit(struct ref
*src
, struct ref
*dst
,
967 struct ref
***dst_tail
,
970 struct ref
*matched_src
, *matched_dst
;
973 const char *dst_value
= rs
->dst
;
976 if (rs
->pattern
|| rs
->matching
)
979 matched_src
= matched_dst
= NULL
;
980 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
985 /* The source could be in the get_sha1() format
986 * not a reference name. :refs/other is a
987 * way to delete 'other' ref at the remote end.
989 matched_src
= try_explicit_object_name(rs
->src
);
991 return error("src refspec %s does not match any.", rs
->src
);
995 return error("src refspec %s matches more than one.", rs
->src
);
999 unsigned char sha1
[20];
1002 dst_value
= resolve_ref(matched_src
->name
, sha1
, 1, &flag
);
1004 ((flag
& REF_ISSYMREF
) &&
1005 prefixcmp(dst_value
, "refs/heads/")))
1006 die("%s cannot be resolved to branch.",
1010 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
1014 if (!memcmp(dst_value
, "refs/", 5))
1015 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
1016 else if((dst_guess
= guess_ref(dst_value
, matched_src
)))
1017 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
1019 error("unable to push to unqualified destination: %s\n"
1020 "The destination refspec neither matches an "
1021 "existing ref on the remote nor\n"
1022 "begins with refs/, and we are unable to "
1023 "guess a prefix based on the source ref.",
1028 error("dst refspec %s matches more than one.",
1034 if (matched_dst
->peer_ref
)
1035 return error("dst ref %s receives from more than one src.",
1038 matched_dst
->peer_ref
= copy_src
? copy_ref(matched_src
) : matched_src
;
1039 matched_dst
->force
= rs
->force
;
1044 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
1045 struct ref
***dst_tail
, struct refspec
*rs
,
1049 for (i
= errs
= 0; i
< rs_nr
; i
++)
1050 errs
+= match_explicit(src
, dst
, dst_tail
, &rs
[i
]);
1054 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
1056 const struct ref
*src
)
1059 int matching_refs
= -1;
1060 for (i
= 0; i
< rs_nr
; i
++) {
1061 if (rs
[i
].matching
&&
1062 (matching_refs
== -1 || rs
[i
].force
)) {
1067 if (rs
[i
].pattern
&& match_name_with_pattern(rs
[i
].src
, src
->name
,
1071 if (matching_refs
!= -1)
1072 return rs
+ matching_refs
;
1078 * Note. This is used only by "push"; refspec matching rules for
1079 * push and fetch are subtly different, so do not try to reuse it
1082 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
1083 int nr_refspec
, const char **refspec
, int flags
)
1086 int send_all
= flags
& MATCH_REFS_ALL
;
1087 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
1089 static const char *default_refspec
[] = { ":", 0 };
1093 refspec
= default_refspec
;
1095 rs
= parse_push_refspec(nr_refspec
, (const char **) refspec
);
1096 errs
= match_explicit_refs(src
, dst
, dst_tail
, rs
, nr_refspec
);
1098 /* pick the remainder */
1099 for ( ; src
; src
= src
->next
) {
1100 struct ref
*dst_peer
;
1101 const struct refspec
*pat
= NULL
;
1106 pat
= check_pattern_match(rs
, nr_refspec
, src
);
1110 if (pat
->matching
) {
1112 * "matching refs"; traditionally we pushed everything
1113 * including refs outside refs/heads/ hierarchy, but
1114 * that does not make much sense these days.
1116 if (!send_mirror
&& prefixcmp(src
->name
, "refs/heads/"))
1118 dst_name
= xstrdup(src
->name
);
1121 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
1122 if (!match_name_with_pattern(pat
->src
, src
->name
,
1123 dst_side
, &dst_name
))
1124 die("Didn't think it matches any more");
1126 dst_peer
= find_ref_by_name(dst
, dst_name
);
1128 if (dst_peer
->peer_ref
)
1129 /* We're already sending something to this ref. */
1133 if (pat
->matching
&& !(send_all
|| send_mirror
))
1135 * Remote doesn't have it, and we have no
1136 * explicit pattern, and we don't have
1137 * --all nor --mirror.
1141 /* Create a new one and link it */
1142 dst_peer
= make_linked_ref(dst_name
, dst_tail
);
1143 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
1145 dst_peer
->peer_ref
= copy_ref(src
);
1146 dst_peer
->force
= pat
->force
;
1155 struct branch
*branch_get(const char *name
)
1160 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1161 ret
= current_branch
;
1163 ret
= make_branch(name
, 0);
1164 if (ret
&& ret
->remote_name
) {
1165 ret
->remote
= remote_get(ret
->remote_name
);
1166 if (ret
->merge_nr
) {
1168 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
1170 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1171 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1172 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1173 remote_find_tracking(ret
->remote
,
1181 int branch_has_merge_config(struct branch
*branch
)
1183 return branch
&& !!branch
->merge
;
1186 int branch_merge_matches(struct branch
*branch
,
1188 const char *refname
)
1190 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1192 return refname_match(branch
->merge
[i
]->src
, refname
, ref_fetch_rules
);
1195 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
1196 const struct refspec
*refspec
)
1198 const struct ref
*ref
;
1199 struct ref
*ret
= NULL
;
1200 struct ref
**tail
= &ret
;
1204 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1205 if (strchr(ref
->name
, '^'))
1206 continue; /* a dereference item */
1207 if (match_name_with_pattern(refspec
->src
, ref
->name
,
1208 refspec
->dst
, &expn_name
)) {
1209 struct ref
*cpy
= copy_ref(ref
);
1211 cpy
->peer_ref
= alloc_ref(expn_name
);
1214 cpy
->peer_ref
->force
= 1;
1223 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
1225 const struct ref
*ref
;
1226 for (ref
= refs
; ref
; ref
= ref
->next
) {
1227 if (refname_match(name
, ref
->name
, ref_fetch_rules
))
1233 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
1235 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
1240 return copy_ref(ref
);
1243 static struct ref
*get_local_ref(const char *name
)
1248 if (!prefixcmp(name
, "refs/"))
1249 return alloc_ref(name
);
1251 if (!prefixcmp(name
, "heads/") ||
1252 !prefixcmp(name
, "tags/") ||
1253 !prefixcmp(name
, "remotes/"))
1254 return alloc_ref_with_prefix("refs/", 5, name
);
1256 return alloc_ref_with_prefix("refs/heads/", 11, name
);
1259 int get_fetch_map(const struct ref
*remote_refs
,
1260 const struct refspec
*refspec
,
1264 struct ref
*ref_map
, **rmp
;
1266 if (refspec
->pattern
) {
1267 ref_map
= get_expanded_map(remote_refs
, refspec
);
1269 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
1271 ref_map
= get_remote_ref(remote_refs
, name
);
1272 if (!missing_ok
&& !ref_map
)
1273 die("Couldn't find remote ref %s", name
);
1275 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
1276 if (ref_map
->peer_ref
&& refspec
->force
)
1277 ref_map
->peer_ref
->force
= 1;
1281 for (rmp
= &ref_map
; *rmp
; ) {
1282 if ((*rmp
)->peer_ref
) {
1283 int st
= check_ref_format((*rmp
)->peer_ref
->name
+ 5);
1284 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
) {
1285 struct ref
*ignore
= *rmp
;
1286 error("* Ignoring funny ref '%s' locally",
1287 (*rmp
)->peer_ref
->name
);
1288 *rmp
= (*rmp
)->next
;
1289 free(ignore
->peer_ref
);
1294 rmp
= &((*rmp
)->next
);
1298 tail_link_ref(ref_map
, tail
);
1303 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
1307 for (; list
; list
= list
->next
)
1308 if (!strcmp(ref
->symref
, list
->name
)) {
1309 hashcpy(ref
->old_sha1
, list
->old_sha1
);
1315 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
1318 struct commit_list
*temp
= list
;
1319 temp
->item
->object
.flags
&= ~mark
;
1325 int ref_newer(const unsigned char *new_sha1
, const unsigned char *old_sha1
)
1328 struct commit
*old
, *new;
1329 struct commit_list
*list
, *used
;
1332 /* Both new and old must be commit-ish and new is descendant of
1333 * old. Otherwise we require --force.
1335 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
1336 if (!o
|| o
->type
!= OBJ_COMMIT
)
1338 old
= (struct commit
*) o
;
1340 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
1341 if (!o
|| o
->type
!= OBJ_COMMIT
)
1343 new = (struct commit
*) o
;
1345 if (parse_commit(new) < 0)
1349 commit_list_insert(new, &list
);
1351 new = pop_most_recent_commit(&list
, TMP_MARK
);
1352 commit_list_insert(new, &used
);
1358 unmark_and_free(list
, TMP_MARK
);
1359 unmark_and_free(used
, TMP_MARK
);
1364 * Return true if there is anything to report, otherwise false.
1366 int stat_tracking_info(struct branch
*branch
, int *num_ours
, int *num_theirs
)
1368 unsigned char sha1
[20];
1369 struct commit
*ours
, *theirs
;
1371 struct rev_info revs
;
1372 const char *rev_argv
[10], *base
;
1376 * Nothing to report unless we are marked to build on top of
1380 !branch
->merge
|| !branch
->merge
[0] || !branch
->merge
[0]->dst
)
1384 * If what we used to build on no longer exists, there is
1385 * nothing to report.
1387 base
= branch
->merge
[0]->dst
;
1388 if (!resolve_ref(base
, sha1
, 1, NULL
))
1390 theirs
= lookup_commit(sha1
);
1394 if (!resolve_ref(branch
->refname
, sha1
, 1, NULL
))
1396 ours
= lookup_commit(sha1
);
1400 /* are we the same? */
1404 /* Run "rev-list --left-right ours...theirs" internally... */
1406 rev_argv
[rev_argc
++] = NULL
;
1407 rev_argv
[rev_argc
++] = "--left-right";
1408 rev_argv
[rev_argc
++] = symmetric
;
1409 rev_argv
[rev_argc
++] = "--";
1410 rev_argv
[rev_argc
] = NULL
;
1412 strcpy(symmetric
, sha1_to_hex(ours
->object
.sha1
));
1413 strcpy(symmetric
+ 40, "...");
1414 strcpy(symmetric
+ 43, sha1_to_hex(theirs
->object
.sha1
));
1416 init_revisions(&revs
, NULL
);
1417 setup_revisions(rev_argc
, rev_argv
, &revs
, NULL
);
1418 prepare_revision_walk(&revs
);
1420 /* ... and count the commits on each side. */
1424 struct commit
*c
= get_revision(&revs
);
1427 if (c
->object
.flags
& SYMMETRIC_LEFT
)
1433 /* clear object flags smudged by the above traversal */
1434 clear_commit_marks(ours
, ALL_REV_FLAGS
);
1435 clear_commit_marks(theirs
, ALL_REV_FLAGS
);
1440 * Return true when there is anything to report, otherwise false.
1442 int format_tracking_info(struct branch
*branch
, struct strbuf
*sb
)
1444 int num_ours
, num_theirs
;
1447 if (!stat_tracking_info(branch
, &num_ours
, &num_theirs
))
1450 base
= branch
->merge
[0]->dst
;
1451 if (!prefixcmp(base
, "refs/remotes/")) {
1452 base
+= strlen("refs/remotes/");
1455 strbuf_addf(sb
, "Your branch is ahead of '%s' "
1456 "by %d commit%s.\n",
1457 base
, num_ours
, (num_ours
== 1) ? "" : "s");
1459 strbuf_addf(sb
, "Your branch is behind '%s' "
1461 "and can be fast-forwarded.\n",
1462 base
, num_theirs
, (num_theirs
== 1) ? "" : "s");
1464 strbuf_addf(sb
, "Your branch and '%s' have diverged,\n"
1465 "and have %d and %d different commit(s) each, "
1467 base
, num_ours
, num_theirs
);
1471 static int one_local_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
1473 struct ref
***local_tail
= cb_data
;
1477 /* we already know it starts with refs/ to get here */
1478 if (check_ref_format(refname
+ 5))
1481 len
= strlen(refname
) + 1;
1482 ref
= xcalloc(1, sizeof(*ref
) + len
);
1483 hashcpy(ref
->new_sha1
, sha1
);
1484 memcpy(ref
->name
, refname
, len
);
1486 *local_tail
= &ref
->next
;
1490 struct ref
*get_local_heads(void)
1492 struct ref
*local_refs
, **local_tail
= &local_refs
;
1493 for_each_ref(one_local_ref
, &local_tail
);
1497 struct ref
*guess_remote_head(const struct ref
*head
,
1498 const struct ref
*refs
,
1501 const struct ref
*r
;
1502 struct ref
*list
= NULL
;
1503 struct ref
**tail
= &list
;
1509 * Some transports support directly peeking at
1510 * where HEAD points; if that is the case, then
1511 * we don't have to guess.
1514 return copy_ref(find_ref_by_name(refs
, head
->symref
));
1516 /* If refs/heads/master could be right, it is. */
1518 r
= find_ref_by_name(refs
, "refs/heads/master");
1519 if (r
&& !hashcmp(r
->old_sha1
, head
->old_sha1
))
1523 /* Look for another ref that points there */
1524 for (r
= refs
; r
; r
= r
->next
) {
1525 if (r
!= head
&& !hashcmp(r
->old_sha1
, head
->old_sha1
)) {
1526 *tail
= copy_ref(r
);
1527 tail
= &((*tail
)->next
);