8 static struct refspec s_tag_refspec
= {
16 const struct refspec
*tag_refspec
= &s_tag_refspec
;
18 struct counted_string
{
25 struct counted_string
*instead_of
;
30 static struct remote
**remotes
;
31 static int remotes_alloc
;
32 static int remotes_nr
;
34 static struct branch
**branches
;
35 static int branches_alloc
;
36 static int branches_nr
;
38 static struct branch
*current_branch
;
39 static const char *default_remote_name
;
41 static struct rewrite
**rewrite
;
42 static int rewrite_alloc
;
43 static int rewrite_nr
;
45 #define BUF_SIZE (2048)
46 static char buffer
[BUF_SIZE
];
48 static const char *alias_url(const char *url
)
52 struct counted_string
*longest
;
57 for (i
= 0; i
< rewrite_nr
; i
++) {
60 for (j
= 0; j
< rewrite
[i
]->instead_of_nr
; j
++) {
61 if (!prefixcmp(url
, rewrite
[i
]->instead_of
[j
].s
) &&
63 longest
->len
< rewrite
[i
]->instead_of
[j
].len
)) {
64 longest
= &(rewrite
[i
]->instead_of
[j
]);
72 ret
= malloc(rewrite
[longest_i
]->baselen
+
73 (strlen(url
) - longest
->len
) + 1);
74 strcpy(ret
, rewrite
[longest_i
]->base
);
75 strcpy(ret
+ rewrite
[longest_i
]->baselen
, url
+ longest
->len
);
79 static void add_push_refspec(struct remote
*remote
, const char *ref
)
81 ALLOC_GROW(remote
->push_refspec
,
82 remote
->push_refspec_nr
+ 1,
83 remote
->push_refspec_alloc
);
84 remote
->push_refspec
[remote
->push_refspec_nr
++] = ref
;
87 static void add_fetch_refspec(struct remote
*remote
, const char *ref
)
89 ALLOC_GROW(remote
->fetch_refspec
,
90 remote
->fetch_refspec_nr
+ 1,
91 remote
->fetch_refspec_alloc
);
92 remote
->fetch_refspec
[remote
->fetch_refspec_nr
++] = ref
;
95 static void add_url(struct remote
*remote
, const char *url
)
97 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
98 remote
->url
[remote
->url_nr
++] = url
;
101 static void add_url_alias(struct remote
*remote
, const char *url
)
103 add_url(remote
, alias_url(url
));
106 static struct remote
*make_remote(const char *name
, int len
)
111 for (i
= 0; i
< remotes_nr
; i
++) {
112 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
113 !remotes
[i
]->name
[len
]) :
114 !strcmp(name
, remotes
[i
]->name
))
118 ret
= xcalloc(1, sizeof(struct remote
));
119 ALLOC_GROW(remotes
, remotes_nr
+ 1, remotes_alloc
);
120 remotes
[remotes_nr
++] = ret
;
122 ret
->name
= xstrndup(name
, len
);
124 ret
->name
= xstrdup(name
);
128 static void add_merge(struct branch
*branch
, const char *name
)
130 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
131 branch
->merge_alloc
);
132 branch
->merge_name
[branch
->merge_nr
++] = name
;
135 static struct branch
*make_branch(const char *name
, int len
)
141 for (i
= 0; i
< branches_nr
; i
++) {
142 if (len
? (!strncmp(name
, branches
[i
]->name
, len
) &&
143 !branches
[i
]->name
[len
]) :
144 !strcmp(name
, branches
[i
]->name
))
148 ALLOC_GROW(branches
, branches_nr
+ 1, branches_alloc
);
149 ret
= xcalloc(1, sizeof(struct branch
));
150 branches
[branches_nr
++] = ret
;
152 ret
->name
= xstrndup(name
, len
);
154 ret
->name
= xstrdup(name
);
155 refname
= malloc(strlen(name
) + strlen("refs/heads/") + 1);
156 strcpy(refname
, "refs/heads/");
157 strcpy(refname
+ strlen("refs/heads/"), ret
->name
);
158 ret
->refname
= refname
;
163 static struct rewrite
*make_rewrite(const char *base
, int len
)
168 for (i
= 0; i
< rewrite_nr
; i
++) {
170 ? (len
== rewrite
[i
]->baselen
&&
171 !strncmp(base
, rewrite
[i
]->base
, len
))
172 : !strcmp(base
, rewrite
[i
]->base
))
176 ALLOC_GROW(rewrite
, rewrite_nr
+ 1, rewrite_alloc
);
177 ret
= xcalloc(1, sizeof(struct rewrite
));
178 rewrite
[rewrite_nr
++] = ret
;
180 ret
->base
= xstrndup(base
, len
);
184 ret
->base
= xstrdup(base
);
185 ret
->baselen
= strlen(base
);
190 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
192 ALLOC_GROW(rewrite
->instead_of
, rewrite
->instead_of_nr
+ 1, rewrite
->instead_of_alloc
);
193 rewrite
->instead_of
[rewrite
->instead_of_nr
].s
= instead_of
;
194 rewrite
->instead_of
[rewrite
->instead_of_nr
].len
= strlen(instead_of
);
195 rewrite
->instead_of_nr
++;
198 static void read_remotes_file(struct remote
*remote
)
200 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
204 while (fgets(buffer
, BUF_SIZE
, f
)) {
208 if (!prefixcmp(buffer
, "URL:")) {
211 } else if (!prefixcmp(buffer
, "Push:")) {
214 } else if (!prefixcmp(buffer
, "Pull:")) {
226 while (isspace(p
[-1]))
229 switch (value_list
) {
231 add_url_alias(remote
, xstrdup(s
));
234 add_push_refspec(remote
, xstrdup(s
));
237 add_fetch_refspec(remote
, xstrdup(s
));
244 static void read_branches_file(struct remote
*remote
)
246 const char *slash
= strchr(remote
->name
, '/');
248 struct strbuf branch
;
249 int n
= slash
? slash
- remote
->name
: 1000;
250 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
256 s
= fgets(buffer
, BUF_SIZE
, f
);
265 while (isspace(p
[-1]))
269 len
+= strlen(slash
);
270 p
= xmalloc(len
+ 1);
276 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
277 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
278 * the partial URL obtained from the branches file plus
279 * "/netdev-2.6" and does not store it in any tracking ref.
280 * #branch specifier in the file is ignored.
282 * Otherwise, the branches file would have URL and optionally
283 * #branch specified. The "master" (or specified) branch is
284 * fetched and stored in the local branch of the same name.
286 strbuf_init(&branch
, 0);
287 frag
= strchr(p
, '#');
290 strbuf_addf(&branch
, "refs/heads/%s", frag
);
292 strbuf_addstr(&branch
, "refs/heads/master");
294 strbuf_addf(&branch
, ":refs/heads/%s", remote
->name
);
296 strbuf_reset(&branch
);
297 strbuf_addstr(&branch
, "HEAD:");
299 add_url_alias(remote
, p
);
300 add_fetch_refspec(remote
, strbuf_detach(&branch
, 0));
301 remote
->fetch_tags
= 1; /* always auto-follow */
304 static int handle_config(const char *key
, const char *value
, void *cb
)
308 struct remote
*remote
;
309 struct branch
*branch
;
310 if (!prefixcmp(key
, "branch.")) {
312 subkey
= strrchr(name
, '.');
315 branch
= make_branch(name
, subkey
- name
);
316 if (!strcmp(subkey
, ".remote")) {
318 return config_error_nonbool(key
);
319 branch
->remote_name
= xstrdup(value
);
320 if (branch
== current_branch
)
321 default_remote_name
= branch
->remote_name
;
322 } else if (!strcmp(subkey
, ".merge")) {
324 return config_error_nonbool(key
);
325 add_merge(branch
, xstrdup(value
));
329 if (!prefixcmp(key
, "url.")) {
330 struct rewrite
*rewrite
;
332 subkey
= strrchr(name
, '.');
335 rewrite
= make_rewrite(name
, subkey
- name
);
336 if (!strcmp(subkey
, ".insteadof")) {
338 return config_error_nonbool(key
);
339 add_instead_of(rewrite
, xstrdup(value
));
342 if (prefixcmp(key
, "remote."))
345 subkey
= strrchr(name
, '.');
347 return error("Config with no key for remote %s", name
);
348 if (*subkey
== '/') {
349 warning("Config remote shorthand cannot begin with '/': %s", name
);
352 remote
= make_remote(name
, subkey
- name
);
353 if (!strcmp(subkey
, ".mirror"))
354 remote
->mirror
= git_config_bool(key
, value
);
355 else if (!strcmp(subkey
, ".skipdefaultupdate"))
356 remote
->skip_default_update
= git_config_bool(key
, value
);
358 else if (!strcmp(subkey
, ".url")) {
360 if (git_config_string(&v
, key
, value
))
363 } else if (!strcmp(subkey
, ".push")) {
365 if (git_config_string(&v
, key
, value
))
367 add_push_refspec(remote
, v
);
368 } else if (!strcmp(subkey
, ".fetch")) {
370 if (git_config_string(&v
, key
, value
))
372 add_fetch_refspec(remote
, v
);
373 } else if (!strcmp(subkey
, ".receivepack")) {
375 if (git_config_string(&v
, key
, value
))
377 if (!remote
->receivepack
)
378 remote
->receivepack
= v
;
380 error("more than one receivepack given, using the first");
381 } else if (!strcmp(subkey
, ".uploadpack")) {
383 if (git_config_string(&v
, key
, value
))
385 if (!remote
->uploadpack
)
386 remote
->uploadpack
= v
;
388 error("more than one uploadpack given, using the first");
389 } else if (!strcmp(subkey
, ".tagopt")) {
390 if (!strcmp(value
, "--no-tags"))
391 remote
->fetch_tags
= -1;
392 } else if (!strcmp(subkey
, ".proxy")) {
393 return git_config_string((const char **)&remote
->http_proxy
,
399 static void alias_all_urls(void)
402 for (i
= 0; i
< remotes_nr
; i
++) {
405 for (j
= 0; j
< remotes
[i
]->url_nr
; j
++) {
406 remotes
[i
]->url
[j
] = alias_url(remotes
[i
]->url
[j
]);
411 static void read_config(void)
413 unsigned char sha1
[20];
414 const char *head_ref
;
416 if (default_remote_name
) // did this already
418 default_remote_name
= xstrdup("origin");
419 current_branch
= NULL
;
420 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
421 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
422 !prefixcmp(head_ref
, "refs/heads/")) {
424 make_branch(head_ref
+ strlen("refs/heads/"), 0);
426 git_config(handle_config
, NULL
);
431 * We need to make sure the tracking branches are well formed, but a
432 * wildcard refspec in "struct refspec" must have a trailing slash. We
433 * temporarily drop the trailing '/' while calling check_ref_format(),
434 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
435 * error return is Ok for a wildcard refspec.
437 static int verify_refname(char *name
, int is_glob
)
439 int result
, len
= -1;
443 assert(name
[len
- 1] == '/');
444 name
[len
- 1] = '\0';
446 result
= check_ref_format(name
);
452 static struct refspec
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
456 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
458 for (i
= 0; i
< nr_refspec
; i
++) {
461 const char *lhs
, *rhs
;
471 rhs
= strrchr(lhs
, ':');
474 * Before going on, special case ":" (or "+:") as a refspec
477 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
483 size_t rlen
= strlen(++rhs
);
484 is_glob
= (2 <= rlen
&& !strcmp(rhs
+ rlen
- 2, "/*"));
485 rs
[i
].dst
= xstrndup(rhs
, rlen
- is_glob
);
488 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
489 if (2 <= llen
&& !memcmp(lhs
+ llen
- 2, "/*", 2)) {
490 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
494 } else if (rhs
&& is_glob
) {
498 rs
[i
].pattern
= is_glob
;
499 rs
[i
].src
= xstrndup(lhs
, llen
);
504 * - empty is allowed; it means HEAD.
505 * - otherwise it must be a valid looking ref.
510 st
= verify_refname(rs
[i
].src
, is_glob
);
511 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
516 * - missing is ok, and is same as empty.
517 * - empty is ok; it means not to store.
518 * - otherwise it must be a valid looking ref.
522 } else if (!*rs
[i
].dst
) {
525 st
= verify_refname(rs
[i
].dst
, is_glob
);
526 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
532 * - empty is allowed; it means delete.
533 * - when wildcarded, it must be a valid looking ref.
534 * - otherwise, it must be an extended SHA-1, but
535 * there is no existing way to validate this.
540 st
= verify_refname(rs
[i
].src
, is_glob
);
541 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
545 ; /* anything goes, for now */
548 * - missing is allowed, but LHS then must be a
550 * - empty is not allowed.
551 * - otherwise it must be a valid looking ref.
554 st
= verify_refname(rs
[i
].src
, is_glob
);
555 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
557 } else if (!*rs
[i
].dst
) {
560 st
= verify_refname(rs
[i
].dst
, is_glob
);
561 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
573 die("Invalid refspec '%s'", refspec
[i
]);
576 int valid_fetch_refspec(const char *fetch_refspec_str
)
578 const char *fetch_refspec
[] = { fetch_refspec_str
};
579 struct refspec
*refspec
;
581 refspec
= parse_refspec_internal(1, fetch_refspec
, 1, 1);
586 struct refspec
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
588 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
591 struct refspec
*parse_push_refspec(int nr_refspec
, const char **refspec
)
593 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
596 static int valid_remote_nick(const char *name
)
598 if (!name
[0] || /* not empty */
599 (name
[0] == '.' && /* not "." */
600 (!name
[1] || /* not ".." */
601 (name
[1] == '.' && !name
[2]))))
603 return !strchr(name
, '/'); /* no slash */
606 struct remote
*remote_get(const char *name
)
612 name
= default_remote_name
;
613 ret
= make_remote(name
, 0);
614 if (valid_remote_nick(name
)) {
616 read_remotes_file(ret
);
618 read_branches_file(ret
);
621 add_url_alias(ret
, name
);
624 ret
->fetch
= parse_fetch_refspec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
625 ret
->push
= parse_push_refspec(ret
->push_refspec_nr
, ret
->push_refspec
);
629 int for_each_remote(each_remote_fn fn
, void *priv
)
633 for (i
= 0; i
< remotes_nr
&& !result
; i
++) {
634 struct remote
*r
= remotes
[i
];
638 r
->fetch
= parse_fetch_refspec(r
->fetch_refspec_nr
,
641 r
->push
= parse_push_refspec(r
->push_refspec_nr
,
643 result
= fn(r
, priv
);
648 void ref_remove_duplicates(struct ref
*ref_map
)
652 for (; ref_map
; ref_map
= ref_map
->next
) {
653 if (!ref_map
->peer_ref
)
655 posn
= &ref_map
->next
;
657 if ((*posn
)->peer_ref
&&
658 !strcmp((*posn
)->peer_ref
->name
,
659 ref_map
->peer_ref
->name
)) {
660 if (strcmp((*posn
)->name
, ref_map
->name
))
661 die("%s tracks both %s and %s",
662 ref_map
->peer_ref
->name
,
663 (*posn
)->name
, ref_map
->name
);
664 next
= (*posn
)->next
;
665 free((*posn
)->peer_ref
);
669 posn
= &(*posn
)->next
;
675 int remote_has_url(struct remote
*remote
, const char *url
)
678 for (i
= 0; i
< remote
->url_nr
; i
++) {
679 if (!strcmp(remote
->url
[i
], url
))
685 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
687 int find_src
= refspec
->src
== NULL
;
688 char *needle
, **result
;
693 return error("find_tracking: need either src or dst");
694 needle
= refspec
->dst
;
695 result
= &refspec
->src
;
697 needle
= refspec
->src
;
698 result
= &refspec
->dst
;
701 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
702 struct refspec
*fetch
= &remote
->fetch
[i
];
703 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
704 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
707 if (fetch
->pattern
) {
708 if (!prefixcmp(needle
, key
)) {
709 *result
= xmalloc(strlen(value
) +
712 strcpy(*result
, value
);
713 strcpy(*result
+ strlen(value
),
714 needle
+ strlen(key
));
715 refspec
->force
= fetch
->force
;
718 } else if (!strcmp(needle
, key
)) {
719 *result
= xstrdup(value
);
720 refspec
->force
= fetch
->force
;
727 struct ref
*alloc_ref(unsigned namelen
)
729 struct ref
*ret
= xmalloc(sizeof(struct ref
) + namelen
);
730 memset(ret
, 0, sizeof(struct ref
) + namelen
);
734 struct ref
*alloc_ref_from_str(const char* str
)
736 struct ref
*ret
= alloc_ref(strlen(str
) + 1);
737 strcpy(ret
->name
, str
);
741 static struct ref
*copy_ref(const struct ref
*ref
)
743 struct ref
*ret
= xmalloc(sizeof(struct ref
) + strlen(ref
->name
) + 1);
744 memcpy(ret
, ref
, sizeof(struct ref
) + strlen(ref
->name
) + 1);
749 struct ref
*copy_ref_list(const struct ref
*ref
)
751 struct ref
*ret
= NULL
;
752 struct ref
**tail
= &ret
;
754 *tail
= copy_ref(ref
);
756 tail
= &((*tail
)->next
);
761 void free_ref(struct ref
*ref
)
765 free(ref
->remote_status
);
770 void free_refs(struct ref
*ref
)
781 static int count_refspec_match(const char *pattern
,
783 struct ref
**matched_ref
)
785 int patlen
= strlen(pattern
);
786 struct ref
*matched_weak
= NULL
;
787 struct ref
*matched
= NULL
;
791 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
792 char *name
= refs
->name
;
793 int namelen
= strlen(name
);
795 if (!refname_match(pattern
, name
, ref_rev_parse_rules
))
798 /* A match is "weak" if it is with refs outside
799 * heads or tags, and did not specify the pattern
800 * in full (e.g. "refs/remotes/origin/master") or at
801 * least from the toplevel (e.g. "remotes/origin/master");
802 * otherwise "git push $URL master" would result in
803 * ambiguity between remotes/origin/master and heads/master
804 * at the remote site.
806 if (namelen
!= patlen
&&
807 patlen
!= namelen
- 5 &&
808 prefixcmp(name
, "refs/heads/") &&
809 prefixcmp(name
, "refs/tags/")) {
810 /* We want to catch the case where only weak
811 * matches are found and there are multiple
812 * matches, and where more than one strong
813 * matches are found, as ambiguous. One
814 * strong match with zero or more weak matches
815 * are acceptable as a unique match.
826 *matched_ref
= matched_weak
;
830 *matched_ref
= matched
;
835 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
843 static struct ref
*try_explicit_object_name(const char *name
)
845 unsigned char sha1
[20];
850 strcpy(ref
->name
, "(delete)");
851 hashclr(ref
->new_sha1
);
854 if (get_sha1(name
, sha1
))
856 ref
= alloc_ref_from_str(name
);
857 hashcpy(ref
->new_sha1
, sha1
);
861 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
863 struct ref
*ret
= alloc_ref_from_str(name
);
864 tail_link_ref(ret
, tail
);
868 static char *guess_ref(const char *name
, struct ref
*peer
)
870 struct strbuf buf
= STRBUF_INIT
;
871 unsigned char sha1
[20];
873 const char *r
= resolve_ref(peer
->name
, sha1
, 1, NULL
);
877 if (!prefixcmp(r
, "refs/heads/"))
878 strbuf_addstr(&buf
, "refs/heads/");
879 else if (!prefixcmp(r
, "refs/tags/"))
880 strbuf_addstr(&buf
, "refs/tags/");
884 strbuf_addstr(&buf
, name
);
885 return strbuf_detach(&buf
, NULL
);
888 static int match_explicit(struct ref
*src
, struct ref
*dst
,
889 struct ref
***dst_tail
,
892 struct ref
*matched_src
, *matched_dst
;
894 const char *dst_value
= rs
->dst
;
897 if (rs
->pattern
|| rs
->matching
)
900 matched_src
= matched_dst
= NULL
;
901 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
905 /* The source could be in the get_sha1() format
906 * not a reference name. :refs/other is a
907 * way to delete 'other' ref at the remote end.
909 matched_src
= try_explicit_object_name(rs
->src
);
911 return error("src refspec %s does not match any.", rs
->src
);
914 return error("src refspec %s matches more than one.", rs
->src
);
918 unsigned char sha1
[20];
921 dst_value
= resolve_ref(matched_src
->name
, sha1
, 1, &flag
);
923 ((flag
& REF_ISSYMREF
) &&
924 prefixcmp(dst_value
, "refs/heads/")))
925 die("%s cannot be resolved to branch.",
929 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
933 if (!memcmp(dst_value
, "refs/", 5))
934 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
935 else if((dst_guess
= guess_ref(dst_value
, matched_src
)))
936 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
938 error("unable to push to unqualified destination: %s\n"
939 "The destination refspec neither matches an "
940 "existing ref on the remote nor\n"
941 "begins with refs/, and we are unable to "
942 "guess a prefix based on the source ref.",
947 error("dst refspec %s matches more than one.",
953 if (matched_dst
->peer_ref
)
954 return error("dst ref %s receives from more than one src.",
957 matched_dst
->peer_ref
= matched_src
;
958 matched_dst
->force
= rs
->force
;
963 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
964 struct ref
***dst_tail
, struct refspec
*rs
,
968 for (i
= errs
= 0; i
< rs_nr
; i
++)
969 errs
+= match_explicit(src
, dst
, dst_tail
, &rs
[i
]);
973 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
975 const struct ref
*src
)
978 int matching_refs
= -1;
979 for (i
= 0; i
< rs_nr
; i
++) {
980 if (rs
[i
].matching
&&
981 (matching_refs
== -1 || rs
[i
].force
)) {
986 if (rs
[i
].pattern
&& !prefixcmp(src
->name
, rs
[i
].src
))
989 if (matching_refs
!= -1)
990 return rs
+ matching_refs
;
996 * Note. This is used only by "push"; refspec matching rules for
997 * push and fetch are subtly different, so do not try to reuse it
1000 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
1001 int nr_refspec
, const char **refspec
, int flags
)
1004 int send_all
= flags
& MATCH_REFS_ALL
;
1005 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
1006 static const char *default_refspec
[] = { ":", 0 };
1010 refspec
= default_refspec
;
1012 rs
= parse_push_refspec(nr_refspec
, (const char **) refspec
);
1013 if (match_explicit_refs(src
, dst
, dst_tail
, rs
, nr_refspec
))
1016 /* pick the remainder */
1017 for ( ; src
; src
= src
->next
) {
1018 struct ref
*dst_peer
;
1019 const struct refspec
*pat
= NULL
;
1024 pat
= check_pattern_match(rs
, nr_refspec
, src
);
1028 if (pat
->matching
) {
1030 * "matching refs"; traditionally we pushed everything
1031 * including refs outside refs/heads/ hierarchy, but
1032 * that does not make much sense these days.
1034 if (!send_mirror
&& prefixcmp(src
->name
, "refs/heads/"))
1036 dst_name
= xstrdup(src
->name
);
1039 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
1040 dst_name
= xmalloc(strlen(dst_side
) +
1042 strlen(pat
->src
) + 2);
1043 strcpy(dst_name
, dst_side
);
1044 strcat(dst_name
, src
->name
+ strlen(pat
->src
));
1046 dst_peer
= find_ref_by_name(dst
, dst_name
);
1048 if (dst_peer
->peer_ref
)
1049 /* We're already sending something to this ref. */
1053 if (pat
->matching
&& !(send_all
|| send_mirror
))
1055 * Remote doesn't have it, and we have no
1056 * explicit pattern, and we don't have
1057 * --all nor --mirror.
1061 /* Create a new one and link it */
1062 dst_peer
= make_linked_ref(dst_name
, dst_tail
);
1063 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
1065 dst_peer
->peer_ref
= src
;
1066 dst_peer
->force
= pat
->force
;
1073 struct branch
*branch_get(const char *name
)
1078 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1079 ret
= current_branch
;
1081 ret
= make_branch(name
, 0);
1082 if (ret
&& ret
->remote_name
) {
1083 ret
->remote
= remote_get(ret
->remote_name
);
1084 if (ret
->merge_nr
) {
1086 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
1088 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1089 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1090 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1091 remote_find_tracking(ret
->remote
,
1099 int branch_has_merge_config(struct branch
*branch
)
1101 return branch
&& !!branch
->merge
;
1104 int branch_merge_matches(struct branch
*branch
,
1106 const char *refname
)
1108 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1110 return refname_match(branch
->merge
[i
]->src
, refname
, ref_fetch_rules
);
1113 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
1114 const struct refspec
*refspec
)
1116 const struct ref
*ref
;
1117 struct ref
*ret
= NULL
;
1118 struct ref
**tail
= &ret
;
1120 int remote_prefix_len
= strlen(refspec
->src
);
1121 int local_prefix_len
= strlen(refspec
->dst
);
1123 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1124 if (strchr(ref
->name
, '^'))
1125 continue; /* a dereference item */
1126 if (!prefixcmp(ref
->name
, refspec
->src
)) {
1128 struct ref
*cpy
= copy_ref(ref
);
1129 match
= ref
->name
+ remote_prefix_len
;
1131 cpy
->peer_ref
= alloc_ref(local_prefix_len
+
1133 sprintf(cpy
->peer_ref
->name
, "%s%s",
1134 refspec
->dst
, match
);
1136 cpy
->peer_ref
->force
= 1;
1145 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
1147 const struct ref
*ref
;
1148 for (ref
= refs
; ref
; ref
= ref
->next
) {
1149 if (refname_match(name
, ref
->name
, ref_fetch_rules
))
1155 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
1157 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
1162 return copy_ref(ref
);
1165 static struct ref
*get_local_ref(const char *name
)
1171 if (!prefixcmp(name
, "refs/")) {
1172 return alloc_ref_from_str(name
);
1175 if (!prefixcmp(name
, "heads/") ||
1176 !prefixcmp(name
, "tags/") ||
1177 !prefixcmp(name
, "remotes/")) {
1178 ret
= alloc_ref(strlen(name
) + 6);
1179 sprintf(ret
->name
, "refs/%s", name
);
1183 ret
= alloc_ref(strlen(name
) + 12);
1184 sprintf(ret
->name
, "refs/heads/%s", name
);
1188 int get_fetch_map(const struct ref
*remote_refs
,
1189 const struct refspec
*refspec
,
1193 struct ref
*ref_map
, **rmp
;
1195 if (refspec
->pattern
) {
1196 ref_map
= get_expanded_map(remote_refs
, refspec
);
1198 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
1200 ref_map
= get_remote_ref(remote_refs
, name
);
1201 if (!missing_ok
&& !ref_map
)
1202 die("Couldn't find remote ref %s", name
);
1204 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
1205 if (ref_map
->peer_ref
&& refspec
->force
)
1206 ref_map
->peer_ref
->force
= 1;
1210 for (rmp
= &ref_map
; *rmp
; ) {
1211 if ((*rmp
)->peer_ref
) {
1212 int st
= check_ref_format((*rmp
)->peer_ref
->name
+ 5);
1213 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
) {
1214 struct ref
*ignore
= *rmp
;
1215 error("* Ignoring funny ref '%s' locally",
1216 (*rmp
)->peer_ref
->name
);
1217 *rmp
= (*rmp
)->next
;
1218 free(ignore
->peer_ref
);
1223 rmp
= &((*rmp
)->next
);
1227 tail_link_ref(ref_map
, tail
);
1232 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
1236 for (; list
; list
= list
->next
)
1237 if (!strcmp(ref
->symref
, list
->name
)) {
1238 hashcpy(ref
->old_sha1
, list
->old_sha1
);
1245 * Return true if there is anything to report, otherwise false.
1247 int stat_tracking_info(struct branch
*branch
, int *num_ours
, int *num_theirs
)
1249 unsigned char sha1
[20];
1250 struct commit
*ours
, *theirs
;
1252 struct rev_info revs
;
1253 const char *rev_argv
[10], *base
;
1257 * Nothing to report unless we are marked to build on top of
1261 !branch
->merge
|| !branch
->merge
[0] || !branch
->merge
[0]->dst
)
1265 * If what we used to build on no longer exists, there is
1266 * nothing to report.
1268 base
= branch
->merge
[0]->dst
;
1269 if (!resolve_ref(base
, sha1
, 1, NULL
))
1271 theirs
= lookup_commit(sha1
);
1275 if (!resolve_ref(branch
->refname
, sha1
, 1, NULL
))
1277 ours
= lookup_commit(sha1
);
1281 /* are we the same? */
1285 /* Run "rev-list --left-right ours...theirs" internally... */
1287 rev_argv
[rev_argc
++] = NULL
;
1288 rev_argv
[rev_argc
++] = "--left-right";
1289 rev_argv
[rev_argc
++] = symmetric
;
1290 rev_argv
[rev_argc
++] = "--";
1291 rev_argv
[rev_argc
] = NULL
;
1293 strcpy(symmetric
, sha1_to_hex(ours
->object
.sha1
));
1294 strcpy(symmetric
+ 40, "...");
1295 strcpy(symmetric
+ 43, sha1_to_hex(theirs
->object
.sha1
));
1297 init_revisions(&revs
, NULL
);
1298 setup_revisions(rev_argc
, rev_argv
, &revs
, NULL
);
1299 prepare_revision_walk(&revs
);
1301 /* ... and count the commits on each side. */
1305 struct commit
*c
= get_revision(&revs
);
1308 if (c
->object
.flags
& SYMMETRIC_LEFT
)
1314 /* clear object flags smudged by the above traversal */
1315 clear_commit_marks(ours
, ALL_REV_FLAGS
);
1316 clear_commit_marks(theirs
, ALL_REV_FLAGS
);
1321 * Return true when there is anything to report, otherwise false.
1323 int format_tracking_info(struct branch
*branch
, struct strbuf
*sb
)
1325 int num_ours
, num_theirs
;
1328 if (!stat_tracking_info(branch
, &num_ours
, &num_theirs
))
1331 base
= branch
->merge
[0]->dst
;
1332 if (!prefixcmp(base
, "refs/remotes/")) {
1333 base
+= strlen("refs/remotes/");
1336 strbuf_addf(sb
, "Your branch is ahead of '%s' "
1337 "by %d commit%s.\n",
1338 base
, num_ours
, (num_ours
== 1) ? "" : "s");
1340 strbuf_addf(sb
, "Your branch is behind '%s' "
1342 "and can be fast-forwarded.\n",
1343 base
, num_theirs
, (num_theirs
== 1) ? "" : "s");
1345 strbuf_addf(sb
, "Your branch and '%s' have diverged,\n"
1346 "and have %d and %d different commit(s) each, "
1348 base
, num_ours
, num_theirs
);