Skip to content

Commit 9d53c5c

Browse files
julianknutsenclaude
andcommitted
Fix branch checkout using CLI instead of session-scoped SQL procedures
DOLT_CHECKOUT via `dolt sql` is session-scoped and does not persist across separate dolt sql invocations. This caused PR-mode mutations (post, claim, done) to commit on main instead of the intended branch. Switch CheckoutBranch and CheckoutMain to use `dolt checkout` and `dolt branch` CLI commands, which correctly persist branch state to .dolt/repo_state.json. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 36f6534 commit 9d53c5c

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

internal/commons/dolt.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,37 @@ func BranchExists(dbDir, branch string) (bool, error) {
114114
}
115115

116116
// CheckoutBranch creates the branch if it doesn't exist, then checks it out.
117+
// Uses dolt CLI commands (not SQL DOLT_CHECKOUT) because the SQL stored
118+
// procedure is session-scoped and does not persist across dolt sql invocations.
117119
func CheckoutBranch(dbDir, branch string) error {
118120
exists, err := BranchExists(dbDir, branch)
119121
if err != nil {
120122
return fmt.Errorf("checking branch %s: %w", branch, err)
121123
}
122124
if !exists {
123-
if err := doltSQLScript(dbDir, fmt.Sprintf(
124-
"CALL DOLT_BRANCH('%s');", strings.ReplaceAll(branch, "'", "''"),
125-
)); err != nil {
125+
if err := doltExec(dbDir, "branch", branch); err != nil {
126126
return fmt.Errorf("creating branch %s: %w", branch, err)
127127
}
128128
}
129-
return doltSQLScript(dbDir, fmt.Sprintf(
130-
"CALL DOLT_CHECKOUT('%s');", strings.ReplaceAll(branch, "'", "''"),
131-
))
129+
return doltExec(dbDir, "checkout", branch)
132130
}
133131

134132
// CheckoutMain switches the working directory back to the main branch.
135133
func CheckoutMain(dbDir string) error {
136-
return doltSQLScript(dbDir, "CALL DOLT_CHECKOUT('main');")
134+
return doltExec(dbDir, "checkout", "main")
135+
}
136+
137+
// doltExec runs a dolt CLI command in the given database directory.
138+
func doltExec(dbDir string, args ...string) error {
139+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
140+
defer cancel()
141+
cmd := exec.CommandContext(ctx, "dolt", args...)
142+
cmd.Dir = dbDir
143+
output, err := cmd.CombinedOutput()
144+
if err != nil {
145+
return fmt.Errorf("dolt %s: %w (%s)", strings.Join(args, " "), err, strings.TrimSpace(string(output)))
146+
}
147+
return nil
137148
}
138149

139150
// PushBranch pushes a named branch to origin.

0 commit comments

Comments
 (0)