# Special Files

Three scopes are named: `work@` is the current directory (it does not search
upward), `global@` is the CLI alias directory, and `special@` gathers the
non-alias files that each have their own dedicated commands — `config`, `tap`,
and `shell`. Only the scope may be abbreviated, to a unique prefix — `w@` /
`wo@` / `wor@`, `g@`, `s@`.

`work@` and `global@` look for alias files; `special@` is their inverse: it
points at **files that are not aliases**, each with its own dedicated commands.
None of them is an alias file or an operating-system "system file": `config` is
byk's own configuration inside the CLI home (`~/.byk/`), `tap` is the tap
repositories under `~/.byk/tap/`, and `shell` is the user's shell startup file.

```bash
byk special@file command [args...]
```

Three special files exist today:

| Form | Target |
|------|--------|
| `special@config` | `~/.byk/config.json` |
| `special@tap` | `~/.byk/tap/`, with `index.json` as its ledger |
| `special@shell` | Your shell's startup file, e.g. `~/.zshrc` |

* Special files never take part in alias lookup, and have no `clone` / `link`:
  there is no notion of several of them referencing each other.
* The name after `special@` must be a registered special file; a typo reports
  `Unknown special file` and lists the available names.
* Commands follow the name after a space, not a dot: `special@config.set` reports
  `Invalid special file`.
* `config` and `tap` point at byk's own files, so the worst case is falling back
  to defaults; `shell` points at **someone else's file**, so it only inserts or
  removes one marked block and never rewrites the file.

## config

### Commands

With no command it prints every setting (the common case); `-h` prints usage
help instead.

| Command | What it does |
|---------|--------------|
| `list` | Print every setting (same as no command) |
| `get <key>` | Print one value, safe for `$(...)` |
| `set <key> <value>` | Validate, then write |
| `unset <key>` | Remove the key, falling back to its default |

```bash
byk special@config                    # every setting
byk special@config get banner         # auto
byk special@config set banner never   # drop the box above `-h`
byk special@config unset banner       # back to the default
byk special@config set net-github ghproxy  # read GitHub files through gh-proxy
```

`get` prints a bare value; `set` / `unset` report on stderr, so pipelines stay
clean.

### Settings

| Key | Values | Default | Purpose |
|-----|--------|---------|---------|
| `banner` | `auto` / `always` / `never` | `auto` | The decorative box above `-h` |
| `net-github` | `direct` / `ghproxy` | `direct` | The channel used to reach GitHub files |

`auto` hides it when stdout is not a terminal, so `byk -h | head`, scripts, and
editor plugins get clean output for free. `always` and `never` are literal.

When scanning alias files turns up problems, the `N alias files have problems`
line **ignores this setting**: it is a warning, not decoration.

### The file

```json
{ "banner": "auto" }
```

* A missing file is normal — every key falls back to its default, no error.
* Unreadable content (broken JSON, a non-object top level) prints one warning
  and falls back to defaults with the usual exit code; a broken config must not
  take `-h` down with it.
* Writes are atomic and keep every key they read, including keys this version
  does not know yet.
* Keys are written back in alphabetical order, so a hand-written order gets
  normalized.
* The `Config:` line of `byk --paths` is this file.

## tap (`special@tap`)

`special@tap` does two things: put a tap repository into
`~/.byk/tap/<user>/<repo>`, and bring it up to date with upstream — no hand-written
`git clone` / `git pull` either way. It only knows git, so `add` and `update`
need git on the machine.

```bash
byk special@tap                      # what is installed (same as list)
byk special@tap add aaa/bb           # GitHub by default, ref defaults to HEAD
byk special@tap add aaa/bb@v1        # pin a ref
byk special@tap add my/tools https://git.example.com/my/tools.git   # any git URL
byk special@tap update               # bring every repository in the ledger up to date
byk special@tap update aaa/bb        # update just that one
byk special@tap remove aaa/bb        # unrecord and delete
```

| Command | What it does |
|---------|--------------|
| `list` | Show the repositories in the ledger (same as no command) |
| `add <user>/<repo>[@<ref>] [<git-url>]` | Clone, validate, move into place, then record |
| `update [<user>/<repo>]` | `fetch` upstream, then `reset --hard`; every repository when unnamed |
| `remove <user>/<repo>` | Unrecord, then delete the directory |

`<user>` and `<repo>` accept letters, digits, `.`, `-`, and `_` only: they are
directory names.

### Custom sources

The second value is handed to `git clone` verbatim: `https://`, `ssh://`,
`git@host:path`, `file://`, and plain local paths all work. There is no
protocol allowlist — git itself already refuses `ext::` and friends, where a
URL turns into a command.

```bash
byk special@tap add my/tools                         # https://github.com/my/tools.git
byk special@tap add my/tools git@git.example.com:tools/tap.git
byk special@tap add local/scratch ~/src/tap          # a local repository, origin `local`
```

* **`<user>/<repo>` is only the local name**: it decides the directory and the
  ledger key, says nothing about where the code comes from, and is not checked
  against the URL — same as `brew tap user/repo URL`.
* The only requirement is that it does not look like an option: no leading `-`,
  no whitespace, or git would read it as a flag instead of an address.
* Without a URL the source is `https://github.com/<user>/<repo>.git`.
* Private repositories ride on your own ssh keys or credential helper; byk
  disables interactive prompting, so a failed login errors out instead of
  waiting for a password.

### Updating (`update`)

Underneath it is just "make the checkout you cloned match upstream" — `git fetch`
plus `git reset --hard`, deliberately not `git pull`:

* a tap is a read-only mirror, there are no local commits to merge, so the
  merge/rebase semantics buy nothing;
* branches that were force-pushed and tags that were moved still catch up
  (`fetch --force`);
* the shallow clone stays shallow instead of quietly turning into full history.

```bash
byk special@tap update               # every repository in the ledger
byk special@tap update aaa/bb        # just one
# Updated aaa/bb (1a2b3c4 → 5d6e7f8)
# ccc/dd is already up to date
```

* **`update` follows the ledger's `ref`**: `update aaa/bb` will not jump back to
  the default branch just because you left `@v1` off the command line. Pinned to
  a tag, an untouched tag means there is nothing to do.
* **`update` never writes the ledger**: what is installed and which ref it is
  pinned to have not changed, only the commits in the checkout. `list` reads the
  same before and after.
* **A checkout with local changes is left alone**: `reset --hard` would throw
  those edits away, so it reports `has local changes` and lets you decide
  (commit, move them aside, or `remove` + `add`).
* **A lost index rolls back**: if upstream deleted `byk.tap.json`, the update
  returns to the previous commit rather than leaving a checkout that `--tap`
  cannot use.
* With no argument, **one failure does not stop the rest**: every repository is
  reported, then a single `Failed to update 1 of 2 tap repositories: …` closes
  the run with exit code 1.
* It only touches the ledger: `update` never looks at a directory you
  `git clone`d in by hand, and a deleted directory reports `Tap repository is
  missing`.

### Directory and index

The ledger is `~/.byk/tap/index.json`, and only `special@tap` reads or writes it:

```json
{
  "aaa/bb": { "origin": "github", "ref": "HEAD" },
  "my/tools": {
    "origin": "git.example.com",
    "ref": "HEAD",
    "url": "ssh://git@git.example.com/my/tools.git"
  },
  "local/scratch": { "origin": "local", "ref": "HEAD", "url": "/Users/coke/src/tap" }
}
```

* The key is `<user>/<repo>`; the directory follows from it, so no absolute
  paths are stored — moving machines or your home directory cannot make the
  ledger lie.
* `origin` is the source host (`github.com` is written `github`, and sources
  without a host are `local`); `ref` is the ref used at install time, and the
  target `update` follows afterwards.
* `url` only appears when a second value was given: the default source's address
  is derivable, so storing it would just be a second copy.
* **Losing the ledger does not affect `clone --tap`**: it scans directories and
  never reads this file. You lose a list, not the feature.
* Entries that cannot be read are still listed (`origin` / `ref` show `?`),
  otherwise you could neither see nor remove them.
* Writes are atomic, sharing the same path as `config.json`.

### Rules of behaviour

* `add` **never overwrites** an existing directory; `remove` first to change
  versions.
* The repository root must contain a parsable `byk.tap.json`, or the install
  rolls back right after landing and leaves nothing behind — once a local index exists
  it is authoritative, and a broken one would keep every key of that repository
  failing.
* `add` only ever runs `git clone --depth 1`, so what lands is a real repository
  you can later pull with `git -C ~/.byk/tap/<user>/<repo> pull`. There is no
  curl + tar fallback: two paths produce different things, need the semantics
  explained twice, and double the failure surface.
* **`add` and `update` require git** (Homebrew's taps do too; it does not bundle
  git). `list` and `remove` do not — they only read the ledger and delete
  directories, so they keep working without git. If you have no git and still
  need a repository in place, put it at `~/.byk/tap/<user>/<repo>` by hand.
* `remove` only knows the keys in the ledger. A repository you `git clone`d in
  by hand is not in it, so `remove` reports `not installed` and will not delete
  it.
* A shallow clone carries no history, but `pull` works, and `update` keeps
  fetching shallow; switching refs always takes `remove` + `add`.
* Downloads follow the usual proxy settings such as `BYK_PROXY`, translated to
  git's `-c http.proxy`. Writing the ledger does not touch the network.
* Installing a third-party tap means trusting the alias files it ships. `add`
  only moves data around; the risk shows up later, when `clone --tap` reads
  those aliases and something runs them.

## shell (`special@shell`)

Manages shell completion: insert one marked block into your shell's startup
file, or take it out.

```bash
byk special@shell                    # same as -h: shell, file, state, and the usage in one screen
byk special@shell add completion     # enable completion for the current shell
byk special@shell remove completion  # disable it
```

| Command | What it does |
|---------|--------------|
| `add completion` | Write the completion block; reports and stops if it is already there |
| `remove completion` | Take the block out; every other byte of the file stays put |

No argument and `-h` are the same screen, byte for byte: the file has two verbs,
so a separate "status" page would only mean typing twice to find out what can be
done. `special@config -h` and `special@tap -h` open with those same few lines.

The item name may be abbreviated to a unique prefix: `byk special@shell add comp` is
the same as `byk special@shell add completion`. Only a unique match is accepted —
if a future second item also matched, it reports the ambiguity instead of
picking one.

The target shell is whichever one is running byk; it cannot be named, see
"No `switch`, no naming a shell" below.

### Which file it targets

Not by operating system — by environment. macOS defaults to zsh but `/bin/bash`
is always there, and Windows has Git Bash. Guessing by OS guesses wrong;
identifying the shell does not. PowerShell is the one place where the OS does
come in: its `$PROFILE` location differs per operating system — that is
PowerShell's own rule, not guessing the shell.

| shell | Startup file |
|-------|--------------|
| zsh | `$ZDOTDIR/.zshrc`, else `~/.zshrc` |
| bash | the first existing of `.bash_profile` → `.bash_login` → `.profile` → `.bashrc`; otherwise `.bash_profile` is created |
| fish | `$XDG_CONFIG_HOME/fish/config.fish`, else `~/.config/fish/config.fish` |
| pwsh | `$PROFILE`: `$XDG_CONFIG_HOME/powershell/Microsoft.PowerShell_profile.ps1` on mac / Linux (else `~/.config/powershell/…`), `PowerShell\Microsoft.PowerShell_profile.ps1` under Documents on Windows |
| powershell | Windows PowerShell 5.1's `$PROFILE`: `WindowsPowerShell\Microsoft.PowerShell_profile.ps1` under Documents |

The bash order is deliberate: a login shell only reads the first three, while
`.bashrc` is read by non-login interactive shells — so it comes last, and only
when nothing else exists does it tell you which path you are on. Creating
`.bash_profile` when none exist is safe, because there is nothing for it to
shadow; creating it while `.profile` exists would shadow that file entirely.

The shell is identified as **parent process name → `$SHELL`**. `$SHELL` names the
login shell, so after `exec bash` it still says zsh; the parent process is what
is actually running. The name comes from `/proc` or `ps` on Unix and a Toolhelp
snapshot on Windows, so `powershell.exe` / `pwsh.exe` / `bash.exe` are recognised
the same way. Only when that is unavailable (restricted environments, editors
that spawn byk directly) does the environment variable take over. `special@shell`
prints the answer together with where it came from:

```bash
$ byk special@shell

Shell: zsh (from parent process)
FilePath: /Users/coke/.zshrc
Completion: installed (lines 3-5)

Usage: byk special@shell [OPTIONS] [COMMAND] [ITEM]

Options:
  --help, -h  Show this help

Commands:
  add     Enable shell completion
  remove  Disable shell completion

Items:
  completion  Shell completion
```

The first three are one level: same label colour, one space after the colon and
then the value — no padding for alignment, since paths differ so much in length
that the filler would outweigh the information. One blank line later comes the
usage, so "which file is this" and "what can I do" share a screen instead of
costing two runs.

When `.zshrc` is a symlink into a dotfiles repository, a `Target:` line in the
same column names the file that is really written — and that is where the write
goes, so the link itself never turns into a regular file.

### No `switch`, no naming a shell

The target follows the environment, so there is neither a "change the startup
file" subcommand nor an argument to name one — asking for it is refused, and the
refusal says where to run the command instead:

```bash
$ byk special@shell add completion bash
`add` takes no shell argument: it always updates the startup file of the shell running byk
Hint: run `byk special@shell add completion` from bash to set up bash
```

The reason is that this kind of mismatch **fails silently**: install into
`.bash_profile` from a zsh terminal and the terminal in front of you does
nothing, with no message saying it went to the wrong place. To set up bash,
run the command in bash — then there is exactly one answer to "which file am I
changing", and it matches both the line `special@shell` prints and the terminal you
are looking at.

### Other shells: cmd, sh

Completion generators cover `zsh`, `bash`, `fish`, and PowerShell (`pwsh` /
Windows PowerShell 5.1). Running `special@shell` in any other shell is not an error —
it simply answers that the current shell points at no file:

```bash
$ byk special@shell

Shell: sh (from $SHELL)
FilePath: (none)
Completion: not available
Only zsh, bash, fish and PowerShell have a startup file byk can edit
```

The screen ends there, with no `Usage` / `Commands`: there is no file to edit, so
`add` and `remove` cannot do anything on this machine, and a list of commands
that do not apply is just noise. An explicit `-h` prints the same thing — both
entry points are one output. The reason sits right under the fields, since it
explains `Completion: not available`, and the screen keeps a blank line above
and below it, just like the zsh one.

The exit code is 0: that is a normal answer, not a failure. Actually changing
something (`add` / `remove`) does fail, with exit code 1 and those same two
sentences, because nothing was changed:

```bash
$ byk special@shell add completion
No startup file for sh (from $SHELL)
Only zsh, bash, fish and PowerShell have a startup file byk can edit
```

When neither the parent process nor `$SHELL` gives an answer (common in Windows
cmd), the `Shell:` line reads `unknown` and the note becomes
`Only zsh, bash, fish and PowerShell are supported; run byk from one of them`.
To use completion, run byk inside one of those four.

### Write rules

This is the only `special@` member that edits **someone else's** file, so its rules
differ from `config.json`:

* only the marked block (`# The following lines have been added by byk to enable
  byk cli completions.` through `# End of byk cli completions`) is inserted or
  removed — every other byte stays, including hand-written order and CRLF
  endings. The markers are full sentences rather than `# >>> ... >>>` because a
  startup file is read by people;
* symlinks are written through, and stay symlinks;
* the unmarked bare line earlier `install.sh` versions wrote is recognised too
  (`uninstall.sh` matches it as well): `remove` deletes it, while `add` only
  reports "already enabled" and suggests removing and re-adding;
* idempotent: a second `add` adds nothing, and `remove` with nothing installed
  does nothing and exits 0.

The installer no longer writes that line — like pip, it only puts the binary in
place. Turn completion on once, yourself:

```bash
byk special@shell add completion
```

## Completion

Each special file owns its grammar, so the candidates come from the file itself
rather than from the file-command verb list:

* typing `special@` lists `config`, `tap`, and `shell`, each with a one-line
  description;
* after `special@config` / `special@tap` / `special@shell` a tab offers the verbs, and `-` offers
  `--help` / `-h` — those live in the verb slot only, since `special@config set -h`
  passes `-h` to `set`;
* `get`, `set`, and `unset` complete setting names, and `set <name>` completes
  that key's values (`banner` → `auto` / `always` / `never`);
* `remove` and `update` complete the `<user>/<repo>` keys in the ledger;
  `add` takes its source from the network, so it offers nothing;
* `add` and `remove` complete `completion` in the first slot and nothing after
  it — the target shell comes from the environment, so offering `bash` there
  would be teaching a command that gets refused.

Verbs that take no arguments (`list`, `get` without a name) offer no
candidates, so the shell has nothing to insert.

***

byk v0.9.0 · this page may lag behind HEAD; `byk __api__` and `byk -v` are authoritative.
