diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 5e5f4a88a..68003aae6 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -554,7 +554,7 @@ func (d *Daemon) process(ctx context.Context, task *store.Task) { // Clone the worktree before Docker mount setup. The container // binds /data/worktree to workDir on the host — the directory // must exist before Acquire is called. - _, branch, err := d.Trees.Prepare(ctx, task.Owner, task.Repo, repo.Base, task.IssueNumber) + _, branch, err := d.Trees.Prepare(ctx, task.Owner, task.Repo, repo.Base, task.IssueNumber, task.Title) if err != nil { d.Log.Error("worktree prepare failed", "err", err) _ = d.Store.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, "worktree prepare failed: "+err.Error()) diff --git a/internal/workflow/steps.go b/internal/workflow/steps.go index c6fb19e19..66010a108 100644 --- a/internal/workflow/steps.go +++ b/internal/workflow/steps.go @@ -21,7 +21,7 @@ import ( // containers require the tree before acquire). func StagePrepareWorktree() Stage { return Stage{Name: "prepare", Run: func(ctx context.Context, tc *TaskContext) error { - dir, branch, err := tc.Trees.Prepare(ctx, tc.Task.Owner, tc.Task.Repo, tc.Repo.BaseBranch(), tc.Task.IssueNumber) + dir, branch, err := tc.Trees.Prepare(ctx, tc.Task.Owner, tc.Task.Repo, tc.Repo.BaseBranch(), tc.Task.IssueNumber, tc.Task.Title) if err != nil { return err } diff --git a/internal/worktree/worktree.go b/internal/worktree/worktree.go index d099d7995..77254e5bc 100644 --- a/internal/worktree/worktree.go +++ b/internal/worktree/worktree.go @@ -69,9 +69,9 @@ func (m *Manager) git(ctx context.Context, dir string, args ...string) (string, // Any leftover worktree from a prior attempt is removed first. // If the worktree is already prepared (the daemon may have done this // before container acquisition), it returns the existing directory. -func (m *Manager) Prepare(ctx context.Context, owner, repo, base string, issue int) (dir, branch string, err error) { +func (m *Manager) Prepare(ctx context.Context, owner, repo, base string, issue int, title string) (dir, branch string, err error) { dir = m.Dir(owner, repo, issue) - branch = fmt.Sprintf("archie/issue-%d", issue) + branch = archieBranch(issue, title) // Already prepared (daemon cloned before container acquire). if st, statErr := os.Stat(filepath.Join(dir, ".git")); statErr == nil && st.IsDir() { @@ -99,6 +99,38 @@ func (m *Manager) Prepare(ctx context.Context, owner, repo, base string, issue i return dir, branch, nil } +// archieBranch builds a descriptive branch name from issue number and +// a slug of the title. Example: "archie/62-clear-finished-button". +func archieBranch(issue int, title string) string { + slug := branchSlug(title) + if slug == "" { + return fmt.Sprintf("archie/%d", issue) + } + return fmt.Sprintf("archie/%d-%s", issue, slug) +} + +// branchSlug converts a title to a kebab-case slug suitable for a git +// branch. Only keeps lowercase alphanumerics and hyphens, max 40 chars. +func branchSlug(title string) string { + var b strings.Builder + for _, r := range strings.ToLower(title) { + if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' { + b.WriteRune(r) + } else if r == ' ' || r == '-' || r == '_' { + b.WriteRune('-') + } + if b.Len() >= 40 { + break + } + } + slug := strings.Trim(b.String(), "-") + // Collapse multiple hyphens. + for strings.Contains(slug, "--") { + slug = strings.ReplaceAll(slug, "--", "-") + } + return slug +} + func (m *Manager) cloneURL(owner, repo string) string { if m.BaseURL != "" { return fmt.Sprintf("%s/%s/%s.git", m.BaseURL, owner, repo) diff --git a/internal/worktree/worktree_test.go b/internal/worktree/worktree_test.go index 3265b0ce7..bcf6bb912 100644 --- a/internal/worktree/worktree_test.go +++ b/internal/worktree/worktree_test.go @@ -63,11 +63,11 @@ func TestPrepareCommitPushRoundTrip(t *testing.T) { BaseURL: "file://" + host, } - dir, branch, err := m.Prepare(ctx, "sam", "todo", "main", 42) + dir, branch, err := m.Prepare(ctx, "sam", "todo", "main", 42, "test issue title") if err != nil { t.Fatalf("prepare: %v", err) } - if branch != "archie/issue-42" { + if branch != "archie/42-test-issue-title" { t.Fatalf("branch = %q", branch) }