9 #include "string-list.h"
11 static struct refspec s_tag_refspec
= {
19 const struct refspec
*tag_refspec
= &s_tag_refspec
;
21 struct counted_string
{
28 struct counted_string
*instead_of
;
33 struct rewrite
**rewrite
;
38 static struct remote
**remotes
;
39 static int remotes_alloc
;
40 static int remotes_nr
;
42 static struct branch
**branches
;
43 static int branches_alloc
;
44 static int branches_nr
;
46 static struct branch
*current_branch
;
47 static const char *default_remote_name
;
48 static int explicit_default_remote_name
;
50 static struct rewrites rewrites
;
51 static struct rewrites rewrites_push
;
53 #define BUF_SIZE (2048)
54 static char buffer
[BUF_SIZE
];
56 static const char *alias_url(const char *url
, struct rewrites
*r
)
60 struct counted_string
*longest
;
65 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
68 for (j
= 0; j
< r
->rewrite
[i
]->instead_of_nr
; j
++) {
69 if (!prefixcmp(url
, r
->rewrite
[i
]->instead_of
[j
].s
) &&
71 longest
->len
< r
->rewrite
[i
]->instead_of
[j
].len
)) {
72 longest
= &(r
->rewrite
[i
]->instead_of
[j
]);
80 ret
= xmalloc(r
->rewrite
[longest_i
]->baselen
+
81 (strlen(url
) - longest
->len
) + 1);
82 strcpy(ret
, r
->rewrite
[longest_i
]->base
);
83 strcpy(ret
+ r
->rewrite
[longest_i
]->baselen
, url
+ longest
->len
);
87 static void add_push_refspec(struct remote
*remote
, const char *ref
)
89 ALLOC_GROW(remote
->push_refspec
,
90 remote
->push_refspec_nr
+ 1,
91 remote
->push_refspec_alloc
);
92 remote
->push_refspec
[remote
->push_refspec_nr
++] = ref
;
95 static void add_fetch_refspec(struct remote
*remote
, const char *ref
)
97 ALLOC_GROW(remote
->fetch_refspec
,
98 remote
->fetch_refspec_nr
+ 1,
99 remote
->fetch_refspec_alloc
);
100 remote
->fetch_refspec
[remote
->fetch_refspec_nr
++] = ref
;
103 static void add_url(struct remote
*remote
, const char *url
)
105 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
106 remote
->url
[remote
->url_nr
++] = url
;
109 static void add_pushurl(struct remote
*remote
, const char *pushurl
)
111 ALLOC_GROW(remote
->pushurl
, remote
->pushurl_nr
+ 1, remote
->pushurl_alloc
);
112 remote
->pushurl
[remote
->pushurl_nr
++] = pushurl
;
115 static void add_pushurl_alias(struct remote
*remote
, const char *url
)
117 const char *pushurl
= alias_url(url
, &rewrites_push
);
119 add_pushurl(remote
, pushurl
);
122 static void add_url_alias(struct remote
*remote
, const char *url
)
124 add_url(remote
, alias_url(url
, &rewrites
));
125 add_pushurl_alias(remote
, url
);
128 static struct remote
*make_remote(const char *name
, int len
)
133 for (i
= 0; i
< remotes_nr
; i
++) {
134 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
135 !remotes
[i
]->name
[len
]) :
136 !strcmp(name
, remotes
[i
]->name
))
140 ret
= xcalloc(1, sizeof(struct remote
));
141 ALLOC_GROW(remotes
, remotes_nr
+ 1, remotes_alloc
);
142 remotes
[remotes_nr
++] = ret
;
144 ret
->name
= xstrndup(name
, len
);
146 ret
->name
= xstrdup(name
);
150 static void add_merge(struct branch
*branch
, const char *name
)
152 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
153 branch
->merge_alloc
);
154 branch
->merge_name
[branch
->merge_nr
++] = name
;
157 static struct branch
*make_branch(const char *name
, int len
)
163 for (i
= 0; i
< branches_nr
; i
++) {
164 if (len
? (!strncmp(name
, branches
[i
]->name
, len
) &&
165 !branches
[i
]->name
[len
]) :
166 !strcmp(name
, branches
[i
]->name
))
170 ALLOC_GROW(branches
, branches_nr
+ 1, branches_alloc
);
171 ret
= xcalloc(1, sizeof(struct branch
));
172 branches
[branches_nr
++] = ret
;
174 ret
->name
= xstrndup(name
, len
);
176 ret
->name
= xstrdup(name
);
177 refname
= xmalloc(strlen(name
) + strlen("refs/heads/") + 1);
178 strcpy(refname
, "refs/heads/");
179 strcpy(refname
+ strlen("refs/heads/"), ret
->name
);
180 ret
->refname
= refname
;
185 static struct rewrite
*make_rewrite(struct rewrites
*r
, const char *base
, int len
)
190 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
192 ? (len
== r
->rewrite
[i
]->baselen
&&
193 !strncmp(base
, r
->rewrite
[i
]->base
, len
))
194 : !strcmp(base
, r
->rewrite
[i
]->base
))
195 return r
->rewrite
[i
];
198 ALLOC_GROW(r
->rewrite
, r
->rewrite_nr
+ 1, r
->rewrite_alloc
);
199 ret
= xcalloc(1, sizeof(struct rewrite
));
200 r
->rewrite
[r
->rewrite_nr
++] = ret
;
202 ret
->base
= xstrndup(base
, len
);
206 ret
->base
= xstrdup(base
);
207 ret
->baselen
= strlen(base
);
212 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
214 ALLOC_GROW(rewrite
->instead_of
, rewrite
->instead_of_nr
+ 1, rewrite
->instead_of_alloc
);
215 rewrite
->instead_of
[rewrite
->instead_of_nr
].s
= instead_of
;
216 rewrite
->instead_of
[rewrite
->instead_of_nr
].len
= strlen(instead_of
);
217 rewrite
->instead_of_nr
++;
220 static void read_remotes_file(struct remote
*remote
)
222 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
226 remote
->origin
= REMOTE_REMOTES
;
227 while (fgets(buffer
, BUF_SIZE
, f
)) {
231 if (!prefixcmp(buffer
, "URL:")) {
234 } else if (!prefixcmp(buffer
, "Push:")) {
237 } else if (!prefixcmp(buffer
, "Pull:")) {
249 while (isspace(p
[-1]))
252 switch (value_list
) {
254 add_url_alias(remote
, xstrdup(s
));
257 add_push_refspec(remote
, xstrdup(s
));
260 add_fetch_refspec(remote
, xstrdup(s
));
267 static void read_branches_file(struct remote
*remote
)
269 const char *slash
= strchr(remote
->name
, '/');
271 struct strbuf branch
= STRBUF_INIT
;
272 int n
= slash
? slash
- remote
->name
: 1000;
273 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
279 s
= fgets(buffer
, BUF_SIZE
, f
);
287 remote
->origin
= REMOTE_BRANCHES
;
289 while (isspace(p
[-1]))
293 len
+= strlen(slash
);
294 p
= xmalloc(len
+ 1);
300 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
301 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
302 * the partial URL obtained from the branches file plus
303 * "/netdev-2.6" and does not store it in any tracking ref.
304 * #branch specifier in the file is ignored.
306 * Otherwise, the branches file would have URL and optionally
307 * #branch specified. The "master" (or specified) branch is
308 * fetched and stored in the local branch of the same name.
310 frag
= strchr(p
, '#');
313 strbuf_addf(&branch
, "refs/heads/%s", frag
);
315 strbuf_addstr(&branch
, "refs/heads/master");
317 strbuf_addf(&branch
, ":refs/heads/%s", remote
->name
);
319 strbuf_reset(&branch
);
320 strbuf_addstr(&branch
, "HEAD:");
322 add_url_alias(remote
, p
);
323 add_fetch_refspec(remote
, strbuf_detach(&branch
, NULL
));
325 * Cogito compatible push: push current HEAD to remote #branch
326 * (master if missing)
328 strbuf_init(&branch
, 0);
329 strbuf_addstr(&branch
, "HEAD");
331 strbuf_addf(&branch
, ":refs/heads/%s", frag
);
333 strbuf_addstr(&branch
, ":refs/heads/master");
334 add_push_refspec(remote
, strbuf_detach(&branch
, NULL
));
335 remote
->fetch_tags
= 1; /* always auto-follow */
338 static int handle_config(const char *key
, const char *value
, void *cb
)
342 struct remote
*remote
;
343 struct branch
*branch
;
344 if (!prefixcmp(key
, "branch.")) {
346 subkey
= strrchr(name
, '.');
349 branch
= make_branch(name
, subkey
- name
);
350 if (!strcmp(subkey
, ".remote")) {
352 return config_error_nonbool(key
);
353 branch
->remote_name
= xstrdup(value
);
354 if (branch
== current_branch
) {
355 default_remote_name
= branch
->remote_name
;
356 explicit_default_remote_name
= 1;
358 } else if (!strcmp(subkey
, ".merge")) {
360 return config_error_nonbool(key
);
361 add_merge(branch
, xstrdup(value
));
365 if (!prefixcmp(key
, "url.")) {
366 struct rewrite
*rewrite
;
368 subkey
= strrchr(name
, '.');
371 if (!strcmp(subkey
, ".insteadof")) {
372 rewrite
= make_rewrite(&rewrites
, name
, subkey
- name
);
374 return config_error_nonbool(key
);
375 add_instead_of(rewrite
, xstrdup(value
));
376 } else if (!strcmp(subkey
, ".pushinsteadof")) {
377 rewrite
= make_rewrite(&rewrites_push
, name
, subkey
- name
);
379 return config_error_nonbool(key
);
380 add_instead_of(rewrite
, xstrdup(value
));
383 if (prefixcmp(key
, "remote."))
387 warning("Config remote shorthand cannot begin with '/': %s",
391 subkey
= strrchr(name
, '.');
394 remote
= make_remote(name
, subkey
- name
);
395 remote
->origin
= REMOTE_CONFIG
;
396 if (!strcmp(subkey
, ".mirror"))
397 remote
->mirror
= git_config_bool(key
, value
);
398 else if (!strcmp(subkey
, ".skipdefaultupdate"))
399 remote
->skip_default_update
= git_config_bool(key
, value
);
400 else if (!strcmp(subkey
, ".skipfetchall"))
401 remote
->skip_default_update
= git_config_bool(key
, value
);
402 else if (!strcmp(subkey
, ".url")) {
404 if (git_config_string(&v
, key
, value
))
407 } else if (!strcmp(subkey
, ".pushurl")) {
409 if (git_config_string(&v
, key
, value
))
411 add_pushurl(remote
, v
);
412 } else if (!strcmp(subkey
, ".push")) {
414 if (git_config_string(&v
, key
, value
))
416 add_push_refspec(remote
, v
);
417 } else if (!strcmp(subkey
, ".fetch")) {
419 if (git_config_string(&v
, key
, value
))
421 add_fetch_refspec(remote
, v
);
422 } else if (!strcmp(subkey
, ".receivepack")) {
424 if (git_config_string(&v
, key
, value
))
426 if (!remote
->receivepack
)
427 remote
->receivepack
= v
;
429 error("more than one receivepack given, using the first");
430 } else if (!strcmp(subkey
, ".uploadpack")) {
432 if (git_config_string(&v
, key
, value
))
434 if (!remote
->uploadpack
)
435 remote
->uploadpack
= v
;
437 error("more than one uploadpack given, using the first");
438 } else if (!strcmp(subkey
, ".tagopt")) {
439 if (!strcmp(value
, "--no-tags"))
440 remote
->fetch_tags
= -1;
441 } else if (!strcmp(subkey
, ".proxy")) {
442 return git_config_string((const char **)&remote
->http_proxy
,
448 static void alias_all_urls(void)
451 for (i
= 0; i
< remotes_nr
; i
++) {
452 int add_pushurl_aliases
;
455 for (j
= 0; j
< remotes
[i
]->pushurl_nr
; j
++) {
456 remotes
[i
]->pushurl
[j
] = alias_url(remotes
[i
]->pushurl
[j
], &rewrites
);
458 add_pushurl_aliases
= remotes
[i
]->pushurl_nr
== 0;
459 for (j
= 0; j
< remotes
[i
]->url_nr
; j
++) {
460 if (add_pushurl_aliases
)
461 add_pushurl_alias(remotes
[i
], remotes
[i
]->url
[j
]);
462 remotes
[i
]->url
[j
] = alias_url(remotes
[i
]->url
[j
], &rewrites
);
467 static void read_config(void)
469 unsigned char sha1
[20];
470 const char *head_ref
;
472 if (default_remote_name
) // did this already
474 default_remote_name
= xstrdup("origin");
475 current_branch
= NULL
;
476 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
477 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
478 !prefixcmp(head_ref
, "refs/heads/")) {
480 make_branch(head_ref
+ strlen("refs/heads/"), 0);
482 git_config(handle_config
, NULL
);
487 * We need to make sure the tracking branches are well formed, but a
488 * wildcard refspec in "struct refspec" must have a trailing slash. We
489 * temporarily drop the trailing '/' while calling check_ref_format(),
490 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
491 * error return is Ok for a wildcard refspec.
493 static int verify_refname(char *name
, int is_glob
)
497 result
= check_ref_format(name
);
498 if (is_glob
&& result
== CHECK_REF_FORMAT_WILDCARD
)
499 result
= CHECK_REF_FORMAT_OK
;
504 * This function frees a refspec array.
505 * Warning: code paths should be checked to ensure that the src
506 * and dst pointers are always freeable pointers as well
507 * as the refspec pointer itself.
509 static void free_refspecs(struct refspec
*refspec
, int nr_refspec
)
516 for (i
= 0; i
< nr_refspec
; i
++) {
517 free(refspec
[i
].src
);
518 free(refspec
[i
].dst
);
523 static struct refspec
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
527 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
529 for (i
= 0; i
< nr_refspec
; i
++) {
532 const char *lhs
, *rhs
;
542 rhs
= strrchr(lhs
, ':');
545 * Before going on, special case ":" (or "+:") as a refspec
548 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
554 size_t rlen
= strlen(++rhs
);
555 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
556 rs
[i
].dst
= xstrndup(rhs
, rlen
);
559 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
560 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
561 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
564 } else if (rhs
&& is_glob
) {
568 rs
[i
].pattern
= is_glob
;
569 rs
[i
].src
= xstrndup(lhs
, llen
);
574 * - empty is allowed; it means HEAD.
575 * - otherwise it must be a valid looking ref.
580 st
= verify_refname(rs
[i
].src
, is_glob
);
581 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
586 * - missing is ok, and is same as empty.
587 * - empty is ok; it means not to store.
588 * - otherwise it must be a valid looking ref.
592 } else if (!*rs
[i
].dst
) {
595 st
= verify_refname(rs
[i
].dst
, is_glob
);
596 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
602 * - empty is allowed; it means delete.
603 * - when wildcarded, it must be a valid looking ref.
604 * - otherwise, it must be an extended SHA-1, but
605 * there is no existing way to validate this.
610 st
= verify_refname(rs
[i
].src
, is_glob
);
611 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
615 ; /* anything goes, for now */
618 * - missing is allowed, but LHS then must be a
620 * - empty is not allowed.
621 * - otherwise it must be a valid looking ref.
624 st
= verify_refname(rs
[i
].src
, is_glob
);
625 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
627 } else if (!*rs
[i
].dst
) {
630 st
= verify_refname(rs
[i
].dst
, is_glob
);
631 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
641 * nr_refspec must be greater than zero and i must be valid
642 * since it is only possible to reach this point from within
643 * the for loop above.
645 free_refspecs(rs
, i
+1);
648 die("Invalid refspec '%s'", refspec
[i
]);
651 int valid_fetch_refspec(const char *fetch_refspec_str
)
653 const char *fetch_refspec
[] = { fetch_refspec_str
};
654 struct refspec
*refspec
;
656 refspec
= parse_refspec_internal(1, fetch_refspec
, 1, 1);
657 free_refspecs(refspec
, 1);
661 struct refspec
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
663 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
666 static struct refspec
*parse_push_refspec(int nr_refspec
, const char **refspec
)
668 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
671 static int valid_remote_nick(const char *name
)
673 if (!name
[0] || is_dot_or_dotdot(name
))
675 return !strchr(name
, '/'); /* no slash */
678 struct remote
*remote_get(const char *name
)
687 name
= default_remote_name
;
688 name_given
= explicit_default_remote_name
;
691 ret
= make_remote(name
, 0);
692 if (valid_remote_nick(name
)) {
694 read_remotes_file(ret
);
696 read_branches_file(ret
);
698 if (name_given
&& !ret
->url
)
699 add_url_alias(ret
, name
);
702 ret
->fetch
= parse_fetch_refspec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
703 ret
->push
= parse_push_refspec(ret
->push_refspec_nr
, ret
->push_refspec
);
707 int remote_is_configured(const char *name
)
712 for (i
= 0; i
< remotes_nr
; i
++)
713 if (!strcmp(name
, remotes
[i
]->name
))
718 int for_each_remote(each_remote_fn fn
, void *priv
)
722 for (i
= 0; i
< remotes_nr
&& !result
; i
++) {
723 struct remote
*r
= remotes
[i
];
727 r
->fetch
= parse_fetch_refspec(r
->fetch_refspec_nr
,
730 r
->push
= parse_push_refspec(r
->push_refspec_nr
,
732 result
= fn(r
, priv
);
737 void ref_remove_duplicates(struct ref
*ref_map
)
741 for (; ref_map
; ref_map
= ref_map
->next
) {
742 if (!ref_map
->peer_ref
)
744 posn
= &ref_map
->next
;
746 if ((*posn
)->peer_ref
&&
747 !strcmp((*posn
)->peer_ref
->name
,
748 ref_map
->peer_ref
->name
)) {
749 if (strcmp((*posn
)->name
, ref_map
->name
))
750 die("%s tracks both %s and %s",
751 ref_map
->peer_ref
->name
,
752 (*posn
)->name
, ref_map
->name
);
753 next
= (*posn
)->next
;
754 free((*posn
)->peer_ref
);
758 posn
= &(*posn
)->next
;
764 int remote_has_url(struct remote
*remote
, const char *url
)
767 for (i
= 0; i
< remote
->url_nr
; i
++) {
768 if (!strcmp(remote
->url
[i
], url
))
774 static int match_name_with_pattern(const char *key
, const char *name
,
775 const char *value
, char **result
)
777 const char *kstar
= strchr(key
, '*');
783 die("Key '%s' of pattern had no '*'", key
);
785 ksuffixlen
= strlen(kstar
+ 1);
786 namelen
= strlen(name
);
787 ret
= !strncmp(name
, key
, klen
) && namelen
>= klen
+ ksuffixlen
&&
788 !memcmp(name
+ namelen
- ksuffixlen
, kstar
+ 1, ksuffixlen
);
790 const char *vstar
= strchr(value
, '*');
794 die("Value '%s' of pattern has no '*'", value
);
795 vlen
= vstar
- value
;
796 vsuffixlen
= strlen(vstar
+ 1);
797 *result
= xmalloc(vlen
+ vsuffixlen
+
799 klen
- ksuffixlen
+ 1);
800 strncpy(*result
, value
, vlen
);
801 strncpy(*result
+ vlen
,
802 name
+ klen
, namelen
- klen
- ksuffixlen
);
803 strcpy(*result
+ vlen
+ namelen
- klen
- ksuffixlen
,
809 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
811 int find_src
= refspec
->src
== NULL
;
812 char *needle
, **result
;
817 return error("find_tracking: need either src or dst");
818 needle
= refspec
->dst
;
819 result
= &refspec
->src
;
821 needle
= refspec
->src
;
822 result
= &refspec
->dst
;
825 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
826 struct refspec
*fetch
= &remote
->fetch
[i
];
827 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
828 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
831 if (fetch
->pattern
) {
832 if (match_name_with_pattern(key
, needle
, value
, result
)) {
833 refspec
->force
= fetch
->force
;
836 } else if (!strcmp(needle
, key
)) {
837 *result
= xstrdup(value
);
838 refspec
->force
= fetch
->force
;
845 static struct ref
*alloc_ref_with_prefix(const char *prefix
, size_t prefixlen
,
848 size_t len
= strlen(name
);
849 struct ref
*ref
= xcalloc(1, sizeof(struct ref
) + prefixlen
+ len
+ 1);
850 memcpy(ref
->name
, prefix
, prefixlen
);
851 memcpy(ref
->name
+ prefixlen
, name
, len
);
855 struct ref
*alloc_ref(const char *name
)
857 return alloc_ref_with_prefix("", 0, name
);
860 static struct ref
*copy_ref(const struct ref
*ref
)
866 len
= strlen(ref
->name
);
867 cpy
= xmalloc(sizeof(struct ref
) + len
+ 1);
868 memcpy(cpy
, ref
, sizeof(struct ref
) + len
+ 1);
870 cpy
->symref
= ref
->symref
? xstrdup(ref
->symref
) : NULL
;
871 cpy
->remote_status
= ref
->remote_status
? xstrdup(ref
->remote_status
) : NULL
;
872 cpy
->peer_ref
= copy_ref(ref
->peer_ref
);
876 struct ref
*copy_ref_list(const struct ref
*ref
)
878 struct ref
*ret
= NULL
;
879 struct ref
**tail
= &ret
;
881 *tail
= copy_ref(ref
);
883 tail
= &((*tail
)->next
);
888 static void free_ref(struct ref
*ref
)
892 free_ref(ref
->peer_ref
);
893 free(ref
->remote_status
);
898 void free_refs(struct ref
*ref
)
908 static int count_refspec_match(const char *pattern
,
910 struct ref
**matched_ref
)
912 int patlen
= strlen(pattern
);
913 struct ref
*matched_weak
= NULL
;
914 struct ref
*matched
= NULL
;
918 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
919 char *name
= refs
->name
;
920 int namelen
= strlen(name
);
922 if (!refname_match(pattern
, name
, ref_rev_parse_rules
))
925 /* A match is "weak" if it is with refs outside
926 * heads or tags, and did not specify the pattern
927 * in full (e.g. "refs/remotes/origin/master") or at
928 * least from the toplevel (e.g. "remotes/origin/master");
929 * otherwise "git push $URL master" would result in
930 * ambiguity between remotes/origin/master and heads/master
931 * at the remote site.
933 if (namelen
!= patlen
&&
934 patlen
!= namelen
- 5 &&
935 prefixcmp(name
, "refs/heads/") &&
936 prefixcmp(name
, "refs/tags/")) {
937 /* We want to catch the case where only weak
938 * matches are found and there are multiple
939 * matches, and where more than one strong
940 * matches are found, as ambiguous. One
941 * strong match with zero or more weak matches
942 * are acceptable as a unique match.
953 *matched_ref
= matched_weak
;
957 *matched_ref
= matched
;
962 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
970 static struct ref
*try_explicit_object_name(const char *name
)
972 unsigned char sha1
[20];
976 ref
= alloc_ref("(delete)");
977 hashclr(ref
->new_sha1
);
980 if (get_sha1(name
, sha1
))
982 ref
= alloc_ref(name
);
983 hashcpy(ref
->new_sha1
, sha1
);
987 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
989 struct ref
*ret
= alloc_ref(name
);
990 tail_link_ref(ret
, tail
);
994 static char *guess_ref(const char *name
, struct ref
*peer
)
996 struct strbuf buf
= STRBUF_INIT
;
997 unsigned char sha1
[20];
999 const char *r
= resolve_ref(peer
->name
, sha1
, 1, NULL
);
1003 if (!prefixcmp(r
, "refs/heads/"))
1004 strbuf_addstr(&buf
, "refs/heads/");
1005 else if (!prefixcmp(r
, "refs/tags/"))
1006 strbuf_addstr(&buf
, "refs/tags/");
1010 strbuf_addstr(&buf
, name
);
1011 return strbuf_detach(&buf
, NULL
);
1014 static int match_explicit(struct ref
*src
, struct ref
*dst
,
1015 struct ref
***dst_tail
,
1018 struct ref
*matched_src
, *matched_dst
;
1021 const char *dst_value
= rs
->dst
;
1024 if (rs
->pattern
|| rs
->matching
)
1027 matched_src
= matched_dst
= NULL
;
1028 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
1033 /* The source could be in the get_sha1() format
1034 * not a reference name. :refs/other is a
1035 * way to delete 'other' ref at the remote end.
1037 matched_src
= try_explicit_object_name(rs
->src
);
1039 return error("src refspec %s does not match any.", rs
->src
);
1043 return error("src refspec %s matches more than one.", rs
->src
);
1047 unsigned char sha1
[20];
1050 dst_value
= resolve_ref(matched_src
->name
, sha1
, 1, &flag
);
1052 ((flag
& REF_ISSYMREF
) &&
1053 prefixcmp(dst_value
, "refs/heads/")))
1054 die("%s cannot be resolved to branch.",
1058 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
1062 if (!memcmp(dst_value
, "refs/", 5))
1063 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
1064 else if ((dst_guess
= guess_ref(dst_value
, matched_src
)))
1065 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
1067 error("unable to push to unqualified destination: %s\n"
1068 "The destination refspec neither matches an "
1069 "existing ref on the remote nor\n"
1070 "begins with refs/, and we are unable to "
1071 "guess a prefix based on the source ref.",
1076 error("dst refspec %s matches more than one.",
1082 if (matched_dst
->peer_ref
)
1083 return error("dst ref %s receives from more than one src.",
1086 matched_dst
->peer_ref
= copy_src
? copy_ref(matched_src
) : matched_src
;
1087 matched_dst
->force
= rs
->force
;
1092 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
1093 struct ref
***dst_tail
, struct refspec
*rs
,
1097 for (i
= errs
= 0; i
< rs_nr
; i
++)
1098 errs
+= match_explicit(src
, dst
, dst_tail
, &rs
[i
]);
1102 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
1104 const struct ref
*src
)
1107 int matching_refs
= -1;
1108 for (i
= 0; i
< rs_nr
; i
++) {
1109 if (rs
[i
].matching
&&
1110 (matching_refs
== -1 || rs
[i
].force
)) {
1115 if (rs
[i
].pattern
&& match_name_with_pattern(rs
[i
].src
, src
->name
,
1119 if (matching_refs
!= -1)
1120 return rs
+ matching_refs
;
1125 static struct ref
**tail_ref(struct ref
**head
)
1127 struct ref
**tail
= head
;
1129 tail
= &((*tail
)->next
);
1134 * Note. This is used only by "push"; refspec matching rules for
1135 * push and fetch are subtly different, so do not try to reuse it
1138 int match_refs(struct ref
*src
, struct ref
**dst
,
1139 int nr_refspec
, const char **refspec
, int flags
)
1142 int send_all
= flags
& MATCH_REFS_ALL
;
1143 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
1145 static const char *default_refspec
[] = { ":", NULL
};
1146 struct ref
**dst_tail
= tail_ref(dst
);
1150 refspec
= default_refspec
;
1152 rs
= parse_push_refspec(nr_refspec
, (const char **) refspec
);
1153 errs
= match_explicit_refs(src
, *dst
, &dst_tail
, rs
, nr_refspec
);
1155 /* pick the remainder */
1156 for ( ; src
; src
= src
->next
) {
1157 struct ref
*dst_peer
;
1158 const struct refspec
*pat
= NULL
;
1163 pat
= check_pattern_match(rs
, nr_refspec
, src
);
1167 if (pat
->matching
) {
1169 * "matching refs"; traditionally we pushed everything
1170 * including refs outside refs/heads/ hierarchy, but
1171 * that does not make much sense these days.
1173 if (!send_mirror
&& prefixcmp(src
->name
, "refs/heads/"))
1175 dst_name
= xstrdup(src
->name
);
1178 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
1179 if (!match_name_with_pattern(pat
->src
, src
->name
,
1180 dst_side
, &dst_name
))
1181 die("Didn't think it matches any more");
1183 dst_peer
= find_ref_by_name(*dst
, dst_name
);
1185 if (dst_peer
->peer_ref
)
1186 /* We're already sending something to this ref. */
1190 if (pat
->matching
&& !(send_all
|| send_mirror
))
1192 * Remote doesn't have it, and we have no
1193 * explicit pattern, and we don't have
1194 * --all nor --mirror.
1198 /* Create a new one and link it */
1199 dst_peer
= make_linked_ref(dst_name
, &dst_tail
);
1200 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
1202 dst_peer
->peer_ref
= copy_ref(src
);
1203 dst_peer
->force
= pat
->force
;
1212 struct branch
*branch_get(const char *name
)
1217 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1218 ret
= current_branch
;
1220 ret
= make_branch(name
, 0);
1221 if (ret
&& ret
->remote_name
) {
1222 ret
->remote
= remote_get(ret
->remote_name
);
1223 if (ret
->merge_nr
) {
1225 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
1227 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1228 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1229 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1230 if (remote_find_tracking(ret
->remote
, ret
->merge
[i
])
1231 && !strcmp(ret
->remote_name
, "."))
1232 ret
->merge
[i
]->dst
= xstrdup(ret
->merge_name
[i
]);
1239 int branch_has_merge_config(struct branch
*branch
)
1241 return branch
&& !!branch
->merge
;
1244 int branch_merge_matches(struct branch
*branch
,
1246 const char *refname
)
1248 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1250 return refname_match(branch
->merge
[i
]->src
, refname
, ref_fetch_rules
);
1253 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
1254 const struct refspec
*refspec
)
1256 const struct ref
*ref
;
1257 struct ref
*ret
= NULL
;
1258 struct ref
**tail
= &ret
;
1262 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1263 if (strchr(ref
->name
, '^'))
1264 continue; /* a dereference item */
1265 if (match_name_with_pattern(refspec
->src
, ref
->name
,
1266 refspec
->dst
, &expn_name
)) {
1267 struct ref
*cpy
= copy_ref(ref
);
1269 cpy
->peer_ref
= alloc_ref(expn_name
);
1272 cpy
->peer_ref
->force
= 1;
1281 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
1283 const struct ref
*ref
;
1284 for (ref
= refs
; ref
; ref
= ref
->next
) {
1285 if (refname_match(name
, ref
->name
, ref_fetch_rules
))
1291 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
1293 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
1298 return copy_ref(ref
);
1301 static struct ref
*get_local_ref(const char *name
)
1303 if (!name
|| name
[0] == '\0')
1306 if (!prefixcmp(name
, "refs/"))
1307 return alloc_ref(name
);
1309 if (!prefixcmp(name
, "heads/") ||
1310 !prefixcmp(name
, "tags/") ||
1311 !prefixcmp(name
, "remotes/"))
1312 return alloc_ref_with_prefix("refs/", 5, name
);
1314 return alloc_ref_with_prefix("refs/heads/", 11, name
);
1317 int get_fetch_map(const struct ref
*remote_refs
,
1318 const struct refspec
*refspec
,
1322 struct ref
*ref_map
, **rmp
;
1324 if (refspec
->pattern
) {
1325 ref_map
= get_expanded_map(remote_refs
, refspec
);
1327 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
1329 ref_map
= get_remote_ref(remote_refs
, name
);
1330 if (!missing_ok
&& !ref_map
)
1331 die("Couldn't find remote ref %s", name
);
1333 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
1334 if (ref_map
->peer_ref
&& refspec
->force
)
1335 ref_map
->peer_ref
->force
= 1;
1339 for (rmp
= &ref_map
; *rmp
; ) {
1340 if ((*rmp
)->peer_ref
) {
1341 int st
= check_ref_format((*rmp
)->peer_ref
->name
+ 5);
1342 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
) {
1343 struct ref
*ignore
= *rmp
;
1344 error("* Ignoring funny ref '%s' locally",
1345 (*rmp
)->peer_ref
->name
);
1346 *rmp
= (*rmp
)->next
;
1347 free(ignore
->peer_ref
);
1352 rmp
= &((*rmp
)->next
);
1356 tail_link_ref(ref_map
, tail
);
1361 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
1365 for (; list
; list
= list
->next
)
1366 if (!strcmp(ref
->symref
, list
->name
)) {
1367 hashcpy(ref
->old_sha1
, list
->old_sha1
);
1373 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
1376 struct commit_list
*temp
= list
;
1377 temp
->item
->object
.flags
&= ~mark
;
1383 int ref_newer(const unsigned char *new_sha1
, const unsigned char *old_sha1
)
1386 struct commit
*old
, *new;
1387 struct commit_list
*list
, *used
;
1390 /* Both new and old must be commit-ish and new is descendant of
1391 * old. Otherwise we require --force.
1393 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
1394 if (!o
|| o
->type
!= OBJ_COMMIT
)
1396 old
= (struct commit
*) o
;
1398 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
1399 if (!o
|| o
->type
!= OBJ_COMMIT
)
1401 new = (struct commit
*) o
;
1403 if (parse_commit(new) < 0)
1407 commit_list_insert(new, &list
);
1409 new = pop_most_recent_commit(&list
, TMP_MARK
);
1410 commit_list_insert(new, &used
);
1416 unmark_and_free(list
, TMP_MARK
);
1417 unmark_and_free(used
, TMP_MARK
);
1422 * Return true if there is anything to report, otherwise false.
1424 int stat_tracking_info(struct branch
*branch
, int *num_ours
, int *num_theirs
)
1426 unsigned char sha1
[20];
1427 struct commit
*ours
, *theirs
;
1429 struct rev_info revs
;
1430 const char *rev_argv
[10], *base
;
1434 * Nothing to report unless we are marked to build on top of
1438 !branch
->merge
|| !branch
->merge
[0] || !branch
->merge
[0]->dst
)
1442 * If what we used to build on no longer exists, there is
1443 * nothing to report.
1445 base
= branch
->merge
[0]->dst
;
1446 if (!resolve_ref(base
, sha1
, 1, NULL
))
1448 theirs
= lookup_commit_reference(sha1
);
1452 if (!resolve_ref(branch
->refname
, sha1
, 1, NULL
))
1454 ours
= lookup_commit_reference(sha1
);
1458 /* are we the same? */
1462 /* Run "rev-list --left-right ours...theirs" internally... */
1464 rev_argv
[rev_argc
++] = NULL
;
1465 rev_argv
[rev_argc
++] = "--left-right";
1466 rev_argv
[rev_argc
++] = symmetric
;
1467 rev_argv
[rev_argc
++] = "--";
1468 rev_argv
[rev_argc
] = NULL
;
1470 strcpy(symmetric
, sha1_to_hex(ours
->object
.sha1
));
1471 strcpy(symmetric
+ 40, "...");
1472 strcpy(symmetric
+ 43, sha1_to_hex(theirs
->object
.sha1
));
1474 init_revisions(&revs
, NULL
);
1475 setup_revisions(rev_argc
, rev_argv
, &revs
, NULL
);
1476 prepare_revision_walk(&revs
);
1478 /* ... and count the commits on each side. */
1482 struct commit
*c
= get_revision(&revs
);
1485 if (c
->object
.flags
& SYMMETRIC_LEFT
)
1491 /* clear object flags smudged by the above traversal */
1492 clear_commit_marks(ours
, ALL_REV_FLAGS
);
1493 clear_commit_marks(theirs
, ALL_REV_FLAGS
);
1498 * Return true when there is anything to report, otherwise false.
1500 int format_tracking_info(struct branch
*branch
, struct strbuf
*sb
)
1502 int num_ours
, num_theirs
;
1505 if (!stat_tracking_info(branch
, &num_ours
, &num_theirs
))
1508 base
= branch
->merge
[0]->dst
;
1509 base
= shorten_unambiguous_ref(base
, 0);
1511 strbuf_addf(sb
, "Your branch is ahead of '%s' "
1512 "by %d commit%s.\n",
1513 base
, num_ours
, (num_ours
== 1) ? "" : "s");
1515 strbuf_addf(sb
, "Your branch is behind '%s' "
1517 "and can be fast-forwarded.\n",
1518 base
, num_theirs
, (num_theirs
== 1) ? "" : "s");
1520 strbuf_addf(sb
, "Your branch and '%s' have diverged,\n"
1521 "and have %d and %d different commit(s) each, "
1523 base
, num_ours
, num_theirs
);
1527 static int one_local_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
1529 struct ref
***local_tail
= cb_data
;
1533 /* we already know it starts with refs/ to get here */
1534 if (check_ref_format(refname
+ 5))
1537 len
= strlen(refname
) + 1;
1538 ref
= xcalloc(1, sizeof(*ref
) + len
);
1539 hashcpy(ref
->new_sha1
, sha1
);
1540 memcpy(ref
->name
, refname
, len
);
1542 *local_tail
= &ref
->next
;
1546 struct ref
*get_local_heads(void)
1548 struct ref
*local_refs
= NULL
, **local_tail
= &local_refs
;
1549 for_each_ref(one_local_ref
, &local_tail
);
1553 struct ref
*guess_remote_head(const struct ref
*head
,
1554 const struct ref
*refs
,
1557 const struct ref
*r
;
1558 struct ref
*list
= NULL
;
1559 struct ref
**tail
= &list
;
1565 * Some transports support directly peeking at
1566 * where HEAD points; if that is the case, then
1567 * we don't have to guess.
1570 return copy_ref(find_ref_by_name(refs
, head
->symref
));
1572 /* If refs/heads/master could be right, it is. */
1574 r
= find_ref_by_name(refs
, "refs/heads/master");
1575 if (r
&& !hashcmp(r
->old_sha1
, head
->old_sha1
))
1579 /* Look for another ref that points there */
1580 for (r
= refs
; r
; r
= r
->next
) {
1581 if (r
!= head
&& !hashcmp(r
->old_sha1
, head
->old_sha1
)) {
1582 *tail
= copy_ref(r
);
1583 tail
= &((*tail
)->next
);
1592 struct stale_heads_info
{
1593 struct remote
*remote
;
1594 struct string_list
*ref_names
;
1595 struct ref
**stale_refs_tail
;
1598 static int get_stale_heads_cb(const char *refname
,
1599 const unsigned char *sha1
, int flags
, void *cb_data
)
1601 struct stale_heads_info
*info
= cb_data
;
1602 struct refspec refspec
;
1603 memset(&refspec
, 0, sizeof(refspec
));
1604 refspec
.dst
= (char *)refname
;
1605 if (!remote_find_tracking(info
->remote
, &refspec
)) {
1606 if (!((flags
& REF_ISSYMREF
) ||
1607 string_list_has_string(info
->ref_names
, refspec
.src
))) {
1608 struct ref
*ref
= make_linked_ref(refname
, &info
->stale_refs_tail
);
1609 hashcpy(ref
->new_sha1
, sha1
);
1615 struct ref
*get_stale_heads(struct remote
*remote
, struct ref
*fetch_map
)
1617 struct ref
*ref
, *stale_refs
= NULL
;
1618 struct string_list ref_names
= { NULL
, 0, 0, 0 };
1619 struct stale_heads_info info
;
1620 info
.remote
= remote
;
1621 info
.ref_names
= &ref_names
;
1622 info
.stale_refs_tail
= &stale_refs
;
1623 for (ref
= fetch_map
; ref
; ref
= ref
->next
)
1624 string_list_append(ref
->name
, &ref_names
);
1625 sort_string_list(&ref_names
);
1626 for_each_ref(get_stale_heads_cb
, &info
);
1627 string_list_clear(&ref_names
, 0);