The receive.denyCurrentBranch config option controls what happens if you push to a branch that is checkout into a non-bare repository. By default, it rejects it. It can be disabled via ignore or warn. Another yet trickier option is updateInstead. When receive.denyCurrentBranch is set to updateInstead, a push that tries to update the branch that is currently checked out is accepted only when the index and the working tree exactly matches the currently checked out commit, in which case the index and the working tree are updated to match the pushed commit. Otherwise, the push is refused. However, this setting was forgotten when the git worktree command was introduced: only the main worktree's current branch is respected. [ fixes: #331 ] Incidently, this change also fixes another bug i.e. receive.denyCurrentBranch = true was ignored when pushing into a non-bare repository using ref namespaces. Thanks, @dscho for helping me out. Regards, Hariom Hariom Verma (4): get_main_worktree(): allow it to be called in the Git directory t5509: initialized `pushee` as bare repository bug: denyCurrentBranch and unborn branch with ref namespace receive.denyCurrentBranch: respect all worktrees builtin/receive-pack.c | 37 +++++++++++++++++--------------- t/t5509-fetch-push-namespaces.sh | 11 +++++++++- t/t5516-fetch-push.sh | 11 ++++++++++ worktree.c | 1 + 4 files changed, 42 insertions(+), 18 deletions(-) base-commit: 232378479ee6c66206d47a9be175e3a39682aea6 Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-535%2Fharry-hov%2Fdeny-v2 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-535/harry-hov/deny-v2 Pull-Request: https://github.com/gitgitgadget/git/pull/535 Range-diff vs v1: 1: 902c8a3f171 = 1: 8718facbc95 get_main_worktree(): allow it to be called in the Git directory 2: d156d04ca87 ! 2: 5c749e044a3 t5509: initialized `pushee` as bare repository @@ -3,9 +3,9 @@ t5509: initialized `pushee` as bare repository `receive.denyCurrentBranch` currently has a bug where it allows pushing - into the current branch of a non-bare repository as long as it does not - have any commits. This would cause t5509 to fail once that bug is fixed - because it pushes into an unborn current branch. + into non-bare repository using namespaces as long as it does not have any + commits. This would cause t5509 to fail once that bug is fixed because it + pushes into an unborn current branch. In t5509, no operations are performed inside `pushee`, as it is only a target for `git push` and `git ls-remote` calls. Therefore it does not -: ----------- > 3: b3e573d44a9 bug: denyCurrentBranch and unborn branch with ref namespace 3: 3352c0bffc1 ! 4: 61e5f75a6f9 receive.denyCurrentBranch: respect all worktrees @@ -14,10 +14,10 @@ That change also leads to revealing another bug, i.e. `receive.denyCurrentBranch = true` was ignored when pushing into a - non-bare repository's unborn current branch. As `is_ref_checked_out()` - returns 0 which means `receive-pack` does not get into conditional - statement to switch `deny_current_branch` accordingly(ignore, warn, - refuse, unconfigured, updateInstead). + non-bare repository's unborn current branch using ref namespaces. As + `is_ref_checked_out()` returns 0 which means `receive-pack` does not get + into conditional statement to switch `deny_current_branch` accordingly + (ignore, warn, refuse, unconfigured, updateInstead). receive.denyCurrentBranch uses the function `refs_resolve_ref_unsafe()` (called via `resolve_refdup()`) to resolve the symbolic ref HEAD, but @@ -26,7 +26,7 @@ `find_shared_symref()`, which has no problem finding the worktree for a given branch even if it is unborn yet, this bug is fixed at the same time: receive.denyCurrentBranch now also handles worktrees with unborn - branches as intended. + branches as intended even while using ref namespaces. Helped-by: Johannes Schindelin <johannes.schindelin@xxxxxx> Signed-off-by: Hariom Verma <hariom18599@xxxxxxxxx> @@ -127,6 +127,19 @@ return ret; } + diff --git a/t/t5509-fetch-push-namespaces.sh b/t/t5509-fetch-push-namespaces.sh + --- a/t/t5509-fetch-push-namespaces.sh + +++ b/t/t5509-fetch-push-namespaces.sh +@@ + test_cmp expect actual + ' + +-test_expect_failure 'denyCurrentBranch and unborn branch with ref namespace' ' ++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" && + diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh --- a/t/t5516-fetch-push.sh +++ b/t/t5516-fetch-push.sh @@ -135,7 +148,7 @@ ' +test_expect_success 'denyCurrentBranch and worktrees' ' -+ git worktree add new-wt && ++ git worktree add new-wt && + git clone . cloned && + test_commit -C cloned first && + test_config receive.denyCurrentBranch refuse && -- gitgitgadget