Merge branch 'hv/receive-denycurrent-everywhere'

"git push" should stop from updating a branch that is checked out
when receive.denyCurrentBranch configuration is set, but it failed
to pay attention to checkouts in secondary worktrees.  This has
been corrected.

* hv/receive-denycurrent-everywhere:
  t2402: test worktree path when called in .git directory
  receive.denyCurrentBranch: respect all worktrees
  t5509: use a bare repository for test push target
  get_main_worktree(): allow it to be called in the Git directory
This commit is contained in:
Junio C Hamano 2020-03-05 10:43:02 -08:00
Родитель 49e5043b09 4d864895a2
Коммит 4a2e91db65
5 изменённых файлов: 49 добавлений и 18 удалений

Просмотреть файл

@ -27,6 +27,7 @@
#include "object-store.h"
#include "protocol.h"
#include "commit-reach.h"
#include "worktree.h"
static const char * const receive_pack_usage[] = {
N_("git receive-pack <git-dir>"),
@ -816,16 +817,6 @@ static int run_update_hook(struct command *cmd)
return finish_command(&proc);
}
static int is_ref_checked_out(const char *ref)
{
if (is_bare_repository())
return 0;
if (!head_name)
return 0;
return !strcmp(head_name, ref);
}
static char *refuse_unconfigured_deny_msg =
N_("By default, updating the current branch in a non-bare repository\n"
"is denied, because it will make the index and work tree inconsistent\n"
@ -997,16 +988,26 @@ static const char *push_to_checkout(unsigned char *hash,
return NULL;
}
static const char *update_worktree(unsigned char *sha1)
static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree)
{
const char *retval;
const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : "..";
const char *retval, *work_tree, *git_dir = NULL;
struct argv_array env = ARGV_ARRAY_INIT;
if (worktree && worktree->path)
work_tree = worktree->path;
else if (git_work_tree_cfg)
work_tree = git_work_tree_cfg;
else
work_tree = "..";
if (is_bare_repository())
return "denyCurrentBranch = updateInstead needs a worktree";
if (worktree)
git_dir = get_worktree_git_dir(worktree);
if (!git_dir)
git_dir = get_git_dir();
argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir()));
argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir));
if (!find_hook(push_to_checkout_hook))
retval = push_to_deploy(sha1, &env, work_tree);
@ -1026,6 +1027,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
struct object_id *old_oid = &cmd->old_oid;
struct object_id *new_oid = &cmd->new_oid;
int do_update_worktree = 0;
const struct worktree *worktree = is_bare_repository() ? NULL : find_shared_symref("HEAD", name);
/* only refs/... are allowed */
if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
@ -1037,7 +1039,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
free(namespaced_name);
namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
if (is_ref_checked_out(namespaced_name)) {
if (worktree) {
switch (deny_current_branch) {
case DENY_IGNORE:
break;
@ -1069,7 +1071,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
return "deletion prohibited";
}
if (head_name && !strcmp(namespaced_name, head_name)) {
if (worktree || (head_name && !strcmp(namespaced_name, head_name))) {
switch (deny_delete_current) {
case DENY_IGNORE:
break;
@ -1118,7 +1120,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
}
if (do_update_worktree) {
ret = update_worktree(new_oid->hash);
ret = update_worktree(new_oid->hash, find_shared_symref("HEAD", name));
if (ret)
return ret;
}

Просмотреть файл

@ -151,4 +151,10 @@ test_expect_success 'linked worktrees are sorted' '
test_cmp expected sorted/main/actual
'
test_expect_success 'worktree path when called in .git directory' '
git worktree list >list1&&
git -C .git worktree list >list2 &&
test_cmp list1 list2
'
test_done

Просмотреть файл

@ -20,7 +20,7 @@ test_expect_success setup '
) &&
commit0=$(cd original && git rev-parse HEAD^) &&
commit1=$(cd original && git rev-parse HEAD) &&
git init pushee &&
git init --bare pushee &&
git init puller
'
@ -152,4 +152,15 @@ test_expect_success 'clone chooses correct HEAD (v2)' '
test_cmp expect actual
'
test_expect_success 'denyCurrentBranch and unborn branch with ref namespace' '
(
cd original &&
git init unborn &&
git remote add unborn-namespaced "ext::git --namespace=namespace %s unborn" &&
test_must_fail git push unborn-namespaced HEAD:master &&
git -C unborn config receive.denyCurrentBranch updateInstead &&
git push unborn-namespaced HEAD:master
)
'
test_done

Просмотреть файл

@ -1712,4 +1712,15 @@ test_expect_success 'updateInstead with push-to-checkout hook' '
)
'
test_expect_success 'denyCurrentBranch and worktrees' '
git worktree add new-wt &&
git clone . cloned &&
test_commit -C cloned first &&
test_config receive.denyCurrentBranch refuse &&
test_must_fail git -C cloned push origin HEAD:new-wt &&
test_config receive.denyCurrentBranch updateInstead &&
git -C cloned push origin HEAD:new-wt &&
test_must_fail git -C cloned push --delete origin new-wt
'
test_done

Просмотреть файл

@ -50,6 +50,7 @@ static struct worktree *get_main_worktree(void)
struct strbuf worktree_path = STRBUF_INIT;
strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
strbuf_strip_suffix(&worktree_path, "/.");
if (!strbuf_strip_suffix(&worktree_path, "/.git"))
strbuf_strip_suffix(&worktree_path, "/.");