1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
6 #include "environment.h"
13 #include "object-name.h"
14 #include "object-store-ll.h"
21 #include "string-list.h"
23 #include "commit-reach.h"
26 #include "parse-options.h"
28 enum map_direction
{ FROM_SRC
, FROM_DST
};
30 struct counted_string
{
35 static int valid_remote(const struct remote
*remote
)
37 return !!remote
->url
.nr
;
40 static char *alias_url(const char *url
, struct rewrites
*r
)
43 struct counted_string
*longest
;
48 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
51 for (j
= 0; j
< r
->rewrite
[i
]->instead_of_nr
; j
++) {
52 if (starts_with(url
, r
->rewrite
[i
]->instead_of
[j
].s
) &&
54 longest
->len
< r
->rewrite
[i
]->instead_of
[j
].len
)) {
55 longest
= &(r
->rewrite
[i
]->instead_of
[j
]);
63 return xstrfmt("%s%s", r
->rewrite
[longest_i
]->base
, url
+ longest
->len
);
66 static void add_url(struct remote
*remote
, const char *url
)
69 strvec_push(&remote
->url
, url
);
71 strvec_clear(&remote
->url
);
74 static void add_pushurl(struct remote
*remote
, const char *pushurl
)
77 strvec_push(&remote
->pushurl
, pushurl
);
79 strvec_clear(&remote
->pushurl
);
82 static void add_pushurl_alias(struct remote_state
*remote_state
,
83 struct remote
*remote
, const char *url
)
85 char *alias
= alias_url(url
, &remote_state
->rewrites_push
);
87 add_pushurl(remote
, alias
);
91 static void add_url_alias(struct remote_state
*remote_state
,
92 struct remote
*remote
, const char *url
)
94 char *alias
= alias_url(url
, &remote_state
->rewrites
);
95 add_url(remote
, alias
? alias
: url
);
96 add_pushurl_alias(remote_state
, remote
, url
);
100 struct remotes_hash_key
{
105 static int remotes_hash_cmp(const void *cmp_data UNUSED
,
106 const struct hashmap_entry
*eptr
,
107 const struct hashmap_entry
*entry_or_key
,
110 const struct remote
*a
, *b
;
111 const struct remotes_hash_key
*key
= keydata
;
113 a
= container_of(eptr
, const struct remote
, ent
);
114 b
= container_of(entry_or_key
, const struct remote
, ent
);
117 return !!xstrncmpz(a
->name
, key
->str
, key
->len
);
119 return strcmp(a
->name
, b
->name
);
122 static struct remote
*make_remote(struct remote_state
*remote_state
,
123 const char *name
, int len
)
126 struct remotes_hash_key lookup
;
127 struct hashmap_entry lookup_entry
, *e
;
134 hashmap_entry_init(&lookup_entry
, memhash(name
, len
));
136 e
= hashmap_get(&remote_state
->remotes_hash
, &lookup_entry
, &lookup
);
138 return container_of(e
, struct remote
, ent
);
140 CALLOC_ARRAY(ret
, 1);
141 ret
->prune
= -1; /* unspecified */
142 ret
->prune_tags
= -1; /* unspecified */
143 ret
->name
= xstrndup(name
, len
);
144 refspec_init(&ret
->push
, REFSPEC_PUSH
);
145 refspec_init(&ret
->fetch
, REFSPEC_FETCH
);
147 ALLOC_GROW(remote_state
->remotes
, remote_state
->remotes_nr
+ 1,
148 remote_state
->remotes_alloc
);
149 remote_state
->remotes
[remote_state
->remotes_nr
++] = ret
;
151 hashmap_entry_init(&ret
->ent
, lookup_entry
.hash
);
152 if (hashmap_put_entry(&remote_state
->remotes_hash
, ret
, ent
))
153 BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
157 static void remote_clear(struct remote
*remote
)
159 free((char *)remote
->name
);
160 free((char *)remote
->foreign_vcs
);
162 strvec_clear(&remote
->url
);
163 strvec_clear(&remote
->pushurl
);
165 free((char *)remote
->receivepack
);
166 free((char *)remote
->uploadpack
);
167 FREE_AND_NULL(remote
->http_proxy
);
168 FREE_AND_NULL(remote
->http_proxy_authmethod
);
171 static void add_merge(struct branch
*branch
, const char *name
)
173 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
174 branch
->merge_alloc
);
175 branch
->merge_name
[branch
->merge_nr
++] = name
;
178 struct branches_hash_key
{
183 static int branches_hash_cmp(const void *cmp_data UNUSED
,
184 const struct hashmap_entry
*eptr
,
185 const struct hashmap_entry
*entry_or_key
,
188 const struct branch
*a
, *b
;
189 const struct branches_hash_key
*key
= keydata
;
191 a
= container_of(eptr
, const struct branch
, ent
);
192 b
= container_of(entry_or_key
, const struct branch
, ent
);
195 return !!xstrncmpz(a
->name
, key
->str
, key
->len
);
197 return strcmp(a
->name
, b
->name
);
200 static struct branch
*find_branch(struct remote_state
*remote_state
,
201 const char *name
, size_t len
)
203 struct branches_hash_key lookup
;
204 struct hashmap_entry lookup_entry
, *e
;
208 hashmap_entry_init(&lookup_entry
, memhash(name
, len
));
210 e
= hashmap_get(&remote_state
->branches_hash
, &lookup_entry
, &lookup
);
212 return container_of(e
, struct branch
, ent
);
217 static void die_on_missing_branch(struct repository
*repo
,
218 struct branch
*branch
)
220 /* branch == NULL is always valid because it represents detached HEAD. */
222 branch
!= find_branch(repo
->remote_state
, branch
->name
,
223 strlen(branch
->name
)))
224 die("branch %s was not found in the repository", branch
->name
);
227 static struct branch
*make_branch(struct remote_state
*remote_state
,
228 const char *name
, size_t len
)
232 ret
= find_branch(remote_state
, name
, len
);
236 CALLOC_ARRAY(ret
, 1);
237 ret
->name
= xstrndup(name
, len
);
238 ret
->refname
= xstrfmt("refs/heads/%s", ret
->name
);
240 hashmap_entry_init(&ret
->ent
, memhash(name
, len
));
241 if (hashmap_put_entry(&remote_state
->branches_hash
, ret
, ent
))
242 BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
246 static struct rewrite
*make_rewrite(struct rewrites
*r
,
247 const char *base
, size_t len
)
252 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
253 if (len
== r
->rewrite
[i
]->baselen
&&
254 !strncmp(base
, r
->rewrite
[i
]->base
, len
))
255 return r
->rewrite
[i
];
258 ALLOC_GROW(r
->rewrite
, r
->rewrite_nr
+ 1, r
->rewrite_alloc
);
259 CALLOC_ARRAY(ret
, 1);
260 r
->rewrite
[r
->rewrite_nr
++] = ret
;
261 ret
->base
= xstrndup(base
, len
);
266 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
268 ALLOC_GROW(rewrite
->instead_of
, rewrite
->instead_of_nr
+ 1, rewrite
->instead_of_alloc
);
269 rewrite
->instead_of
[rewrite
->instead_of_nr
].s
= instead_of
;
270 rewrite
->instead_of
[rewrite
->instead_of_nr
].len
= strlen(instead_of
);
271 rewrite
->instead_of_nr
++;
274 static const char *skip_spaces(const char *s
)
281 static void read_remotes_file(struct remote_state
*remote_state
,
282 struct remote
*remote
)
284 struct strbuf buf
= STRBUF_INIT
;
285 FILE *f
= fopen_or_warn(git_path("remotes/%s", remote
->name
), "r");
289 remote
->configured_in_repo
= 1;
290 remote
->origin
= REMOTE_REMOTES
;
291 while (strbuf_getline(&buf
, f
) != EOF
) {
296 if (skip_prefix(buf
.buf
, "URL:", &v
))
297 add_url_alias(remote_state
, remote
,
299 else if (skip_prefix(buf
.buf
, "Push:", &v
))
300 refspec_append(&remote
->push
, skip_spaces(v
));
301 else if (skip_prefix(buf
.buf
, "Pull:", &v
))
302 refspec_append(&remote
->fetch
, skip_spaces(v
));
304 strbuf_release(&buf
);
308 static void read_branches_file(struct remote_state
*remote_state
,
309 struct remote
*remote
)
311 char *frag
, *to_free
= NULL
;
312 struct strbuf buf
= STRBUF_INIT
;
313 FILE *f
= fopen_or_warn(git_path("branches/%s", remote
->name
), "r");
318 strbuf_getline_lf(&buf
, f
);
322 strbuf_release(&buf
);
326 remote
->configured_in_repo
= 1;
327 remote
->origin
= REMOTE_BRANCHES
;
330 * The branches file would have URL and optionally
331 * #branch specified. The default (or specified) branch is
332 * fetched and stored in the local branch matching the
335 frag
= strchr(buf
.buf
, '#');
339 frag
= to_free
= repo_default_branch_name(the_repository
, 0);
341 add_url_alias(remote_state
, remote
, buf
.buf
);
342 refspec_appendf(&remote
->fetch
, "refs/heads/%s:refs/heads/%s",
346 * Cogito compatible push: push current HEAD to remote #branch
347 * (master if missing)
349 refspec_appendf(&remote
->push
, "HEAD:refs/heads/%s", frag
);
350 remote
->fetch_tags
= 1; /* always auto-follow */
352 strbuf_release(&buf
);
356 static int handle_config(const char *key
, const char *value
,
357 const struct config_context
*ctx
, void *cb
)
362 struct remote
*remote
;
363 struct branch
*branch
;
364 struct remote_state
*remote_state
= cb
;
365 const struct key_value_info
*kvi
= ctx
->kvi
;
367 if (parse_config_key(key
, "branch", &name
, &namelen
, &subkey
) >= 0) {
368 /* There is no subsection. */
371 /* There is a subsection, but it is empty. */
374 branch
= make_branch(remote_state
, name
, namelen
);
375 if (!strcmp(subkey
, "remote")) {
376 return git_config_string(&branch
->remote_name
, key
, value
);
377 } else if (!strcmp(subkey
, "pushremote")) {
378 return git_config_string(&branch
->pushremote_name
, key
, value
);
379 } else if (!strcmp(subkey
, "merge")) {
381 return config_error_nonbool(key
);
382 add_merge(branch
, xstrdup(value
));
386 if (parse_config_key(key
, "url", &name
, &namelen
, &subkey
) >= 0) {
387 struct rewrite
*rewrite
;
390 if (!strcmp(subkey
, "insteadof")) {
392 return config_error_nonbool(key
);
393 rewrite
= make_rewrite(&remote_state
->rewrites
, name
,
395 add_instead_of(rewrite
, xstrdup(value
));
396 } else if (!strcmp(subkey
, "pushinsteadof")) {
398 return config_error_nonbool(key
);
399 rewrite
= make_rewrite(&remote_state
->rewrites_push
,
401 add_instead_of(rewrite
, xstrdup(value
));
405 if (parse_config_key(key
, "remote", &name
, &namelen
, &subkey
) < 0)
408 /* Handle remote.* variables */
409 if (!name
&& !strcmp(subkey
, "pushdefault"))
410 return git_config_string(&remote_state
->pushremote_name
, key
,
415 /* Handle remote.<name>.* variables */
417 warning(_("config remote shorthand cannot begin with '/': %s"),
421 remote
= make_remote(remote_state
, name
, namelen
);
422 remote
->origin
= REMOTE_CONFIG
;
423 if (kvi
->scope
== CONFIG_SCOPE_LOCAL
||
424 kvi
->scope
== CONFIG_SCOPE_WORKTREE
)
425 remote
->configured_in_repo
= 1;
426 if (!strcmp(subkey
, "mirror"))
427 remote
->mirror
= git_config_bool(key
, value
);
428 else if (!strcmp(subkey
, "skipdefaultupdate"))
429 remote
->skip_default_update
= git_config_bool(key
, value
);
430 else if (!strcmp(subkey
, "skipfetchall"))
431 remote
->skip_default_update
= git_config_bool(key
, value
);
432 else if (!strcmp(subkey
, "prune"))
433 remote
->prune
= git_config_bool(key
, value
);
434 else if (!strcmp(subkey
, "prunetags"))
435 remote
->prune_tags
= git_config_bool(key
, value
);
436 else if (!strcmp(subkey
, "url")) {
438 return config_error_nonbool(key
);
439 add_url(remote
, value
);
440 } else if (!strcmp(subkey
, "pushurl")) {
442 return config_error_nonbool(key
);
443 add_pushurl(remote
, value
);
444 } else if (!strcmp(subkey
, "push")) {
446 if (git_config_string(&v
, key
, value
))
448 refspec_append(&remote
->push
, v
);
450 } else if (!strcmp(subkey
, "fetch")) {
452 if (git_config_string(&v
, key
, value
))
454 refspec_append(&remote
->fetch
, v
);
456 } else if (!strcmp(subkey
, "receivepack")) {
458 if (git_config_string(&v
, key
, value
))
460 if (!remote
->receivepack
)
461 remote
->receivepack
= v
;
463 error(_("more than one receivepack given, using the first"));
464 } else if (!strcmp(subkey
, "uploadpack")) {
466 if (git_config_string(&v
, key
, value
))
468 if (!remote
->uploadpack
)
469 remote
->uploadpack
= v
;
471 error(_("more than one uploadpack given, using the first"));
472 } else if (!strcmp(subkey
, "tagopt")) {
473 if (!strcmp(value
, "--no-tags"))
474 remote
->fetch_tags
= -1;
475 else if (!strcmp(value
, "--tags"))
476 remote
->fetch_tags
= 2;
477 } else if (!strcmp(subkey
, "proxy")) {
478 return git_config_string(&remote
->http_proxy
,
480 } else if (!strcmp(subkey
, "proxyauthmethod")) {
481 return git_config_string(&remote
->http_proxy_authmethod
,
483 } else if (!strcmp(subkey
, "vcs")) {
484 return git_config_string(&remote
->foreign_vcs
, key
, value
);
489 static void alias_all_urls(struct remote_state
*remote_state
)
492 for (i
= 0; i
< remote_state
->remotes_nr
; i
++) {
493 int add_pushurl_aliases
;
494 if (!remote_state
->remotes
[i
])
496 for (j
= 0; j
< remote_state
->remotes
[i
]->pushurl
.nr
; j
++) {
497 char *alias
= alias_url(remote_state
->remotes
[i
]->pushurl
.v
[j
],
498 &remote_state
->rewrites
);
500 strvec_replace(&remote_state
->remotes
[i
]->pushurl
,
503 add_pushurl_aliases
= remote_state
->remotes
[i
]->pushurl
.nr
== 0;
504 for (j
= 0; j
< remote_state
->remotes
[i
]->url
.nr
; j
++) {
506 if (add_pushurl_aliases
)
508 remote_state
, remote_state
->remotes
[i
],
509 remote_state
->remotes
[i
]->url
.v
[j
]);
510 alias
= alias_url(remote_state
->remotes
[i
]->url
.v
[j
],
511 &remote_state
->rewrites
);
513 strvec_replace(&remote_state
->remotes
[i
]->url
,
519 static void read_config(struct repository
*repo
, int early
)
523 if (repo
->remote_state
->initialized
)
525 repo
->remote_state
->initialized
= 1;
527 repo
->remote_state
->current_branch
= NULL
;
528 if (startup_info
->have_repository
&& !early
) {
529 const char *head_ref
= refs_resolve_ref_unsafe(
530 get_main_ref_store(repo
), "HEAD", 0, NULL
, &flag
);
531 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
532 skip_prefix(head_ref
, "refs/heads/", &head_ref
)) {
533 repo
->remote_state
->current_branch
= make_branch(
534 repo
->remote_state
, head_ref
, strlen(head_ref
));
537 repo_config(repo
, handle_config
, repo
->remote_state
);
538 alias_all_urls(repo
->remote_state
);
541 static int valid_remote_nick(const char *name
)
543 if (!name
[0] || is_dot_or_dotdot(name
))
546 /* remote nicknames cannot contain slashes */
548 if (is_dir_sep(*name
++))
553 static const char *remotes_remote_for_branch(struct remote_state
*remote_state
,
554 struct branch
*branch
,
557 if (branch
&& branch
->remote_name
) {
560 return branch
->remote_name
;
564 if (remote_state
->remotes_nr
== 1)
565 return remote_state
->remotes
[0]->name
;
569 const char *remote_for_branch(struct branch
*branch
, int *explicit)
571 read_config(the_repository
, 0);
572 die_on_missing_branch(the_repository
, branch
);
574 return remotes_remote_for_branch(the_repository
->remote_state
, branch
,
579 remotes_pushremote_for_branch(struct remote_state
*remote_state
,
580 struct branch
*branch
, int *explicit)
582 if (branch
&& branch
->pushremote_name
) {
585 return branch
->pushremote_name
;
587 if (remote_state
->pushremote_name
) {
590 return remote_state
->pushremote_name
;
592 return remotes_remote_for_branch(remote_state
, branch
, explicit);
595 const char *pushremote_for_branch(struct branch
*branch
, int *explicit)
597 read_config(the_repository
, 0);
598 die_on_missing_branch(the_repository
, branch
);
600 return remotes_pushremote_for_branch(the_repository
->remote_state
,
604 static struct remote
*remotes_remote_get(struct remote_state
*remote_state
,
607 const char *remote_ref_for_branch(struct branch
*branch
, int for_push
)
609 read_config(the_repository
, 0);
610 die_on_missing_branch(the_repository
, branch
);
614 if (branch
->merge_nr
) {
615 return branch
->merge_name
[0];
619 *remote_name
= remotes_pushremote_for_branch(
620 the_repository
->remote_state
, branch
,
622 struct remote
*remote
= remotes_remote_get(
623 the_repository
->remote_state
, remote_name
);
625 if (remote
&& remote
->push
.nr
&&
626 (dst
= apply_refspecs(&remote
->push
,
635 static void validate_remote_url(struct remote
*remote
)
639 struct strbuf redacted
= STRBUF_INIT
;
642 if (git_config_get_string_tmp("transfer.credentialsinurl", &value
))
645 if (!strcmp("warn", value
))
647 else if (!strcmp("die", value
))
649 else if (!strcmp("allow", value
))
652 die(_("unrecognized value transfer.credentialsInUrl: '%s'"), value
);
654 for (i
= 0; i
< remote
->url
.nr
; i
++) {
655 struct url_info url_info
= { 0 };
657 if (!url_normalize(remote
->url
.v
[i
], &url_info
) ||
658 !url_info
.passwd_off
)
661 strbuf_reset(&redacted
);
662 strbuf_add(&redacted
, url_info
.url
, url_info
.passwd_off
);
663 strbuf_addstr(&redacted
, "<redacted>");
664 strbuf_addstr(&redacted
,
665 url_info
.url
+ url_info
.passwd_off
+ url_info
.passwd_len
);
668 warning(_("URL '%s' uses plaintext credentials"), redacted
.buf
);
670 die(_("URL '%s' uses plaintext credentials"), redacted
.buf
);
676 strbuf_release(&redacted
);
679 static struct remote
*
680 remotes_remote_get_1(struct remote_state
*remote_state
, const char *name
,
681 const char *(*get_default
)(struct remote_state
*,
682 struct branch
*, int *))
690 name
= get_default(remote_state
, remote_state
->current_branch
,
693 ret
= make_remote(remote_state
, name
, 0);
694 if (valid_remote_nick(name
) && have_git_dir()) {
695 if (!valid_remote(ret
))
696 read_remotes_file(remote_state
, ret
);
697 if (!valid_remote(ret
))
698 read_branches_file(remote_state
, ret
);
700 if (name_given
&& !valid_remote(ret
))
701 add_url_alias(remote_state
, ret
, name
);
702 if (!valid_remote(ret
))
705 validate_remote_url(ret
);
710 static inline struct remote
*
711 remotes_remote_get(struct remote_state
*remote_state
, const char *name
)
713 return remotes_remote_get_1(remote_state
, name
,
714 remotes_remote_for_branch
);
717 struct remote
*remote_get(const char *name
)
719 read_config(the_repository
, 0);
720 return remotes_remote_get(the_repository
->remote_state
, name
);
723 struct remote
*remote_get_early(const char *name
)
725 read_config(the_repository
, 1);
726 return remotes_remote_get(the_repository
->remote_state
, name
);
729 static inline struct remote
*
730 remotes_pushremote_get(struct remote_state
*remote_state
, const char *name
)
732 return remotes_remote_get_1(remote_state
, name
,
733 remotes_pushremote_for_branch
);
736 struct remote
*pushremote_get(const char *name
)
738 read_config(the_repository
, 0);
739 return remotes_pushremote_get(the_repository
->remote_state
, name
);
742 int remote_is_configured(struct remote
*remote
, int in_repo
)
747 return remote
->configured_in_repo
;
748 return !!remote
->origin
;
751 int for_each_remote(each_remote_fn fn
, void *priv
)
754 read_config(the_repository
, 0);
755 for (i
= 0; i
< the_repository
->remote_state
->remotes_nr
&& !result
;
757 struct remote
*remote
=
758 the_repository
->remote_state
->remotes
[i
];
761 result
= fn(remote
, priv
);
766 static void handle_duplicate(struct ref
*ref1
, struct ref
*ref2
)
768 if (strcmp(ref1
->name
, ref2
->name
)) {
769 if (ref1
->fetch_head_status
!= FETCH_HEAD_IGNORE
&&
770 ref2
->fetch_head_status
!= FETCH_HEAD_IGNORE
) {
771 die(_("Cannot fetch both %s and %s to %s"),
772 ref1
->name
, ref2
->name
, ref2
->peer_ref
->name
);
773 } else if (ref1
->fetch_head_status
!= FETCH_HEAD_IGNORE
&&
774 ref2
->fetch_head_status
== FETCH_HEAD_IGNORE
) {
775 warning(_("%s usually tracks %s, not %s"),
776 ref2
->peer_ref
->name
, ref2
->name
, ref1
->name
);
777 } else if (ref1
->fetch_head_status
== FETCH_HEAD_IGNORE
&&
778 ref2
->fetch_head_status
== FETCH_HEAD_IGNORE
) {
779 die(_("%s tracks both %s and %s"),
780 ref2
->peer_ref
->name
, ref1
->name
, ref2
->name
);
783 * This last possibility doesn't occur because
784 * FETCH_HEAD_IGNORE entries always appear at
785 * the end of the list.
787 BUG("Internal error");
790 free(ref2
->peer_ref
);
794 struct ref
*ref_remove_duplicates(struct ref
*ref_map
)
796 struct string_list refs
= STRING_LIST_INIT_NODUP
;
797 struct ref
*retval
= NULL
;
798 struct ref
**p
= &retval
;
801 struct ref
*ref
= ref_map
;
803 ref_map
= ref_map
->next
;
806 if (!ref
->peer_ref
) {
810 struct string_list_item
*item
=
811 string_list_insert(&refs
, ref
->peer_ref
->name
);
814 /* Entry already existed */
815 handle_duplicate((struct ref
*)item
->util
, ref
);
824 string_list_clear(&refs
, 0);
828 int remote_has_url(struct remote
*remote
, const char *url
)
831 for (i
= 0; i
< remote
->url
.nr
; i
++) {
832 if (!strcmp(remote
->url
.v
[i
], url
))
838 struct strvec
*push_url_of_remote(struct remote
*remote
)
840 return remote
->pushurl
.nr
? &remote
->pushurl
: &remote
->url
;
843 static int match_name_with_pattern(const char *key
, const char *name
,
844 const char *value
, char **result
)
846 const char *kstar
= strchr(key
, '*');
852 die(_("key '%s' of pattern had no '*'"), key
);
854 ksuffixlen
= strlen(kstar
+ 1);
855 namelen
= strlen(name
);
856 ret
= !strncmp(name
, key
, klen
) && namelen
>= klen
+ ksuffixlen
&&
857 !memcmp(name
+ namelen
- ksuffixlen
, kstar
+ 1, ksuffixlen
);
859 struct strbuf sb
= STRBUF_INIT
;
860 const char *vstar
= strchr(value
, '*');
862 die(_("value '%s' of pattern has no '*'"), value
);
863 strbuf_add(&sb
, value
, vstar
- value
);
864 strbuf_add(&sb
, name
+ klen
, namelen
- klen
- ksuffixlen
);
865 strbuf_addstr(&sb
, vstar
+ 1);
866 *result
= strbuf_detach(&sb
, NULL
);
871 static int refspec_match(const struct refspec_item
*refspec
,
874 if (refspec
->pattern
)
875 return match_name_with_pattern(refspec
->src
, name
, NULL
, NULL
);
877 return !strcmp(refspec
->src
, name
);
880 int omit_name_by_refspec(const char *name
, struct refspec
*rs
)
884 for (i
= 0; i
< rs
->nr
; i
++) {
885 if (rs
->items
[i
].negative
&& refspec_match(&rs
->items
[i
], name
))
891 struct ref
*apply_negative_refspecs(struct ref
*ref_map
, struct refspec
*rs
)
895 for (tail
= &ref_map
; *tail
; ) {
896 struct ref
*ref
= *tail
;
898 if (omit_name_by_refspec(ref
->name
, rs
)) {
909 static int query_matches_negative_refspec(struct refspec
*rs
, struct refspec_item
*query
)
911 int i
, matched_negative
= 0;
912 int find_src
= !query
->src
;
913 struct string_list reversed
= STRING_LIST_INIT_DUP
;
914 const char *needle
= find_src
? query
->dst
: query
->src
;
917 * Check whether the queried ref matches any negative refpsec. If so,
918 * then we should ultimately treat this as not matching the query at
921 * Note that negative refspecs always match the source, but the query
922 * item uses the destination. To handle this, we apply pattern
923 * refspecs in reverse to figure out if the query source matches any
924 * of the negative refspecs.
926 * The first loop finds and expands all positive refspecs
927 * matched by the queried ref.
929 * The second loop checks if any of the results of the first loop
930 * match any negative refspec.
932 for (i
= 0; i
< rs
->nr
; i
++) {
933 struct refspec_item
*refspec
= &rs
->items
[i
];
936 if (refspec
->negative
)
939 /* Note the reversal of src and dst */
940 if (refspec
->pattern
) {
941 const char *key
= refspec
->dst
? refspec
->dst
: refspec
->src
;
942 const char *value
= refspec
->src
;
944 if (match_name_with_pattern(key
, needle
, value
, &expn_name
))
945 string_list_append_nodup(&reversed
, expn_name
);
946 } else if (refspec
->matching
) {
947 /* For the special matching refspec, any query should match */
948 string_list_append(&reversed
, needle
);
949 } else if (!refspec
->src
) {
950 BUG("refspec->src should not be null here");
951 } else if (!strcmp(needle
, refspec
->src
)) {
952 string_list_append(&reversed
, refspec
->src
);
956 for (i
= 0; !matched_negative
&& i
< reversed
.nr
; i
++) {
957 if (omit_name_by_refspec(reversed
.items
[i
].string
, rs
))
958 matched_negative
= 1;
961 string_list_clear(&reversed
, 0);
963 return matched_negative
;
966 static void query_refspecs_multiple(struct refspec
*rs
,
967 struct refspec_item
*query
,
968 struct string_list
*results
)
971 int find_src
= !query
->src
;
973 if (find_src
&& !query
->dst
)
974 BUG("query_refspecs_multiple: need either src or dst");
976 if (query_matches_negative_refspec(rs
, query
))
979 for (i
= 0; i
< rs
->nr
; i
++) {
980 struct refspec_item
*refspec
= &rs
->items
[i
];
981 const char *key
= find_src
? refspec
->dst
: refspec
->src
;
982 const char *value
= find_src
? refspec
->src
: refspec
->dst
;
983 const char *needle
= find_src
? query
->dst
: query
->src
;
984 char **result
= find_src
? &query
->src
: &query
->dst
;
986 if (!refspec
->dst
|| refspec
->negative
)
988 if (refspec
->pattern
) {
989 if (match_name_with_pattern(key
, needle
, value
, result
))
990 string_list_append_nodup(results
, *result
);
991 } else if (!strcmp(needle
, key
)) {
992 string_list_append(results
, value
);
997 int query_refspecs(struct refspec
*rs
, struct refspec_item
*query
)
1000 int find_src
= !query
->src
;
1001 const char *needle
= find_src
? query
->dst
: query
->src
;
1002 char **result
= find_src
? &query
->src
: &query
->dst
;
1004 if (find_src
&& !query
->dst
)
1005 BUG("query_refspecs: need either src or dst");
1007 if (query_matches_negative_refspec(rs
, query
))
1010 for (i
= 0; i
< rs
->nr
; i
++) {
1011 struct refspec_item
*refspec
= &rs
->items
[i
];
1012 const char *key
= find_src
? refspec
->dst
: refspec
->src
;
1013 const char *value
= find_src
? refspec
->src
: refspec
->dst
;
1015 if (!refspec
->dst
|| refspec
->negative
)
1017 if (refspec
->pattern
) {
1018 if (match_name_with_pattern(key
, needle
, value
, result
)) {
1019 query
->force
= refspec
->force
;
1022 } else if (!strcmp(needle
, key
)) {
1023 *result
= xstrdup(value
);
1024 query
->force
= refspec
->force
;
1031 char *apply_refspecs(struct refspec
*rs
, const char *name
)
1033 struct refspec_item query
;
1035 memset(&query
, 0, sizeof(struct refspec_item
));
1036 query
.src
= (char *)name
;
1038 if (query_refspecs(rs
, &query
))
1044 int remote_find_tracking(struct remote
*remote
, struct refspec_item
*refspec
)
1046 return query_refspecs(&remote
->fetch
, refspec
);
1049 static struct ref
*alloc_ref_with_prefix(const char *prefix
, size_t prefixlen
,
1052 size_t len
= strlen(name
);
1053 struct ref
*ref
= xcalloc(1, st_add4(sizeof(*ref
), prefixlen
, len
, 1));
1054 memcpy(ref
->name
, prefix
, prefixlen
);
1055 memcpy(ref
->name
+ prefixlen
, name
, len
);
1059 struct ref
*alloc_ref(const char *name
)
1061 return alloc_ref_with_prefix("", 0, name
);
1064 struct ref
*copy_ref(const struct ref
*ref
)
1070 len
= st_add3(sizeof(struct ref
), strlen(ref
->name
), 1);
1072 memcpy(cpy
, ref
, len
);
1074 cpy
->symref
= xstrdup_or_null(ref
->symref
);
1075 cpy
->remote_status
= xstrdup_or_null(ref
->remote_status
);
1076 cpy
->peer_ref
= copy_ref(ref
->peer_ref
);
1080 struct ref
*copy_ref_list(const struct ref
*ref
)
1082 struct ref
*ret
= NULL
;
1083 struct ref
**tail
= &ret
;
1085 *tail
= copy_ref(ref
);
1087 tail
= &((*tail
)->next
);
1092 void free_one_ref(struct ref
*ref
)
1096 free_one_ref(ref
->peer_ref
);
1097 free(ref
->remote_status
);
1102 void free_refs(struct ref
*ref
)
1112 int count_refspec_match(const char *pattern
,
1114 struct ref
**matched_ref
)
1116 int patlen
= strlen(pattern
);
1117 struct ref
*matched_weak
= NULL
;
1118 struct ref
*matched
= NULL
;
1122 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
1123 char *name
= refs
->name
;
1124 int namelen
= strlen(name
);
1126 if (!refname_match(pattern
, name
))
1129 /* A match is "weak" if it is with refs outside
1130 * heads or tags, and did not specify the pattern
1131 * in full (e.g. "refs/remotes/origin/master") or at
1132 * least from the toplevel (e.g. "remotes/origin/master");
1133 * otherwise "git push $URL master" would result in
1134 * ambiguity between remotes/origin/master and heads/master
1135 * at the remote site.
1137 if (namelen
!= patlen
&&
1138 patlen
!= namelen
- 5 &&
1139 !starts_with(name
, "refs/heads/") &&
1140 !starts_with(name
, "refs/tags/")) {
1141 /* We want to catch the case where only weak
1142 * matches are found and there are multiple
1143 * matches, and where more than one strong
1144 * matches are found, as ambiguous. One
1145 * strong match with zero or more weak matches
1146 * are acceptable as a unique match.
1148 matched_weak
= refs
;
1158 *matched_ref
= matched_weak
;
1163 *matched_ref
= matched
;
1168 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
1176 static struct ref
*alloc_delete_ref(void)
1178 struct ref
*ref
= alloc_ref("(delete)");
1179 oidclr(&ref
->new_oid
, the_repository
->hash_algo
);
1183 static int try_explicit_object_name(const char *name
,
1186 struct object_id oid
;
1190 *match
= alloc_delete_ref();
1194 if (repo_get_oid(the_repository
, name
, &oid
))
1198 *match
= alloc_ref(name
);
1199 oidcpy(&(*match
)->new_oid
, &oid
);
1204 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
1206 struct ref
*ret
= alloc_ref(name
);
1207 tail_link_ref(ret
, tail
);
1211 static char *guess_ref(const char *name
, struct ref
*peer
)
1213 struct strbuf buf
= STRBUF_INIT
;
1215 const char *r
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
1217 RESOLVE_REF_READING
,
1222 if (starts_with(r
, "refs/heads/")) {
1223 strbuf_addstr(&buf
, "refs/heads/");
1224 } else if (starts_with(r
, "refs/tags/")) {
1225 strbuf_addstr(&buf
, "refs/tags/");
1230 strbuf_addstr(&buf
, name
);
1231 return strbuf_detach(&buf
, NULL
);
1234 static int match_explicit_lhs(struct ref
*src
,
1235 struct refspec_item
*rs
,
1237 int *allocated_match
)
1239 switch (count_refspec_match(rs
->src
, src
, match
)) {
1241 if (allocated_match
)
1242 *allocated_match
= 0;
1245 /* The source could be in the get_sha1() format
1246 * not a reference name. :refs/other is a
1247 * way to delete 'other' ref at the remote end.
1249 if (try_explicit_object_name(rs
->src
, match
) < 0)
1250 return error(_("src refspec %s does not match any"), rs
->src
);
1251 if (allocated_match
)
1252 *allocated_match
= 1;
1255 return error(_("src refspec %s matches more than one"), rs
->src
);
1259 static void show_push_unqualified_ref_name_error(const char *dst_value
,
1260 const char *matched_src_name
)
1262 struct object_id oid
;
1263 enum object_type type
;
1266 * TRANSLATORS: "matches '%s'%" is the <dst> part of "git push
1267 * <remote> <src>:<dst>" push, and "being pushed ('%s')" is
1270 error(_("The destination you provided is not a full refname (i.e.,\n"
1271 "starting with \"refs/\"). We tried to guess what you meant by:\n"
1273 "- Looking for a ref that matches '%s' on the remote side.\n"
1274 "- Checking if the <src> being pushed ('%s')\n"
1275 " is a ref in \"refs/{heads,tags}/\". If so we add a corresponding\n"
1276 " refs/{heads,tags}/ prefix on the remote side.\n"
1278 "Neither worked, so we gave up. You must fully qualify the ref."),
1279 dst_value
, matched_src_name
);
1281 if (!advice_enabled(ADVICE_PUSH_UNQUALIFIED_REF_NAME
))
1284 if (repo_get_oid(the_repository
, matched_src_name
, &oid
))
1285 BUG("'%s' is not a valid object, "
1286 "match_explicit_lhs() should catch this!",
1288 type
= oid_object_info(the_repository
, &oid
, NULL
);
1289 if (type
== OBJ_COMMIT
) {
1290 advise(_("The <src> part of the refspec is a commit object.\n"
1291 "Did you mean to create a new branch by pushing to\n"
1292 "'%s:refs/heads/%s'?"),
1293 matched_src_name
, dst_value
);
1294 } else if (type
== OBJ_TAG
) {
1295 advise(_("The <src> part of the refspec is a tag object.\n"
1296 "Did you mean to create a new tag by pushing to\n"
1297 "'%s:refs/tags/%s'?"),
1298 matched_src_name
, dst_value
);
1299 } else if (type
== OBJ_TREE
) {
1300 advise(_("The <src> part of the refspec is a tree object.\n"
1301 "Did you mean to tag a new tree by pushing to\n"
1302 "'%s:refs/tags/%s'?"),
1303 matched_src_name
, dst_value
);
1304 } else if (type
== OBJ_BLOB
) {
1305 advise(_("The <src> part of the refspec is a blob object.\n"
1306 "Did you mean to tag a new blob by pushing to\n"
1307 "'%s:refs/tags/%s'?"),
1308 matched_src_name
, dst_value
);
1310 BUG("'%s' should be commit/tag/tree/blob, is '%d'",
1311 matched_src_name
, type
);
1315 static int match_explicit(struct ref
*src
, struct ref
*dst
,
1316 struct ref
***dst_tail
,
1317 struct refspec_item
*rs
)
1319 struct ref
*matched_src
, *matched_dst
;
1322 const char *dst_value
= rs
->dst
;
1325 if (rs
->pattern
|| rs
->matching
|| rs
->negative
)
1328 matched_src
= matched_dst
= NULL
;
1329 if (match_explicit_lhs(src
, rs
, &matched_src
, &allocated_src
) < 0)
1335 dst_value
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
1337 RESOLVE_REF_READING
,
1340 ((flag
& REF_ISSYMREF
) &&
1341 !starts_with(dst_value
, "refs/heads/")))
1342 die(_("%s cannot be resolved to branch"),
1346 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
1350 if (starts_with(dst_value
, "refs/")) {
1351 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
1352 } else if (is_null_oid(&matched_src
->new_oid
)) {
1353 error(_("unable to delete '%s': remote ref does not exist"),
1355 } else if ((dst_guess
= guess_ref(dst_value
, matched_src
))) {
1356 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
1359 show_push_unqualified_ref_name_error(dst_value
,
1365 error(_("dst refspec %s matches more than one"),
1371 if (matched_dst
->peer_ref
)
1372 return error(_("dst ref %s receives from more than one src"),
1375 matched_dst
->peer_ref
= allocated_src
?
1377 copy_ref(matched_src
);
1378 matched_dst
->force
= rs
->force
;
1383 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
1384 struct ref
***dst_tail
, struct refspec
*rs
)
1387 for (i
= errs
= 0; i
< rs
->nr
; i
++)
1388 errs
+= match_explicit(src
, dst
, dst_tail
, &rs
->items
[i
]);
1392 static char *get_ref_match(const struct refspec
*rs
, const struct ref
*ref
,
1393 int send_mirror
, int direction
,
1394 const struct refspec_item
**ret_pat
)
1396 const struct refspec_item
*pat
;
1399 int matching_refs
= -1;
1400 for (i
= 0; i
< rs
->nr
; i
++) {
1401 const struct refspec_item
*item
= &rs
->items
[i
];
1406 if (item
->matching
&&
1407 (matching_refs
== -1 || item
->force
)) {
1412 if (item
->pattern
) {
1413 const char *dst_side
= item
->dst
? item
->dst
: item
->src
;
1415 if (direction
== FROM_SRC
)
1416 match
= match_name_with_pattern(item
->src
, ref
->name
, dst_side
, &name
);
1418 match
= match_name_with_pattern(dst_side
, ref
->name
, item
->src
, &name
);
1425 if (matching_refs
== -1)
1428 pat
= &rs
->items
[matching_refs
];
1429 if (pat
->matching
) {
1431 * "matching refs"; traditionally we pushed everything
1432 * including refs outside refs/heads/ hierarchy, but
1433 * that does not make much sense these days.
1435 if (!send_mirror
&& !starts_with(ref
->name
, "refs/heads/"))
1437 name
= xstrdup(ref
->name
);
1444 static struct ref
**tail_ref(struct ref
**head
)
1446 struct ref
**tail
= head
;
1448 tail
= &((*tail
)->next
);
1453 struct commit
**tip
;
1457 static void add_to_tips(struct tips
*tips
, const struct object_id
*oid
)
1459 struct commit
*commit
;
1461 if (is_null_oid(oid
))
1463 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
1464 if (!commit
|| (commit
->object
.flags
& TMP_MARK
))
1466 commit
->object
.flags
|= TMP_MARK
;
1467 ALLOC_GROW(tips
->tip
, tips
->nr
+ 1, tips
->alloc
);
1468 tips
->tip
[tips
->nr
++] = commit
;
1471 static void add_missing_tags(struct ref
*src
, struct ref
**dst
, struct ref
***dst_tail
)
1473 struct string_list dst_tag
= STRING_LIST_INIT_NODUP
;
1474 struct string_list src_tag
= STRING_LIST_INIT_NODUP
;
1475 struct string_list_item
*item
;
1477 struct tips sent_tips
;
1480 * Collect everything we know they would have at the end of
1481 * this push, and collect all tags they have.
1483 memset(&sent_tips
, 0, sizeof(sent_tips
));
1484 for (ref
= *dst
; ref
; ref
= ref
->next
) {
1485 if (ref
->peer_ref
&&
1486 !is_null_oid(&ref
->peer_ref
->new_oid
))
1487 add_to_tips(&sent_tips
, &ref
->peer_ref
->new_oid
);
1489 add_to_tips(&sent_tips
, &ref
->old_oid
);
1490 if (starts_with(ref
->name
, "refs/tags/"))
1491 string_list_append(&dst_tag
, ref
->name
);
1493 clear_commit_marks_many(sent_tips
.nr
, sent_tips
.tip
, TMP_MARK
);
1495 string_list_sort(&dst_tag
);
1497 /* Collect tags they do not have. */
1498 for (ref
= src
; ref
; ref
= ref
->next
) {
1499 if (!starts_with(ref
->name
, "refs/tags/"))
1500 continue; /* not a tag */
1501 if (string_list_has_string(&dst_tag
, ref
->name
))
1502 continue; /* they already have it */
1503 if (oid_object_info(the_repository
, &ref
->new_oid
, NULL
) != OBJ_TAG
)
1504 continue; /* be conservative */
1505 item
= string_list_append(&src_tag
, ref
->name
);
1508 string_list_clear(&dst_tag
, 0);
1511 * At this point, src_tag lists tags that are missing from
1512 * dst, and sent_tips lists the tips we are pushing or those
1513 * that we know they already have. An element in the src_tag
1514 * that is an ancestor of any of the sent_tips needs to be
1515 * sent to the other side.
1518 const int reachable_flag
= 1;
1519 struct commit_list
*found_commits
;
1520 struct commit
**src_commits
;
1521 int nr_src_commits
= 0, alloc_src_commits
= 16;
1522 ALLOC_ARRAY(src_commits
, alloc_src_commits
);
1524 for_each_string_list_item(item
, &src_tag
) {
1525 struct ref
*ref
= item
->util
;
1526 struct commit
*commit
;
1528 if (is_null_oid(&ref
->new_oid
))
1530 commit
= lookup_commit_reference_gently(the_repository
,
1534 /* not pushing a commit, which is not an error */
1537 ALLOC_GROW(src_commits
, nr_src_commits
+ 1, alloc_src_commits
);
1538 src_commits
[nr_src_commits
++] = commit
;
1541 found_commits
= get_reachable_subset(sent_tips
.tip
, sent_tips
.nr
,
1542 src_commits
, nr_src_commits
,
1545 for_each_string_list_item(item
, &src_tag
) {
1546 struct ref
*dst_ref
;
1547 struct ref
*ref
= item
->util
;
1548 struct commit
*commit
;
1550 if (is_null_oid(&ref
->new_oid
))
1552 commit
= lookup_commit_reference_gently(the_repository
,
1556 /* not pushing a commit, which is not an error */
1560 * Is this tag, which they do not have, reachable from
1561 * any of the commits we are sending?
1563 if (!(commit
->object
.flags
& reachable_flag
))
1567 dst_ref
= make_linked_ref(ref
->name
, dst_tail
);
1568 oidcpy(&dst_ref
->new_oid
, &ref
->new_oid
);
1569 dst_ref
->peer_ref
= copy_ref(ref
);
1572 clear_commit_marks_many(nr_src_commits
, src_commits
, reachable_flag
);
1574 free_commit_list(found_commits
);
1577 string_list_clear(&src_tag
, 0);
1578 free(sent_tips
.tip
);
1581 struct ref
*find_ref_by_name(const struct ref
*list
, const char *name
)
1583 for ( ; list
; list
= list
->next
)
1584 if (!strcmp(list
->name
, name
))
1585 return (struct ref
*)list
;
1589 static void prepare_ref_index(struct string_list
*ref_index
, struct ref
*ref
)
1591 for ( ; ref
; ref
= ref
->next
)
1592 string_list_append_nodup(ref_index
, ref
->name
)->util
= ref
;
1594 string_list_sort(ref_index
);
1598 * Given only the set of local refs, sanity-check the set of push
1599 * refspecs. We can't catch all errors that match_push_refs would,
1600 * but we can catch some errors early before even talking to the
1603 int check_push_refs(struct ref
*src
, struct refspec
*rs
)
1608 for (i
= 0; i
< rs
->nr
; i
++) {
1609 struct refspec_item
*item
= &rs
->items
[i
];
1611 if (item
->pattern
|| item
->matching
|| item
->negative
)
1614 ret
|= match_explicit_lhs(src
, item
, NULL
, NULL
);
1621 * Given the set of refs the local repository has, the set of refs the
1622 * remote repository has, and the refspec used for push, determine
1623 * what remote refs we will update and with what value by setting
1624 * peer_ref (which object is being pushed) and force (if the push is
1625 * forced) in elements of "dst". The function may add new elements to
1626 * dst (e.g. pushing to a new branch, done in match_explicit_refs).
1628 int match_push_refs(struct ref
*src
, struct ref
**dst
,
1629 struct refspec
*rs
, int flags
)
1631 int send_all
= flags
& MATCH_REFS_ALL
;
1632 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
1633 int send_prune
= flags
& MATCH_REFS_PRUNE
;
1635 struct ref
*ref
, **dst_tail
= tail_ref(dst
);
1636 struct string_list dst_ref_index
= STRING_LIST_INIT_NODUP
;
1638 /* If no refspec is provided, use the default ":" */
1640 refspec_append(rs
, ":");
1642 errs
= match_explicit_refs(src
, *dst
, &dst_tail
, rs
);
1644 /* pick the remainder */
1645 for (ref
= src
; ref
; ref
= ref
->next
) {
1646 struct string_list_item
*dst_item
;
1647 struct ref
*dst_peer
;
1648 const struct refspec_item
*pat
= NULL
;
1651 dst_name
= get_ref_match(rs
, ref
, send_mirror
, FROM_SRC
, &pat
);
1655 if (!dst_ref_index
.nr
)
1656 prepare_ref_index(&dst_ref_index
, *dst
);
1658 dst_item
= string_list_lookup(&dst_ref_index
, dst_name
);
1659 dst_peer
= dst_item
? dst_item
->util
: NULL
;
1661 if (dst_peer
->peer_ref
)
1662 /* We're already sending something to this ref. */
1665 if (pat
->matching
&& !(send_all
|| send_mirror
))
1667 * Remote doesn't have it, and we have no
1668 * explicit pattern, and we don't have
1669 * --all or --mirror.
1673 /* Create a new one and link it */
1674 dst_peer
= make_linked_ref(dst_name
, &dst_tail
);
1675 oidcpy(&dst_peer
->new_oid
, &ref
->new_oid
);
1676 string_list_insert(&dst_ref_index
,
1677 dst_peer
->name
)->util
= dst_peer
;
1679 dst_peer
->peer_ref
= copy_ref(ref
);
1680 dst_peer
->force
= pat
->force
;
1685 string_list_clear(&dst_ref_index
, 0);
1687 if (flags
& MATCH_REFS_FOLLOW_TAGS
)
1688 add_missing_tags(src
, dst
, &dst_tail
);
1691 struct string_list src_ref_index
= STRING_LIST_INIT_NODUP
;
1692 /* check for missing refs on the remote */
1693 for (ref
= *dst
; ref
; ref
= ref
->next
) {
1697 /* We're already sending something to this ref. */
1700 src_name
= get_ref_match(rs
, ref
, send_mirror
, FROM_DST
, NULL
);
1702 if (!src_ref_index
.nr
)
1703 prepare_ref_index(&src_ref_index
, src
);
1704 if (!string_list_has_string(&src_ref_index
,
1706 ref
->peer_ref
= alloc_delete_ref();
1710 string_list_clear(&src_ref_index
, 0);
1713 *dst
= apply_negative_refspecs(*dst
, rs
);
1720 void set_ref_status_for_push(struct ref
*remote_refs
, int send_mirror
,
1725 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1726 int force_ref_update
= ref
->force
|| force_update
;
1727 int reject_reason
= 0;
1730 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1731 else if (!send_mirror
)
1734 ref
->deletion
= is_null_oid(&ref
->new_oid
);
1735 if (!ref
->deletion
&&
1736 oideq(&ref
->old_oid
, &ref
->new_oid
)) {
1737 ref
->status
= REF_STATUS_UPTODATE
;
1742 * If the remote ref has moved and is now different
1743 * from what we expect, reject any push.
1745 * It also is an error if the user told us to check
1746 * with the remote-tracking branch to find the value
1747 * to expect, but we did not have such a tracking
1750 * If the tip of the remote-tracking ref is unreachable
1751 * from any reflog entry of its local ref indicating a
1752 * possible update since checkout; reject the push.
1754 if (ref
->expect_old_sha1
) {
1755 if (!oideq(&ref
->old_oid
, &ref
->old_oid_expect
))
1756 reject_reason
= REF_STATUS_REJECT_STALE
;
1757 else if (ref
->check_reachable
&& ref
->unreachable
)
1759 REF_STATUS_REJECT_REMOTE_UPDATED
;
1762 * If the ref isn't stale, and is reachable
1763 * from one of the reflog entries of
1764 * the local branch, force the update.
1766 force_ref_update
= 1;
1770 * If the update isn't already rejected then check
1771 * the usual "must fast-forward" rules.
1773 * Decide whether an individual refspec A:B can be
1774 * pushed. The push will succeed if any of the
1775 * following are true:
1777 * (1) the remote reference B does not exist
1779 * (2) the remote reference B is being removed (i.e.,
1780 * pushing :B where no source is specified)
1782 * (3) the destination is not under refs/tags/, and
1783 * if the old and new value is a commit, the new
1784 * is a descendant of the old.
1786 * (4) it is forced using the +A:B notation, or by
1787 * passing the --force argument
1790 if (!reject_reason
&& !ref
->deletion
&& !is_null_oid(&ref
->old_oid
)) {
1791 if (starts_with(ref
->name
, "refs/tags/"))
1792 reject_reason
= REF_STATUS_REJECT_ALREADY_EXISTS
;
1793 else if (!repo_has_object_file_with_flags(the_repository
, &ref
->old_oid
, OBJECT_INFO_SKIP_FETCH_OBJECT
))
1794 reject_reason
= REF_STATUS_REJECT_FETCH_FIRST
;
1795 else if (!lookup_commit_reference_gently(the_repository
, &ref
->old_oid
, 1) ||
1796 !lookup_commit_reference_gently(the_repository
, &ref
->new_oid
, 1))
1797 reject_reason
= REF_STATUS_REJECT_NEEDS_FORCE
;
1798 else if (!ref_newer(&ref
->new_oid
, &ref
->old_oid
))
1799 reject_reason
= REF_STATUS_REJECT_NONFASTFORWARD
;
1803 * "--force" will defeat any rejection implemented
1804 * by the rules above.
1806 if (!force_ref_update
)
1807 ref
->status
= reject_reason
;
1808 else if (reject_reason
)
1809 ref
->forced_update
= 1;
1813 static void set_merge(struct remote_state
*remote_state
, struct branch
*ret
)
1815 struct remote
*remote
;
1817 struct object_id oid
;
1821 return; /* no branch */
1823 return; /* already run */
1824 if (!ret
->remote_name
|| !ret
->merge_nr
) {
1826 * no merge config; let's make sure we don't confuse callers
1827 * with a non-zero merge_nr but a NULL merge
1833 remote
= remotes_remote_get(remote_state
, ret
->remote_name
);
1835 CALLOC_ARRAY(ret
->merge
, ret
->merge_nr
);
1836 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1837 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1838 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1839 if (!remote_find_tracking(remote
, ret
->merge
[i
]) ||
1840 strcmp(ret
->remote_name
, "."))
1842 if (repo_dwim_ref(the_repository
, ret
->merge_name
[i
],
1843 strlen(ret
->merge_name
[i
]), &oid
, &ref
,
1845 ret
->merge
[i
]->dst
= ref
;
1847 ret
->merge
[i
]->dst
= xstrdup(ret
->merge_name
[i
]);
1851 struct branch
*branch_get(const char *name
)
1855 read_config(the_repository
, 0);
1856 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1857 ret
= the_repository
->remote_state
->current_branch
;
1859 ret
= make_branch(the_repository
->remote_state
, name
,
1861 set_merge(the_repository
->remote_state
, ret
);
1865 int branch_has_merge_config(struct branch
*branch
)
1867 return branch
&& !!branch
->merge
;
1870 int branch_merge_matches(struct branch
*branch
,
1872 const char *refname
)
1874 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1876 return refname_match(branch
->merge
[i
]->src
, refname
);
1879 __attribute__((format (printf
,2,3)))
1880 static const char *error_buf(struct strbuf
*err
, const char *fmt
, ...)
1885 strbuf_vaddf(err
, fmt
, ap
);
1891 const char *branch_get_upstream(struct branch
*branch
, struct strbuf
*err
)
1894 return error_buf(err
, _("HEAD does not point to a branch"));
1896 if (!branch
->merge
|| !branch
->merge
[0]) {
1898 * no merge config; is it because the user didn't define any,
1899 * or because it is not a real branch, and get_branch
1902 if (!refs_ref_exists(get_main_ref_store(the_repository
), branch
->refname
))
1903 return error_buf(err
, _("no such branch: '%s'"),
1905 return error_buf(err
,
1906 _("no upstream configured for branch '%s'"),
1910 if (!branch
->merge
[0]->dst
)
1911 return error_buf(err
,
1912 _("upstream branch '%s' not stored as a remote-tracking branch"),
1913 branch
->merge
[0]->src
);
1915 return branch
->merge
[0]->dst
;
1918 static const char *tracking_for_push_dest(struct remote
*remote
,
1919 const char *refname
,
1924 ret
= apply_refspecs(&remote
->fetch
, refname
);
1926 return error_buf(err
,
1927 _("push destination '%s' on remote '%s' has no local tracking branch"),
1928 refname
, remote
->name
);
1932 static const char *branch_get_push_1(struct remote_state
*remote_state
,
1933 struct branch
*branch
, struct strbuf
*err
)
1935 struct remote
*remote
;
1937 remote
= remotes_remote_get(
1939 remotes_pushremote_for_branch(remote_state
, branch
, NULL
));
1941 return error_buf(err
,
1942 _("branch '%s' has no remote for pushing"),
1945 if (remote
->push
.nr
) {
1949 dst
= apply_refspecs(&remote
->push
, branch
->refname
);
1951 return error_buf(err
,
1952 _("push refspecs for '%s' do not include '%s'"),
1953 remote
->name
, branch
->name
);
1955 ret
= tracking_for_push_dest(remote
, dst
, err
);
1961 return tracking_for_push_dest(remote
, branch
->refname
, err
);
1963 switch (push_default
) {
1964 case PUSH_DEFAULT_NOTHING
:
1965 return error_buf(err
, _("push has no destination (push.default is 'nothing')"));
1967 case PUSH_DEFAULT_MATCHING
:
1968 case PUSH_DEFAULT_CURRENT
:
1969 return tracking_for_push_dest(remote
, branch
->refname
, err
);
1971 case PUSH_DEFAULT_UPSTREAM
:
1972 return branch_get_upstream(branch
, err
);
1974 case PUSH_DEFAULT_UNSPECIFIED
:
1975 case PUSH_DEFAULT_SIMPLE
:
1977 const char *up
, *cur
;
1979 up
= branch_get_upstream(branch
, err
);
1982 cur
= tracking_for_push_dest(remote
, branch
->refname
, err
);
1985 if (strcmp(cur
, up
))
1986 return error_buf(err
,
1987 _("cannot resolve 'simple' push to a single destination"));
1992 BUG("unhandled push situation");
1995 const char *branch_get_push(struct branch
*branch
, struct strbuf
*err
)
1997 read_config(the_repository
, 0);
1998 die_on_missing_branch(the_repository
, branch
);
2001 return error_buf(err
, _("HEAD does not point to a branch"));
2003 if (!branch
->push_tracking_ref
)
2004 branch
->push_tracking_ref
= branch_get_push_1(
2005 the_repository
->remote_state
, branch
, err
);
2006 return branch
->push_tracking_ref
;
2009 static int ignore_symref_update(const char *refname
, struct strbuf
*scratch
)
2011 return !refs_read_symbolic_ref(get_main_ref_store(the_repository
), refname
, scratch
);
2015 * Create and return a list of (struct ref) consisting of copies of
2016 * each remote_ref that matches refspec. refspec must be a pattern.
2017 * Fill in the copies' peer_ref to describe the local tracking refs to
2018 * which they map. Omit any references that would map to an existing
2019 * local symbolic ref.
2021 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
2022 const struct refspec_item
*refspec
)
2024 struct strbuf scratch
= STRBUF_INIT
;
2025 const struct ref
*ref
;
2026 struct ref
*ret
= NULL
;
2027 struct ref
**tail
= &ret
;
2029 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
2030 char *expn_name
= NULL
;
2032 strbuf_reset(&scratch
);
2034 if (strchr(ref
->name
, '^'))
2035 continue; /* a dereference item */
2036 if (match_name_with_pattern(refspec
->src
, ref
->name
,
2037 refspec
->dst
, &expn_name
) &&
2038 !ignore_symref_update(expn_name
, &scratch
)) {
2039 struct ref
*cpy
= copy_ref(ref
);
2041 cpy
->peer_ref
= alloc_ref(expn_name
);
2043 cpy
->peer_ref
->force
= 1;
2050 strbuf_release(&scratch
);
2054 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
2056 const struct ref
*ref
;
2057 const struct ref
*best_match
= NULL
;
2060 for (ref
= refs
; ref
; ref
= ref
->next
) {
2061 int score
= refname_match(name
, ref
->name
);
2063 if (best_score
< score
) {
2071 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
2073 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
2078 return copy_ref(ref
);
2081 static struct ref
*get_local_ref(const char *name
)
2083 if (!name
|| name
[0] == '\0')
2086 if (starts_with(name
, "refs/"))
2087 return alloc_ref(name
);
2089 if (starts_with(name
, "heads/") ||
2090 starts_with(name
, "tags/") ||
2091 starts_with(name
, "remotes/"))
2092 return alloc_ref_with_prefix("refs/", 5, name
);
2094 return alloc_ref_with_prefix("refs/heads/", 11, name
);
2097 int get_fetch_map(const struct ref
*remote_refs
,
2098 const struct refspec_item
*refspec
,
2102 struct ref
*ref_map
, **rmp
;
2104 if (refspec
->negative
)
2107 if (refspec
->pattern
) {
2108 ref_map
= get_expanded_map(remote_refs
, refspec
);
2110 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
2112 if (refspec
->exact_sha1
) {
2113 ref_map
= alloc_ref(name
);
2114 get_oid_hex(name
, &ref_map
->old_oid
);
2115 ref_map
->exact_oid
= 1;
2117 ref_map
= get_remote_ref(remote_refs
, name
);
2119 if (!missing_ok
&& !ref_map
)
2120 die(_("couldn't find remote ref %s"), name
);
2122 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
2123 if (ref_map
->peer_ref
&& refspec
->force
)
2124 ref_map
->peer_ref
->force
= 1;
2128 for (rmp
= &ref_map
; *rmp
; ) {
2129 if ((*rmp
)->peer_ref
) {
2130 if (!starts_with((*rmp
)->peer_ref
->name
, "refs/") ||
2131 check_refname_format((*rmp
)->peer_ref
->name
, 0)) {
2132 struct ref
*ignore
= *rmp
;
2133 error(_("* Ignoring funny ref '%s' locally"),
2134 (*rmp
)->peer_ref
->name
);
2135 *rmp
= (*rmp
)->next
;
2136 free(ignore
->peer_ref
);
2141 rmp
= &((*rmp
)->next
);
2145 tail_link_ref(ref_map
, tail
);
2150 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
2154 for (; list
; list
= list
->next
)
2155 if (!strcmp(ref
->symref
, list
->name
)) {
2156 oidcpy(&ref
->old_oid
, &list
->old_oid
);
2163 * Compute the commit ahead/behind values for the pair branch_name, base.
2165 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
2166 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
2167 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
2170 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., ref
2171 * does not exist). Returns 0 if the commits are identical. Returns 1 if
2172 * commits are different.
2175 static int stat_branch_pair(const char *branch_name
, const char *base
,
2176 int *num_ours
, int *num_theirs
,
2177 enum ahead_behind_flags abf
)
2179 struct object_id oid
;
2180 struct commit
*ours
, *theirs
;
2181 struct rev_info revs
;
2182 struct setup_revision_opt opt
= {
2183 .free_removed_argv_elements
= 1,
2185 struct strvec argv
= STRVEC_INIT
;
2187 /* Cannot stat if what we used to build on no longer exists */
2188 if (refs_read_ref(get_main_ref_store(the_repository
), base
, &oid
))
2190 theirs
= lookup_commit_reference(the_repository
, &oid
);
2194 if (refs_read_ref(get_main_ref_store(the_repository
), branch_name
, &oid
))
2196 ours
= lookup_commit_reference(the_repository
, &oid
);
2200 *num_theirs
= *num_ours
= 0;
2202 /* are we the same? */
2205 if (abf
== AHEAD_BEHIND_QUICK
)
2207 if (abf
!= AHEAD_BEHIND_FULL
)
2208 BUG("stat_branch_pair: invalid abf '%d'", abf
);
2210 /* Run "rev-list --left-right ours...theirs" internally... */
2211 strvec_push(&argv
, ""); /* ignored */
2212 strvec_push(&argv
, "--left-right");
2213 strvec_pushf(&argv
, "%s...%s",
2214 oid_to_hex(&ours
->object
.oid
),
2215 oid_to_hex(&theirs
->object
.oid
));
2216 strvec_push(&argv
, "--");
2218 repo_init_revisions(the_repository
, &revs
, NULL
);
2219 setup_revisions(argv
.nr
, argv
.v
, &revs
, &opt
);
2220 if (prepare_revision_walk(&revs
))
2221 die(_("revision walk setup failed"));
2223 /* ... and count the commits on each side. */
2225 struct commit
*c
= get_revision(&revs
);
2228 if (c
->object
.flags
& SYMMETRIC_LEFT
)
2234 /* clear object flags smudged by the above traversal */
2235 clear_commit_marks(ours
, ALL_REV_FLAGS
);
2236 clear_commit_marks(theirs
, ALL_REV_FLAGS
);
2238 strvec_clear(&argv
);
2239 release_revisions(&revs
);
2244 * Lookup the tracking branch for the given branch and if present, optionally
2245 * compute the commit ahead/behind values for the pair.
2247 * If for_push is true, the tracking branch refers to the push branch,
2248 * otherwise it refers to the upstream branch.
2250 * The name of the tracking branch (or NULL if it is not defined) is
2251 * returned via *tracking_name, if it is not itself NULL.
2253 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
2254 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
2255 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
2258 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
2259 * upstream defined, or ref does not exist). Returns 0 if the commits are
2260 * identical. Returns 1 if commits are different.
2262 int stat_tracking_info(struct branch
*branch
, int *num_ours
, int *num_theirs
,
2263 const char **tracking_name
, int for_push
,
2264 enum ahead_behind_flags abf
)
2268 /* Cannot stat unless we are marked to build on top of somebody else. */
2269 base
= for_push
? branch_get_push(branch
, NULL
) :
2270 branch_get_upstream(branch
, NULL
);
2272 *tracking_name
= base
;
2276 return stat_branch_pair(branch
->refname
, base
, num_ours
, num_theirs
, abf
);
2280 * Return true when there is anything to report, otherwise false.
2282 int format_tracking_info(struct branch
*branch
, struct strbuf
*sb
,
2283 enum ahead_behind_flags abf
,
2284 int show_divergence_advice
)
2286 int ours
, theirs
, sti
;
2287 const char *full_base
;
2289 int upstream_is_gone
= 0;
2291 sti
= stat_tracking_info(branch
, &ours
, &theirs
, &full_base
, 0, abf
);
2295 upstream_is_gone
= 1;
2298 base
= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository
),
2300 if (upstream_is_gone
) {
2302 _("Your branch is based on '%s', but the upstream is gone.\n"),
2304 if (advice_enabled(ADVICE_STATUS_HINTS
))
2306 _(" (use \"git branch --unset-upstream\" to fixup)\n"));
2309 _("Your branch is up to date with '%s'.\n"),
2311 } else if (abf
== AHEAD_BEHIND_QUICK
) {
2313 _("Your branch and '%s' refer to different commits.\n"),
2315 if (advice_enabled(ADVICE_STATUS_HINTS
))
2316 strbuf_addf(sb
, _(" (use \"%s\" for details)\n"),
2317 "git status --ahead-behind");
2318 } else if (!theirs
) {
2320 Q_("Your branch is ahead of '%s' by %d commit.\n",
2321 "Your branch is ahead of '%s' by %d commits.\n",
2324 if (advice_enabled(ADVICE_STATUS_HINTS
))
2326 _(" (use \"git push\" to publish your local commits)\n"));
2329 Q_("Your branch is behind '%s' by %d commit, "
2330 "and can be fast-forwarded.\n",
2331 "Your branch is behind '%s' by %d commits, "
2332 "and can be fast-forwarded.\n",
2335 if (advice_enabled(ADVICE_STATUS_HINTS
))
2337 _(" (use \"git pull\" to update your local branch)\n"));
2340 Q_("Your branch and '%s' have diverged,\n"
2341 "and have %d and %d different commit each, "
2343 "Your branch and '%s' have diverged,\n"
2344 "and have %d and %d different commits each, "
2347 base
, ours
, theirs
);
2348 if (show_divergence_advice
&&
2349 advice_enabled(ADVICE_STATUS_HINTS
))
2351 _(" (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
2357 static int one_local_ref(const char *refname
, const struct object_id
*oid
,
2361 struct ref
***local_tail
= cb_data
;
2364 /* we already know it starts with refs/ to get here */
2365 if (check_refname_format(refname
+ 5, 0))
2368 ref
= alloc_ref(refname
);
2369 oidcpy(&ref
->new_oid
, oid
);
2371 *local_tail
= &ref
->next
;
2375 struct ref
*get_local_heads(void)
2377 struct ref
*local_refs
= NULL
, **local_tail
= &local_refs
;
2379 refs_for_each_ref(get_main_ref_store(the_repository
), one_local_ref
,
2384 struct ref
*guess_remote_head(const struct ref
*head
,
2385 const struct ref
*refs
,
2388 const struct ref
*r
;
2389 struct ref
*list
= NULL
;
2390 struct ref
**tail
= &list
;
2396 * Some transports support directly peeking at
2397 * where HEAD points; if that is the case, then
2398 * we don't have to guess.
2401 return copy_ref(find_ref_by_name(refs
, head
->symref
));
2403 /* If a remote branch exists with the default branch name, let's use it. */
2405 char *default_branch
= repo_default_branch_name(the_repository
, 0);
2406 char *ref
= xstrfmt("refs/heads/%s", default_branch
);
2408 r
= find_ref_by_name(refs
, ref
);
2410 free(default_branch
);
2412 if (r
&& oideq(&r
->old_oid
, &head
->old_oid
))
2415 /* Fall back to the hard-coded historical default */
2416 r
= find_ref_by_name(refs
, "refs/heads/master");
2417 if (r
&& oideq(&r
->old_oid
, &head
->old_oid
))
2421 /* Look for another ref that points there */
2422 for (r
= refs
; r
; r
= r
->next
) {
2424 starts_with(r
->name
, "refs/heads/") &&
2425 oideq(&r
->old_oid
, &head
->old_oid
)) {
2426 *tail
= copy_ref(r
);
2427 tail
= &((*tail
)->next
);
2436 struct stale_heads_info
{
2437 struct string_list
*ref_names
;
2438 struct ref
**stale_refs_tail
;
2442 static int get_stale_heads_cb(const char *refname
, const struct object_id
*oid
,
2443 int flags
, void *cb_data
)
2445 struct stale_heads_info
*info
= cb_data
;
2446 struct string_list matches
= STRING_LIST_INIT_DUP
;
2447 struct refspec_item query
;
2449 memset(&query
, 0, sizeof(struct refspec_item
));
2450 query
.dst
= (char *)refname
;
2452 query_refspecs_multiple(info
->rs
, &query
, &matches
);
2453 if (matches
.nr
== 0)
2454 goto clean_exit
; /* No matches */
2457 * If we did find a suitable refspec and it's not a symref and
2458 * it's not in the list of refs that currently exist in that
2459 * remote, we consider it to be stale. In order to deal with
2460 * overlapping refspecs, we need to go over all of the
2463 if (flags
& REF_ISSYMREF
)
2466 for (i
= 0; stale
&& i
< matches
.nr
; i
++)
2467 if (string_list_has_string(info
->ref_names
, matches
.items
[i
].string
))
2471 struct ref
*ref
= make_linked_ref(refname
, &info
->stale_refs_tail
);
2472 oidcpy(&ref
->new_oid
, oid
);
2476 string_list_clear(&matches
, 0);
2480 struct ref
*get_stale_heads(struct refspec
*rs
, struct ref
*fetch_map
)
2482 struct ref
*ref
, *stale_refs
= NULL
;
2483 struct string_list ref_names
= STRING_LIST_INIT_NODUP
;
2484 struct stale_heads_info info
;
2486 info
.ref_names
= &ref_names
;
2487 info
.stale_refs_tail
= &stale_refs
;
2489 for (ref
= fetch_map
; ref
; ref
= ref
->next
)
2490 string_list_append(&ref_names
, ref
->name
);
2491 string_list_sort(&ref_names
);
2492 refs_for_each_ref(get_main_ref_store(the_repository
),
2493 get_stale_heads_cb
, &info
);
2494 string_list_clear(&ref_names
, 0);
2501 static void clear_cas_option(struct push_cas_option
*cas
)
2505 for (i
= 0; i
< cas
->nr
; i
++)
2506 free(cas
->entry
[i
].refname
);
2508 memset(cas
, 0, sizeof(*cas
));
2511 static struct push_cas
*add_cas_entry(struct push_cas_option
*cas
,
2512 const char *refname
,
2515 struct push_cas
*entry
;
2516 ALLOC_GROW(cas
->entry
, cas
->nr
+ 1, cas
->alloc
);
2517 entry
= &cas
->entry
[cas
->nr
++];
2518 memset(entry
, 0, sizeof(*entry
));
2519 entry
->refname
= xmemdupz(refname
, refnamelen
);
2523 static int parse_push_cas_option(struct push_cas_option
*cas
, const char *arg
, int unset
)
2526 struct push_cas
*entry
;
2529 /* "--no-<option>" */
2530 clear_cas_option(cas
);
2535 /* just "--<option>" */
2536 cas
->use_tracking_for_rest
= 1;
2540 /* "--<option>=refname" or "--<option>=refname:value" */
2541 colon
= strchrnul(arg
, ':');
2542 entry
= add_cas_entry(cas
, arg
, colon
- arg
);
2544 entry
->use_tracking
= 1;
2546 oidclr(&entry
->expect
, the_repository
->hash_algo
);
2547 else if (repo_get_oid(the_repository
, colon
+ 1, &entry
->expect
))
2548 return error(_("cannot parse expected object name '%s'"),
2553 int parseopt_push_cas_option(const struct option
*opt
, const char *arg
, int unset
)
2555 return parse_push_cas_option(opt
->value
, arg
, unset
);
2558 int is_empty_cas(const struct push_cas_option
*cas
)
2560 return !cas
->use_tracking_for_rest
&& !cas
->nr
;
2564 * Look at remote.fetch refspec and see if we have a remote
2565 * tracking branch for the refname there. Fill the name of
2566 * the remote-tracking branch in *dst_refname, and the name
2567 * of the commit object at its tip in oid[].
2568 * If we cannot do so, return negative to signal an error.
2570 static int remote_tracking(struct remote
*remote
, const char *refname
,
2571 struct object_id
*oid
, char **dst_refname
)
2575 dst
= apply_refspecs(&remote
->fetch
, refname
);
2577 return -1; /* no tracking ref for refname at remote */
2578 if (refs_read_ref(get_main_ref_store(the_repository
), dst
, oid
))
2579 return -1; /* we know what the tracking ref is but we cannot read it */
2586 * The struct "reflog_commit_array" and related helper functions
2587 * are used for collecting commits into an array during reflog
2588 * traversals in "check_and_collect_until()".
2590 struct reflog_commit_array
{
2591 struct commit
**item
;
2595 #define REFLOG_COMMIT_ARRAY_INIT { 0 }
2597 /* Append a commit to the array. */
2598 static void append_commit(struct reflog_commit_array
*arr
,
2599 struct commit
*commit
)
2601 ALLOC_GROW(arr
->item
, arr
->nr
+ 1, arr
->alloc
);
2602 arr
->item
[arr
->nr
++] = commit
;
2605 /* Free and reset the array. */
2606 static void free_commit_array(struct reflog_commit_array
*arr
)
2608 FREE_AND_NULL(arr
->item
);
2609 arr
->nr
= arr
->alloc
= 0;
2612 struct check_and_collect_until_cb_data
{
2613 struct commit
*remote_commit
;
2614 struct reflog_commit_array
*local_commits
;
2615 timestamp_t remote_reflog_timestamp
;
2618 /* Get the timestamp of the latest entry. */
2619 static int peek_reflog(struct object_id
*o_oid UNUSED
,
2620 struct object_id
*n_oid UNUSED
,
2621 const char *ident UNUSED
,
2622 timestamp_t timestamp
, int tz UNUSED
,
2623 const char *message UNUSED
, void *cb_data
)
2625 timestamp_t
*ts
= cb_data
;
2630 static int check_and_collect_until(struct object_id
*o_oid UNUSED
,
2631 struct object_id
*n_oid
,
2632 const char *ident UNUSED
,
2633 timestamp_t timestamp
, int tz UNUSED
,
2634 const char *message UNUSED
, void *cb_data
)
2636 struct commit
*commit
;
2637 struct check_and_collect_until_cb_data
*cb
= cb_data
;
2639 /* An entry was found. */
2640 if (oideq(n_oid
, &cb
->remote_commit
->object
.oid
))
2643 if ((commit
= lookup_commit_reference(the_repository
, n_oid
)))
2644 append_commit(cb
->local_commits
, commit
);
2647 * If the reflog entry timestamp is older than the remote ref's
2648 * latest reflog entry, there is no need to check or collect
2649 * entries older than this one.
2651 if (timestamp
< cb
->remote_reflog_timestamp
)
2657 #define MERGE_BASES_BATCH_SIZE 8
2660 * Iterate through the reflog of the local ref to check if there is an entry
2661 * for the given remote-tracking ref; runs until the timestamp of an entry is
2662 * older than latest timestamp of remote-tracking ref's reflog. Any commits
2663 * are that seen along the way are collected into an array to check if the
2664 * remote-tracking ref is reachable from any of them.
2666 static int is_reachable_in_reflog(const char *local
, const struct ref
*remote
)
2669 struct commit
*commit
;
2670 struct commit
**chunk
;
2671 struct check_and_collect_until_cb_data cb
;
2672 struct reflog_commit_array arr
= REFLOG_COMMIT_ARRAY_INIT
;
2676 commit
= lookup_commit_reference(the_repository
, &remote
->old_oid
);
2678 goto cleanup_return
;
2681 * Get the timestamp from the latest entry
2682 * of the remote-tracking ref's reflog.
2684 refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository
),
2685 remote
->tracking_ref
, peek_reflog
,
2688 cb
.remote_commit
= commit
;
2689 cb
.local_commits
= &arr
;
2690 cb
.remote_reflog_timestamp
= date
;
2691 ret
= refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository
),
2692 local
, check_and_collect_until
,
2695 /* We found an entry in the reflog. */
2697 goto cleanup_return
;
2700 * Check if the remote commit is reachable from any
2701 * of the commits in the collected array, in batches.
2703 for (chunk
= arr
.item
; chunk
< arr
.item
+ arr
.nr
; chunk
+= size
) {
2704 size
= arr
.item
+ arr
.nr
- chunk
;
2705 if (MERGE_BASES_BATCH_SIZE
< size
)
2706 size
= MERGE_BASES_BATCH_SIZE
;
2708 if ((ret
= repo_in_merge_bases_many(the_repository
, commit
, size
, chunk
, 0)))
2713 free_commit_array(&arr
);
2718 * Check for reachability of a remote-tracking
2719 * ref in the reflog entries of its local ref.
2721 static void check_if_includes_upstream(struct ref
*remote
)
2723 struct ref
*local
= get_local_ref(remote
->name
);
2727 if (is_reachable_in_reflog(local
->name
, remote
) <= 0)
2728 remote
->unreachable
= 1;
2731 static void apply_cas(struct push_cas_option
*cas
,
2732 struct remote
*remote
,
2737 /* Find an explicit --<option>=<name>[:<value>] entry */
2738 for (i
= 0; i
< cas
->nr
; i
++) {
2739 struct push_cas
*entry
= &cas
->entry
[i
];
2740 if (!refname_match(entry
->refname
, ref
->name
))
2742 ref
->expect_old_sha1
= 1;
2743 if (!entry
->use_tracking
)
2744 oidcpy(&ref
->old_oid_expect
, &entry
->expect
);
2745 else if (remote_tracking(remote
, ref
->name
,
2746 &ref
->old_oid_expect
,
2747 &ref
->tracking_ref
))
2748 oidclr(&ref
->old_oid_expect
, the_repository
->hash_algo
);
2750 ref
->check_reachable
= cas
->use_force_if_includes
;
2754 /* Are we using "--<option>" to cover all? */
2755 if (!cas
->use_tracking_for_rest
)
2758 ref
->expect_old_sha1
= 1;
2759 if (remote_tracking(remote
, ref
->name
,
2760 &ref
->old_oid_expect
,
2761 &ref
->tracking_ref
))
2762 oidclr(&ref
->old_oid_expect
, the_repository
->hash_algo
);
2764 ref
->check_reachable
= cas
->use_force_if_includes
;
2767 void apply_push_cas(struct push_cas_option
*cas
,
2768 struct remote
*remote
,
2769 struct ref
*remote_refs
)
2772 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
2773 apply_cas(cas
, remote
, ref
);
2776 * If "compare-and-swap" is in "use_tracking[_for_rest]"
2777 * mode, and if "--force-if-includes" was specified, run
2780 if (ref
->check_reachable
)
2781 check_if_includes_upstream(ref
);
2785 struct remote_state
*remote_state_new(void)
2787 struct remote_state
*r
= xmalloc(sizeof(*r
));
2789 memset(r
, 0, sizeof(*r
));
2791 hashmap_init(&r
->remotes_hash
, remotes_hash_cmp
, NULL
, 0);
2792 hashmap_init(&r
->branches_hash
, branches_hash_cmp
, NULL
, 0);
2796 void remote_state_clear(struct remote_state
*remote_state
)
2800 for (i
= 0; i
< remote_state
->remotes_nr
; i
++)
2801 remote_clear(remote_state
->remotes
[i
]);
2802 FREE_AND_NULL(remote_state
->remotes
);
2803 remote_state
->remotes_alloc
= 0;
2804 remote_state
->remotes_nr
= 0;
2806 hashmap_clear_and_free(&remote_state
->remotes_hash
, struct remote
, ent
);
2807 hashmap_clear_and_free(&remote_state
->branches_hash
, struct remote
, ent
);
2811 * Returns 1 if it was the last chop before ':'.
2813 static int chop_last_dir(char **remoteurl
, int is_relative
)
2815 char *rfind
= find_last_dir_sep(*remoteurl
);
2821 rfind
= strrchr(*remoteurl
, ':');
2827 if (is_relative
|| !strcmp(".", *remoteurl
))
2828 die(_("cannot strip one component off url '%s'"),
2832 *remoteurl
= xstrdup(".");
2836 char *relative_url(const char *remote_url
, const char *url
,
2837 const char *up_path
)
2839 int is_relative
= 0;
2843 struct strbuf sb
= STRBUF_INIT
;
2846 if (!url_is_local_not_ssh(url
) || is_absolute_path(url
))
2847 return xstrdup(url
);
2849 len
= strlen(remote_url
);
2851 BUG("invalid empty remote_url");
2853 remoteurl
= xstrdup(remote_url
);
2854 if (is_dir_sep(remoteurl
[len
-1]))
2855 remoteurl
[len
-1] = '\0';
2857 if (!url_is_local_not_ssh(remoteurl
) || is_absolute_path(remoteurl
))
2862 * Prepend a './' to ensure all relative
2863 * remoteurls start with './' or '../'
2865 if (!starts_with_dot_slash_native(remoteurl
) &&
2866 !starts_with_dot_dot_slash_native(remoteurl
)) {
2868 strbuf_addf(&sb
, "./%s", remoteurl
);
2870 remoteurl
= strbuf_detach(&sb
, NULL
);
2874 * When the url starts with '../', remove that and the
2875 * last directory in remoteurl.
2878 if (starts_with_dot_dot_slash_native(url
)) {
2880 colonsep
|= chop_last_dir(&remoteurl
, is_relative
);
2881 } else if (starts_with_dot_slash_native(url
))
2887 strbuf_addf(&sb
, "%s%s%s", remoteurl
, colonsep
? ":" : "/", url
);
2888 if (ends_with(url
, "/"))
2889 strbuf_setlen(&sb
, sb
.len
- 1);
2892 if (starts_with_dot_slash_native(sb
.buf
))
2893 out
= xstrdup(sb
.buf
+ 2);
2895 out
= xstrdup(sb
.buf
);
2897 if (!up_path
|| !is_relative
) {
2898 strbuf_release(&sb
);
2903 strbuf_addf(&sb
, "%s%s", up_path
, out
);
2905 return strbuf_detach(&sb
, NULL
);