Skip to content

fix(cli): devops branches create creates the branch instead of failing on checkout -b -- #900

Closed

Description

@dan-petty

Problem

create_branch (src/devops_cli/git/operations.py:224-231) runs repo.git.checkout("-b", "--", branch_name) at :231. Git's -b takes the next argument as the new branch's name, so git tries to create a branch called -- that starts at branch_name. The call always fails. With git 2.55.0 in a scratch repository, git checkout -b -- feat/x exits 128 with fatal: 'feat/x' is not a commit and a branch '--' cannot be created from it.

The only caller is devops branches jira <ticket> [--slug] (src/devops_cli/commands/branches.py:90-121, call at :116). There is no devops branches create command. Running devops branches jira PROJ-1 --repo <scratch repo> on the release/v0.2.25 tip (9c879ae) creates no branch. It prints a Rich traceback that ends in GitCommandError: ... cmdline: git checkout -b -- feature/PROJ-1. The traceback appears because jira catches only ValueError (branches.py:119), and GitPython's GitCommandError is not a ValueError. The command has failed this way since create_branch was added in 1b81724 (v0.1.10), because the -- was there from the start.

No test caught it:

  • tests/test_git_operations.py:147-166 mocks gitlib.Repo and asserts the broken argv: assert_called_once_with("-b", "--", "feat/test") at :166.
  • tests/test_branches.py:78 patches create_branch out of the jira CLI test.

The leading-hyphen guard (operations.py:226-227) already stops the name from being read as an option, so dropping -- needs no other guard. No other git call in src/ puts -- where an option expects its value. resolve_merge_base runs git merge-base -- <base> <head> (operations.py:361), which works. The other calls put -- before pathspecs, which is what -- is for.

The docs name a command that does not exist, and this issue's title copied that name. docs/cheatsheets/git_and_github.md:23 gives devops branches create feat/my-feature and says it "ensures branch is created off the current active release branch". Neither claim is true: the command does not exist, and create_branch branches from the current HEAD. The same table has two more errors:

  • :24 names devops branches cleanup, but the command is clean (branches.py:162-163).
  • :22 says devops branches list shows upstream tracking and ahead/behind counts. It shows the repository, the branch and a marker on the current branch (branches.py:144-154).

No open item covers this file. #921 corrects hand-written docs but does not list docs/cheatsheets/, and #956 and #843 touch only security_and_ssh.md.

Changes since this issue was written: nothing in create_branch, jira or their tests has changed. #946 (9da1309) and #593 (89cbaa2) edited other functions in operations.py and tests/test_git_operations.py, so the cited lines moved. The checkout call is now at :231 (was cited as :226, which is the guard), and the mocked assertion is at :166 (was :160).

Acceptance criteria

  • create_branch runs repo.git.checkout("-b", branch_name) without --. The leading-hyphen guard and the BranchAlreadyExistsError check stay.
  • When git refuses to create the branch, create_branch catches the GitCommandError and raises GitOperationError(..., operation="branch_create") from it, with git's stderr reason as the message. operations.py:123 and :178 already re-raise lower-level errors as GitOperationError this way. jira already catches this error, because GitOperationError is a ValueError (src/devops_cli/exceptions/git.py:15). The user sees one error line and exit code 1.
  • tests/test_git_operations.py::test_create_branch uses a real repository with one commit in tmp_path, built with the shared git fixture (tests/conftest.py:425). It asserts that create_branch(repo, "feat/test") creates feat/test and leaves it checked out (git branch --show-current). It keeps the leading-hyphen check and checks that an existing branch raises BranchAlreadyExistsError. The mocked argv assertion is removed. The test fails on the current code.
  • A CLI test in tests/test_branches.py runs jira PROJ-123 --slug "my feature" --repo <repo> against a real repository, without patching create_branch. It asserts exit code 0 and that feature/PROJ-123-my-feature is the current branch. It fails on the current code.
  • A CLI test runs jira PROJ-123 --repo <repo> in a real repository that already has a branch named feature, so git refuses feature/PROJ-123 (cannot lock ref ... 'refs/heads/feature' exists). It asserts exit code 1 and that result.exception is a SystemExit, not a GitCommandError. It also asserts that the output contains git's reason and no traceback.
  • The new tests run git only on local repositories under tmp_path, with no network. They do not depend on the host's init.defaultBranch or git identity.
  • docs/cheatsheets/git_and_github.md:22-24 names devops branches list, devops branches jira <ticket> [--slug] and devops branches clean, and says what each one does. The file no longer mentions devops branches create or devops branches cleanup.
  • changelog.d/900.md records the fix under ### Fixed. CHANGELOG.md is not edited.
  • uv run devops ci passes.
  • (person-run) In a scratch repository, devops branches jira PROJ-1 --slug smoke switches to feature/PROJ-1-smoke.

Key questions

  • Should the fix use checkout -b <name>, switch -c <name> or GitPython's create_head(name).checkout()? (proposed) checkout -b <name>. It is the smallest change, git still checks the ref name and writes the error text, and the leading-hyphen guard already stops the name from being read as an option.
  • Should create_branch wrap git's failure, or leave it to the caller? (proposed) Wrap it in GitOperationError. AGENTS.md:393 requires typed domain exceptions, operations.py already re-raises host-key and clone-destination errors this way (:123, :178), and jira then prints the error instead of a traceback without any change to jira itself.
  • Should this issue fix the cheat sheet, or leave it to docs: hand-written docs name only commands, options and components that exist #921? (proposed) This issue fixes lines 22-24 only, the devops branches rows. The wrong command name in this issue's title came from that table, and no open item covers the file. The rest of docs/cheatsheets/ is docs: hand-written docs name only commands, options and components that exist #921's to correct, if the owner adds the folder to docs: hand-written docs name only commands, options and components that exist #921's scope.
  • Should a devops branches create <name> command be added, since the cheat sheet advertises one? (proposed) No. That would be a new feature, not this defect, and git switch -c already does the job. File it separately if it is wanted.
  • Should create_branch branch from the active release branch, as the cheat sheet claims? (proposed) No. It branches from the current HEAD, as its docstring says (operations.py:225). The cheat sheet is corrected instead.

Depends on: none.
Order: no ordering constraint. The fix touches git/operations.py (create_branch only), tests/test_git_operations.py, tests/test_branches.py and docs/cheatsheets/git_and_github.md. No other open v0.2.25 to v0.2.29 item edits those parts, so this can land in any v0.2.29 slot.
Related: #787 (the same -- misuse, in git show -- <rev>:<path>; fixed in v0.2.24, and this was found in its review). #921 (hand-written docs; does not list docs/cheatsheets/). #920 (its command-resolution test covers only the knowledge base, and #921 extends it to other docs).

Activity

  1. added
    type/bug
    scope/cli
    priority/p2-medium
    on Oct 1, 2026
  2. added
    priority/p1-high
    and removed
    priority/p2-medium
    on Oct 2, 2026
  3. dan-petty commented on Oct 2, 2026

    @dan-petty
    OwnerAuthor

    Grooming (2026-10-02): Priority p2-medium → p1-high.

    Checked against the code at the release/v0.2.25 tip.

  4. added this to the v0.2.29 milestone on Oct 2, 2026
  5. dan-petty commented on Oct 2, 2026

    @dan-petty
    OwnerAuthor

    Placed in v0.2.29 on 2026-10-02. v0.2.29 holds P1 work that has to land before v0.3.0, which v0.2.25 to v0.2.28 had no room for. Backlog P1 items were ranked by Value, then Effort, with defects and security fixes first at a tie. This item ranked in the top ten, and two of the release's twelve slots stay open for P1 items that surface before then.

  6. dan-petty commented on Oct 2, 2026

    @dan-petty
    OwnerAuthor

    Refined to Ready on 2026-10-02 for v0.2.29. The body was checked claim by claim against release/v0.2.25 at 9c879ae, and a second agent then tried to refute it; every major objection was resolved in a revision. Cited lines were updated for code that moved, and the scope was corrected where merged work changed it. Acceptance criteria were tightened so each fails on the current code, and every key question now has a proposed answer.

  7. modified the milestones: v0.2.29, v0.2.30 on Oct 8, 2026
  8. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Moved to v0.2.30: v0.2.29 started with more than 12 items, and this was its lowest-ranked unstarted item. (devops roadmap reprioritize)

  9. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Linked: #1399 and #1114. Each wraps a GitCommandError from git/operations.py in a GitOperationError with git's stderr through mask_secrets; whichever lands later reuses the shape. Found by the 2026-10-08 backlog review.

  10. dan-petty-bot commented on Oct 9, 2026

    @dan-petty-bot
    Collaborator

    What changed

    #1460, merged into release/v0.2.30 as 1a07122.

    Summary

    • create_branch (src/devops_cli/git/operations.py) now runs git checkout -b <name> with no --. Before, git read -- as the new branch's name, so devops branches jira never created a branch and ended in a GitCommandError traceback. The leading-hyphen guard still stops a name from being read as an option, and the BranchAlreadyExistsError check is unchanged.
    • When git refuses a branch, create_branch raises GitOperationError(operation="branch_create") with git's own reason as the message. jira already catches this error (it is a ValueError), so the user sees one error line and exit code 1. How it works: GitPython's GitCommandError keeps stderr only pre-formatted ("\n stderr: '...'"). So create_branch calls checkout in GitPython's documented non-raising mode (with_extended_output=True, with_exceptions=False) and builds the GitOperationError from git's raw stderr, passed through mask_secrets. That gives the same git command, a typed error, a one-line message and exit 1, with no exception to chain from. mask_secrets is imported only when git fails, so git.operations does not load the scanner package.
    • Tests use real local repositories under tmp_path, built with the shared git fixture, in place of the mocked argv assertion:
      • test_create_branch
      • test_create_branch_raises_gits_reason_when_git_refuses
      • test_jira_creates_and_checks_out_the_feature_branch
      • `test_jira_prints_gits_reason_when_git_refuses_the_branch…

    6 file(s) changed.

    How it was verified

    Check runs at 4d86ff8:

    • Prune PR DevContainer Images: pass
    • Tests & Coverage: pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping
    • CodeQL: pass
    • Static Analysis: pass
    • Analyze (actions): pass
    • Analyze (python): pass

    Acceptance Criteria of docs/agent/tasks/task-900-branches-jira-creates-branch.md:

    • create_branch runs git checkout -b <name> without --, and the leading-hyphen guard and the BranchAlreadyExistsError check stay: tests/test_git_operations.py::test_create_branch.
    • When git refuses the branch, create_branch raises GitOperationError with operation="branch_create" and git's reason as the message: tests/test_git_operations.py::test_create_branch_raises_gits_reason_when_git_refuses.
      • GitPython's GitCommandError keeps stderr only pre-formatted ("\n stderr: '...'"). So create_branch calls checkout in GitPython's documented non-raising mode (with_extended_output=True, with_exceptions=False) and raises GitOperationError from git's raw stderr through mask_secrets. That gives the same argv, the typed error, a one-line message and exit 1, with no exception to chain from.
    • test_create_branch uses a real repository with one commit in tmp_path, built with the shared git fixture. It asserts that create_branch(repo, "feat/test") leaves feat/test checked out (git branch --show-current), keeps the leading-hyphen check, and checks that an existing branch raises BranchAlreadyExistsError. The mocked argv assertion is gone, and the test failed on the old code.
    • jira PROJ-123 --slug "my feature" --repo <repo> against a real repository exits 0 with feature/PROJ-123-my-feature checked out, without patching create_branch: tests/test_branches.py::test_jira_creates_and_checks_out_the_feature_branch. It failed on the old code.
    • jira PROJ-123 --repo <repo> in a repository with a branch named feature exits 1 through SystemExit, prints git's reason ('refs/heads/feature' exists) and no traceback: tests/test_branches.py::test_jira_prints_gits_reason_when_git_refuses_the_branch. It failed on the old code.
    • The new tests run git only on local repositories under tmp_path, with no network. They set -b main on git init and pass their identity through the git fixture, so neither the host's init.defaultBranch nor its git identity matters.
    • docs/cheatsheets/git_and_github.md rows 22-24 name devops branches list [--all], devops branches jira <ticket> [--slug <text>] and devops branches clean [--dry-run] and say what each does. The file no longer mentions devops branches create or devops branches cleanup.
    • changelog.d/900.md records the fix under ### Fixed. CHANGELOG.md and docs/ROADMAP.md are not edited.
    • uv run devops ci passes.
    • Pending a person: in a scratch repository with one commit run devops branches jira PROJ-1 --slug smoke, then git branch --show-current; it prints feature/PROJ-1-smoke.
  11. added a commit that references this issue on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/clitype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions