Skip to content

Commit bdd9758

Browse files
julianknutsenclaude
andcommitted
Fix claim-after-discard: check branch data instead of branch existence
After discard clears item data from a branch (when DOLT_BRANCH deletion fails), the branch still exists but is empty. The old forkBranchExists check saw the branch and used write/{branch}/{branch}, starting from the empty state — making the PR show the item as removed. branchHasData queries the specific wanted item on the branch. Dead branches (cleared by discard) return false, so Exec starts fresh from main via write/main/{branch}. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4fefd69 commit bdd9758

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

internal/backend/remote.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (r *RemoteDB) Exec(branch, _ string, _ bool, stmts ...string) error {
8989
// If the target branch already exists on the fork, write from that branch
9090
// to preserve prior mutations (e.g. claim → done). Otherwise write from main.
9191
fromBranch := "main"
92-
if branch != "main" && r.forkBranchExists(branch) {
92+
if branch != "main" && r.branchHasData(branch) {
9393
fromBranch = branch
9494
}
9595

@@ -386,8 +386,28 @@ func formatDiffRow(buf *strings.Builder, header, fields []string) {
386386
}
387387
}
388388

389-
// forkBranchExists checks whether a branch exists on the fork database.
390-
func (r *RemoteDB) forkBranchExists(branch string) bool {
389+
// branchHasData checks whether a wl/ branch has item data worth preserving.
390+
// Branches cleared by discard (no wanted row) should start fresh from main.
391+
func (r *RemoteDB) branchHasData(branch string) bool {
392+
// Extract wanted ID from wl/{rig}/{wantedID} convention.
393+
parts := strings.SplitN(branch, "/", 3)
394+
if len(parts) != 3 || parts[0] != "wl" || parts[2] == "" {
395+
// Not a wl branch — fall back to branch existence check.
396+
return r.branchExists(branch)
397+
}
398+
wantedID := strings.ReplaceAll(parts[2], "'", "''")
399+
sql := fmt.Sprintf("SELECT COUNT(*) AS cnt FROM wanted WHERE id='%s'", wantedID)
400+
csv, err := r.queryForkBranch(sql, branch)
401+
if err != nil {
402+
// Branch probably doesn't exist — start from main.
403+
return false
404+
}
405+
lines := strings.Split(strings.TrimSpace(csv), "\n")
406+
return len(lines) >= 2 && strings.TrimSpace(lines[1]) != "0"
407+
}
408+
409+
// branchExists checks whether a branch exists on the fork database.
410+
func (r *RemoteDB) branchExists(branch string) bool {
391411
escaped := strings.ReplaceAll(branch, "'", "''")
392412
sql := fmt.Sprintf("SELECT COUNT(*) AS cnt FROM dolt_branches WHERE name='%s'", escaped)
393413
csv, err := r.queryForkBranch(sql, "main")

0 commit comments

Comments
 (0)