Skip to content
Open
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
2 changes: 1 addition & 1 deletion docs/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Shared and host-local Git settings, identities and credentials.
- `~/.gitconfig` → symlink into the repo, identical on all hosts. `git config --global alias.x ...` lands there directly → commit, done.
- `~/.gitconfig.local` → host-local, not in git, included after the shared settings. `machine setup` creates it once from the context (`work` → work user, otherwise private) and never overwrites it; on work hosts an existing file only gets a missing `http.sslCAInfo` appended. Signing key, `safe.directory`, credentials and proxy go here.
- Owner rules at the end of `~/.gitconfig` override the host default by remote URL: github.com `bosch-*`/`bosch_*`/`*_bosch` owners (HTTPS and SSH), GHES and Bitbucket → `~/.config/git/work.gitconfig`, any other github.com owner → `~/.config/git/private.gitconfig`. Each sets identity plus the GitHub credential username and helper. A repository name ending in `_bosch` does not select the work identity.
- `git-credential-env VAR` serves tokens from the environment or `~/.env` (`GITHUB_TOKEN`, `BDC_GITHUB_TOKEN`, `GITHUB_PRIVATE_TOKEN`) and never stores them. When a GUI-started process lacks the token, it sources only `~/.shell/common/environment.sh` in Bash; it does not start interactive shell initialization. For github.com the variable follows the host type: `GITHUB_TOKEN` on private hosts, `GITHUB_PRIVATE_TOKEN` on work hosts (where `GITHUB_TOKEN` is the work token); `machine` writes the choice to `~/.config/git/github-token.gitconfig` on every dotfile apply. On private hosts `machine setup` also switches the checkout's `origin` to SSH (`git@github.com:…`) when the SSH key authenticates at GitHub, so pushing needs no token. `~/.netrc` entries for the same host take precedence in curl, so keep GitHub out of it. `machine env pull` writes `~/.netrc` from Bitwarden (see [secrets.md](secrets.md)) and warns for a `github.com` block. When such a block is needed (for example by the work devcontainers), `private.gitconfig` sets `http.https://github.com/.proactiveAuth = basic`, so Git asks the credential helper first and the netrc entry is not used for pushes and fetches in a repository; this needs Git 2.46 or newer, and a first `git clone` is not covered because no repository config exists yet.
- `git-credential-env VAR` serves tokens from the environment or `~/.env` (`GITHUB_TOKEN`, `BDC_GITHUB_TOKEN`, `GITHUB_PRIVATE_TOKEN`) and never stores them. When a GUI-started process lacks the token, it sources only `~/.shell/common/environment.sh` in Bash; it does not start interactive shell initialization. For github.com the variable follows the host type: `GITHUB_TOKEN` on private hosts, `GITHUB_PRIVATE_TOKEN` on work hosts (where `GITHUB_TOKEN` is the work token); `machine` writes the choice to `~/.config/git/github-token.gitconfig` on every dotfile apply. On private hosts `machine setup` also switches the checkout's `origin` to SSH (`git@github.com:…`) when the SSH key authenticates at GitHub, so pushing needs no token. `~/.netrc` entries for the same host take precedence in curl, so keep GitHub out of it. `machine env pull` writes `~/.netrc` from Bitwarden (see [secrets.md](secrets.md)) and warns for a `github.com` block. When such a block is needed (for example by the work devcontainers), `machine` adds `http.https://github.com/.proactiveAuth = basic` to `~/.config/git/github-token.gitconfig`, so Git asks the credential helper first and the netrc entry is not used for github.com clones, fetches and pushes; this needs Git 2.46 or newer. It does so only when the token variable is set at apply time (environment or `~/.env*`): without a token, proactive auth would make Git ask for a password even for public repositories (for example the zsh plugin clones on a fresh host), so Git then stays anonymous and runs the helper only when GitHub asks for a credential. A host that gets its token later is covered by its next `machine update`.
- Single repo with a different identity: `git user-work` / `git user-private` (repo-local include of the same snippet), undo with `git user-unset-local`; show: `git user-show`.
- `machine update` and `machine commit` move host-only entries from the shared config into `~/.gitconfig.local`. A key that is absent locally moves, an equal value is dropped from the shared file, and a different local value is a conflict that `machine update` asks about (non-interactive runs keep both and leave the shared file dirty). Repeated values are preserved. Backups live under `~/.local/share/machine/local-overrides-backup/`. Text added to `~/.bashrc` is handled the same way: known text is removed silently, new text is moved only when you select it, and moved text stands between `# machine-local begin/end` markers in `~/.bashrc-local`.
- The installed pre-commit hook does the same before an ordinary Git commit. If it changes the shared file, review and stage the cleaned file, then commit again.
Expand Down
7 changes: 2 additions & 5 deletions home/core/.config/git/private.gitconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
# empty value drops earlier helpers (store), so tokens never land in ~/.git-credentials
helper =

# Run the credential helper before the first request. Otherwise curl sends a
# github.com entry of ~/.netrc up front and Git never asks the helper (Git 2.46+).
[http "https://github.com/"]
proactiveAuth = basic

# The token helper itself, written by `machine` from the host config: GITHUB_TOKEN on
# private hosts, GITHUB_PRIVATE_TOKEN on work hosts (where GITHUB_TOKEN is the work
# token). Must come after the reset above; a missing file means no token helper.
# With a token it also runs the helper before the first request, so a github.com
# entry of ~/.netrc does not replace the token (http.proactiveAuth, Git 2.46+).
[include]
path = ~/.config/git/github-token.gitconfig
35 changes: 31 additions & 4 deletions machine_cli/dotfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,38 @@ def github_token_variable(context: str) -> str:
return "GITHUB_PRIVATE_TOKEN" if context == "work" else "GITHUB_TOKEN"


def render_github_token_config(context: str) -> str:
return (
def render_github_token_config(context: str, proactive_auth: bool = True) -> str:
text = (
"# Written by `machine` from the host config on every dotfile apply; do not edit.\n"
"# Included by ~/.config/git/private.gitconfig after its credential reset.\n"
'[credential "https://github.com"]\n'
" helper = !$HOME/.local/bin/git-credential-env "
f"{github_token_variable(context)}\n"
)
if proactive_auth:
# Run the helper before the first request, so a github.com entry in ~/.netrc
# does not replace the token (Git 2.46+). Only with a token: without one, Git
# would ask for a password even for public repositories.
text += '\n[http "https://github.com/"]\n proactiveAuth = basic\n'
return text


def github_token_available(variable: str, repo: Path, home: Path) -> bool:
"""Resolve the token like git-credential-env: environment, else the shell
environment loader (~/.env*). The checkout's loader, because the first apply runs
before $HOME is linked."""
if os.environ.get(variable):
return True
loader = Path(repo) / "home/core/.shell/common/environment.sh"
if not loader.is_file() or not system.have("bash"):
return False
result = system.run(
["bash", "-c", '. "$1" >/dev/null 2>&1; printenv "$2"', "_", str(loader), variable],
env={**os.environ, "HOME": str(home)},
check=False,
capture=True,
) # fmt: skip
return result.returncode == 0 and bool((result.stdout or "").strip())


def github_ssh_url(url: str) -> str:
Expand Down Expand Up @@ -296,12 +320,15 @@ def run_dotfiles_apply(self) -> None:

def write_github_token_config(self, context: str) -> None:
path = self.home / GITHUB_TOKEN_CONFIG
text = render_github_token_config(context)
variable = github_token_variable(context)
proactive = github_token_available(variable, Path(self.repo), self.home)
text = render_github_token_config(context, proactive)
if path.is_file() and path.read_text(encoding="utf-8") == text:
return
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding="utf-8")
log(f"GitHub token for github.com: {github_token_variable(context)} ({path})")
state = "set, helper first" if proactive else "not set, anonymous until asked"
log(f"GitHub token for github.com: {variable} ({state}; {path})")

def switch_origin_to_ssh(self, context: str, device: str) -> None:
"""Private hosts push with their SSH key: move an https github.com origin to SSH,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Design

## Context

See proposal.md for the motivation.

Current state:
- `private.gitconfig` is included for every github.com remote (`includeIf hasconfig:remote.*.url`). It sets `credential.https://github.com.username = ditschi`, resets the helpers, includes the machine-written `github-token.gitconfig` (helper `git-credential-env GITHUB_TOKEN`, or `GITHUB_PRIVATE_TOKEN` on work hosts) and sets `http.https://github.com/.proactiveAuth = basic` (change `private-github-url-user`, 2026-11-17), so a `github.com` entry in `~/.netrc` does not replace the personal token.
- Checked on this host (2026-11-26, Git 2.56) with the repository's Git config files in a temporary HOME and no token: `git clone --depth 1 https://github.com/zsh-users/zsh-autosuggestions` runs the helper (the `hasconfig` rule is already active during a clone, contrary to the note in the earlier design), the helper prints its hint, Git asks for a password and exits 128 (`terminal prompts disabled`).
- With proactive auth Git passes `wwwauth[]=Basic` to the helper just as after a real 401, so the helper cannot tell the two cases apart.
- GitHub answers 401 to empty or wrong Basic credentials also for public repositories (anonymous: 200), so no placeholder credential can stand in for a missing token.
- The zsh plugin manager (antidote, `home/core/.shell/zsh/plugins.zsh`) clones antidote and the plugins from `https://github.com/...` at the first shell start and retries the bundle at every start while it fails. The `machine apply` health check runs `zsh -i` and fails on its stderr (CI run 44610448518, commit 0a3d695).
- Hosts: every host and container without a token in its environment or `~/.env*` is affected; hosts with the token behave as before.

## Goals / Non-Goals

**Goals:**
- Public github.com clones and fetches work without the token.
- Hosts with the token keep the personal token in front of a `github.com` netrc entry.

**Non-Goals:**
- Silencing the helper's hint: when GitHub asks for a credential and none is set, the hint is correct.
- A change to the health check: the errors were real.

## Decisions

**Proactive auth only with an available token** (owner decision, 2026-11-26). `machine` writes `[http "https://github.com/"] proactiveAuth = basic` into `github-token.gitconfig` when the token variable that the file names resolves to a non-empty value at apply time, in the same way the helper resolves it: the `machine` environment first, else the checkout's `home/core/.shell/common/environment.sh` sourced in Bash (it reads `~/.env*`). The checkout's loader is used because the first apply writes the file before `$HOME` is linked. Git config cannot depend on an environment variable at run time, so the decision is made at apply time. `machine update` pulls the env files before the dotfile apply, so a host that gets its token is covered on its next update. The setting moves out of `private.gitconfig`; because that file includes `github-token.gitconfig`, the setting still applies to every github.com remote, also to work owners, as before.

**Alternatives considered and rejected:**
- Making the helper quiet without a token: Git still asks for a password and the clone still fails.
- Proactive auth only with a `github.com` netrc entry (and the token): more conditions to keep correct for no practical gain; with the token proactive auth is harmless.
- Proactive auth per repository (e.g. `git user-private` or the dotfiles checkout): gives up the rule for every github.com remote.

## Risks / Trade-offs

- [A host gets its token after the apply] → proactive auth is off until the next apply; until then a `github.com` netrc entry wins for personal pushes, as before the earlier change.
- [A work host without `GITHUB_PRIVATE_TOKEN` but with the work netrc entry] → work-owner and public requests use the netrc credential; personal pushes fail as they would without a token anyway.
- [The token is removed after the apply] → proactive auth stays on until the next apply and public clones fail as today; the next apply drops it.

## Migration Plan

None: the next dotfile apply rewrites `github-token.gitconfig`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Proposal

## Why

On a host without the GitHub token (a fresh container, the APT integration tests in CI, a host before its first `machine env pull`), Git cannot reach public github.com repositories over HTTPS. `private.gitconfig` sets `http.https://github.com/.proactiveAuth = basic` for every github.com remote, so Git demands a credential before the first request; the helper has none, Git asks for a password and fails without a terminal. The zsh plugin manager clones its plugins from github.com at the first shell start, so the shell start prints `git-credential-env: GITHUB_TOKEN is not set` and Git errors, the plugins stay missing, and the `machine apply` health check fails (CI jobs `apt-rpi-zero (26.04)` and `apt-home-laptop`). A missing token must not break public clones; it should only matter when GitHub actually asks for a credential.

## What Changes

- `machine` writes `http.https://github.com/.proactiveAuth = basic` into the generated `~/.config/git/github-token.gitconfig` only when the host's GitHub token variable is set at apply time (environment, or `~/.env*` through the shell environment loader of the checkout).
- `private.gitconfig` no longer sets `proactiveAuth` itself.
- Without the token, Git clones and fetches public github.com repositories anonymously and asks the credential helper only after GitHub answers 401; the helper's hint about the missing variable stays for that case.
- `docs/git.md` describes the condition.

## Capabilities

### New Capabilities

None.

### Modified Capabilities

- `git-config`: the requirement "Credential helper before netrc for github.com" applies only when the token is available; a new requirement covers anonymous access without the token.

Affected contexts: `private` and `work`, all devices; in practice hosts without the token (containers, fresh hosts, CI).

## Impact

- Code: `machine_cli/dotfiles.py` (token config rendering), `home/core/.config/git/private.gitconfig`.
- Tests: `tests/unit/test_dotfiles.py`, `tests/unit/test_gitconfig.py`; the APT host-yaml integration test covers the clean install.
- Docs: `docs/git.md`.
- Hosts: the next dotfile apply rewrites `github-token.gitconfig`. A host that gets its token later gets proactive auth on the next apply (`machine update` pulls the env files before it applies the dotfiles).

## Where the change is made

In the worktree `$(machine repo)/.worktrees/github-proactive-auth-token` on branch `github-proactive-auth-token`, because the change edits Git configuration that every shell and tool on the host reads.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Spec Delta

## MODIFIED Requirements

### Requirement: Credential helper before netrc for github.com
When the github.com token variable is set at dotfile apply time (environment or `~/.env*`), Git SHALL ask its credential helper for github.com before the first request (`http.https://github.com/.proactiveAuth = basic`, written by `machine` into `github-token.gitconfig`), so that a `github.com` entry in `~/.netrc` does not replace the configured credential. Work owners SHALL keep the credential helper of `work.gitconfig`. Git versions that do not know the key SHALL behave as before.

#### Scenario: Personal repository with a work netrc entry
- **WHEN** the token is set, `~/.netrc` has a `machine github.com` entry for another login and a repository has the remote `https://github.com/ditschi/dotfiles.git`
- **THEN** Git pushes with the personal token from its credential helper and not with the netrc credentials

#### Scenario: Work owner
- **WHEN** a repository has the remote `https://github.com/bosch-example/repo.git`
- **THEN** the credential helper of `work.gitconfig` is the effective helper and the identity is the work identity

#### Scenario: Any other owner
- **WHEN** the token is set and a repository has a github.com remote of an owner that does not match a work rule
- **THEN** the rule is active through `private.gitconfig` without naming the owner

#### Scenario: Remote unchanged
- **WHEN** a user runs `git remote -v`
- **THEN** the remotes show the URLs as configured

#### Scenario: Git without the key
- **WHEN** Git is older than 2.46
- **THEN** it ignores the key and reports no error

## ADDED Requirements

### Requirement: Anonymous github.com access without a token
When the github.com token variable is not set at dotfile apply time, `github-token.gitconfig` SHALL NOT enable proactive auth, so Git clones and fetches public github.com repositories anonymously and runs the credential helper only when GitHub asks for a credential.

#### Scenario: Fresh container clones zsh plugins
- **WHEN** a host without the token starts zsh for the first time and the plugin manager clones from `https://github.com/`
- **THEN** the clones succeed and the credential helper prints nothing

#### Scenario: Private repository without a token
- **WHEN** a host without the token fetches a repository for which GitHub answers 401
- **THEN** the credential helper reports that the token variable is not set
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Tasks

Work only in the worktree `$(machine repo)/.worktrees/github-proactive-auth-token` (see proposal.md).

## 1. Token config

- [x] 1.1 In `machine_cli/dotfiles.py`, render `[http "https://github.com/"] proactiveAuth = basic` into `github-token.gitconfig` only when the token variable resolves (environment, else the checkout's `home/core/.shell/common/environment.sh` in Bash) and log whether it is on; cover with unit tests in `tests/unit/test_dotfiles.py` (token in environment, token only in `~/.env`, no token).
- [x] 1.2 Remove `proactiveAuth` from `home/core/.config/git/private.gitconfig`; in `tests/unit/test_gitconfig.py` assert that the effective `http.proactiveauth` of a personal and a work-owner github.com remote is empty when the token file is rendered without the token, and stays `basic` with it.

## 2. Documentation

- [x] 2.1 Update the `~/.netrc` note in `docs/git.md`: proactive auth only with the token, and correct the note that a first clone is not covered.

## 3. Validation

- [x] 3.1 Run `pytest tests/unit/test_dotfiles.py tests/unit/test_gitconfig.py -q`.
- [x] 3.2 Reproduce the CI failure check by hand: with the repository's Git config files in a temporary HOME and no token, `git clone --depth 1 https://github.com/zsh-users/zsh-autosuggestions` succeeds and prints nothing from the helper.
- [x] 3.3 Run the APT integration tests `tests/integration/test_host_yaml_install.py -k "rpi_zero or home_laptop"` if Docker is available.
- [x] 3.4 Run `pre-commit run --files <changed paths>` including untracked additions, then `openspec validate github-proactive-auth-token --strict`.

## Workflow follow-up

- Archive with `/opsx-archive` following the archive guidance in `openspec/config.yaml`.
Loading
Loading