fix: Validate sync-branch at config-time and runtime (closes #1166) (#1168)

* fix(config): validate sync-branch at config time

Add sync-branch validation to validateYamlConfigValue() to reject
main/master at config time, preventing the validation bypass in GH#1166.

- Add case for sync-branch and sync.branch keys
- Inline validation logic to avoid import cycle (config <-> syncbranch)
- Add unit tests for rejection (main/master) and acceptance (valid names)

Part of: #1166

* fix(sync): add runtime guard for sync-branch == current-branch

Add dynamic runtime check before worktree operations to catch cases
where sync-branch matches the current branch. This provides defense
in depth for manual YAML edits, pre-fix configs, or non-main/master
branch names (trunk, develop, production, etc.).

- Check IsSyncBranchSameAsCurrent() after hasSyncBranchConfig is set
- Position check BEFORE worktree entry (CWD changes inside worktree)
- Add integration test TestSync_FailsWhenOnSyncBranch

Part of: #1166

* docs: note main/master restriction in sync-branch FAQ

Clarifies that git worktrees cannot checkout the same branch in
multiple locations, so main/master cannot be used as sync branch.
This commit is contained in:
Peter Chanthamynavong
2026-01-19 10:11:06 -08:00
committed by GitHub
parent 4fffdb7fae
commit 2cbf3a5497
5 changed files with 148 additions and 1 deletions

View File

@@ -273,6 +273,17 @@ The --full flag provides the legacy full sync behavior for backwards compatibili
}
hasSyncBranchConfig := syncBranchName != ""
// GH#1166: Block sync if currently on the sync branch
// This must happen BEFORE worktree operations - after entering a worktree,
// GetCurrentBranch() would return the worktree's branch, not the original.
if hasSyncBranchConfig {
if syncbranch.IsSyncBranchSameAsCurrent(ctx, syncBranchName) {
FatalError("Cannot sync to '%s': it's your current branch. "+
"Checkout a different branch first, or use a dedicated sync branch like 'beads-sync'.",
syncBranchName)
}
}
// bd-wayc3: Check for redirect + sync-branch incompatibility
// Redirect and sync-branch are mutually exclusive:
// - Redirect says: "My database is in another repo (I am a client)"