gastownhall/beads · error

get current branch: %w

Error message

get current branch: %w

What it means

CurrentBranch queries Dolt's active_branch() SQL function and wraps any driver error with 'get current branch: %w'. This wrapper marks failures to read the session's active branch name — typically a connection problem, a non-Dolt database, or a database with no branch context. It preserves the underlying driver error for inspection via errors.Is/As.

Source

Thrown at internal/storage/versioncontrolops/branches.go:31

	}
	defer rows.Close()

	var branches []string
	for rows.Next() {
		var name string
		if err := rows.Scan(&name); err != nil {
			return nil, fmt.Errorf("scan branch: %w", err)
		}
		branches = append(branches, name)
	}
	return branches, rows.Err()
}

// CurrentBranch returns the name of the active branch.
func CurrentBranch(ctx context.Context, db DBConn) (string, error) {
	var branch string
	if err := db.QueryRowContext(ctx, "SELECT active_branch()").Scan(&branch); err != nil {
		return "", fmt.Errorf("get current branch: %w", err)
	}
	return branch, nil
}

// CreateBranch creates a new Dolt branch from the current HEAD.
func CreateBranch(ctx context.Context, db DBConn, name string) error {
	if _, err := db.ExecContext(ctx, "CALL DOLT_BRANCH(?)", name); err != nil {
		return fmt.Errorf("create branch %s: %w", name, err)
	}
	return nil
}

// DeleteBranch force-deletes a Dolt branch.
func DeleteBranch(ctx context.Context, db DBConn, name string) error {
	if _, err := db.ExecContext(ctx, "CALL DOLT_BRANCH('-D', ?)", name); err != nil {
		return fmt.Errorf("delete branch %s: %w", name, err)
	}
	return nil

View on GitHub (pinned to 71377f2769)

Solutions

  1. Verify the database is actually a Dolt database (SELECT active_branch() works on Dolt only) and the Dolt version is recent enough
  2. Check the embedded driver error (errors.Is/As) for connection/refused causes and reconnect with a fresh DBConn
  3. Validate connectivity with a trivial ping query before calling CurrentBranch

Example fix

// before
branch, err := versioncontrolops.CurrentBranch(ctx, staleDB)
// after
if err := db.PingContext(ctx); err != nil {
    db, err = openDolt(ctx) // reopen after detecting dead connection
}
branch, err := versioncontrolops.CurrentBranch(ctx, db)
Defensive patterns

Strategy: try-catch

Validate before calling

var one string
if err := db.QueryRowContext(ctx, "SELECT 1").Scan(&one); err != nil {
    // connection is not usable; skip branch query
}

Try / catch

branch, err := versioncontrolops.CurrentBranch(ctx, db)
if err != nil {
    var driverErr error
    if errors.As(err, &driverErr) {
        log.Printf("current branch query failed: %v", driverErr) // decide reconnect vs fatal
    }
    return fmt.Errorf("branch detection unavailable: %w", err)
}

Prevention

When it happens

Trigger: Calling versioncontrolops.CurrentBranch with a DBConn whose underlying query 'SELECT active_branch()' fails: closed/lost connection, server not a Dolt server, or Dolt version lacking active_branch().

Common situations: Dolt server restarted or network dropped mid-session; pointing at a plain MySQL database instead of Dolt; running an old Dolt version without active_branch(); connection pool returned a stale connection.

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/4d56993151d3f661. Report an issue: GitHub.