1 #include "git-compat-util.h"
4 #include "environment.h"
11 #include "object-name.h"
12 #include "object-store-ll.h"
19 #include "string-list.h"
21 #include "commit-reach.h"
24 #include "parse-options.h"
26 enum map_direction
{ FROM_SRC
, FROM_DST
};
28 struct counted_string
{
33 static int valid_remote(const struct remote
*remote
)
35 return (!!remote
->url
) || (!!remote
->foreign_vcs
);
38 static const char *alias_url(const char *url
, struct rewrites
*r
)
41 struct counted_string
*longest
;
46 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
49 for (j
= 0; j
< r
->rewrite
[i
]->instead_of_nr
; j
++) {
50 if (starts_with(url
, r
->rewrite
[i
]->instead_of
[j
].s
) &&
52 longest
->len
< r
->rewrite
[i
]->instead_of
[j
].len
)) {
53 longest
= &(r
->rewrite
[i
]->instead_of
[j
]);
61 return xstrfmt("%s%s", r
->rewrite
[longest_i
]->base
, url
+ longest
->len
);
64 static void add_url(struct remote
*remote
, const char *url
)
66 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
67 remote
->url
[remote
->url_nr
++] = url
;
70 static void add_pushurl(struct remote
*remote
, const char *pushurl
)
72 ALLOC_GROW(remote
->pushurl
, remote
->pushurl_nr
+ 1, remote
->pushurl_alloc
);
73 remote
->pushurl
[remote
->pushurl_nr
++] = pushurl
;
76 static void add_pushurl_alias(struct remote_state
*remote_state
,
77 struct remote
*remote
, const char *url
)
79 const char *pushurl
= alias_url(url
, &remote_state
->rewrites_push
);
81 add_pushurl(remote
, pushurl
);
84 static void add_url_alias(struct remote_state
*remote_state
,
85 struct remote
*remote
, const char *url
)
87 add_url(remote
, alias_url(url
, &remote_state
->rewrites
));
88 add_pushurl_alias(remote_state
, remote
, url
);
91 struct remotes_hash_key
{
96 static int remotes_hash_cmp(const void *cmp_data UNUSED
,
97 const struct hashmap_entry
*eptr
,
98 const struct hashmap_entry
*entry_or_key
,
101 const struct remote
*a
, *b
;
102 const struct remotes_hash_key
*key
= keydata
;
104 a
= container_of(eptr
, const struct remote
, ent
);
105 b
= container_of(entry_or_key
, const struct remote
, ent
);
108 return strncmp(a
->name
, key
->str
, key
->len
) || a
->name
[key
->len
];
110 return strcmp(a
->name
, b
->name
);
113 static struct remote
*make_remote(struct remote_state
*remote_state
,
114 const char *name
, int len
)
117 struct remotes_hash_key lookup
;
118 struct hashmap_entry lookup_entry
, *e
;
125 hashmap_entry_init(&lookup_entry
, memhash(name
, len
));
127 e
= hashmap_get(&remote_state
->remotes_hash
, &lookup_entry
, &lookup
);
129 return container_of(e
, struct remote
, ent
);
131 CALLOC_ARRAY(ret
, 1);
132 ret
->prune
= -1; /* unspecified */
133 ret
->prune_tags
= -1; /* unspecified */
134 ret
->name
= xstrndup(name
, len
);
135 refspec_init(&ret
->push
, REFSPEC_PUSH
);
136 refspec_init(&ret
->fetch
, REFSPEC_FETCH
);
138 ALLOC_GROW(remote_state
->remotes
, remote_state
->remotes_nr
+ 1,
139 remote_state
->remotes_alloc
);
140 remote_state
->remotes
[remote_state
->remotes_nr
++] = ret
;
142 hashmap_entry_init(&ret
->ent
, lookup_entry
.hash
);
143 if (hashmap_put_entry(&remote_state
->remotes_hash
, ret
, ent
))
144 BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
148 static void remote_clear(struct remote
*remote
)
152 free((char *)remote
->name
);
153 free((char *)remote
->foreign_vcs
);
155 for (i
= 0; i
< remote
->url_nr
; i
++)
156 free((char *)remote
->url
[i
]);
157 FREE_AND_NULL(remote
->url
);
159 for (i
= 0; i
< remote
->pushurl_nr
; i
++)
160 free((char *)remote
->pushurl
[i
]);
161 FREE_AND_NULL(remote
->pushurl
);
162 free((char *)remote
->receivepack
);
163 free((char *)remote
->uploadpack
);
164 FREE_AND_NULL(remote
->http_proxy
);
165 FREE_AND_NULL(remote
->http_proxy_authmethod
);
168 static void add_merge(struct branch
*branch
, const char *name
)
170 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
171 branch
->merge_alloc
);
172 branch
->merge_name
[branch
->merge_nr
++] = name
;
175 struct branches_hash_key
{
180 static int branches_hash_cmp(const void *cmp_data UNUSED
,
181 const struct hashmap_entry
*eptr
,
182 const struct hashmap_entry
*entry_or_key
,
185 const struct branch
*a
, *b
;
186 const struct branches_hash_key
*key
= keydata
;
188 a
= container_of(eptr
, const struct branch
, ent
);
189 b
= container_of(entry_or_key
, const struct branch
, ent
);
192 return strncmp(a
->name
, key
->str
, key
->len
) ||
195 return strcmp(a
->name
, b
->name
);
198 static struct branch
*find_branch(struct remote_state
*remote_state
,
199 const char *name
, size_t len
)
201 struct branches_hash_key lookup
;
202 struct hashmap_entry lookup_entry
, *e
;
206 hashmap_entry_init(&lookup_entry
, memhash(name
, len
));
208 e
= hashmap_get(&remote_state
->branches_hash
, &lookup_entry
, &lookup
);
210 return container_of(e
, struct branch
, ent
);
215 static void die_on_missing_branch(struct repository
*repo
,
216 struct branch
*branch
)
218 /* branch == NULL is always valid because it represents detached HEAD. */
220 branch
!= find_branch(repo
->remote_state
, branch
->name
,
221 strlen(branch
->name
)))
222 die("branch %s was not found in the repository", branch
->name
);
225 static struct branch
*make_branch(struct remote_state
*remote_state
,
226 const char *name
, size_t len
)
230 ret
= find_branch(remote_state
, name
, len
);
234 CALLOC_ARRAY(ret
, 1);
235 ret
->name
= xstrndup(name
, len
);
236 ret
->refname
= xstrfmt("refs/heads/%s", ret
->name
);
238 hashmap_entry_init(&ret
->ent
, memhash(name
, len
));
239 if (hashmap_put_entry(&remote_state
->branches_hash
, ret
, ent
))
240 BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
244 static struct rewrite
*make_rewrite(struct rewrites
*r
,
245 const char *base
, size_t len
)
250 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
251 if (len
== r
->rewrite
[i
]->baselen
&&
252 !strncmp(base
, r
->rewrite
[i
]->base
, len
))
253 return r
->rewrite
[i
];
256 ALLOC_GROW(r
->rewrite
, r
->rewrite_nr
+ 1, r
->rewrite_alloc
);
257 CALLOC_ARRAY(ret
, 1);
258 r
->rewrite
[r
->rewrite_nr
++] = ret
;
259 ret
->base
= xstrndup(base
, len
);
264 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
266 ALLOC_GROW(rewrite
->instead_of
, rewrite
->instead_of_nr
+ 1, rewrite
->instead_of_alloc
);
267 rewrite
->instead_of
[rewrite
->instead_of_nr
].s
= instead_of
;
268 rewrite
->instead_of
[rewrite
->instead_of_nr
].len
= strlen(instead_of
);
269 rewrite
->instead_of_nr
++;
272 static const char *skip_spaces(const char *s
)
279 static void read_remotes_file(struct remote_state
*remote_state
,
280 struct remote
*remote
)
282 struct strbuf buf
= STRBUF_INIT
;
283 FILE *f
= fopen_or_warn(git_path("remotes/%s", remote
->name
), "r");
287 remote
->configured_in_repo
= 1;
288 remote
->origin
= REMOTE_REMOTES
;
289 while (strbuf_getline(&buf
, f
) != EOF
) {
294 if (skip_prefix(buf
.buf
, "URL:", &v
))
295 add_url_alias(remote_state
, remote
,
296 xstrdup(skip_spaces(v
)));
297 else if (skip_prefix(buf
.buf
, "Push:", &v
))
298 refspec_append(&remote
->push
, skip_spaces(v
));
299 else if (skip_prefix(buf
.buf
, "Pull:", &v
))
300 refspec_append(&remote
->fetch
, skip_spaces(v
));
302 strbuf_release(&buf
);
306 static void read_branches_file(struct remote_state
*remote_state
,
307 struct remote
*remote
)
310 struct strbuf buf
= STRBUF_INIT
;
311 FILE *f
= fopen_or_warn(git_path("branches/%s", remote
->name
), "r");
316 strbuf_getline_lf(&buf
, f
);
320 strbuf_release(&buf
);
324 remote
->configured_in_repo
= 1;
325 remote
->origin
= REMOTE_BRANCHES
;
328 * The branches file would have URL and optionally
329 * #branch specified. The default (or specified) branch is
330 * fetched and stored in the local branch matching the
333 frag
= strchr(buf
.buf
, '#');
337 frag
= (char *)git_default_branch_name(0);
339 add_url_alias(remote_state
, remote
, strbuf_detach(&buf
, NULL
));
340 refspec_appendf(&remote
->fetch
, "refs/heads/%s:refs/heads/%s",
344 * Cogito compatible push: push current HEAD to remote #branch
345 * (master if missing)
347 refspec_appendf(&remote
->push
, "HEAD:refs/heads/%s", frag
);
348 remote
->fetch_tags
= 1; /* always auto-follow */
351 static int handle_config(const char *key
, const char *value
,
352 const struct config_context
*ctx
, void *cb
)
357 struct remote
*remote
;
358 struct branch
*branch
;
359 struct remote_state
*remote_state
= cb
;
360 const struct key_value_info
*kvi
= ctx
->kvi
;
362 if (parse_config_key(key
, "branch", &name
, &namelen
, &subkey
) >= 0) {
363 /* There is no subsection. */
366 /* There is a subsection, but it is empty. */
369 branch
= make_branch(remote_state
, name
, namelen
);
370 if (!strcmp(subkey
, "remote")) {
371 return git_config_string(&branch
->remote_name
, key
, value
);
372 } else if (!strcmp(subkey
, "pushremote")) {
373 return git_config_string(&branch
->pushremote_name
, key
, value
);
374 } else if (!strcmp(subkey
, "merge")) {
376 return config_error_nonbool(key
);
377 add_merge(branch
, xstrdup(value
));
381 if (parse_config_key(key
, "url", &name
, &namelen
, &subkey
) >= 0) {
382 struct rewrite
*rewrite
;
385 if (!strcmp(subkey
, "insteadof")) {
387 return config_error_nonbool(key
);
388 rewrite
= make_rewrite(&remote_state
->rewrites
, name
,
390 add_instead_of(rewrite
, xstrdup(value
));
391 } else if (!strcmp(subkey
, "pushinsteadof")) {
393 return config_error_nonbool(key
);
394 rewrite
= make_rewrite(&remote_state
->rewrites_push
,
396 add_instead_of(rewrite
, xstrdup(value
));
400 if (parse_config_key(key
, "remote", &name
, &namelen
, &subkey
) < 0)
403 /* Handle remote.* variables */
404 if (!name
&& !strcmp(subkey
, "pushdefault"))
405 return git_config_string(&remote_state
->pushremote_name
, key
,
410 /* Handle remote.<name>.* variables */
412 warning(_("config remote shorthand cannot begin with '/': %s"),
416 remote
= make_remote(remote_state
, name
, namelen
);
417 remote
->origin
= REMOTE_CONFIG
;
418 if (kvi
->scope
== CONFIG_SCOPE_LOCAL
||
419 kvi
->scope
== CONFIG_SCOPE_WORKTREE
)
420 remote
->configured_in_repo
= 1;
421 if (!strcmp(subkey
, "mirror"))
422 remote
->mirror
= git_config_bool(key
, value
);
423 else if (!strcmp(subkey
, "skipdefaultupdate"))
424 remote
->skip_default_update
= git_config_bool(key
, value
);
425 else if (!strcmp(subkey
, "skipfetchall"))
426 remote
->skip_default_update
= git_config_bool(key
, value
);
427 else if (!strcmp(subkey
, "prune"))
428 remote
->prune
= git_config_bool(key
, value
);
429 else if (!strcmp(subkey
, "prunetags"))
430 remote
->prune_tags
= git_config_bool(key
, value
);
431 else if (!strcmp(subkey
, "url")) {
433 if (git_config_string(&v
, key
, value
))
436 } else if (!strcmp(subkey
, "pushurl")) {
438 if (git_config_string(&v
, key
, value
))
440 add_pushurl(remote
, v
);
441 } else if (!strcmp(subkey
, "push")) {
443 if (git_config_string(&v
, key
, value
))
445 refspec_append(&remote
->push
, v
);
447 } else if (!strcmp(subkey
, "fetch")) {
449 if (git_config_string(&v
, key
, value
))
451 refspec_append(&remote
->fetch
, v
);
453 } else if (!strcmp(subkey
, "receivepack")) {
455 if (git_config_string(&v
, key
, value
))
457 if (!remote
->receivepack
)
458 remote
->receivepack
= v
;
460 error(_("more than one receivepack given, using the first"));
461 } else if (!strcmp(subkey
, "uploadpack")) {
463 if (git_config_string(&v
, key
, value
))
465 if (!remote
->uploadpack
)
466 remote
->uploadpack
= v
;
468 error(_("more than one uploadpack given, using the first"));
469 } else if (!strcmp(subkey
, "tagopt")) {
470 if (!strcmp(value
, "--no-tags"))
471 remote
->fetch_tags
= -1;
472 else if (!strcmp(value
, "--tags"))
473 remote
->fetch_tags
= 2;
474 } else if (!strcmp(subkey
, "proxy")) {
475 return git_config_string((const char **)&remote
->http_proxy
,
477 } else if (!strcmp(subkey
, "proxyauthmethod")) {
478 return git_config_string((const char **)&remote
->http_proxy_authmethod
,
480 } else if (!strcmp(subkey
, "vcs")) {
481 return git_config_string(&remote
->foreign_vcs
, key
, value
);
486 static void alias_all_urls(struct remote_state
*remote_state
)
489 for (i
= 0; i
< remote_state
->remotes_nr
; i
++) {
490 int add_pushurl_aliases
;
491 if (!remote_state
->remotes
[i
])
493 for (j
= 0; j
< remote_state
->remotes
[i
]->pushurl_nr
; j
++) {
494 remote_state
->remotes
[i
]->pushurl
[j
] =
495 alias_url(remote_state
->remotes
[i
]->pushurl
[j
],
496 &remote_state
->rewrites
);
498 add_pushurl_aliases
= remote_state
->remotes
[i
]->pushurl_nr
== 0;
499 for (j
= 0; j
< remote_state
->remotes
[i
]->url_nr
; j
++) {
500 if (add_pushurl_aliases
)
502 remote_state
, remote_state
->remotes
[i
],
503 remote_state
->remotes
[i
]->url
[j
]);
504 remote_state
->remotes
[i
]->url
[j
] =
505 alias_url(remote_state
->remotes
[i
]->url
[j
],
506 &remote_state
->rewrites
);
511 static void read_config(struct repository
*repo
, int early
)
515 if (repo
->remote_state
->initialized
)
517 repo
->remote_state
->initialized
= 1;
519 repo
->remote_state
->current_branch
= NULL
;
520 if (startup_info
->have_repository
&& !early
) {
521 const char *head_ref
= refs_resolve_ref_unsafe(
522 get_main_ref_store(repo
), "HEAD", 0, NULL
, &flag
);
523 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
524 skip_prefix(head_ref
, "refs/heads/", &head_ref
)) {
525 repo
->remote_state
->current_branch
= make_branch(
526 repo
->remote_state
, head_ref
, strlen(head_ref
));
529 repo_config(repo
, handle_config
, repo
->remote_state
);
530 alias_all_urls(repo
->remote_state
);
533 static int valid_remote_nick(const char *name
)
535 if (!name
[0] || is_dot_or_dotdot(name
))
538 /* remote nicknames cannot contain slashes */
540 if (is_dir_sep(*name
++))
545 static const char *remotes_remote_for_branch(struct remote_state
*remote_state
,
546 struct branch
*branch
,
549 if (branch
&& branch
->remote_name
) {
552 return branch
->remote_name
;
556 if (remote_state
->remotes_nr
== 1)
557 return remote_state
->remotes
[0]->name
;
561 const char *remote_for_branch(struct branch
*branch
, int *explicit)
563 read_config(the_repository
, 0);
564 die_on_missing_branch(the_repository
, branch
);
566 return remotes_remote_for_branch(the_repository
->remote_state
, branch
,
571 remotes_pushremote_for_branch(struct remote_state
*remote_state
,
572 struct branch
*branch
, int *explicit)
574 if (branch
&& branch
->pushremote_name
) {
577 return branch
->pushremote_name
;
579 if (remote_state
->pushremote_name
) {
582 return remote_state
->pushremote_name
;
584 return remotes_remote_for_branch(remote_state
, branch
, explicit);
587 const char *pushremote_for_branch(struct branch
*branch
, int *explicit)
589 read_config(the_repository
, 0);
590 die_on_missing_branch(the_repository
, branch
);
592 return remotes_pushremote_for_branch(the_repository
->remote_state
,
596 static struct remote
*remotes_remote_get(struct remote_state
*remote_state
,
599 const char *remote_ref_for_branch(struct branch
*branch
, int for_push
)
601 read_config(the_repository
, 0);
602 die_on_missing_branch(the_repository
, branch
);
606 if (branch
->merge_nr
) {
607 return branch
->merge_name
[0];
611 *remote_name
= remotes_pushremote_for_branch(
612 the_repository
->remote_state
, branch
,
614 struct remote
*remote
= remotes_remote_get(
615 the_repository
->remote_state
, remote_name
);
617 if (remote
&& remote
->push
.nr
&&
618 (dst
= apply_refspecs(&remote
->push
,
627 static void validate_remote_url(struct remote
*remote
)
631 struct strbuf redacted
= STRBUF_INIT
;
634 if (git_config_get_string_tmp("transfer.credentialsinurl", &value
))
637 if (!strcmp("warn", value
))
639 else if (!strcmp("die", value
))
641 else if (!strcmp("allow", value
))
644 die(_("unrecognized value transfer.credentialsInUrl: '%s'"), value
);
646 for (i
= 0; i
< remote
->url_nr
; i
++) {
647 struct url_info url_info
= { 0 };
649 if (!url_normalize(remote
->url
[i
], &url_info
) ||
650 !url_info
.passwd_off
)
653 strbuf_reset(&redacted
);
654 strbuf_add(&redacted
, url_info
.url
, url_info
.passwd_off
);
655 strbuf_addstr(&redacted
, "<redacted>");
656 strbuf_addstr(&redacted
,
657 url_info
.url
+ url_info
.passwd_off
+ url_info
.passwd_len
);
660 warning(_("URL '%s' uses plaintext credentials"), redacted
.buf
);
662 die(_("URL '%s' uses plaintext credentials"), redacted
.buf
);
668 strbuf_release(&redacted
);
671 static struct remote
*
672 remotes_remote_get_1(struct remote_state
*remote_state
, const char *name
,
673 const char *(*get_default
)(struct remote_state
*,
674 struct branch
*, int *))
682 name
= get_default(remote_state
, remote_state
->current_branch
,
685 ret
= make_remote(remote_state
, name
, 0);
686 if (valid_remote_nick(name
) && have_git_dir()) {
687 if (!valid_remote(ret
))
688 read_remotes_file(remote_state
, ret
);
689 if (!valid_remote(ret
))
690 read_branches_file(remote_state
, ret
);
692 if (name_given
&& !valid_remote(ret
))
693 add_url_alias(remote_state
, ret
, name
);
694 if (!valid_remote(ret
))
697 validate_remote_url(ret
);
702 static inline struct remote
*
703 remotes_remote_get(struct remote_state
*remote_state
, const char *name
)
705 return remotes_remote_get_1(remote_state
, name
,
706 remotes_remote_for_branch
);
709 struct remote
*remote_get(const char *name
)
711 read_config(the_repository
, 0);
712 return remotes_remote_get(the_repository
->remote_state
, name
);
715 struct remote
*remote_get_early(const char *name
)
717 read_config(the_repository
, 1);
718 return remotes_remote_get(the_repository
->remote_state
, name
);
721 static inline struct remote
*
722 remotes_pushremote_get(struct remote_state
*remote_state
, const char *name
)
724 return remotes_remote_get_1(remote_state
, name
,
725 remotes_pushremote_for_branch
);
728 struct remote
*pushremote_get(const char *name
)
730 read_config(the_repository
, 0);
731 return remotes_pushremote_get(the_repository
->remote_state
, name
);
734 int remote_is_configured(struct remote
*remote
, int in_repo
)
739 return remote
->configured_in_repo
;
740 return !!remote
->origin
;
743 int for_each_remote(each_remote_fn fn
, void *priv
)
746 read_config(the_repository
, 0);
747 for (i
= 0; i
< the_repository
->remote_state
->remotes_nr
&& !result
;
749 struct remote
*remote
=
750 the_repository
->remote_state
->remotes
[i
];
753 result
= fn(remote
, priv
);
758 static void handle_duplicate(struct ref
*ref1
, struct ref
*ref2
)
760 if (strcmp(ref1
->name
, ref2
->name
)) {
761 if (ref1
->fetch_head_status
!= FETCH_HEAD_IGNORE
&&
762 ref2
->fetch_head_status
!= FETCH_HEAD_IGNORE
) {
763 die(_("Cannot fetch both %s and %s to %s"),
764 ref1
->name
, ref2
->name
, ref2
->peer_ref
->name
);
765 } else if (ref1
->fetch_head_status
!= FETCH_HEAD_IGNORE
&&
766 ref2
->fetch_head_status
== FETCH_HEAD_IGNORE
) {
767 warning(_("%s usually tracks %s, not %s"),
768 ref2
->peer_ref
->name
, ref2
->name
, ref1
->name
);
769 } else if (ref1
->fetch_head_status
== FETCH_HEAD_IGNORE
&&
770 ref2
->fetch_head_status
== FETCH_HEAD_IGNORE
) {
771 die(_("%s tracks both %s and %s"),
772 ref2
->peer_ref
->name
, ref1
->name
, ref2
->name
);
775 * This last possibility doesn't occur because
776 * FETCH_HEAD_IGNORE entries always appear at
777 * the end of the list.
779 BUG("Internal error");
782 free(ref2
->peer_ref
);
786 struct ref
*ref_remove_duplicates(struct ref
*ref_map
)
788 struct string_list refs
= STRING_LIST_INIT_NODUP
;
789 struct ref
*retval
= NULL
;
790 struct ref
**p
= &retval
;
793 struct ref
*ref
= ref_map
;
795 ref_map
= ref_map
->next
;
798 if (!ref
->peer_ref
) {
802 struct string_list_item
*item
=
803 string_list_insert(&refs
, ref
->peer_ref
->name
);
806 /* Entry already existed */
807 handle_duplicate((struct ref
*)item
->util
, ref
);
816 string_list_clear(&refs
, 0);
820 int remote_has_url(struct remote
*remote
, const char *url
)
823 for (i
= 0; i
< remote
->url_nr
; i
++) {
824 if (!strcmp(remote
->url
[i
], url
))
830 static int match_name_with_pattern(const char *key
, const char *name
,
831 const char *value
, char **result
)
833 const char *kstar
= strchr(key
, '*');
839 die(_("key '%s' of pattern had no '*'"), key
);
841 ksuffixlen
= strlen(kstar
+ 1);
842 namelen
= strlen(name
);
843 ret
= !strncmp(name
, key
, klen
) && namelen
>= klen
+ ksuffixlen
&&
844 !memcmp(name
+ namelen
- ksuffixlen
, kstar
+ 1, ksuffixlen
);
846 struct strbuf sb
= STRBUF_INIT
;
847 const char *vstar
= strchr(value
, '*');
849 die(_("value '%s' of pattern has no '*'"), value
);
850 strbuf_add(&sb
, value
, vstar
- value
);
851 strbuf_add(&sb
, name
+ klen
, namelen
- klen
- ksuffixlen
);
852 strbuf_addstr(&sb
, vstar
+ 1);
853 *result
= strbuf_detach(&sb
, NULL
);
858 static int refspec_match(const struct refspec_item
*refspec
,
861 if (refspec
->pattern
)
862 return match_name_with_pattern(refspec
->src
, name
, NULL
, NULL
);
864 return !strcmp(refspec
->src
, name
);
867 int omit_name_by_refspec(const char *name
, struct refspec
*rs
)
871 for (i
= 0; i
< rs
->nr
; i
++) {
872 if (rs
->items
[i
].negative
&& refspec_match(&rs
->items
[i
], name
))
878 struct ref
*apply_negative_refspecs(struct ref
*ref_map
, struct refspec
*rs
)
882 for (tail
= &ref_map
; *tail
; ) {
883 struct ref
*ref
= *tail
;
885 if (omit_name_by_refspec(ref
->name
, rs
)) {
896 static int query_matches_negative_refspec(struct refspec
*rs
, struct refspec_item
*query
)
898 int i
, matched_negative
= 0;
899 int find_src
= !query
->src
;
900 struct string_list reversed
= STRING_LIST_INIT_DUP
;
901 const char *needle
= find_src
? query
->dst
: query
->src
;
904 * Check whether the queried ref matches any negative refpsec. If so,
905 * then we should ultimately treat this as not matching the query at
908 * Note that negative refspecs always match the source, but the query
909 * item uses the destination. To handle this, we apply pattern
910 * refspecs in reverse to figure out if the query source matches any
911 * of the negative refspecs.
913 * The first loop finds and expands all positive refspecs
914 * matched by the queried ref.
916 * The second loop checks if any of the results of the first loop
917 * match any negative refspec.
919 for (i
= 0; i
< rs
->nr
; i
++) {
920 struct refspec_item
*refspec
= &rs
->items
[i
];
923 if (refspec
->negative
)
926 /* Note the reversal of src and dst */
927 if (refspec
->pattern
) {
928 const char *key
= refspec
->dst
? refspec
->dst
: refspec
->src
;
929 const char *value
= refspec
->src
;
931 if (match_name_with_pattern(key
, needle
, value
, &expn_name
))
932 string_list_append_nodup(&reversed
, expn_name
);
933 } else if (refspec
->matching
) {
934 /* For the special matching refspec, any query should match */
935 string_list_append(&reversed
, needle
);
936 } else if (!refspec
->src
) {
937 BUG("refspec->src should not be null here");
938 } else if (!strcmp(needle
, refspec
->src
)) {
939 string_list_append(&reversed
, refspec
->src
);
943 for (i
= 0; !matched_negative
&& i
< reversed
.nr
; i
++) {
944 if (omit_name_by_refspec(reversed
.items
[i
].string
, rs
))
945 matched_negative
= 1;
948 string_list_clear(&reversed
, 0);
950 return matched_negative
;
953 static void query_refspecs_multiple(struct refspec
*rs
,
954 struct refspec_item
*query
,
955 struct string_list
*results
)
958 int find_src
= !query
->src
;
960 if (find_src
&& !query
->dst
)
961 BUG("query_refspecs_multiple: need either src or dst");
963 if (query_matches_negative_refspec(rs
, query
))
966 for (i
= 0; i
< rs
->nr
; i
++) {
967 struct refspec_item
*refspec
= &rs
->items
[i
];
968 const char *key
= find_src
? refspec
->dst
: refspec
->src
;
969 const char *value
= find_src
? refspec
->src
: refspec
->dst
;
970 const char *needle
= find_src
? query
->dst
: query
->src
;
971 char **result
= find_src
? &query
->src
: &query
->dst
;
973 if (!refspec
->dst
|| refspec
->negative
)
975 if (refspec
->pattern
) {
976 if (match_name_with_pattern(key
, needle
, value
, result
))
977 string_list_append_nodup(results
, *result
);
978 } else if (!strcmp(needle
, key
)) {
979 string_list_append(results
, value
);
984 int query_refspecs(struct refspec
*rs
, struct refspec_item
*query
)
987 int find_src
= !query
->src
;
988 const char *needle
= find_src
? query
->dst
: query
->src
;
989 char **result
= find_src
? &query
->src
: &query
->dst
;
991 if (find_src
&& !query
->dst
)
992 BUG("query_refspecs: need either src or dst");
994 if (query_matches_negative_refspec(rs
, query
))
997 for (i
= 0; i
< rs
->nr
; i
++) {
998 struct refspec_item
*refspec
= &rs
->items
[i
];
999 const char *key
= find_src
? refspec
->dst
: refspec
->src
;
1000 const char *value
= find_src
? refspec
->src
: refspec
->dst
;
1002 if (!refspec
->dst
|| refspec
->negative
)
1004 if (refspec
->pattern
) {
1005 if (match_name_with_pattern(key
, needle
, value
, result
)) {
1006 query
->force
= refspec
->force
;
1009 } else if (!strcmp(needle
, key
)) {
1010 *result
= xstrdup(value
);
1011 query
->force
= refspec
->force
;
1018 char *apply_refspecs(struct refspec
*rs
, const char *name
)
1020 struct refspec_item query
;
1022 memset(&query
, 0, sizeof(struct refspec_item
));
1023 query
.src
= (char *)name
;
1025 if (query_refspecs(rs
, &query
))
1031 int remote_find_tracking(struct remote
*remote
, struct refspec_item
*refspec
)
1033 return query_refspecs(&remote
->fetch
, refspec
);
1036 static struct ref
*alloc_ref_with_prefix(const char *prefix
, size_t prefixlen
,
1039 size_t len
= strlen(name
);
1040 struct ref
*ref
= xcalloc(1, st_add4(sizeof(*ref
), prefixlen
, len
, 1));
1041 memcpy(ref
->name
, prefix
, prefixlen
);
1042 memcpy(ref
->name
+ prefixlen
, name
, len
);
1046 struct ref
*alloc_ref(const char *name
)
1048 return alloc_ref_with_prefix("", 0, name
);
1051 struct ref
*copy_ref(const struct ref
*ref
)
1057 len
= st_add3(sizeof(struct ref
), strlen(ref
->name
), 1);
1059 memcpy(cpy
, ref
, len
);
1061 cpy
->symref
= xstrdup_or_null(ref
->symref
);
1062 cpy
->remote_status
= xstrdup_or_null(ref
->remote_status
);
1063 cpy
->peer_ref
= copy_ref(ref
->peer_ref
);
1067 struct ref
*copy_ref_list(const struct ref
*ref
)
1069 struct ref
*ret
= NULL
;
1070 struct ref
**tail
= &ret
;
1072 *tail
= copy_ref(ref
);
1074 tail
= &((*tail
)->next
);
1079 void free_one_ref(struct ref
*ref
)
1083 free_one_ref(ref
->peer_ref
);
1084 free(ref
->remote_status
);
1089 void free_refs(struct ref
*ref
)
1099 int count_refspec_match(const char *pattern
,
1101 struct ref
**matched_ref
)
1103 int patlen
= strlen(pattern
);
1104 struct ref
*matched_weak
= NULL
;
1105 struct ref
*matched
= NULL
;
1109 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
1110 char *name
= refs
->name
;
1111 int namelen
= strlen(name
);
1113 if (!refname_match(pattern
, name
))
1116 /* A match is "weak" if it is with refs outside
1117 * heads or tags, and did not specify the pattern
1118 * in full (e.g. "refs/remotes/origin/master") or at
1119 * least from the toplevel (e.g. "remotes/origin/master");
1120 * otherwise "git push $URL master" would result in
1121 * ambiguity between remotes/origin/master and heads/master
1122 * at the remote site.
1124 if (namelen
!= patlen
&&
1125 patlen
!= namelen
- 5 &&
1126 !starts_with(name
, "refs/heads/") &&
1127 !starts_with(name
, "refs/tags/")) {
1128 /* We want to catch the case where only weak
1129 * matches are found and there are multiple
1130 * matches, and where more than one strong
1131 * matches are found, as ambiguous. One
1132 * strong match with zero or more weak matches
1133 * are acceptable as a unique match.
1135 matched_weak
= refs
;
1145 *matched_ref
= matched_weak
;
1150 *matched_ref
= matched
;
1155 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
1163 static struct ref
*alloc_delete_ref(void)
1165 struct ref
*ref
= alloc_ref("(delete)");
1166 oidclr(&ref
->new_oid
);
1170 static int try_explicit_object_name(const char *name
,
1173 struct object_id oid
;
1177 *match
= alloc_delete_ref();
1181 if (repo_get_oid(the_repository
, name
, &oid
))
1185 *match
= alloc_ref(name
);
1186 oidcpy(&(*match
)->new_oid
, &oid
);
1191 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
1193 struct ref
*ret
= alloc_ref(name
);
1194 tail_link_ref(ret
, tail
);
1198 static char *guess_ref(const char *name
, struct ref
*peer
)
1200 struct strbuf buf
= STRBUF_INIT
;
1202 const char *r
= resolve_ref_unsafe(peer
->name
, RESOLVE_REF_READING
,
1207 if (starts_with(r
, "refs/heads/")) {
1208 strbuf_addstr(&buf
, "refs/heads/");
1209 } else if (starts_with(r
, "refs/tags/")) {
1210 strbuf_addstr(&buf
, "refs/tags/");
1215 strbuf_addstr(&buf
, name
);
1216 return strbuf_detach(&buf
, NULL
);
1219 static int match_explicit_lhs(struct ref
*src
,
1220 struct refspec_item
*rs
,
1222 int *allocated_match
)
1224 switch (count_refspec_match(rs
->src
, src
, match
)) {
1226 if (allocated_match
)
1227 *allocated_match
= 0;
1230 /* The source could be in the get_sha1() format
1231 * not a reference name. :refs/other is a
1232 * way to delete 'other' ref at the remote end.
1234 if (try_explicit_object_name(rs
->src
, match
) < 0)
1235 return error(_("src refspec %s does not match any"), rs
->src
);
1236 if (allocated_match
)
1237 *allocated_match
= 1;
1240 return error(_("src refspec %s matches more than one"), rs
->src
);
1244 static void show_push_unqualified_ref_name_error(const char *dst_value
,
1245 const char *matched_src_name
)
1247 struct object_id oid
;
1248 enum object_type type
;
1251 * TRANSLATORS: "matches '%s'%" is the <dst> part of "git push
1252 * <remote> <src>:<dst>" push, and "being pushed ('%s')" is
1255 error(_("The destination you provided is not a full refname (i.e.,\n"
1256 "starting with \"refs/\"). We tried to guess what you meant by:\n"
1258 "- Looking for a ref that matches '%s' on the remote side.\n"
1259 "- Checking if the <src> being pushed ('%s')\n"
1260 " is a ref in \"refs/{heads,tags}/\". If so we add a corresponding\n"
1261 " refs/{heads,tags}/ prefix on the remote side.\n"
1263 "Neither worked, so we gave up. You must fully qualify the ref."),
1264 dst_value
, matched_src_name
);
1266 if (!advice_enabled(ADVICE_PUSH_UNQUALIFIED_REF_NAME
))
1269 if (repo_get_oid(the_repository
, matched_src_name
, &oid
))
1270 BUG("'%s' is not a valid object, "
1271 "match_explicit_lhs() should catch this!",
1273 type
= oid_object_info(the_repository
, &oid
, NULL
);
1274 if (type
== OBJ_COMMIT
) {
1275 advise(_("The <src> part of the refspec is a commit object.\n"
1276 "Did you mean to create a new branch by pushing to\n"
1277 "'%s:refs/heads/%s'?"),
1278 matched_src_name
, dst_value
);
1279 } else if (type
== OBJ_TAG
) {
1280 advise(_("The <src> part of the refspec is a tag object.\n"
1281 "Did you mean to create a new tag by pushing to\n"
1282 "'%s:refs/tags/%s'?"),
1283 matched_src_name
, dst_value
);
1284 } else if (type
== OBJ_TREE
) {
1285 advise(_("The <src> part of the refspec is a tree object.\n"
1286 "Did you mean to tag a new tree by pushing to\n"
1287 "'%s:refs/tags/%s'?"),
1288 matched_src_name
, dst_value
);
1289 } else if (type
== OBJ_BLOB
) {
1290 advise(_("The <src> part of the refspec is a blob object.\n"
1291 "Did you mean to tag a new blob by pushing to\n"
1292 "'%s:refs/tags/%s'?"),
1293 matched_src_name
, dst_value
);
1295 BUG("'%s' should be commit/tag/tree/blob, is '%d'",
1296 matched_src_name
, type
);
1300 static int match_explicit(struct ref
*src
, struct ref
*dst
,
1301 struct ref
***dst_tail
,
1302 struct refspec_item
*rs
)
1304 struct ref
*matched_src
, *matched_dst
;
1307 const char *dst_value
= rs
->dst
;
1310 if (rs
->pattern
|| rs
->matching
|| rs
->negative
)
1313 matched_src
= matched_dst
= NULL
;
1314 if (match_explicit_lhs(src
, rs
, &matched_src
, &allocated_src
) < 0)
1320 dst_value
= resolve_ref_unsafe(matched_src
->name
,
1321 RESOLVE_REF_READING
,
1324 ((flag
& REF_ISSYMREF
) &&
1325 !starts_with(dst_value
, "refs/heads/")))
1326 die(_("%s cannot be resolved to branch"),
1330 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
1334 if (starts_with(dst_value
, "refs/")) {
1335 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
1336 } else if (is_null_oid(&matched_src
->new_oid
)) {
1337 error(_("unable to delete '%s': remote ref does not exist"),
1339 } else if ((dst_guess
= guess_ref(dst_value
, matched_src
))) {
1340 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
1343 show_push_unqualified_ref_name_error(dst_value
,
1349 error(_("dst refspec %s matches more than one"),
1355 if (matched_dst
->peer_ref
)
1356 return error(_("dst ref %s receives from more than one src"),
1359 matched_dst
->peer_ref
= allocated_src
?
1361 copy_ref(matched_src
);
1362 matched_dst
->force
= rs
->force
;
1367 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
1368 struct ref
***dst_tail
, struct refspec
*rs
)
1371 for (i
= errs
= 0; i
< rs
->nr
; i
++)
1372 errs
+= match_explicit(src
, dst
, dst_tail
, &rs
->items
[i
]);
1376 static char *get_ref_match(const struct refspec
*rs
, const struct ref
*ref
,
1377 int send_mirror
, int direction
,
1378 const struct refspec_item
**ret_pat
)
1380 const struct refspec_item
*pat
;
1383 int matching_refs
= -1;
1384 for (i
= 0; i
< rs
->nr
; i
++) {
1385 const struct refspec_item
*item
= &rs
->items
[i
];
1390 if (item
->matching
&&
1391 (matching_refs
== -1 || item
->force
)) {
1396 if (item
->pattern
) {
1397 const char *dst_side
= item
->dst
? item
->dst
: item
->src
;
1399 if (direction
== FROM_SRC
)
1400 match
= match_name_with_pattern(item
->src
, ref
->name
, dst_side
, &name
);
1402 match
= match_name_with_pattern(dst_side
, ref
->name
, item
->src
, &name
);
1409 if (matching_refs
== -1)
1412 pat
= &rs
->items
[matching_refs
];
1413 if (pat
->matching
) {
1415 * "matching refs"; traditionally we pushed everything
1416 * including refs outside refs/heads/ hierarchy, but
1417 * that does not make much sense these days.
1419 if (!send_mirror
&& !starts_with(ref
->name
, "refs/heads/"))
1421 name
= xstrdup(ref
->name
);
1428 static struct ref
**tail_ref(struct ref
**head
)
1430 struct ref
**tail
= head
;
1432 tail
= &((*tail
)->next
);
1437 struct commit
**tip
;
1441 static void add_to_tips(struct tips
*tips
, const struct object_id
*oid
)
1443 struct commit
*commit
;
1445 if (is_null_oid(oid
))
1447 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
1448 if (!commit
|| (commit
->object
.flags
& TMP_MARK
))
1450 commit
->object
.flags
|= TMP_MARK
;
1451 ALLOC_GROW(tips
->tip
, tips
->nr
+ 1, tips
->alloc
);
1452 tips
->tip
[tips
->nr
++] = commit
;
1455 static void add_missing_tags(struct ref
*src
, struct ref
**dst
, struct ref
***dst_tail
)
1457 struct string_list dst_tag
= STRING_LIST_INIT_NODUP
;
1458 struct string_list src_tag
= STRING_LIST_INIT_NODUP
;
1459 struct string_list_item
*item
;
1461 struct tips sent_tips
;
1464 * Collect everything we know they would have at the end of
1465 * this push, and collect all tags they have.
1467 memset(&sent_tips
, 0, sizeof(sent_tips
));
1468 for (ref
= *dst
; ref
; ref
= ref
->next
) {
1469 if (ref
->peer_ref
&&
1470 !is_null_oid(&ref
->peer_ref
->new_oid
))
1471 add_to_tips(&sent_tips
, &ref
->peer_ref
->new_oid
);
1473 add_to_tips(&sent_tips
, &ref
->old_oid
);
1474 if (starts_with(ref
->name
, "refs/tags/"))
1475 string_list_append(&dst_tag
, ref
->name
);
1477 clear_commit_marks_many(sent_tips
.nr
, sent_tips
.tip
, TMP_MARK
);
1479 string_list_sort(&dst_tag
);
1481 /* Collect tags they do not have. */
1482 for (ref
= src
; ref
; ref
= ref
->next
) {
1483 if (!starts_with(ref
->name
, "refs/tags/"))
1484 continue; /* not a tag */
1485 if (string_list_has_string(&dst_tag
, ref
->name
))
1486 continue; /* they already have it */
1487 if (oid_object_info(the_repository
, &ref
->new_oid
, NULL
) != OBJ_TAG
)
1488 continue; /* be conservative */
1489 item
= string_list_append(&src_tag
, ref
->name
);
1492 string_list_clear(&dst_tag
, 0);
1495 * At this point, src_tag lists tags that are missing from
1496 * dst, and sent_tips lists the tips we are pushing or those
1497 * that we know they already have. An element in the src_tag
1498 * that is an ancestor of any of the sent_tips needs to be
1499 * sent to the other side.
1502 const int reachable_flag
= 1;
1503 struct commit_list
*found_commits
;
1504 struct commit
**src_commits
;
1505 int nr_src_commits
= 0, alloc_src_commits
= 16;
1506 ALLOC_ARRAY(src_commits
, alloc_src_commits
);
1508 for_each_string_list_item(item
, &src_tag
) {
1509 struct ref
*ref
= item
->util
;
1510 struct commit
*commit
;
1512 if (is_null_oid(&ref
->new_oid
))
1514 commit
= lookup_commit_reference_gently(the_repository
,
1518 /* not pushing a commit, which is not an error */
1521 ALLOC_GROW(src_commits
, nr_src_commits
+ 1, alloc_src_commits
);
1522 src_commits
[nr_src_commits
++] = commit
;
1525 found_commits
= get_reachable_subset(sent_tips
.tip
, sent_tips
.nr
,
1526 src_commits
, nr_src_commits
,
1529 for_each_string_list_item(item
, &src_tag
) {
1530 struct ref
*dst_ref
;
1531 struct ref
*ref
= item
->util
;
1532 struct commit
*commit
;
1534 if (is_null_oid(&ref
->new_oid
))
1536 commit
= lookup_commit_reference_gently(the_repository
,
1540 /* not pushing a commit, which is not an error */
1544 * Is this tag, which they do not have, reachable from
1545 * any of the commits we are sending?
1547 if (!(commit
->object
.flags
& reachable_flag
))
1551 dst_ref
= make_linked_ref(ref
->name
, dst_tail
);
1552 oidcpy(&dst_ref
->new_oid
, &ref
->new_oid
);
1553 dst_ref
->peer_ref
= copy_ref(ref
);
1556 clear_commit_marks_many(nr_src_commits
, src_commits
, reachable_flag
);
1558 free_commit_list(found_commits
);
1561 string_list_clear(&src_tag
, 0);
1562 free(sent_tips
.tip
);
1565 struct ref
*find_ref_by_name(const struct ref
*list
, const char *name
)
1567 for ( ; list
; list
= list
->next
)
1568 if (!strcmp(list
->name
, name
))
1569 return (struct ref
*)list
;
1573 static void prepare_ref_index(struct string_list
*ref_index
, struct ref
*ref
)
1575 for ( ; ref
; ref
= ref
->next
)
1576 string_list_append_nodup(ref_index
, ref
->name
)->util
= ref
;
1578 string_list_sort(ref_index
);
1582 * Given only the set of local refs, sanity-check the set of push
1583 * refspecs. We can't catch all errors that match_push_refs would,
1584 * but we can catch some errors early before even talking to the
1587 int check_push_refs(struct ref
*src
, struct refspec
*rs
)
1592 for (i
= 0; i
< rs
->nr
; i
++) {
1593 struct refspec_item
*item
= &rs
->items
[i
];
1595 if (item
->pattern
|| item
->matching
|| item
->negative
)
1598 ret
|= match_explicit_lhs(src
, item
, NULL
, NULL
);
1605 * Given the set of refs the local repository has, the set of refs the
1606 * remote repository has, and the refspec used for push, determine
1607 * what remote refs we will update and with what value by setting
1608 * peer_ref (which object is being pushed) and force (if the push is
1609 * forced) in elements of "dst". The function may add new elements to
1610 * dst (e.g. pushing to a new branch, done in match_explicit_refs).
1612 int match_push_refs(struct ref
*src
, struct ref
**dst
,
1613 struct refspec
*rs
, int flags
)
1615 int send_all
= flags
& MATCH_REFS_ALL
;
1616 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
1617 int send_prune
= flags
& MATCH_REFS_PRUNE
;
1619 struct ref
*ref
, **dst_tail
= tail_ref(dst
);
1620 struct string_list dst_ref_index
= STRING_LIST_INIT_NODUP
;
1622 /* If no refspec is provided, use the default ":" */
1624 refspec_append(rs
, ":");
1626 errs
= match_explicit_refs(src
, *dst
, &dst_tail
, rs
);
1628 /* pick the remainder */
1629 for (ref
= src
; ref
; ref
= ref
->next
) {
1630 struct string_list_item
*dst_item
;
1631 struct ref
*dst_peer
;
1632 const struct refspec_item
*pat
= NULL
;
1635 dst_name
= get_ref_match(rs
, ref
, send_mirror
, FROM_SRC
, &pat
);
1639 if (!dst_ref_index
.nr
)
1640 prepare_ref_index(&dst_ref_index
, *dst
);
1642 dst_item
= string_list_lookup(&dst_ref_index
, dst_name
);
1643 dst_peer
= dst_item
? dst_item
->util
: NULL
;
1645 if (dst_peer
->peer_ref
)
1646 /* We're already sending something to this ref. */
1649 if (pat
->matching
&& !(send_all
|| send_mirror
))
1651 * Remote doesn't have it, and we have no
1652 * explicit pattern, and we don't have
1653 * --all or --mirror.
1657 /* Create a new one and link it */
1658 dst_peer
= make_linked_ref(dst_name
, &dst_tail
);
1659 oidcpy(&dst_peer
->new_oid
, &ref
->new_oid
);
1660 string_list_insert(&dst_ref_index
,
1661 dst_peer
->name
)->util
= dst_peer
;
1663 dst_peer
->peer_ref
= copy_ref(ref
);
1664 dst_peer
->force
= pat
->force
;
1669 string_list_clear(&dst_ref_index
, 0);
1671 if (flags
& MATCH_REFS_FOLLOW_TAGS
)
1672 add_missing_tags(src
, dst
, &dst_tail
);
1675 struct string_list src_ref_index
= STRING_LIST_INIT_NODUP
;
1676 /* check for missing refs on the remote */
1677 for (ref
= *dst
; ref
; ref
= ref
->next
) {
1681 /* We're already sending something to this ref. */
1684 src_name
= get_ref_match(rs
, ref
, send_mirror
, FROM_DST
, NULL
);
1686 if (!src_ref_index
.nr
)
1687 prepare_ref_index(&src_ref_index
, src
);
1688 if (!string_list_has_string(&src_ref_index
,
1690 ref
->peer_ref
= alloc_delete_ref();
1694 string_list_clear(&src_ref_index
, 0);
1697 *dst
= apply_negative_refspecs(*dst
, rs
);
1704 void set_ref_status_for_push(struct ref
*remote_refs
, int send_mirror
,
1709 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1710 int force_ref_update
= ref
->force
|| force_update
;
1711 int reject_reason
= 0;
1714 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1715 else if (!send_mirror
)
1718 ref
->deletion
= is_null_oid(&ref
->new_oid
);
1719 if (!ref
->deletion
&&
1720 oideq(&ref
->old_oid
, &ref
->new_oid
)) {
1721 ref
->status
= REF_STATUS_UPTODATE
;
1726 * If the remote ref has moved and is now different
1727 * from what we expect, reject any push.
1729 * It also is an error if the user told us to check
1730 * with the remote-tracking branch to find the value
1731 * to expect, but we did not have such a tracking
1734 * If the tip of the remote-tracking ref is unreachable
1735 * from any reflog entry of its local ref indicating a
1736 * possible update since checkout; reject the push.
1738 if (ref
->expect_old_sha1
) {
1739 if (!oideq(&ref
->old_oid
, &ref
->old_oid_expect
))
1740 reject_reason
= REF_STATUS_REJECT_STALE
;
1741 else if (ref
->check_reachable
&& ref
->unreachable
)
1743 REF_STATUS_REJECT_REMOTE_UPDATED
;
1746 * If the ref isn't stale, and is reachable
1747 * from one of the reflog entries of
1748 * the local branch, force the update.
1750 force_ref_update
= 1;
1754 * If the update isn't already rejected then check
1755 * the usual "must fast-forward" rules.
1757 * Decide whether an individual refspec A:B can be
1758 * pushed. The push will succeed if any of the
1759 * following are true:
1761 * (1) the remote reference B does not exist
1763 * (2) the remote reference B is being removed (i.e.,
1764 * pushing :B where no source is specified)
1766 * (3) the destination is not under refs/tags/, and
1767 * if the old and new value is a commit, the new
1768 * is a descendant of the old.
1770 * (4) it is forced using the +A:B notation, or by
1771 * passing the --force argument
1774 if (!reject_reason
&& !ref
->deletion
&& !is_null_oid(&ref
->old_oid
)) {
1775 if (starts_with(ref
->name
, "refs/tags/"))
1776 reject_reason
= REF_STATUS_REJECT_ALREADY_EXISTS
;
1777 else if (!repo_has_object_file(the_repository
, &ref
->old_oid
))
1778 reject_reason
= REF_STATUS_REJECT_FETCH_FIRST
;
1779 else if (!lookup_commit_reference_gently(the_repository
, &ref
->old_oid
, 1) ||
1780 !lookup_commit_reference_gently(the_repository
, &ref
->new_oid
, 1))
1781 reject_reason
= REF_STATUS_REJECT_NEEDS_FORCE
;
1782 else if (!ref_newer(&ref
->new_oid
, &ref
->old_oid
))
1783 reject_reason
= REF_STATUS_REJECT_NONFASTFORWARD
;
1787 * "--force" will defeat any rejection implemented
1788 * by the rules above.
1790 if (!force_ref_update
)
1791 ref
->status
= reject_reason
;
1792 else if (reject_reason
)
1793 ref
->forced_update
= 1;
1797 static void set_merge(struct remote_state
*remote_state
, struct branch
*ret
)
1799 struct remote
*remote
;
1801 struct object_id oid
;
1805 return; /* no branch */
1807 return; /* already run */
1808 if (!ret
->remote_name
|| !ret
->merge_nr
) {
1810 * no merge config; let's make sure we don't confuse callers
1811 * with a non-zero merge_nr but a NULL merge
1817 remote
= remotes_remote_get(remote_state
, ret
->remote_name
);
1819 CALLOC_ARRAY(ret
->merge
, ret
->merge_nr
);
1820 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1821 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1822 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1823 if (!remote_find_tracking(remote
, ret
->merge
[i
]) ||
1824 strcmp(ret
->remote_name
, "."))
1826 if (repo_dwim_ref(the_repository
, ret
->merge_name
[i
],
1827 strlen(ret
->merge_name
[i
]), &oid
, &ref
,
1829 ret
->merge
[i
]->dst
= ref
;
1831 ret
->merge
[i
]->dst
= xstrdup(ret
->merge_name
[i
]);
1835 struct branch
*branch_get(const char *name
)
1839 read_config(the_repository
, 0);
1840 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1841 ret
= the_repository
->remote_state
->current_branch
;
1843 ret
= make_branch(the_repository
->remote_state
, name
,
1845 set_merge(the_repository
->remote_state
, ret
);
1849 int branch_has_merge_config(struct branch
*branch
)
1851 return branch
&& !!branch
->merge
;
1854 int branch_merge_matches(struct branch
*branch
,
1856 const char *refname
)
1858 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1860 return refname_match(branch
->merge
[i
]->src
, refname
);
1863 __attribute__((format (printf
,2,3)))
1864 static const char *error_buf(struct strbuf
*err
, const char *fmt
, ...)
1869 strbuf_vaddf(err
, fmt
, ap
);
1875 const char *branch_get_upstream(struct branch
*branch
, struct strbuf
*err
)
1878 return error_buf(err
, _("HEAD does not point to a branch"));
1880 if (!branch
->merge
|| !branch
->merge
[0]) {
1882 * no merge config; is it because the user didn't define any,
1883 * or because it is not a real branch, and get_branch
1886 if (!ref_exists(branch
->refname
))
1887 return error_buf(err
, _("no such branch: '%s'"),
1889 return error_buf(err
,
1890 _("no upstream configured for branch '%s'"),
1894 if (!branch
->merge
[0]->dst
)
1895 return error_buf(err
,
1896 _("upstream branch '%s' not stored as a remote-tracking branch"),
1897 branch
->merge
[0]->src
);
1899 return branch
->merge
[0]->dst
;
1902 static const char *tracking_for_push_dest(struct remote
*remote
,
1903 const char *refname
,
1908 ret
= apply_refspecs(&remote
->fetch
, refname
);
1910 return error_buf(err
,
1911 _("push destination '%s' on remote '%s' has no local tracking branch"),
1912 refname
, remote
->name
);
1916 static const char *branch_get_push_1(struct remote_state
*remote_state
,
1917 struct branch
*branch
, struct strbuf
*err
)
1919 struct remote
*remote
;
1921 remote
= remotes_remote_get(
1923 remotes_pushremote_for_branch(remote_state
, branch
, NULL
));
1925 return error_buf(err
,
1926 _("branch '%s' has no remote for pushing"),
1929 if (remote
->push
.nr
) {
1933 dst
= apply_refspecs(&remote
->push
, branch
->refname
);
1935 return error_buf(err
,
1936 _("push refspecs for '%s' do not include '%s'"),
1937 remote
->name
, branch
->name
);
1939 ret
= tracking_for_push_dest(remote
, dst
, err
);
1945 return tracking_for_push_dest(remote
, branch
->refname
, err
);
1947 switch (push_default
) {
1948 case PUSH_DEFAULT_NOTHING
:
1949 return error_buf(err
, _("push has no destination (push.default is 'nothing')"));
1951 case PUSH_DEFAULT_MATCHING
:
1952 case PUSH_DEFAULT_CURRENT
:
1953 return tracking_for_push_dest(remote
, branch
->refname
, err
);
1955 case PUSH_DEFAULT_UPSTREAM
:
1956 return branch_get_upstream(branch
, err
);
1958 case PUSH_DEFAULT_UNSPECIFIED
:
1959 case PUSH_DEFAULT_SIMPLE
:
1961 const char *up
, *cur
;
1963 up
= branch_get_upstream(branch
, err
);
1966 cur
= tracking_for_push_dest(remote
, branch
->refname
, err
);
1969 if (strcmp(cur
, up
))
1970 return error_buf(err
,
1971 _("cannot resolve 'simple' push to a single destination"));
1976 BUG("unhandled push situation");
1979 const char *branch_get_push(struct branch
*branch
, struct strbuf
*err
)
1981 read_config(the_repository
, 0);
1982 die_on_missing_branch(the_repository
, branch
);
1985 return error_buf(err
, _("HEAD does not point to a branch"));
1987 if (!branch
->push_tracking_ref
)
1988 branch
->push_tracking_ref
= branch_get_push_1(
1989 the_repository
->remote_state
, branch
, err
);
1990 return branch
->push_tracking_ref
;
1993 static int ignore_symref_update(const char *refname
, struct strbuf
*scratch
)
1995 return !refs_read_symbolic_ref(get_main_ref_store(the_repository
), refname
, scratch
);
1999 * Create and return a list of (struct ref) consisting of copies of
2000 * each remote_ref that matches refspec. refspec must be a pattern.
2001 * Fill in the copies' peer_ref to describe the local tracking refs to
2002 * which they map. Omit any references that would map to an existing
2003 * local symbolic ref.
2005 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
2006 const struct refspec_item
*refspec
)
2008 struct strbuf scratch
= STRBUF_INIT
;
2009 const struct ref
*ref
;
2010 struct ref
*ret
= NULL
;
2011 struct ref
**tail
= &ret
;
2013 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
2014 char *expn_name
= NULL
;
2016 strbuf_reset(&scratch
);
2018 if (strchr(ref
->name
, '^'))
2019 continue; /* a dereference item */
2020 if (match_name_with_pattern(refspec
->src
, ref
->name
,
2021 refspec
->dst
, &expn_name
) &&
2022 !ignore_symref_update(expn_name
, &scratch
)) {
2023 struct ref
*cpy
= copy_ref(ref
);
2025 cpy
->peer_ref
= alloc_ref(expn_name
);
2027 cpy
->peer_ref
->force
= 1;
2034 strbuf_release(&scratch
);
2038 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
2040 const struct ref
*ref
;
2041 const struct ref
*best_match
= NULL
;
2044 for (ref
= refs
; ref
; ref
= ref
->next
) {
2045 int score
= refname_match(name
, ref
->name
);
2047 if (best_score
< score
) {
2055 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
2057 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
2062 return copy_ref(ref
);
2065 static struct ref
*get_local_ref(const char *name
)
2067 if (!name
|| name
[0] == '\0')
2070 if (starts_with(name
, "refs/"))
2071 return alloc_ref(name
);
2073 if (starts_with(name
, "heads/") ||
2074 starts_with(name
, "tags/") ||
2075 starts_with(name
, "remotes/"))
2076 return alloc_ref_with_prefix("refs/", 5, name
);
2078 return alloc_ref_with_prefix("refs/heads/", 11, name
);
2081 int get_fetch_map(const struct ref
*remote_refs
,
2082 const struct refspec_item
*refspec
,
2086 struct ref
*ref_map
, **rmp
;
2088 if (refspec
->negative
)
2091 if (refspec
->pattern
) {
2092 ref_map
= get_expanded_map(remote_refs
, refspec
);
2094 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
2096 if (refspec
->exact_sha1
) {
2097 ref_map
= alloc_ref(name
);
2098 get_oid_hex(name
, &ref_map
->old_oid
);
2099 ref_map
->exact_oid
= 1;
2101 ref_map
= get_remote_ref(remote_refs
, name
);
2103 if (!missing_ok
&& !ref_map
)
2104 die(_("couldn't find remote ref %s"), name
);
2106 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
2107 if (ref_map
->peer_ref
&& refspec
->force
)
2108 ref_map
->peer_ref
->force
= 1;
2112 for (rmp
= &ref_map
; *rmp
; ) {
2113 if ((*rmp
)->peer_ref
) {
2114 if (!starts_with((*rmp
)->peer_ref
->name
, "refs/") ||
2115 check_refname_format((*rmp
)->peer_ref
->name
, 0)) {
2116 struct ref
*ignore
= *rmp
;
2117 error(_("* Ignoring funny ref '%s' locally"),
2118 (*rmp
)->peer_ref
->name
);
2119 *rmp
= (*rmp
)->next
;
2120 free(ignore
->peer_ref
);
2125 rmp
= &((*rmp
)->next
);
2129 tail_link_ref(ref_map
, tail
);
2134 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
2138 for (; list
; list
= list
->next
)
2139 if (!strcmp(ref
->symref
, list
->name
)) {
2140 oidcpy(&ref
->old_oid
, &list
->old_oid
);
2147 * Compute the commit ahead/behind values for the pair branch_name, base.
2149 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
2150 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
2151 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
2154 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., ref
2155 * does not exist). Returns 0 if the commits are identical. Returns 1 if
2156 * commits are different.
2159 static int stat_branch_pair(const char *branch_name
, const char *base
,
2160 int *num_ours
, int *num_theirs
,
2161 enum ahead_behind_flags abf
)
2163 struct object_id oid
;
2164 struct commit
*ours
, *theirs
;
2165 struct rev_info revs
;
2166 struct setup_revision_opt opt
= {
2167 .free_removed_argv_elements
= 1,
2169 struct strvec argv
= STRVEC_INIT
;
2171 /* Cannot stat if what we used to build on no longer exists */
2172 if (read_ref(base
, &oid
))
2174 theirs
= lookup_commit_reference(the_repository
, &oid
);
2178 if (read_ref(branch_name
, &oid
))
2180 ours
= lookup_commit_reference(the_repository
, &oid
);
2184 *num_theirs
= *num_ours
= 0;
2186 /* are we the same? */
2189 if (abf
== AHEAD_BEHIND_QUICK
)
2191 if (abf
!= AHEAD_BEHIND_FULL
)
2192 BUG("stat_branch_pair: invalid abf '%d'", abf
);
2194 /* Run "rev-list --left-right ours...theirs" internally... */
2195 strvec_push(&argv
, ""); /* ignored */
2196 strvec_push(&argv
, "--left-right");
2197 strvec_pushf(&argv
, "%s...%s",
2198 oid_to_hex(&ours
->object
.oid
),
2199 oid_to_hex(&theirs
->object
.oid
));
2200 strvec_push(&argv
, "--");
2202 repo_init_revisions(the_repository
, &revs
, NULL
);
2203 setup_revisions(argv
.nr
, argv
.v
, &revs
, &opt
);
2204 if (prepare_revision_walk(&revs
))
2205 die(_("revision walk setup failed"));
2207 /* ... and count the commits on each side. */
2209 struct commit
*c
= get_revision(&revs
);
2212 if (c
->object
.flags
& SYMMETRIC_LEFT
)
2218 /* clear object flags smudged by the above traversal */
2219 clear_commit_marks(ours
, ALL_REV_FLAGS
);
2220 clear_commit_marks(theirs
, ALL_REV_FLAGS
);
2222 strvec_clear(&argv
);
2223 release_revisions(&revs
);
2228 * Lookup the tracking branch for the given branch and if present, optionally
2229 * compute the commit ahead/behind values for the pair.
2231 * If for_push is true, the tracking branch refers to the push branch,
2232 * otherwise it refers to the upstream branch.
2234 * The name of the tracking branch (or NULL if it is not defined) is
2235 * returned via *tracking_name, if it is not itself NULL.
2237 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
2238 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
2239 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
2242 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
2243 * upstream defined, or ref does not exist). Returns 0 if the commits are
2244 * identical. Returns 1 if commits are different.
2246 int stat_tracking_info(struct branch
*branch
, int *num_ours
, int *num_theirs
,
2247 const char **tracking_name
, int for_push
,
2248 enum ahead_behind_flags abf
)
2252 /* Cannot stat unless we are marked to build on top of somebody else. */
2253 base
= for_push
? branch_get_push(branch
, NULL
) :
2254 branch_get_upstream(branch
, NULL
);
2256 *tracking_name
= base
;
2260 return stat_branch_pair(branch
->refname
, base
, num_ours
, num_theirs
, abf
);
2264 * Return true when there is anything to report, otherwise false.
2266 int format_tracking_info(struct branch
*branch
, struct strbuf
*sb
,
2267 enum ahead_behind_flags abf
,
2268 int show_divergence_advice
)
2270 int ours
, theirs
, sti
;
2271 const char *full_base
;
2273 int upstream_is_gone
= 0;
2275 sti
= stat_tracking_info(branch
, &ours
, &theirs
, &full_base
, 0, abf
);
2279 upstream_is_gone
= 1;
2282 base
= shorten_unambiguous_ref(full_base
, 0);
2283 if (upstream_is_gone
) {
2285 _("Your branch is based on '%s', but the upstream is gone.\n"),
2287 if (advice_enabled(ADVICE_STATUS_HINTS
))
2289 _(" (use \"git branch --unset-upstream\" to fixup)\n"));
2292 _("Your branch is up to date with '%s'.\n"),
2294 } else if (abf
== AHEAD_BEHIND_QUICK
) {
2296 _("Your branch and '%s' refer to different commits.\n"),
2298 if (advice_enabled(ADVICE_STATUS_HINTS
))
2299 strbuf_addf(sb
, _(" (use \"%s\" for details)\n"),
2300 "git status --ahead-behind");
2301 } else if (!theirs
) {
2303 Q_("Your branch is ahead of '%s' by %d commit.\n",
2304 "Your branch is ahead of '%s' by %d commits.\n",
2307 if (advice_enabled(ADVICE_STATUS_HINTS
))
2309 _(" (use \"git push\" to publish your local commits)\n"));
2312 Q_("Your branch is behind '%s' by %d commit, "
2313 "and can be fast-forwarded.\n",
2314 "Your branch is behind '%s' by %d commits, "
2315 "and can be fast-forwarded.\n",
2318 if (advice_enabled(ADVICE_STATUS_HINTS
))
2320 _(" (use \"git pull\" to update your local branch)\n"));
2323 Q_("Your branch and '%s' have diverged,\n"
2324 "and have %d and %d different commit each, "
2326 "Your branch and '%s' have diverged,\n"
2327 "and have %d and %d different commits each, "
2330 base
, ours
, theirs
);
2331 if (show_divergence_advice
&&
2332 advice_enabled(ADVICE_STATUS_HINTS
))
2334 _(" (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
2340 static int one_local_ref(const char *refname
, const struct object_id
*oid
,
2344 struct ref
***local_tail
= cb_data
;
2347 /* we already know it starts with refs/ to get here */
2348 if (check_refname_format(refname
+ 5, 0))
2351 ref
= alloc_ref(refname
);
2352 oidcpy(&ref
->new_oid
, oid
);
2354 *local_tail
= &ref
->next
;
2358 struct ref
*get_local_heads(void)
2360 struct ref
*local_refs
= NULL
, **local_tail
= &local_refs
;
2362 for_each_ref(one_local_ref
, &local_tail
);
2366 struct ref
*guess_remote_head(const struct ref
*head
,
2367 const struct ref
*refs
,
2370 const struct ref
*r
;
2371 struct ref
*list
= NULL
;
2372 struct ref
**tail
= &list
;
2378 * Some transports support directly peeking at
2379 * where HEAD points; if that is the case, then
2380 * we don't have to guess.
2383 return copy_ref(find_ref_by_name(refs
, head
->symref
));
2385 /* If a remote branch exists with the default branch name, let's use it. */
2387 char *ref
= xstrfmt("refs/heads/%s",
2388 git_default_branch_name(0));
2390 r
= find_ref_by_name(refs
, ref
);
2392 if (r
&& oideq(&r
->old_oid
, &head
->old_oid
))
2395 /* Fall back to the hard-coded historical default */
2396 r
= find_ref_by_name(refs
, "refs/heads/master");
2397 if (r
&& oideq(&r
->old_oid
, &head
->old_oid
))
2401 /* Look for another ref that points there */
2402 for (r
= refs
; r
; r
= r
->next
) {
2404 starts_with(r
->name
, "refs/heads/") &&
2405 oideq(&r
->old_oid
, &head
->old_oid
)) {
2406 *tail
= copy_ref(r
);
2407 tail
= &((*tail
)->next
);
2416 struct stale_heads_info
{
2417 struct string_list
*ref_names
;
2418 struct ref
**stale_refs_tail
;
2422 static int get_stale_heads_cb(const char *refname
, const struct object_id
*oid
,
2423 int flags
, void *cb_data
)
2425 struct stale_heads_info
*info
= cb_data
;
2426 struct string_list matches
= STRING_LIST_INIT_DUP
;
2427 struct refspec_item query
;
2429 memset(&query
, 0, sizeof(struct refspec_item
));
2430 query
.dst
= (char *)refname
;
2432 query_refspecs_multiple(info
->rs
, &query
, &matches
);
2433 if (matches
.nr
== 0)
2434 goto clean_exit
; /* No matches */
2437 * If we did find a suitable refspec and it's not a symref and
2438 * it's not in the list of refs that currently exist in that
2439 * remote, we consider it to be stale. In order to deal with
2440 * overlapping refspecs, we need to go over all of the
2443 if (flags
& REF_ISSYMREF
)
2446 for (i
= 0; stale
&& i
< matches
.nr
; i
++)
2447 if (string_list_has_string(info
->ref_names
, matches
.items
[i
].string
))
2451 struct ref
*ref
= make_linked_ref(refname
, &info
->stale_refs_tail
);
2452 oidcpy(&ref
->new_oid
, oid
);
2456 string_list_clear(&matches
, 0);
2460 struct ref
*get_stale_heads(struct refspec
*rs
, struct ref
*fetch_map
)
2462 struct ref
*ref
, *stale_refs
= NULL
;
2463 struct string_list ref_names
= STRING_LIST_INIT_NODUP
;
2464 struct stale_heads_info info
;
2466 info
.ref_names
= &ref_names
;
2467 info
.stale_refs_tail
= &stale_refs
;
2469 for (ref
= fetch_map
; ref
; ref
= ref
->next
)
2470 string_list_append(&ref_names
, ref
->name
);
2471 string_list_sort(&ref_names
);
2472 for_each_ref(get_stale_heads_cb
, &info
);
2473 string_list_clear(&ref_names
, 0);
2480 static void clear_cas_option(struct push_cas_option
*cas
)
2484 for (i
= 0; i
< cas
->nr
; i
++)
2485 free(cas
->entry
[i
].refname
);
2487 memset(cas
, 0, sizeof(*cas
));
2490 static struct push_cas
*add_cas_entry(struct push_cas_option
*cas
,
2491 const char *refname
,
2494 struct push_cas
*entry
;
2495 ALLOC_GROW(cas
->entry
, cas
->nr
+ 1, cas
->alloc
);
2496 entry
= &cas
->entry
[cas
->nr
++];
2497 memset(entry
, 0, sizeof(*entry
));
2498 entry
->refname
= xmemdupz(refname
, refnamelen
);
2502 static int parse_push_cas_option(struct push_cas_option
*cas
, const char *arg
, int unset
)
2505 struct push_cas
*entry
;
2508 /* "--no-<option>" */
2509 clear_cas_option(cas
);
2514 /* just "--<option>" */
2515 cas
->use_tracking_for_rest
= 1;
2519 /* "--<option>=refname" or "--<option>=refname:value" */
2520 colon
= strchrnul(arg
, ':');
2521 entry
= add_cas_entry(cas
, arg
, colon
- arg
);
2523 entry
->use_tracking
= 1;
2525 oidclr(&entry
->expect
);
2526 else if (repo_get_oid(the_repository
, colon
+ 1, &entry
->expect
))
2527 return error(_("cannot parse expected object name '%s'"),
2532 int parseopt_push_cas_option(const struct option
*opt
, const char *arg
, int unset
)
2534 return parse_push_cas_option(opt
->value
, arg
, unset
);
2537 int is_empty_cas(const struct push_cas_option
*cas
)
2539 return !cas
->use_tracking_for_rest
&& !cas
->nr
;
2543 * Look at remote.fetch refspec and see if we have a remote
2544 * tracking branch for the refname there. Fill the name of
2545 * the remote-tracking branch in *dst_refname, and the name
2546 * of the commit object at its tip in oid[].
2547 * If we cannot do so, return negative to signal an error.
2549 static int remote_tracking(struct remote
*remote
, const char *refname
,
2550 struct object_id
*oid
, char **dst_refname
)
2554 dst
= apply_refspecs(&remote
->fetch
, refname
);
2556 return -1; /* no tracking ref for refname at remote */
2557 if (read_ref(dst
, oid
))
2558 return -1; /* we know what the tracking ref is but we cannot read it */
2565 * The struct "reflog_commit_array" and related helper functions
2566 * are used for collecting commits into an array during reflog
2567 * traversals in "check_and_collect_until()".
2569 struct reflog_commit_array
{
2570 struct commit
**item
;
2574 #define REFLOG_COMMIT_ARRAY_INIT { 0 }
2576 /* Append a commit to the array. */
2577 static void append_commit(struct reflog_commit_array
*arr
,
2578 struct commit
*commit
)
2580 ALLOC_GROW(arr
->item
, arr
->nr
+ 1, arr
->alloc
);
2581 arr
->item
[arr
->nr
++] = commit
;
2584 /* Free and reset the array. */
2585 static void free_commit_array(struct reflog_commit_array
*arr
)
2587 FREE_AND_NULL(arr
->item
);
2588 arr
->nr
= arr
->alloc
= 0;
2591 struct check_and_collect_until_cb_data
{
2592 struct commit
*remote_commit
;
2593 struct reflog_commit_array
*local_commits
;
2594 timestamp_t remote_reflog_timestamp
;
2597 /* Get the timestamp of the latest entry. */
2598 static int peek_reflog(struct object_id
*o_oid UNUSED
,
2599 struct object_id
*n_oid UNUSED
,
2600 const char *ident UNUSED
,
2601 timestamp_t timestamp
, int tz UNUSED
,
2602 const char *message UNUSED
, void *cb_data
)
2604 timestamp_t
*ts
= cb_data
;
2609 static int check_and_collect_until(struct object_id
*o_oid UNUSED
,
2610 struct object_id
*n_oid
,
2611 const char *ident UNUSED
,
2612 timestamp_t timestamp
, int tz UNUSED
,
2613 const char *message UNUSED
, void *cb_data
)
2615 struct commit
*commit
;
2616 struct check_and_collect_until_cb_data
*cb
= cb_data
;
2618 /* An entry was found. */
2619 if (oideq(n_oid
, &cb
->remote_commit
->object
.oid
))
2622 if ((commit
= lookup_commit_reference(the_repository
, n_oid
)))
2623 append_commit(cb
->local_commits
, commit
);
2626 * If the reflog entry timestamp is older than the remote ref's
2627 * latest reflog entry, there is no need to check or collect
2628 * entries older than this one.
2630 if (timestamp
< cb
->remote_reflog_timestamp
)
2636 #define MERGE_BASES_BATCH_SIZE 8
2639 * Iterate through the reflog of the local ref to check if there is an entry
2640 * for the given remote-tracking ref; runs until the timestamp of an entry is
2641 * older than latest timestamp of remote-tracking ref's reflog. Any commits
2642 * are that seen along the way are collected into an array to check if the
2643 * remote-tracking ref is reachable from any of them.
2645 static int is_reachable_in_reflog(const char *local
, const struct ref
*remote
)
2648 struct commit
*commit
;
2649 struct commit
**chunk
;
2650 struct check_and_collect_until_cb_data cb
;
2651 struct reflog_commit_array arr
= REFLOG_COMMIT_ARRAY_INIT
;
2655 commit
= lookup_commit_reference(the_repository
, &remote
->old_oid
);
2657 goto cleanup_return
;
2660 * Get the timestamp from the latest entry
2661 * of the remote-tracking ref's reflog.
2663 for_each_reflog_ent_reverse(remote
->tracking_ref
, peek_reflog
, &date
);
2665 cb
.remote_commit
= commit
;
2666 cb
.local_commits
= &arr
;
2667 cb
.remote_reflog_timestamp
= date
;
2668 ret
= for_each_reflog_ent_reverse(local
, check_and_collect_until
, &cb
);
2670 /* We found an entry in the reflog. */
2672 goto cleanup_return
;
2675 * Check if the remote commit is reachable from any
2676 * of the commits in the collected array, in batches.
2678 for (chunk
= arr
.item
; chunk
< arr
.item
+ arr
.nr
; chunk
+= size
) {
2679 size
= arr
.item
+ arr
.nr
- chunk
;
2680 if (MERGE_BASES_BATCH_SIZE
< size
)
2681 size
= MERGE_BASES_BATCH_SIZE
;
2683 if ((ret
= repo_in_merge_bases_many(the_repository
, commit
, size
, chunk
)))
2688 free_commit_array(&arr
);
2693 * Check for reachability of a remote-tracking
2694 * ref in the reflog entries of its local ref.
2696 static void check_if_includes_upstream(struct ref
*remote
)
2698 struct ref
*local
= get_local_ref(remote
->name
);
2702 if (is_reachable_in_reflog(local
->name
, remote
) <= 0)
2703 remote
->unreachable
= 1;
2706 static void apply_cas(struct push_cas_option
*cas
,
2707 struct remote
*remote
,
2712 /* Find an explicit --<option>=<name>[:<value>] entry */
2713 for (i
= 0; i
< cas
->nr
; i
++) {
2714 struct push_cas
*entry
= &cas
->entry
[i
];
2715 if (!refname_match(entry
->refname
, ref
->name
))
2717 ref
->expect_old_sha1
= 1;
2718 if (!entry
->use_tracking
)
2719 oidcpy(&ref
->old_oid_expect
, &entry
->expect
);
2720 else if (remote_tracking(remote
, ref
->name
,
2721 &ref
->old_oid_expect
,
2722 &ref
->tracking_ref
))
2723 oidclr(&ref
->old_oid_expect
);
2725 ref
->check_reachable
= cas
->use_force_if_includes
;
2729 /* Are we using "--<option>" to cover all? */
2730 if (!cas
->use_tracking_for_rest
)
2733 ref
->expect_old_sha1
= 1;
2734 if (remote_tracking(remote
, ref
->name
,
2735 &ref
->old_oid_expect
,
2736 &ref
->tracking_ref
))
2737 oidclr(&ref
->old_oid_expect
);
2739 ref
->check_reachable
= cas
->use_force_if_includes
;
2742 void apply_push_cas(struct push_cas_option
*cas
,
2743 struct remote
*remote
,
2744 struct ref
*remote_refs
)
2747 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
2748 apply_cas(cas
, remote
, ref
);
2751 * If "compare-and-swap" is in "use_tracking[_for_rest]"
2752 * mode, and if "--force-if-includes" was specified, run
2755 if (ref
->check_reachable
)
2756 check_if_includes_upstream(ref
);
2760 struct remote_state
*remote_state_new(void)
2762 struct remote_state
*r
= xmalloc(sizeof(*r
));
2764 memset(r
, 0, sizeof(*r
));
2766 hashmap_init(&r
->remotes_hash
, remotes_hash_cmp
, NULL
, 0);
2767 hashmap_init(&r
->branches_hash
, branches_hash_cmp
, NULL
, 0);
2771 void remote_state_clear(struct remote_state
*remote_state
)
2775 for (i
= 0; i
< remote_state
->remotes_nr
; i
++)
2776 remote_clear(remote_state
->remotes
[i
]);
2777 FREE_AND_NULL(remote_state
->remotes
);
2778 remote_state
->remotes_alloc
= 0;
2779 remote_state
->remotes_nr
= 0;
2781 hashmap_clear_and_free(&remote_state
->remotes_hash
, struct remote
, ent
);
2782 hashmap_clear_and_free(&remote_state
->branches_hash
, struct remote
, ent
);
2786 * Returns 1 if it was the last chop before ':'.
2788 static int chop_last_dir(char **remoteurl
, int is_relative
)
2790 char *rfind
= find_last_dir_sep(*remoteurl
);
2796 rfind
= strrchr(*remoteurl
, ':');
2802 if (is_relative
|| !strcmp(".", *remoteurl
))
2803 die(_("cannot strip one component off url '%s'"),
2807 *remoteurl
= xstrdup(".");
2811 char *relative_url(const char *remote_url
, const char *url
,
2812 const char *up_path
)
2814 int is_relative
= 0;
2818 struct strbuf sb
= STRBUF_INIT
;
2821 if (!url_is_local_not_ssh(url
) || is_absolute_path(url
))
2822 return xstrdup(url
);
2824 len
= strlen(remote_url
);
2826 BUG("invalid empty remote_url");
2828 remoteurl
= xstrdup(remote_url
);
2829 if (is_dir_sep(remoteurl
[len
-1]))
2830 remoteurl
[len
-1] = '\0';
2832 if (!url_is_local_not_ssh(remoteurl
) || is_absolute_path(remoteurl
))
2837 * Prepend a './' to ensure all relative
2838 * remoteurls start with './' or '../'
2840 if (!starts_with_dot_slash_native(remoteurl
) &&
2841 !starts_with_dot_dot_slash_native(remoteurl
)) {
2843 strbuf_addf(&sb
, "./%s", remoteurl
);
2845 remoteurl
= strbuf_detach(&sb
, NULL
);
2849 * When the url starts with '../', remove that and the
2850 * last directory in remoteurl.
2853 if (starts_with_dot_dot_slash_native(url
)) {
2855 colonsep
|= chop_last_dir(&remoteurl
, is_relative
);
2856 } else if (starts_with_dot_slash_native(url
))
2862 strbuf_addf(&sb
, "%s%s%s", remoteurl
, colonsep
? ":" : "/", url
);
2863 if (ends_with(url
, "/"))
2864 strbuf_setlen(&sb
, sb
.len
- 1);
2867 if (starts_with_dot_slash_native(sb
.buf
))
2868 out
= xstrdup(sb
.buf
+ 2);
2870 out
= xstrdup(sb
.buf
);
2872 if (!up_path
|| !is_relative
) {
2873 strbuf_release(&sb
);
2878 strbuf_addf(&sb
, "%s%s", up_path
, out
);
2880 return strbuf_detach(&sb
, NULL
);