(config-aliases)=

# Repository aliases

Generic directory names repeat across workspace roots. A workspace under
`~/work/libtmux/` and a workspace under `~/study/python/` can both contain a
repository entry named `docs`. Running {ref}`vcspull sync <cli-sync>` `docs`
selects both entries, and {ref}`vcspull status <cli-status>` prints two `docs`
rows with nothing in the name to tell them apart.

An *alias* gives a repository entry one extra handle on the command line while
leaving its checkout directory, its `name`, and its merge identity across
{ref}`configuration scopes <config-scopes>` untouched. If every repository
entry in your {ref}`configuration <configuration>` already has a distinct name,
you can stop reading; everything here is optional.

```yaml
~/work/libtmux/:
  libtmux:
    repo: git+https://github.com/tmux-python/libtmux.git
  libtmux-haskell:
    repo: git+https://github.com/tmux-python/libtmux-haskell.git
  docs:
    repo: git+https://github.com/tmux-python/libtmux-docs.git
    alias: libtmux-docs
  assets:
    repo: git+https://github.com/tmux-python/libtmux-assets.git
    alias: libtmux-assets
~/study/python/:
  docs:
    repo: git+https://github.com/python/cpython-docs.git
```

With `alias: libtmux-docs` on the `docs` entry under `~/work/libtmux/`, you can
address `~/work/libtmux/docs` as `libtmux-docs` without matching
`~/study/python/docs`, group the whole `libtmux` family under `'libtmux*'`,
and keep `docs` matching both entries as before.

`alias:` differs from `name:` and `path:`:

- `name:` renames the repository entry and (unless `path:` is also set) its
  checkout directory; the old key stops matching {mod}`fnmatch` handles in
  `sync`, {ref}`list <cli-list>`, `status`, and {ref}`worktree <cli-worktree>`
  (anchor regexes such as `'name:^docs$'` in
  {ref}`vcspull search <cli-search>`).
- `path:` overrides the entry's destination path verbatim without `~` or `$HOME`
  expansion, so pairing `name:` with `path:` requires a machine-specific
  absolute path, and unscoped `vcspull search` still matches `path` and `url`.
- `alias:` adds a second command-line handle and leaves the directory and
  `name` unchanged.

The sections below use three terms: a *handle* is an entry's `name` or `alias`,
a *term* is a pattern you pass on the command line, and a *shared handle* is an
alias that equals another entry's `alias` or `name`.

(config-aliases-value)=

## Allowed alias values

An `alias` is a single case-sensitive string of at most 255 characters. It must
start with an ASCII letter, digit, or underscore (`A-Z`, `a-z`, `0-9`, `_`) and
contain only ASCII letters, digits, `_`, `.`, and `-`. Checkout directory names
stay unrestricted.

Any invalid character or empty string fails when the configuration file loads
and states the rule. For example, when `~/invalid.yaml` sets `alias:
libtmux/docs` on `docs`:

```vcspull-console
$ vcspull list --file ~/invalid.yaml
vcspull: ~/invalid.yaml: '~/work/libtmux/' -> 'docs'.alias: must start with a letter, digit, or underscore, contain only letters, digits, _, ., and -, and be at most 255 characters
```

Each repository entry holds at most one alias. When `~/multi.yaml` passes a
list (`alias: [lt-docs, libtmux-docs]`), loading fails:

```vcspull-console
$ vcspull list --file ~/multi.yaml
vcspull: ~/multi.yaml: '~/work/libtmux/' -> 'docs'.alias: expected a string; an alias must start with a letter, digit, or underscore, contain only letters, digits, _, ., and -, and be at most 255 characters (got a list; an entry holds one alias)
```

An alias equal to its own entry's `name` adds nothing. vcspull ignores a
self-alias when loading the file, treats the entry as having no alias when
attaching via {ref}`vcspull add <cli-add>` `--alias`, and removes the redundant
key when you run {ref}`vcspull fmt <cli-fmt>` `--write`. `vcspull fmt` also
prints a `Notes` section for shared aliases and pinned self-aliases in the file
it formats without altering non-redundant aliases. For example, when
`~/fmt-demo.yaml` sets `alias: docs` on `docs` and `alias: shared-docs` on both
`assets` and `manual`:

```vcspull-console
$ vcspull list --file ~/fmt-demo.yaml docs
• docs → ~/work/libtmux/docs
```

```vcspull-console
$ vcspull fmt --file ~/fmt-demo.yaml
i Found 3 formatting issues in ~/fmt-demo.yaml
  • Redundant alias 'docs' on 'docs' will be removed
  • Directories will be sorted alphabetically
  • Repositories in ~/work/libtmux/ will be sorted alphabetically

Notes (fmt changes nothing for these):
  handle 'shared-docs' is shared by 2 entries in this file; a term naming it selects all of them:
      '~/study/tmux/manual' (alias, ~/fmt-demo.yaml (explicit scope))
      '~/work/libtmux/assets' (alias, ~/fmt-demo.yaml (explicit scope))
  rename one alias to make the handle select one entry (other files may hold it too; see 'vcspull config ls')

→ Run with --write to apply these formatting changes.
```

(config-aliases-matching)=

## Selecting entries by alias

`vcspull sync`, {ref}`vcspull list <cli-list>`, `vcspull status`, and
{ref}`vcspull worktree <cli-worktree>` match {mod}`fnmatch` patterns against
both `name` and `alias`:

```vcspull-console
$ vcspull list 'libtmux*'
• libtmux → ~/work/libtmux/libtmux
• libtmux-haskell → ~/work/libtmux/libtmux-haskell
• docs (alias libtmux-docs) → ~/work/libtmux/docs
• assets (alias libtmux-assets) → ~/work/libtmux/assets
```

```vcspull-console
$ vcspull sync --dry-run 'libtmux*'
Plan: 3 to clone (+), 0 to update (~), 1 unchanged (✓), 0 blocked (⚠), 0 errors (✗)

~/work/libtmux/
  + assets (alias libtmux-assets)  ~/work/libtmux/assets  missing
  + libtmux                        ~/work/libtmux/libtmux  missing
  + libtmux-haskell                ~/work/libtmux/libtmux-haskell  missing
  ✓ docs (alias libtmux-docs)      ~/work/libtmux/docs  configured target matches
Tip: run without --dry-run to apply.
```

```vcspull-console
$ vcspull worktree list libtmux-docs

docs (alias libtmux-docs) (~/work/libtmux/docs)
  + tag:v1.0.0           ~/work/docs-v1 (will create configured worktree)
```

`vcspull sync` classifies terms starting with `git`, `http`, `svn`, or `hg` as
URL patterns, the same rule that applies to repository names. Choose an alias
with another prefix when you want `vcspull sync` to match it as a handle.

{ref}`vcspull search <cli-search>` includes `alias` in its default searched
fields and accepts `alias:` prefixes as well as `--field alias`:

```vcspull-console
$ vcspull search alias:libtmux
• docs (alias libtmux-docs) → ~/work/libtmux/docs
• assets (alias libtmux-assets) → ~/work/libtmux/assets
```

Sharing a directory name across different workspace roots is normal and never
emits a warning. Only an alias can create a shared handle:

```vcspull-console
$ vcspull list docs
• docs (alias libtmux-docs) → ~/work/libtmux/docs
• docs → ~/study/python/docs
```

(config-aliases-shared)=

## Shared handles and warnings

Commands that read configuration never fail when an alias equals another
entry's `alias` or `name`, whether inside one file or across scopes. Using the
same `~/fmt-demo.yaml` file where `assets` and `manual` both declare `alias:
shared-docs`, a term matching that shared handle selects both entries:

```vcspull-console
$ vcspull list --file ~/fmt-demo.yaml shared-docs
handle 'shared-docs' is shared by 2 entries; the term 'shared-docs' selects all of them:
    '~/study/tmux/manual' (alias, ~/fmt-demo.yaml (explicit scope))
    '~/work/libtmux/assets' (alias, ~/fmt-demo.yaml (explicit scope))
rename one alias to make the handle select one entry (see 'vcspull config ls')
• assets (alias shared-docs) → ~/work/libtmux/assets
• manual (alias shared-docs) → ~/study/tmux/manual
```

A handle warning prints when a term matches an entry's `alias` and does not
match that entry's own `name`. Running a command with no term, `--all`, or
`'*'` matches every entry's own `name` as well and stays quiet:

```vcspull-console
$ vcspull list --file ~/fmt-demo.yaml
• docs → ~/work/libtmux/docs
• assets (alias shared-docs) → ~/work/libtmux/assets
• manual (alias shared-docs) → ~/study/tmux/manual
```

At most one warning prints per handle and command. Every handle warning uses
the same three-part layout: what happened, each entry holding the handle on its
own indented line with its file and scope, and the remedy.

Commands that write configuration (`vcspull add --alias`,
{ref}`vcspull discover <cli-discover>`, and {ref}`vcspull import <cli-import>`)
refuse to create a shared handle against your user, system, or explicit
configuration files and name the existing holder:

```vcspull-console
$ vcspull add ~/study/python/libtmux-assets --alias python-docs
Cannot add 'libtmux-assets': alias 'python-docs' equals the alias of '~/study/python/docs' (alias, ~/.vcspull.yaml (user scope)); choose another alias
```

```vcspull-console
$ vcspull discover ~/study/python --yes
! Skipping 'libtmux-assets': name 'libtmux-assets' equals the alias of '~/work/libtmux/assets' (alias, ~/.vcspull.yaml (user scope)). Rename or remove that alias to add it.
! Found 1 existing repositories in configuration:
  • docs (https://github.com/python/cpython-docs.git) at ~/study/python/docs in ~/.vcspull.yaml
✓ All found repositories already exist in the configuration. Nothing to do.
```

When only a project `.vcspull.*` file holds the other entry, those writers warn
and still record your entry so a file you do not own cannot block your own
configuration:

```vcspull-console
$ vcspull add ~/study/python/docs --alias project-docs --yes
Found new repository to import:
  + docs (https://github.com/python/cpython-docs.git)
  • workspace: ~/study/python/
  ↳ path: ~/study/python/docs
? Import this repository with alias 'project-docs'? [y/N]: y (auto-confirm)
Adding 'docs': alias 'project-docs' equals the alias of '~/work/libtmux/docs' (alias, ~/work/libtmux/.vcspull.yaml (project scope)); the handle will select both from inside that project; pass --no-project to ignore project configs (see 'vcspull config ls')
✓ Successfully added 'docs' (git+https://github.com/python/cpython-docs.git) to ~/.vcspull.yaml under '~/study/python/' with alias 'project-docs'.
```

`vcspull add` without `--alias` writes to the target configuration file without
checking aliases.

To audit every shared, kept, ignored, lost, or dropped alias at once without
passing a term, run {ref}`vcspull config ls <cli-config>`:

```vcspull-console
$ vcspull config ls
user     ~/.config/vcspull/base.yaml   2 repos (2 overridden)
user     ~/.vcspull.yaml               3 repos
project  ~/work/libtmux/.vcspull.yaml  1 repo

4 repositories in effect.

shared handles (a term selects every entry listed):
  handle 'shared-lt'
    '~/study/tmux/manual' (alias, ~/.vcspull.yaml (user scope))
    '~/work/libtmux/libtmux-haskell' (alias, ~/.vcspull.yaml (user scope))

project aliases ignored because the entry keeps another alias:
  alias 'project-docs' ignored, alias 'libtmux-docs' kept
    '~/work/libtmux/docs' (alias, entry 'docs' under './', ~/work/libtmux/.vcspull.yaml (project scope))
    '~/work/libtmux/docs' (alias, entry 'docs' under '~/work/libtmux/', ~/.config/vcspull/base.yaml (user scope))

aliases lost because a nearer file replaced the entry:
  alias 'old-assets' lost
    '~/work/libtmux/assets' (alias, entry 'assets' under '~/work/libtmux/', ~/.config/vcspull/base.yaml (user scope))
    '~/work/libtmux/assets' (name, entry 'assets' under '~/work/libtmux/', ~/.vcspull.yaml (user scope))
```

(config-aliases-scopes)=

## Aliases across configuration scopes

Within your own configuration files, a stronger scope replaces the weaker entry
whole (see {ref}`config-scopes`). Redefining an entry in `~/.vcspull.yaml`
without repeating `alias:` drops the alias from `~/.config/vcspull/base.yaml`,
and a term naming the lost alias warns which file replaced it:

```vcspull-console
$ vcspull list old-assets
alias 'old-assets' of the entry 'assets' is lost: ~/.vcspull.yaml (user scope) replaces the entry without an alias:
    '~/work/libtmux/assets' (alias, entry 'assets' under '~/work/libtmux/', ~/.config/vcspull/base.yaml (user scope))
    '~/work/libtmux/assets' (name, entry 'assets' under '~/work/libtmux/', ~/.vcspull.yaml (user scope))
to keep it, add 'alias: old-assets' to the entry in ~/.vcspull.yaml (see 'vcspull config ls')
No repositories found.
```

Project `.vcspull.*` files cannot remove or replace an alias declared in a user
or system configuration file. Even when `~/work/libtmux/.vcspull.yaml`
redefines `./docs` with `alias: project-docs` (or with no alias), your
`libtmux-docs` handle continues to select the entry from inside the project:

```vcspull-console
$ vcspull list libtmux-docs
• docs (alias libtmux-docs) → ~/work/libtmux/docs
```

Querying the ignored project alias explains why the user alias stayed in
effect:

```vcspull-console
$ vcspull list project-docs
alias 'project-docs' of the entry 'docs' in ~/work/libtmux/.vcspull.yaml (project scope) is ignored: the entry keeps its alias 'libtmux-docs', and a project file cannot replace it:
    '~/work/libtmux/docs' (alias, entry 'docs' under './', ~/work/libtmux/.vcspull.yaml (project scope))
    '~/work/libtmux/docs' (alias, entry 'docs' under '~/work/libtmux/', ~/.config/vcspull/base.yaml (user scope))
to select the entry as 'project-docs', put that alias in ~/.config/vcspull/base.yaml in place of 'libtmux-docs'; otherwise nothing needs doing (see 'vcspull config ls')
No repositories found.
```

(config-aliases-writing)=

## Recording and attaching aliases

Pass `--alias HANDLE` to `vcspull add` when registering a new repository entry:

```vcspull-console
$ vcspull add ~/study/python/docs --alias python-docs --yes
Found new repository to import:
  + docs (https://github.com/python/cpython-docs.git)
  • workspace: ~/study/python/
  ↳ path: ~/study/python/docs
? Import this repository with alias 'python-docs'? [y/N]: y (auto-confirm)
✓ Successfully added 'docs' (git+https://github.com/python/cpython-docs.git) to ~/.vcspull.yaml under '~/study/python/' with alias 'python-docs'.
```

When `PATH` already resolves to a configured destination — regardless of its
entry key or `path:` override — `vcspull add PATH --alias HANDLE` attaches the
alias to the winning repository entry in place, preserving section layout, key
order, and all other fields. Before prompting, vcspull renders the updated file
and reloads it through the configuration loader to verify that the target entry
gains `alias` and every other entry stays unchanged:

```vcspull-console
$ vcspull add ~/work/libtmux/assets --alias libtmux-assets --dry-run
Found configured repository to alias:
  + assets (git+https://github.com/tmux-python/libtmux-assets.git)
  • workspace: ~/work/libtmux/
  ↳ path: ~/work/libtmux/assets
  • file: ~/.vcspull.yaml
? Attach alias 'libtmux-assets' to this repository? [y/N]: skipped (dry-run)
→ Would attach alias 'libtmux-assets' to 'assets' in ~/.vcspull.yaml under '~/work/libtmux/'.
```

```vcspull-console
$ vcspull add ~/work/libtmux/assets --alias libtmux-assets --yes
Found configured repository to alias:
  + assets (git+https://github.com/tmux-python/libtmux-assets.git)
  • workspace: ~/work/libtmux/
  ↳ path: ~/work/libtmux/assets
  • file: ~/.vcspull.yaml
? Attach alias 'libtmux-assets' to this repository? [y/N]: y (auto-confirm)
✓ Attached alias 'libtmux-assets' to 'assets' in ~/.vcspull.yaml under '~/work/libtmux/'.
```

An attach refuses before any prompt whenever the edit is not one unambiguous
alias addition:

| Situation | Outcome |
| --- | --- |
| Two or more entries resolve to the directory | Refused, naming each workspace root and key |
| `--file FILE` names a definition overridden by a stronger file | Refused, naming the file that wins |
| `--file FILE` is outside the active stack while a loaded file defines the entry | Refused, naming the loaded file |
| `--file FILE` is outside the active stack and nothing else defines the entry | Edited in `FILE` for commands run with `--file FILE` |
| The entry is defined only in a project or system file | Refused unless `--file` names that file |
| The entry already has a non-redundant alias | Refused; edit the file by hand to change or remove it |
| The entry is {ref}`pinned <config-pin>` (`pin: true` or `pin.add: true`) | Refused; pins guard against every writer |
| You pass `--pin`, `--depth`, `--shallow`, `--name`, `--workspace`, or a different `--url` | Refused; an attach changes only `alias` |

```vcspull-console
$ vcspull add ~/work/libtmux/assets --alias libtmux-assets --pin v1.0.0
Cannot attach alias 'libtmux-assets' to 'assets': an attach changes only the alias; --pin would change the entry (edit the config file, or remove the entry and add it again)
```

`vcspull add` never changes or removes an existing alias; edit the
configuration file directly to rename or delete one:

```vcspull-console
$ vcspull add ~/work/libtmux/assets --alias other-assets
Cannot attach alias 'other-assets' to 'assets': 'assets' already has the alias 'libtmux-assets', held as '~/work/libtmux/assets' (alias, ~/.vcspull.yaml (user scope)); edit ~/.vcspull.yaml to change it
```

`vcspull discover` and `vcspull import` never write `alias:`, and both treat a
repository entry configured with an alias (or custom key) as already covering
its destination (as shown in the `vcspull discover ~/study/python --yes` output
under {ref}`config-aliases-shared`).

(config-aliases-output)=

## Human and JSON output

Human-readable output prints `(alias HANDLE)` immediately after `name` in
`list`, `status`, `search`, `sync`, and `worktree`:

```vcspull-console
$ vcspull status --no-concurrent
✗ libtmux: missing
✗ libtmux-haskell: missing
✓ docs (alias libtmux-docs): up to date
✗ assets (alias libtmux-assets): missing
✗ docs: missing

Summary: 5 repositories, 1 exist, 4 missing
```

```vcspull-console
$ vcspull sync --color never libtmux-docs
✓ Synced docs (alias libtmux-docs) → ~/work/libtmux/docs

Summary: 1 repos, 1 synced, 0 failed
```

`--json` and `--ndjson` repository payloads include `"alias"` (`null` when
unset) while keeping `"name"` unchanged so existing scripts that read `"name"`
continue to work:

```vcspull-console
$ vcspull list --json docs
[
  {
    "name": "docs",
    "alias": "libtmux-docs",
    "url": "git+https://github.com/tmux-python/libtmux-docs.git",
    "path": "~/work/libtmux/docs",
    "workspace_root": "~/work/libtmux/"
  },
  {
    "name": "docs",
    "alias": null,
    "url": "git+https://github.com/python/cpython-docs.git",
    "path": "~/study/python/docs",
    "workspace_root": "~/study/python/"
  }
]
```

## For the Python reader

{func}`vcspull.validator.alias_problem` validates candidate alias strings with
the same rules enforced by {func}`vcspull.validator.validate_repo_entry` and
[the published JSON Schema](https://vcspull.git-pull.com/_static/schemas/vcspull.schema.json).
{func}`vcspull.config.load_scoped_configs` resolves aliases across scopes and
emits any term-triggered handle warnings when you pass `terms`, and
{func}`vcspull.config.filter_repos` matches `name` patterns against both `name`
and case-sensitive `alias` values.
