gastownhall/beads · error

failed to enumerate --merged-into ref %q: %w

Error message

failed to enumerate --merged-into ref %q: %w

What it means

`findWorktreeComparatorRefs` runs `git for-each-ref --format=%(refname) -- <candidates>` to enumerate which candidate refs match a `--merged-into` selector. If the git subprocess fails, the error is wrapped with the selector name.

Source

Thrown at cmd/bd/worktree_cmd.go:1809

	selector string,
) ([]string, error) {
	candidates := []string{
		"refs/" + selector,
		"refs/tags/" + selector,
		"refs/heads/" + selector,
		"refs/remotes/" + selector,
		"refs/remotes/" + selector + "/HEAD",
	}
	candidateSet := make(map[string]struct{}, len(candidates))
	for _, candidate := range candidates {
		candidateSet[candidate] = struct{}{}
	}

	args := []string{"for-each-ref", "--format=%(refname)", "--"}
	args = append(args, candidates...)
	output, err := git.output(ctx, executionRoot, args...)
	if err != nil {
		return nil, fmt.Errorf("failed to enumerate --merged-into ref %q: %w", selector, err)
	}

	matchSet := make(map[string]struct{})
	for _, line := range strings.Split(strings.TrimSpace(string(output)), "\n") {
		ref := strings.TrimSpace(line)
		if _, candidate := candidateSet[ref]; candidate {
			matchSet[ref] = struct{}{}
		}
	}
	matches := make([]string, 0, len(matchSet))
	for ref := range matchSet {
		matches = append(matches, ref)
	}
	sort.Strings(matches)
	return matches, nil
}

func repositoryObjectIDLength(

View on GitHub (pinned to 71377f2769)

Solutions

  1. Run `git for-each-ref --format=%(refname) -- <candidate>` manually to see the raw error
  2. Verify you are inside a valid git repository
  3. Repair repository state (`git fsck`) if refs are corrupt
  4. Retry after confirming the `git` binary works and is up to date

Example fix

// diagnose
git for-each-ref --format=%(refname) -- refs/heads/*
// then retry
bd worktree remove --merged-into refs/heads/main
Defensive patterns

Strategy: try-catch

Validate before calling

// shell pre-check
git for-each-ref --format='%(refname)' -- refs/heads/ >/dev/null 2>&1 || echo "for-each-ref broken or not in repo"

Try / catch

if err != nil {
    var ee *exec.ExitError
    if errors.As(err, nil) || strings.Contains(err.Error(), "for-each-ref") {
        // inspect repository health, retry after git fsck / re-clone
    }
}

Prevention

When it happens

Trigger: `git for-each-ref` exits non-zero — invalid/malformed candidate ref patterns, git subprocess environment failure, or running outside a repository.

Common situations: Corrupt refs directory; repository moved or `.git` broken; git version too old for used flags; a candidate pattern containing characters git rejects.

Related errors


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