Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Use --receive-pack to avoid mutating repo config
Maximilian Roose12b4b4c3238
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex 08fcdb7b1..dfe64d184 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -526,12 +526,6 @@ pub fn handle_push(return Err(merge_commits_found());}- // Configure receive.denyCurrentBranch if needed- let current_config = repo.get_config("receive.denyCurrentBranch")?;- if current_config.as_deref() != Some("updateInstead") {- repo.set_config("receive.denyCurrentBranch", "updateInstead")?;- }-// Check for conflicting changes in target worktree (auto-stash safe changes)let mut target_worktree_stash =repo.prepare_target_worktree(target_worktree_path.as_ref(), &target_branch)?;@@ -616,8 +610,14 @@ pub fn handle_push(let git_common_dir = repo.git_common_dir()?;// Perform the push+ // Use --receive-pack to pass config to the receiving end without permanently mutating repo configlet push_target = format!("HEAD:{}", target_branch);- if let Err(e) = repo.run_command(&["push", git_common_dir.to_str().unwrap(), &push_target]) {+ if let Err(e) = repo.run_command(&[+ "push",+ "--receive-pack=git -c receive.denyCurrentBranch=updateInstead receive-pack",+ git_common_dir.to_str().unwrap(),+ &push_target,+ ]) {if let Some(stash) = target_worktree_stash.take() {stash.restore()?;}diff --git a/tests/integration_tests/.merge.rs.pending-snap b/tests/integration_tests/.merge.rs.pending-snapnew file mode 100644index 000000000..df4b75c58--- /dev/null+++ b/tests/integration_tests/.merge.rs.pending-snap@@ -0,0 +1,12 @@+{"run_id":"1763935809-748534000","line":2364,"new":{"module_name":"integration__integration_tests__merge","snapshot_name":"merge_no_commit_no_squash_no_remove_redundant","metadata":{"source":"tests/integration_tests/merge.rs","assertion_line":2364,"info":{"program":"wt","args":["merge","main","--no-commit","--no-squash","--no-remove"],"env":{"CLICOLOR_FORCE":"1","COLUMNS":"150","GIT_AUTHOR_DATE":"2025-01-01T00:00:00Z","GIT_COMMITTER_DATE":"2025-01-01T00:00:00Z","GIT_CONFIG_GLOBAL":"[TEST_GIT_CONFIG]","GIT_CONFIG_SYSTEM":"/dev/null","GIT_EDITOR":"","LANG":"C","LC_ALL":"C","SOURCE_DATE_EPOCH":"1761609600","WORKTRUNK_CONFIG_PATH":"[TEST_CONFIG]"}}},"snapshot":"success: false\nexit_code: 1\n----- stdout -----\nπ \u001b[36mMerging 1 commit to \u001b[1m\u001b[36mmain\u001b[0m\u001b[36m @ \u001b[2m[SHA]\u001b[0m (no commit/squash/rebase needed)\n\u001b[107m \u001b[0m * [SHA]\u001b[33m (\u001b[m\u001b[1;36mHEAD\u001b[m\u001b[33m -> \u001b[m\u001b[1;32mfeature\u001b[m\u001b[33m)\u001b[m Add feature file\n\u001b[107m \u001b[0m feature.txt | 1 \u001b[32m+\u001b[m\n\u001b[107m \u001b[0m 1 file changed, 1 insertion(+)\nβ \u001b[31mPush failed\u001b[0m\n\u001b[107m \u001b[0m remote: error: refusing to update checked out branch: refs/heads/main \n\u001b[107m \u001b[0m remote: error: By default, updating the current branch in a non-bare repository \n\u001b[107m \u001b[0m remote: is denied, because it will make the index and work tree inconsistent \n\u001b[107m \u001b[0m remote: with what you pushed, and will require 'git reset --hard' to match \n\u001b[107m \u001b[0m remote: the work tree to HEAD. \n\u001b[107m \u001b[0m remote: \n\u001b[107m \u001b[0m remote: You can set the 'receive.denyCurrentBranch' configuration variable \n\u001b[107m \u001b[0m remote: to 'ignore' or 'warn' in the remote repository to allow pushing into \n\u001b[107m \u001b[0m remote: its current branch; however, this is not recommended unless you \n\u001b[107m \u001b[0m remote: arranged to update its work tree to match what you pushed in some \n\u001b[107m \u001b[0m remote: other way. \n\u001b[107m \u001b[0m remote: \n\u001b[107m \u001b[0m remote: To squelch this message and still keep the default behaviour, set \n\u001b[107m \u001b[0m remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'. \n\u001b[107m \u001b[0m To [REPO]/.git\n\u001b[107m \u001b[0m ! [remote rejected] HEAD -> main (branch is currently checked out)\n\u001b[107m \u001b[0m error: failed to push some refs to '[REPO]/.git'\n\n\n----- stderr -----"},"old":{"module_name":"integration__integration_tests__merge","metadata":{},"snapshot":"success: true\nexit_code: 0\n----- stdout -----\nπ \u001b[36mMerging 1 commit to \u001b[1m\u001b[36mmain\u001b[0m\u001b[36m @ \u001b[2m[SHA]\u001b[0m (no commit/squash/rebase needed)\n\u001b[107m \u001b[0m * [SHA]\u001b[33m (\u001b[m\u001b[1;36mHEAD\u001b[m\u001b[33m -> \u001b[m\u001b[1;32mfeature\u001b[m\u001b[33m)\u001b[m Add feature file\n\u001b[107m \u001b[0m feature.txt | 1 \u001b[32m+\u001b[m\n\u001b[107m \u001b[0m 1 file changed, 1 insertion(+)\nβ \u001b[32mMerged to \u001b[1m\u001b[32mmain\u001b[0m\u001b[0m (1 commit, 1 file, \u001b[32m+1\u001b[0m)\nβ \u001b[32mWorktree preserved (--no-remove)\u001b[0m\n\n----- stderr -----"}}+{"run_id":"1763935809-748534000","line":2309,"new":null,"old":null}+{"run_id":"1763935809-748534000","line":2260,"new":{"module_name":"integration__integration_tests__merge","snapshot_name":"merge_no_commit_with_clean_tree","metadata":{"source":"tests/integration_tests/merge.rs","assertion_line":2260,"info":{"program":"wt","args":["merge","main","--no-commit","--no-remove"],"env":{"CLICOLOR_FORCE":"1","COLUMNS":"150","GIT_AUTHOR_DATE":"2025-01-01T00:00:00Z","GIT_COMMITTER_DATE":"2025-01-01T00:00:00Z","GIT_CONFIG_GLOBAL":"[TEST_GIT_CONFIG]","GIT_CONFIG_SYSTEM":"/dev/null","GIT_EDITOR":"","LANG":"C","LC_ALL":"C","SOURCE_DATE_EPOCH":"1761609600","WORKTRUNK_CONFIG_PATH":"[TEST_CONFIG]"}}},"snapshot":"success: false\nexit_code: 1\n----- stdout -----\nπ \u001b[36mMerging 1 commit to \u001b[1m\u001b[36mmain\u001b[0m\u001b[36m @ \u001b[2m[SHA]\u001b[0m (no commit/squash/rebase needed)\n\u001b[107m \u001b[0m * [SHA]\u001b[33m (\u001b[m\u001b[1;36mHEAD\u001b[m\u001b[33m -> \u001b[m\u001b[1;32mfeature\u001b[m\u001b[33m)\u001b[m Add feature file\n\u001b[107m \u001b[0m feature.txt | 1 \u001b[32m+\u001b[m\n\u001b[107m \u001b[0m 1 file changed, 1 insertion(+)\nβ \u001b[31mPush failed\u001b[0m\n\u001b[107m \u001b[0m remote: error: refusing to update checked out branch: refs/heads/main \n\u001b[107m \u001b[0m remote: error: By default, updating the current branch in a non-bare repository \n\u001b[107m \u001b[0m remote: is denied, because it will make the index and work tree inconsistent \n\u001b[107m \u001b[0m remote: with what you pushed, and will require 'git reset --hard' to match \n\u001b[107m \u001b[0m remote: the work tree to HEAD. \n\u001b[107m \u001b[0m remote: \n\u001b[107m \u001b[0m remote: You can set the 'receive.denyCurrentBranch' configuration variable \n\u001b[107m \u001b[0m remote: to 'ignore' or 'warn' in the remote repository to allow pushing into \n\u001b[107m \u001b[0m remote: its current branch; however, this is not recommended unless you \n\u001b[107m \u001b[0m remote: arranged to update its work tree to match what you pushed in some \n\u001b[107m \u001b[0m remote: other way. \n\u001b[107m \u001b[0m remote: \n\u001b[107m \u001b[0m remote: To squelch this message and still keep the default behaviour, set \n\u001b[107m \u001b[0m remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'. \n\u001b[107m \u001b[0m To [REPO]/.git\n\u001b[107m \u001b[0m ! [remote rejected] HEAD -> main (branch is currently checked out)\n\u001b[107m \u001b[0m error: failed to push some refs to '[REPO]/.git'\n\n\n----- stderr -----"},"old":{"module_name":"integration__integration_tests__merge","metadata":{},"snapshot":"success: true\nexit_code: 0\n----- stdout -----\nπ \u001b[36mMerging 1 commit to \u001b[1m\u001b[36mmain\u001b[0m\u001b[36m @ \u001b[2m[SHA]\u001b[0m (no commit/squash/rebase needed)\n\u001b[107m \u001b[0m * [SHA]\u001b[33m (\u001b[m\u001b[1;36mHEAD\u001b[m\u001b[33m -> \u001b[m\u001b[1;32mfeature\u001b[m\u001b[33m)\u001b[m Add feature file\n\u001b[107m \u001b[0m feature.txt | 1 \u001b[32m+\u001b[m\n\u001b[107m \u001b[0m 1 file changed, 1 insertion(+)\nβ \u001b[32mMerged to \u001b[1m\u001b[32mmain\u001b[0m\u001b[0m (1 commit, 1 file, \u001b[32m+1\u001b[0m)\nβ \u001b[32mWorktree preserved (--no-remove)\u001b[0m\n\n----- stderr -----"}}+{"run_id":"1763935889-605369000","line":2309,"new":null,"old":null}+{"run_id":"1763935889-605369000","line":2364,"new":null,"old":null}+{"run_id":"1763935889-605369000","line":2260,"new":null,"old":null}+{"run_id":"1763935927-181361000","line":2309,"new":null,"old":null}+{"run_id":"1763935927-181361000","line":2364,"new":null,"old":null}+{"run_id":"1763935927-181361000","line":2260,"new":null,"old":null}+{"run_id":"1763936206-779718000","line":2364,"new":null,"old":null}+{"run_id":"1763936206-779718000","line":2309,"new":null,"old":null}+{"run_id":"1763936206-779718000","line":2260,"new":null,"old":null}diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rsindex 99fab8cb4..d8331d373 100644--- a/tests/integration_tests/merge.rs+++ b/tests/integration_tests/merge.rs@@ -2933,3 +2933,46 @@ fn test_merge_when_default_branch_missing_worktree() {snapshot_merge("merge_default_branch_missing_worktree", &repo, &[], None);}++#[test]+fn test_merge_does_not_permanently_set_receive_deny_current_branch() {+ let (repo, feature_wt) = setup_merge_scenario();++ // Explicitly set config to "refuse" - this would block pushes to checked-out branches+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["config", "receive.denyCurrentBranch", "refuse"])+ .current_dir(repo.root_path())+ .output()+ .expect("Failed to set config");++ // Perform merge - should succeed despite "refuse" setting because we use --receive-pack+ let mut cmd = make_snapshot_cmd(&repo, "merge", &["main"], Some(&feature_wt));+ let output = cmd.output().expect("Failed to run merge");+ assert!(+ output.status.success(),+ "Merge should succeed even with receive.denyCurrentBranch=refuse.\n\+ stdout: {}\n\+ stderr: {}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr)+ );++ // Check config after merge - should still be "refuse" (not permanently changed)+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ let after = cmd+ .args(["config", "receive.denyCurrentBranch"])+ .current_dir(repo.root_path())+ .output()+ .expect("Failed to get config");+ let after_value = String::from_utf8_lossy(&after.stdout).trim().to_string();++ assert_eq!(+ after_value, "refuse",+ "receive.denyCurrentBranch should not be permanently modified by merge.\n\+ Expected: \"refuse\"\n\+ Got: {:?}",+ after_value+ );+}