Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-14
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Design

## Context

`scaffold_change` computes `branch_name(issue, type, slug)` from the title and returns it. `new_cmd` prints it. `current_branch` returns an empty string on a detached HEAD, and `parse_branch` reads `<type>/<issue>-<slug>`.

## Goals

- The output names the branch that holds the folder.
- The folder and the worktree branch share one slug.

## Decisions

- **Read the branch once.** `_checked_out_branch` catches a git error and returns None, so a checkout without a repository still scaffolds.
- **The branch slug wins for the same issue.** `_slug_for_issue` reads the slug from the branch of the checkout, and the title supplies it otherwise.
- **A computed name off the checkout is a suggestion.** One message states it.

## Risks

- A worktree branch that names the issue but a different slug renames the folder from the title. The folder and the branch then agree, which is the rule of CONTRIBUTING.md.

## Tests

- The output and the folder take the branch slug.
- A branch of another issue gives the suggestion.
- A detached HEAD names no branch.
- The dry run matches a real run.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Proposal

## Why

`devops openspec new` prints "Created <folder> on branch <branch>", and the branch is a name that the command computes from the issue title. It creates, switches to and reads no branch, so a checkout on another branch reads a false fact, and the folder slug can differ from the worktree branch slug (#4593).

## What Changes

- `ScaffoldResult` holds `current_branch`, the branch of the checkout, or None when HEAD is detached.
- The folder slug comes from the branch of the checkout when that branch is `<type>/<issue>-<slug>` for the same issue; otherwise from the title.
- The output names the branch of the checkout, or `HEAD detached`, and calls the computed name a suggestion when the checkout is not on it.
- The change fits one pull request.

## Capabilities

### New Capabilities

None.

### Modified Capabilities

- `openspec-scaffold`: The output names the branch of the checkout, and the folder takes its slug.

## Impact

- Code: `src/devops_cli/openspec/scaffold.py`, `src/devops_cli/commands/openspec.py`, `src/devops_cli/lang/en/messages.py`
- Tests: `tests/openspec/test_openspec_new.py`

## Feasibility

- `current_branch` and `parse_branch` already exist.
- The command already prints from `MESSAGES.openspec`.
- Verification commands: `uv run devops ci test tests/openspec/test_openspec_new.py` and `uv run devops ci --check --only lint,format,typecheck`.

## Existing Tool Considered

GitPython can report the branch, but the project removed it for one git seam. The change reuses `current_branch` and `parse_branch`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## ADDED Requirements

### Requirement: The output names the branch of the checkout
`devops openspec new` SHALL name the branch of the checkout in its output, and SHALL name `HEAD detached` when HEAD is detached.

#### Scenario: The checkout is on the default branch
- **WHEN** the checkout is on `main` and the author runs `devops openspec new 4593 --title "fix(openspec): alpha"`
- **THEN** the output holds `Created openspec/changes/4593-alpha on branch main.` and `Branch: main`

#### Scenario: HEAD is detached
- **WHEN** HEAD is detached and the author runs the command
- **THEN** the output holds `Branch: HEAD detached`, holds no `on branch`, and creates `openspec/changes/4593-alpha/`

### Requirement: The folder takes the slug of the branch of the issue
The command SHALL take the slug of the change folder from the branch of the checkout when that branch is `<type>/<issue>-<slug>` for the same issue, and from the title otherwise.

#### Scenario: The branch slug differs from the title slug
- **WHEN** the worktree is on `fix/4593-alpha` and the author runs `devops openspec new 4593 --title "fix(openspec): beta"`
- **THEN** the command creates `openspec/changes/4593-alpha/` and no folder `openspec/changes/4593-beta/`

### Requirement: A branch of another issue is a suggestion
When the branch of the checkout does not name the same issue, the command SHALL state that the computed branch name is a suggestion and that the checkout is not on it.

#### Scenario: The checkout is on another issue
- **WHEN** the checkout is on `docs/5225-adr-front-matter-and-rules` and the author runs `devops openspec new 4593 --title "fix(openspec): alpha"`
- **THEN** the output holds `Branch: docs/5225-adr-front-matter-and-rules` and `The branch fix/4593-alpha is a suggestion for this change. This checkout is not on it.`

### Requirement: The dry run names the folder and branch of a real run
The dry run SHALL name the folder and the branch that a real run creates in the same checkout, and SHALL create no folder.

#### Scenario: The dry run is in a worktree on the branch of the issue
- **WHEN** the worktree is on `fix/4593-alpha` and the author runs `devops openspec new 4593 --dry-run --title "fix(openspec): beta"`
- **THEN** the output holds `Would create openspec/changes/4593-alpha on branch fix/4593-alpha.` and `Branch: fix/4593-alpha`, and no folder is created
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Tasks

## 1. Name the branch of the checkout

Reads: `src/devops_cli/openspec/scaffold.py`, `src/devops_cli/commands/openspec.py`. Changes: the same files, `src/devops_cli/lang/en/messages.py`, and `tests/openspec/test_openspec_new.py`.

- [x] 1.1 Read the branch of the checkout and take the slug of the same issue from it.
- [x] 1.2 Print the branch of the checkout, or `HEAD detached`, and the suggestion.
- [x] 1.3 Add the four tests.
- Verification: `$ uv run devops ci test tests/openspec/test_openspec_new.py` passes
32 changes: 32 additions & 0 deletions openspec/specs/openspec-scaffold/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,35 @@ Scaffold an OpenSpec change folder in project form for an issue with pre-filled
#### Scenario: Scaffold a new change
- **WHEN** an author runs `devops openspec new 3203 --title 'feat(openspec): scaffold the change folder'`
- **THEN** `openspec/changes/3203-scaffold-the-change-folder/` is created with the four files

### Requirement: The output names the branch of the checkout
`devops openspec new` SHALL name the branch of the checkout in its output, and SHALL name `HEAD detached` when HEAD is detached.

#### Scenario: The checkout is on the default branch
- **WHEN** the checkout is on `main` and the author runs `devops openspec new 4593 --title "fix(openspec): alpha"`
- **THEN** the output holds `Created openspec/changes/4593-alpha on branch main.` and `Branch: main`

#### Scenario: HEAD is detached
- **WHEN** HEAD is detached and the author runs the command
- **THEN** the output holds `Branch: HEAD detached`, holds no `on branch`, and creates `openspec/changes/4593-alpha/`

### Requirement: The folder takes the slug of the branch of the issue
The command SHALL take the slug of the change folder from the branch of the checkout when that branch is `<type>/<issue>-<slug>` for the same issue, and from the title otherwise.

#### Scenario: The branch slug differs from the title slug
- **WHEN** the worktree is on `fix/4593-alpha` and the author runs `devops openspec new 4593 --title "fix(openspec): beta"`
- **THEN** the command creates `openspec/changes/4593-alpha/` and no folder `openspec/changes/4593-beta/`

### Requirement: A branch of another issue is a suggestion
When the branch of the checkout does not name the same issue, the command SHALL state that the computed branch name is a suggestion and that the checkout is not on it.

#### Scenario: The checkout is on another issue
- **WHEN** the checkout is on `docs/5225-adr-front-matter-and-rules` and the author runs `devops openspec new 4593 --title "fix(openspec): alpha"`
- **THEN** the output holds `Branch: docs/5225-adr-front-matter-and-rules` and `The branch fix/4593-alpha is a suggestion for this change. This checkout is not on it.`

### Requirement: The dry run names the folder and branch of a real run
The dry run SHALL name the folder and the branch that a real run creates in the same checkout, and SHALL create no folder.

#### Scenario: The dry run is in a worktree on the branch of the issue
- **WHEN** the worktree is on `fix/4593-alpha` and the author runs `devops openspec new 4593 --dry-run --title "fix(openspec): beta"`
- **THEN** the output holds `Would create openspec/changes/4593-alpha on branch fix/4593-alpha.` and `Branch: fix/4593-alpha`, and no folder is created
25 changes: 18 additions & 7 deletions src/devops_cli/commands/openspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,26 @@ def new_cmd(
print_error(str(exc), prefix=False)
raise typer.Exit(1) from exc

branch = res.current_branch or MESSAGES.openspec.new_detached_branch
suggestion = (
""
if res.current_branch == res.branch
else "\n\n" + MESSAGES.openspec.new_suggestion.format(branch=res.branch)
)
if res.dry_run:
print_info(f"Would create {res.folder} on branch {res.branch}.")
print_info(
MESSAGES.openspec.new_dry_run_output.format(folder=res.folder, branch=branch)
+ suggestion
)
print_info(f"Folder: {res.folder}")
print_info(f"Branch: {res.branch}")
print_info(f"Branch: {branch}")
return

print_success(
f"Created {res.folder} on branch {res.branch}.\n\n"
f"Branch: {res.branch}\n"
f"Checks: uv run devops ci\n"
f"Archive: uv run devops openspec archive {res.change}"
text = (
MESSAGES.openspec.new_next_steps_detached.format(folder=res.folder, change=res.change)
if res.current_branch is None
else MESSAGES.openspec.new_next_steps.format(
folder=res.folder, branch=branch, change=res.change
)
)
print_success(text + suggestion)
11 changes: 11 additions & 0 deletions src/devops_cli/lang/en/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ class OpenSpecMessages:
archive_dry_run_output: str = "Would archive {change}. The commands above are not run."
change_folder_exists: str = "The change folder {path} already exists."
new_dry_run_needs_title: str = "--dry-run requires --title so it makes no external request."
new_detached_branch: str = "HEAD detached"
new_dry_run_output: str = "Would create {folder} on branch {branch}."
new_next_steps: str = (
"Created {folder} on branch {branch}.\n\n"
Expand All @@ -558,6 +559,16 @@ class OpenSpecMessages:
" Checks: uv run devops ci\n"
" Archive: uv run devops openspec archive {change}"
)
new_next_steps_detached: str = (
"Created {folder}.\n\n"
"Next steps:\n"
" Branch: HEAD detached\n"
" Checks: uv run devops ci\n"
" Archive: uv run devops openspec archive {change}"
)
new_suggestion: str = (
"The branch {branch} is a suggestion for this change. This checkout is not on it."
)


@dataclass(frozen=True)
Expand Down
41 changes: 35 additions & 6 deletions src/devops_cli/openspec/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
)
from devops_cli.core.repo import get_repo_origin_name
from devops_cli.exceptions import DevOpsCLIError
from devops_cli.git.branch_form import branch_name, slug_from_title
from devops_cli.git.branch_form import branch_name, parse_branch, slug_from_title
from devops_cli.git.operations import current_branch
from devops_cli.github.issues import get_repository_issue
from devops_cli.lang import MESSAGES
from devops_cli.openspec.tool import isolated_openspec
Expand All @@ -24,12 +25,15 @@

@dataclass(frozen=True)
class ScaffoldResult:
"""The result of scaffolding an OpenSpec change folder."""
"""The result of scaffolding an OpenSpec change folder. `branch` is the computed branch name,
which the roadmap jobs use; `current_branch` is the branch of the checkout, or None when HEAD
is detached or the checkout is no repository (#4593)."""

folder: str
branch: str
change: str
dry_run: bool = False
current_branch: str | None = None


def extract_requirement_from_spec(spec_text: str, requirement_name: str) -> str | None:
Expand Down Expand Up @@ -140,6 +144,21 @@ def scaffold_spec_delta(root: Path, capability: str, requirement: str | None) ->
)


def _checked_out_branch(root: Path) -> str | None:
"""The branch of the checkout, or None when HEAD is detached or the directory is no
repository (#4593)."""
try:
return current_branch(root) or None
except DevOpsCLIError:
return None


def _slug_for_issue(branch: str | None, issue: int) -> str | None:
"""The slug of `branch` when it names `issue` in the project branch form, else None."""
parts = parse_branch(branch)
return parts.slug if parts is not None and parts.issue == issue else None


def scaffold_change(
root: Path,
issue: int,
Expand All @@ -150,24 +169,33 @@ def scaffold_change(
dry_run: bool = False,
repo: str | None = None,
) -> ScaffoldResult:
"""Scaffold an OpenSpec change folder in project form."""
"""Scaffold an OpenSpec change folder in project form. The slug of the folder comes from the
branch of the checkout when that branch names the same issue, so the folder and the worktree
branch hold one slug (#4593)."""
checked_out = _checked_out_branch(root)
if dry_run:
if title is None:
raise DevOpsCLIError(MESSAGES.openspec.new_dry_run_needs_title)
type_ = commit_type(title) or "feat"
slug = slug_from_title(title)
slug = _slug_for_issue(checked_out, issue) or slug_from_title(title)
change_name = f"{issue}-{slug}"
branch = branch_name(issue, type_, slug)
folder = f"{CONST_OPENSPEC_CHANGES_DIR}/{change_name}"
return ScaffoldResult(folder=folder, branch=branch, change=change_name, dry_run=True)
return ScaffoldResult(
folder=folder,
branch=branch,
change=change_name,
dry_run=True,
current_branch=checked_out,
)

if title is None:
target_repo = repo or get_repo_origin_name(root) or "unknown/repo"
issue_view = get_repository_issue(target_repo, issue)
title = issue_view.title

type_ = commit_type(title) or "feat"
slug = slug_from_title(title)
slug = _slug_for_issue(checked_out, issue) or slug_from_title(title)
change_name = f"{issue}-{slug}"
branch = branch_name(issue, type_, slug)
change_folder = root / CONST_OPENSPEC_CHANGES_DIR / change_name
Expand Down Expand Up @@ -204,4 +232,5 @@ def scaffold_change(
branch=branch,
change=change_name,
dry_run=False,
current_branch=checked_out,
)
64 changes: 64 additions & 0 deletions tests/openspec/test_openspec_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,67 @@ def test_skill_points_to_command() -> None:
for skill_text in (claude_skill, agents_skill):
assert "devops openspec new" in skill_text
assert "openspec new change" not in skill_text


def test_the_output_names_the_branch_of_the_checkout_and_its_slug(
cli: Cli, openspec_fake: OpenSpecFake, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The folder takes the slug of the branch of the same issue, and the output names that
branch (#4593)."""
tree = _clean_tree(openspec_fake)
monkeypatch.chdir(tree)
monkeypatch.setattr(
"devops_cli.openspec.scaffold.current_branch", lambda root: "fix/3203-alpha"
)

result = cli("openspec", "new", "3203", "--title", "fix(openspec): beta")

assert "Created openspec/changes/3203-alpha on branch fix/3203-alpha." in result.output
assert "3203-beta" not in result.output


def test_a_branch_of_another_issue_is_a_suggestion(
cli: Cli, openspec_fake: OpenSpecFake, monkeypatch: pytest.MonkeyPatch
) -> None:
tree = _clean_tree(openspec_fake)
monkeypatch.chdir(tree)
current = "docs/5225-adr-front-matter-and-rules"
monkeypatch.setattr("devops_cli.openspec.scaffold.current_branch", lambda root: current)

result = cli("openspec", "new", "3203", "--title", "fix(openspec): alpha")

assert f"Branch: {current}" in result.output
assert (
"The branch fix/3203-alpha is a suggestion for this change. This checkout is not on it."
in result.output
)


def test_a_detached_head_names_no_branch(
cli: Cli, openspec_fake: OpenSpecFake, monkeypatch: pytest.MonkeyPatch
) -> None:
tree = _clean_tree(openspec_fake)
monkeypatch.chdir(tree)
monkeypatch.setattr("devops_cli.openspec.scaffold.current_branch", lambda root: "")

result = cli("openspec", "new", "3203", "--title", "fix(openspec): alpha")

assert "Branch: HEAD detached" in result.output
assert "on branch" not in result.output
assert (tree / "openspec/changes/3203-alpha").is_dir()


def test_the_dry_run_names_the_folder_and_branch_of_a_real_run(
cli: Cli, openspec_fake: OpenSpecFake, monkeypatch: pytest.MonkeyPatch
) -> None:
tree = _clean_tree(openspec_fake)
monkeypatch.chdir(tree)
monkeypatch.setattr(
"devops_cli.openspec.scaffold.current_branch", lambda root: "fix/3203-alpha"
)

result = cli("openspec", "new", "3203", "--dry-run", "--title", "fix(openspec): beta")

assert "Would create openspec/changes/3203-alpha on branch fix/3203-alpha." in result.output
assert "Branch: fix/3203-alpha" in result.output
assert not (tree / "openspec/changes/3203-alpha").exists()