Skip to content

fix(repos): worktrees add has no credential for a private repo #6340

Closed
1 of 1 issue completed
Closed

Description

@dan-petty

Problem Description

devops repos worktrees add <issue> -r <clone of a private repo> fails at git ls-remote --heads origin with no HTTPS credential. Before the failure, it already removed worktrees of the clone. On 2026-11-18 an agent ran it for the clone of dan-petty/homelab (a private repository). The command removed 4 merged worktrees, then stopped with fatal: could not read Username for 'https://github.com': No such device or address. The agent also found the clone in detached HEAD after the run. The same clone fetches with no problem from the shell of the person.

Reproduced on 2026-11-18 with run_subprocess(["git", "-C", <clone>, "ls-remote", "--heads", "origin", "main"], check=False, quiet=True): exit 128 with the error above for the homelab clone, exit 0 for the devops-cli clone. devops-cli is public, so its origin needs no credential.

Cause

  • build_subprocess_env (src/devops_cli/core/process.py) does not pass the GIT_* keys. The devcontainer sets the github.com credential helper gh auth git-credential with GIT_CONFIG_* keys, so a git child of devops has no such helper. The process runner gives GH_TOKEN to a git child that reaches a remote, but git reads that token only through the helper.
  • fix(infra): read the default branch at origin, not the working tree #5162 added CONST_GIT_GITHUB_CREDENTIAL_CONFIG (git -c credential.https://github.com.helper=...) for the git calls of devops infra. src/devops_cli/infra/repo_tree.py:58 and src/devops_cli/k8s/argocd_overrides.py use it. _git in src/devops_cli/git/worktree_add.py:178 does not, so ls-remote and fetch of worktrees add have no credential. The fetch --prune of src/devops_cli/git/operations.py does not use it either.
  • worktrees_add in src/devops_cli/commands/repos.py:654 runs reclaim_merged_worktrees before add_worktree reads origin. A failed read of origin thus comes after the removals.
  • The detached HEAD: no code path is found yet. git/worktree_add.py and the reclaim have no --detach of the main clone.

Expected vs Actual Behavior

Expected: each git call of devops that reaches GitHub gets the credential of the session, as the git calls of devops infra do. A run that cannot pass its preconditions changes nothing in the clone.

Actual: the git calls of worktrees add have no credential for a private repository, and the reclaim removes worktrees before the read of origin fails.

Proposed Fix

  1. Fix the cause in one place: the process runner adds the credential -c values where it gives GH_TOKEN to a git child that reaches a remote (git_reaches_remote). Then each git network call gets the helper and the token together, and no caller must remember the -c values.
  2. Read origin (one git ls-remote --heads origin) before the reclaim, so that a failed read removes no worktree.

Acceptance Criteria

  • A test: a git child that reaches a remote gets the credential helper of the session. It fails on main (devops ci fail-to-pass).
  • A test: when the read of origin fails, worktrees add removes no worktree and does not change the HEAD of the clone.
  • A live run of devops repos worktrees add on a clone of a private repository creates the worktree.

Related

#5529 (the first fetch of a private repository in a new Service pod fails), #5162.

Alternatives check of 2026-11-19: in-use.
The repository already uses this tool.
Tool: cli/cli (https://github.com/cli/cli), MIT, last activity 2026-11-18, maintained.
Reason: Searches found the GitHub CLI credential helper gh auth git-credential, which the project already uses. It supplies GitHub HTTPS credentials from GH_TOKEN to git network calls, but it cannot reorder worktrees add before the reclaim or guard HEAD.

Covers:

  • Supplies GitHub HTTPS credentials from GH_TOKEN
  • Works as a git credential helper for ls-remote and fetch
  • Already configured in the project's devcontainer and used by devops infra

Lacks:

  • Cannot read origin before the reclaim
  • Cannot stop worktree removal when the origin read fails
  • Does not explain or fix the detached HEAD

Proposed design

Problem statement

devops repos worktrees add fails with fatal: could not read Username for 'https://github.com' on a clone of a private repository. The git child that runs git ls-remote --heads origin gets no GitHub credential helper. The command also removes merged worktrees before that read, so the failure changes the clone. The change gives the existing helper to each git child that reaches a remote and reads origin before the reclaim. The detached HEAD that the run left has no known code path and is deferred.

Acceptance criteria

Requirement: Credential for ls-remote

The process runner SHALL give the credential helper of CONST_GIT_GITHUB_CREDENTIAL_CONFIG to a git child that runs ls-remote against a remote.

Scenario: The reproduction form with a token

  • WHEN A test runs git -C <clone> ls-remote --heads origin main through the process runner with GH_TOKEN set to a token and a git executable that records its arguments.
  • THEN The recorded command holds the -c values of CONST_GIT_GITHUB_CREDENTIAL_CONFIG before ls-remote, and the child environment holds GH_TOKEN.

Requirement: Credential for fetch

The process runner SHALL give the credential helper of CONST_GIT_GITHUB_CREDENTIAL_CONFIG to a git child that runs fetch against a remote.

Scenario: A fetch with prune

  • WHEN A test runs git fetch --prune origin through the process runner with GH_TOKEN set to a token and a git executable that records its arguments.
  • THEN The recorded command holds the -c values of CONST_GIT_GITHUB_CREDENTIAL_CONFIG before fetch, and the child environment holds GH_TOKEN.

Requirement: No credential for a local git child

The process runner MUST NOT give the credential helper or a GitHub token to a git child that reaches no remote.

Scenario: A local status

  • WHEN A test runs git status through the process runner with GH_TOKEN set to a token and a git executable that records its arguments.
  • THEN The recorded command holds no -c credential value, and the child environment holds no GH_TOKEN.

Requirement: Failed origin read changes nothing

worktrees add SHALL read origin before it removes any worktree, and MUST change nothing when that read fails.

Scenario: ls-remote exits 128

  • WHEN A test runs worktrees add on a clone whose git ls-remote --heads origin exits 128.
  • THEN The command exits non-zero, the clone keeps every worktree, and the HEAD of the clone is the same as before the run.

Requirement: Successful origin read reclaims and adds

worktrees add SHALL remove the merged worktrees and create the worktree when the origin read succeeds.

Scenario: One merged worktree

  • WHEN A test runs worktrees add on a clone whose git ls-remote --heads origin exits 0 and that holds one merged worktree.
  • THEN The command removes that worktree, creates the worktree of the issue, and exits 0.

Existing Tool Considered

The alternatives check in the issue body records in-use for cli/cli (https://github.com/cli/cli), MIT, maintained. The repository already uses the GitHub CLI credential helper gh auth git-credential. devops infra passes it through CONST_GIT_GITHUB_CREDENTIAL_CONFIG. The block records what the tool covers and lacks. The change gives its -c values to each git child that reaches a remote, and it reads origin before the reclaim. It does not fix the detached HEAD on a successful run, which is deferred.

Key questions

  • Which open, maintained tool covers the GitHub credential that a git child needs?: The alternatives check in the issue body records in-use for cli/cli (gh auth git-credential), because the repository already uses it. The change reuses CONST_GIT_GITHUB_CREDENTIAL_CONFIG in the process runner. The block records what the tool covers and lacks. [sources: src/devops_cli/config/constants.py:1211, src/devops_cli/infra/repo_tree.py, src/devops_cli/core/process.py]
  • Where does the process runner add the credential helper?: The process runner adds the values in the code that gives GH_TOKEN to a git child that git_reaches_remote accepts. The child gets the -c values of CONST_GIT_GITHUB_CREDENTIAL_CONFIG before its subcommand. (best practice) [sources: src/devops_cli/core/process.py, src/devops_cli/config/constants.py:1211, src/devops_cli/git/worktree_add.py, src/devops_cli/git/operations.py]
    • Chosen: Process runner for each git child that reaches a remote. One place covers ls-remote and fetch --prune, and no caller must remember the values.
    • Rejected: Each caller passes the constant. The defect is two callers that forgot it, and a third caller can forget it again.
  • How does the runner give the -c values to the child?: The runner inserts -c <key>=<value> after the git word and before the subcommand. It uses the values of CONST_GIT_GITHUB_CREDENTIAL_CONFIG. (best practice) [sources: src/devops_cli/core/process.py, src/devops_cli/config/constants.py:1211]
    • Chosen: Command-line -c values after the git word. It reuses CONST_GIT_GITHUB_CREDENTIAL_CONFIG and does not depend on the filtered GIT_CONFIG_* keys.
    • Rejected: GIT_CONFIG_COUNT and GIT_CONFIG_KEY_0 in the child environment. build_subprocess_env filters the environment and CONST_GIT_ISOLATED_CONFIG_ENV isolates git config.
  • Does the change cover the fetch --prune of src/devops_cli/git/operations.py?: Yes. The process runner gives the helper and the token to a git child that runs a network subcommand, and fetch is such a subcommand. The caller in src/devops_cli/git/operations.py needs no change. [sources: src/devops_cli/git/operations.py, src/devops_cli/core/process.py]
  • Which order does worktrees_add run in?: worktrees_add reads origin once, before reclaim_merged_worktrees, and uses that result for the add. A failed read then removes no worktree, and the add does not read again. (best practice) [sources: src/devops_cli/commands/repos.py, src/devops_cli/git/worktree_add.py]
    • Chosen: Read origin once before the reclaim. A failed read changes nothing, and a second read cannot fail after the removals.
    • Rejected: Keep the order and make the reclaim undoable. A removed worktree cannot be restored, so the command needs more code and a record of each removal.
  • Is the detached HEAD of the clone in scope?: No. The body records no code path that detaches HEAD. This change stops before any change when the origin read fails, and the test asserts that HEAD is unchanged on that path. A detachment on a successful run needs its own evidence and its own issue, which is deferred. (best practice) [sources: src/devops_cli/git/worktree_add.py, src/devops_cli/commands/repos.py]
    • Chosen: Defer the detached HEAD on a successful run. The body records no code path that detaches HEAD, so this change can state no test and no fix.
    • Rejected: Investigate the detachment in this change. The cause is unknown, so the change cannot state a test or a fix.
  • Does a git child that reaches no remote keep its current behavior?: Yes. _github_pin gives a token only when git_reaches_remote is true. Another git child gets an empty pin, no token lookup and no credential helper. [sources: src/devops_cli/core/process.py]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. added
    type/bug
    source/agent
    status/new
    effort/medium
    value/high
    scope/repos
    on Nov 18, 2026
  2. dan-petty commented on Nov 18, 2026

    @dan-petty
    OwnerAuthor
    • Priority: P1-High. A CLI command fails for private repos and removes worktrees before the failure, causing data loss.
    • Related: fix(core): give the GitHub token only to the calls that need it #5265, which asks for a different change. Both modify the process runner for git children that reach a remote, but #5265 restricts token access while the candidate adds the credential helper to those calls.
  3. added
    priority/p1-high
    on Nov 18, 2026
  4. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    Waiting for the alternatives check to run. The next refine round looks at the item again after the check records a verdict.

  5. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (8)

    Tools considered:

    Searches run:

    • open source git credential helper GitHub HTTPS, results: 5
    • GitHub CLI git credential helper gh auth git-credential, results: 5
    • gh auth setup-git configure git credential helper GitHub CLI manual, results: 5
    • gh auth git-credential credential.https github.com.helper documentation, results: 5
    • cli.github.com/manual/gh_auth_setup-git, results: 5
    • "gh auth git-credential" git helper, results: 5
    • gh auth git-credential, results: 5
    • GitHub Docs caching credentials in Git gh auth setup-git, results: 5

    Sources:

  6. added
    status/ready
    and removed
    status/new
    on Nov 19, 2026
  7. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 5e6822d (see proposed design in issue body).

    Moved to new items: #6354.

  8. added
    owner/service
    and removed
    owner/service
    on Nov 19, 2026
  9. 41 remaining items

  10. added
    status/in-review
    and removed
    status/blocked
    on Nov 21, 2026
  11. removed
    status/in-review
    on Nov 21, 2026
  12. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-21 14:28 UTC

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

    effort/mediumpriority/p1-highscope/repossource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions