]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'hv/receive-denycurrent-everywhere'
authorJunio C Hamano <gitster@pobox.com>
Thu, 5 Mar 2020 18:43:02 +0000 (10:43 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Mar 2020 18:43:03 +0000 (10:43 -0800)
"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

builtin/receive-pack.c
t/t2402-worktree-list.sh
t/t5509-fetch-push-namespaces.sh
t/t5516-fetch-push.sh
worktree.c

index 411e0b4d999acb36fb05ebde7598568f2c88dd4b..2cc18bbffdcf41cf2510becf6fcdd74c5946494a 100644 (file)
@@ -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;
        }
index bb6fb9b12cb7f41cea671a7df830ccc97c592e4f..69ffe865b43965bb84bbe60402fc9c03ecc8ba9b 100755 (executable)
@@ -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
index 75cbfcc392c8cd71e20d901a7a28bf7f6f73451a..a67f792adf4a2fb846ade40eb26acfcb08ba5d1b 100755 (executable)
@@ -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
index f12cbef09728d9c49b0ad66eab77c57043df0596..9ff041a093e71aac932f5e563d4922930bc62ff2 100755 (executable)
@@ -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
index 52971f7082eb00d38282fad793264ab1bfd1a7e3..eba4fd3a03812f046dadc1baf9d7644ba8dfcce9 100644 (file)
@@ -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, "/.");