From 76b1ab4d39ee0534f71f4f69b15a04ae585e8eec Mon Sep 17 00:00:00 2001 From: Daniel Petty Date: Sat, 14 Nov 2026 07:49:12 +0000 Subject: [PATCH 1/3] docs(openspec): plan #4593 name the checkout branch --- .../4593-openspec-new-branch/.openspec.yaml | 2 + .../4593-openspec-new-branch/design.md | 27 ++++++++++++++ .../4593-openspec-new-branch/proposal.md | 37 +++++++++++++++++++ .../specs/openspec-scaffold/spec.md | 33 +++++++++++++++++ .../changes/4593-openspec-new-branch/tasks.md | 10 +++++ 5 files changed, 109 insertions(+) create mode 100644 openspec/changes/4593-openspec-new-branch/.openspec.yaml create mode 100644 openspec/changes/4593-openspec-new-branch/design.md create mode 100644 openspec/changes/4593-openspec-new-branch/proposal.md create mode 100644 openspec/changes/4593-openspec-new-branch/specs/openspec-scaffold/spec.md create mode 100644 openspec/changes/4593-openspec-new-branch/tasks.md diff --git a/openspec/changes/4593-openspec-new-branch/.openspec.yaml b/openspec/changes/4593-openspec-new-branch/.openspec.yaml new file mode 100644 index 0000000000..be831cee61 --- /dev/null +++ b/openspec/changes/4593-openspec-new-branch/.openspec.yaml @@ -0,0 +1,2 @@ +schema: spec-driven +created: 2026-11-14 diff --git a/openspec/changes/4593-openspec-new-branch/design.md b/openspec/changes/4593-openspec-new-branch/design.md new file mode 100644 index 0000000000..53e0dba606 --- /dev/null +++ b/openspec/changes/4593-openspec-new-branch/design.md @@ -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 `/-`. + +## 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. diff --git a/openspec/changes/4593-openspec-new-branch/proposal.md b/openspec/changes/4593-openspec-new-branch/proposal.md new file mode 100644 index 0000000000..0b456720b7 --- /dev/null +++ b/openspec/changes/4593-openspec-new-branch/proposal.md @@ -0,0 +1,37 @@ +# Proposal + +## Why + +`devops openspec new` prints "Created on 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 `/-` 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`. diff --git a/openspec/changes/4593-openspec-new-branch/specs/openspec-scaffold/spec.md b/openspec/changes/4593-openspec-new-branch/specs/openspec-scaffold/spec.md new file mode 100644 index 0000000000..afb3c11135 --- /dev/null +++ b/openspec/changes/4593-openspec-new-branch/specs/openspec-scaffold/spec.md @@ -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 `/-` 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 diff --git a/openspec/changes/4593-openspec-new-branch/tasks.md b/openspec/changes/4593-openspec-new-branch/tasks.md new file mode 100644 index 0000000000..7488d2635a --- /dev/null +++ b/openspec/changes/4593-openspec-new-branch/tasks.md @@ -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`. + +- [ ] 1.1 Read the branch of the checkout and take the slug of the same issue from it. +- [ ] 1.2 Print the branch of the checkout, or `HEAD detached`, and the suggestion. +- [ ] 1.3 Add the four tests. + - Verification: `$ uv run devops ci test tests/openspec/test_openspec_new.py` passes From 6c9f9873f92b378f9e197b76202528546a4d42f8 Mon Sep 17 00:00:00 2001 From: Daniel Petty Date: Sat, 14 Nov 2026 07:49:28 +0000 Subject: [PATCH 2/3] fix(openspec): name the checkout branch in the new output (#4593) --- src/devops_cli/commands/openspec.py | 25 +++++++---- src/devops_cli/lang/en/messages.py | 11 +++++ src/devops_cli/openspec/scaffold.py | 41 +++++++++++++++--- tests/openspec/test_openspec_new.py | 64 +++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 13 deletions(-) diff --git a/src/devops_cli/commands/openspec.py b/src/devops_cli/commands/openspec.py index b962e45802..e6def245ef 100644 --- a/src/devops_cli/commands/openspec.py +++ b/src/devops_cli/commands/openspec.py @@ -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) diff --git a/src/devops_cli/lang/en/messages.py b/src/devops_cli/lang/en/messages.py index 9239a10212..c388b67b24 100644 --- a/src/devops_cli/lang/en/messages.py +++ b/src/devops_cli/lang/en/messages.py @@ -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" @@ -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) diff --git a/src/devops_cli/openspec/scaffold.py b/src/devops_cli/openspec/scaffold.py index f3d26f19fd..ac91adce04 100644 --- a/src/devops_cli/openspec/scaffold.py +++ b/src/devops_cli/openspec/scaffold.py @@ -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 @@ -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: @@ -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, @@ -150,16 +169,25 @@ 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" @@ -167,7 +195,7 @@ def scaffold_change( 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 @@ -204,4 +232,5 @@ def scaffold_change( branch=branch, change=change_name, dry_run=False, + current_branch=checked_out, ) diff --git a/tests/openspec/test_openspec_new.py b/tests/openspec/test_openspec_new.py index a801036b00..338d146f3f 100644 --- a/tests/openspec/test_openspec_new.py +++ b/tests/openspec/test_openspec_new.py @@ -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() From 8262eb199099a1495154f680b0c3f0f60e875520 Mon Sep 17 00:00:00 2001 From: Daniel Petty Date: Sat, 14 Nov 2026 07:49:43 +0000 Subject: [PATCH 3/3] docs(openspec): archive #4593 name the checkout branch --- .../.openspec.yaml | 0 .../design.md | 0 .../proposal.md | 0 .../specs/openspec-scaffold/spec.md | 0 .../tasks.md | 6 ++-- openspec/specs/openspec-scaffold/spec.md | 32 +++++++++++++++++++ 6 files changed, 35 insertions(+), 3 deletions(-) rename openspec/changes/{4593-openspec-new-branch => archive/2026-11-14-4593-openspec-new-branch}/.openspec.yaml (100%) rename openspec/changes/{4593-openspec-new-branch => archive/2026-11-14-4593-openspec-new-branch}/design.md (100%) rename openspec/changes/{4593-openspec-new-branch => archive/2026-11-14-4593-openspec-new-branch}/proposal.md (100%) rename openspec/changes/{4593-openspec-new-branch => archive/2026-11-14-4593-openspec-new-branch}/specs/openspec-scaffold/spec.md (100%) rename openspec/changes/{4593-openspec-new-branch => archive/2026-11-14-4593-openspec-new-branch}/tasks.md (70%) diff --git a/openspec/changes/4593-openspec-new-branch/.openspec.yaml b/openspec/changes/archive/2026-11-14-4593-openspec-new-branch/.openspec.yaml similarity index 100% rename from openspec/changes/4593-openspec-new-branch/.openspec.yaml rename to openspec/changes/archive/2026-11-14-4593-openspec-new-branch/.openspec.yaml diff --git a/openspec/changes/4593-openspec-new-branch/design.md b/openspec/changes/archive/2026-11-14-4593-openspec-new-branch/design.md similarity index 100% rename from openspec/changes/4593-openspec-new-branch/design.md rename to openspec/changes/archive/2026-11-14-4593-openspec-new-branch/design.md diff --git a/openspec/changes/4593-openspec-new-branch/proposal.md b/openspec/changes/archive/2026-11-14-4593-openspec-new-branch/proposal.md similarity index 100% rename from openspec/changes/4593-openspec-new-branch/proposal.md rename to openspec/changes/archive/2026-11-14-4593-openspec-new-branch/proposal.md diff --git a/openspec/changes/4593-openspec-new-branch/specs/openspec-scaffold/spec.md b/openspec/changes/archive/2026-11-14-4593-openspec-new-branch/specs/openspec-scaffold/spec.md similarity index 100% rename from openspec/changes/4593-openspec-new-branch/specs/openspec-scaffold/spec.md rename to openspec/changes/archive/2026-11-14-4593-openspec-new-branch/specs/openspec-scaffold/spec.md diff --git a/openspec/changes/4593-openspec-new-branch/tasks.md b/openspec/changes/archive/2026-11-14-4593-openspec-new-branch/tasks.md similarity index 70% rename from openspec/changes/4593-openspec-new-branch/tasks.md rename to openspec/changes/archive/2026-11-14-4593-openspec-new-branch/tasks.md index 7488d2635a..cf9440cc90 100644 --- a/openspec/changes/4593-openspec-new-branch/tasks.md +++ b/openspec/changes/archive/2026-11-14-4593-openspec-new-branch/tasks.md @@ -4,7 +4,7 @@ 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`. -- [ ] 1.1 Read the branch of the checkout and take the slug of the same issue from it. -- [ ] 1.2 Print the branch of the checkout, or `HEAD detached`, and the suggestion. -- [ ] 1.3 Add the four tests. +- [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 diff --git a/openspec/specs/openspec-scaffold/spec.md b/openspec/specs/openspec-scaffold/spec.md index b84c9cb6a4..a04a6dbf54 100644 --- a/openspec/specs/openspec-scaffold/spec.md +++ b/openspec/specs/openspec-scaffold/spec.md @@ -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 `/-` 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