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
11 changes: 11 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ $ uvx --from 'vcspull' --prerelease allow vcspull
_Notes on upcoming releases will be added here_
<!-- END PLACEHOLDER - ADD NEW CHANGELOG ENTRIES BELOW THIS LINE -->

### Fixes

#### Symlinked user configuration deduplication (#600)

{ref}`Configuration scope resolution <config-scopes>` now deduplicates resolved
paths across `system`, `user`, and `project` scopes so symlinked user dotfiles
(`~/.vcspull.yaml`, `~/.config/vcspull`, `$VCSPULL_CONFIGDIR`, or `~/.vcspull`
pointing into a dotfiles checkout) stay in the `user` scope and are not
re-discovered as `project` configurations when running inside the dotfiles
directory (#600).

## vcspull v1.72.0 (2026-12-05)

vcspull v1.72.0 excludes archived and deletion-scheduled GitLab groups and
Expand Down
7 changes: 5 additions & 2 deletions docs/configuration/scopes.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ named by more than one scope comes from the strongest.
| project | `.vcspull.{yaml,yml,json}` in each ancestor of your working directory, outermost first | gated |

Passing `-f/--file` replaces that entire stack with the one file you name, so
existing scripts and CI jobs behave exactly as they always have. When write
commands classify a target path, files under `/etc/vcspull` or
existing scripts and CI jobs behave exactly as they always have. Symlinked user
configurations — such as `~/.vcspull.yaml` or `~/.config/vcspull/` pointing into
a dotfiles checkout — keep their `user` scope and are not loaded a second time
as `project` configurations when you work inside that dotfiles directory. When
write commands classify a target path, files under `/etc/vcspull` or
`$XDG_CONFIG_DIRS/vcspull` (`/etc/xdg/vcspull` when `XDG_CONFIG_DIRS` is unset)
are also treated as `system` scope.

Expand Down
22 changes: 14 additions & 8 deletions src/vcspull/_internal/private_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,28 @@ def __new__(

@classmethod
def _collapse_home(cls, value: str) -> str:
"""Collapse the user's home directory to ``~`` in ``value``."""
"""Collapse the user's home directory (and its realpath) to ``~``."""
if value.startswith("~"):
return value

home = str(pathlib.Path.home())
if value == home:
return "~"
target_home = pathlib.Path.home().expanduser()
candidates = tuple(
dict.fromkeys((str(target_home), os.path.realpath(target_home))),
)

separators = {os.sep}
if os.altsep:
separators.add(os.altsep)

for sep in separators:
home_with_sep = home + sep
if value.startswith(home_with_sep):
return "~" + value[len(home) :]
for candidate in candidates:
if not candidate or candidate == os.sep:
continue
if value == candidate:
return "~"
for sep in separators:
home_with_sep = candidate + sep
if value.startswith(home_with_sep):
return "~" + value[len(candidate) :]

return value

Expand Down
186 changes: 143 additions & 43 deletions src/vcspull/_internal/scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import pathlib
import typing as t

from ..util import get_config_dir
from ..util import expand_home, get_config_dir, resolve_home, user_config_dirs

if t.TYPE_CHECKING:
from collections.abc import Iterable, Mapping
Expand All @@ -42,6 +42,11 @@
PROJECT_CONFIG_STEM: t.Final = ".vcspull"
"""Basename of a project configuration, before its suffix."""

_PROJECT_FILENAMES: t.Final = frozenset(
f"{PROJECT_CONFIG_STEM}{suffix}" for suffix in CONFIG_SUFFIXES
)
_HOME_DOTFILE_NAMES: t.Final = (".vcspull.yaml", ".vcspull.json")

_TRUTHY: t.Final = frozenset({"1", "true", "yes", "on"})


Expand Down Expand Up @@ -274,7 +279,9 @@ def resolve_sources(
Returns
-------
tuple of ConfigSource
Existing files only, ordered weakest to strongest.
Existing files only, ordered weakest to strongest, deduplicated by
resolved target path so symlinked user dotfiles are never re-added as
project configurations.

Raises
------
Expand All @@ -300,37 +307,87 @@ def resolve_sources(
"""
from ..config import find_home_config_files

home = (home or pathlib.Path.home()).expanduser()
resolved_home = resolve_home(home, environ)
sources: list[ConfigSource] = []

sources.extend(
ConfigSource("system", path, SYSTEM_CONFIG_DIR)
for path in _config_files_in(SYSTEM_CONFIG_DIR)
)

user_dir = get_config_dir()
sources.extend(
ConfigSource("user", path, user_dir) for path in _config_files_in(user_dir)
)
seen: set[pathlib.Path] = set()

def _append_source(
scope: ConfigScope,
path: pathlib.Path,
trust_root: pathlib.Path,
) -> None:
real_path = _real(path)
if real_path in seen:
return
seen.add(real_path)
sources.append(ConfigSource(scope, path, trust_root))

for path in _config_files_in(SYSTEM_CONFIG_DIR):
_append_source("system", path, SYSTEM_CONFIG_DIR)

user_dir = get_config_dir(home=resolved_home, environ=environ)
for path in _config_files_in(user_dir):
_append_source("user", path, user_dir)

# Delegated rather than reimplemented so that owning two home dotfiles
# still raises MultipleConfigError, as it did before scopes existed.
sources.extend(
ConfigSource("user", path, path.parent)
for path in find_home_config_files(home=home)
)
for path in find_home_config_files(home=resolved_home):
_append_source("user", path, path.parent)

if include_project and not env_flag("VCSPULL_NO_PROJECT", environ):
ceilings = ceiling_paths(environ, home=home)
ceilings = ceiling_paths(environ, home=resolved_home)
for directory in project_dirs(cwd, ceilings=ceilings):
sources.extend(
ConfigSource("project", path, directory)
for path in _project_files_in(directory)
)
for path in _project_files_in(directory):
_append_source("project", path, directory)

return tuple(sources)


def _lexical_norm(path: pathlib.Path) -> pathlib.Path:
"""Collapse ``.`` and ``..`` segments in *path* without following symlinks."""
base = path if path.is_absolute() else pathlib.Path.cwd() / path
stack: list[str] = []
for part in base.parts[1:]:
if part in {"", "."}:
continue
if part == "..":
if stack:
stack.pop()
else:
stack.append(part)
return pathlib.Path(base.anchor, *stack)


def _follow_hops(
path: pathlib.Path,
*,
raw_home: pathlib.Path,
real_home: pathlib.Path,
) -> tuple[pathlib.Path, ...]:
"""Return lexical, intermediate symlink, and resolved paths for *path*."""
hops: list[pathlib.Path] = []
visited: set[pathlib.Path] = set()
cur = _lexical_norm(path)

for _ in range(40):
if cur in visited:
break
visited.add(cur)
hops.append(cur)
if raw_home != real_home and _is_within(cur, raw_home):
hops.append(real_home / cur.relative_to(raw_home))
if not cur.is_symlink():
break
try:
target = cur.readlink()
except OSError:
break
cur = _lexical_norm(target if target.is_absolute() else cur.parent / target)

hops.append(_real(path))
return tuple(dict.fromkeys(hops))


def classify_scope(
config_path: pathlib.Path,
*,
Expand All @@ -342,8 +399,8 @@ def classify_scope(

Unlike :func:`resolve_sources`, this answers the question for a path the
caller already holds — a ``--file`` argument, or the file a write command
is about to touch. A path that is neither user, system, nor inside the
working directory is ``external``.
is about to touch. A path that is neither user, system, nor inside an
active project directory is ``external``.

Parameters
----------
Expand Down Expand Up @@ -375,36 +432,79 @@ def classify_scope(
... )
'external'
"""
resolved = _real(config_path)
home = _real(home)
cwd = _real(cwd)

if resolved.parent == home and resolved.name in {
f"{PROJECT_CONFIG_STEM}{suffix}" for suffix in CONFIG_SUFFIXES
}:
return "user"

xdg_config_home = _real(
pathlib.Path(environ.get("XDG_CONFIG_HOME", home / ".config")),
raw_home = _lexical_norm(resolve_home(home, environ))
real_home = _real(raw_home)
lexical_cwd = _lexical_norm(expand_home(cwd, raw_home))
expanded = expand_home(config_path, raw_home)
if not expanded.is_absolute():
expanded = lexical_cwd / expanded

lexical = _lexical_norm(expanded)
candidates: tuple[pathlib.Path, ...] = (
(lexical, real_home / lexical.relative_to(raw_home))
if raw_home != real_home and _is_within(lexical, raw_home)
else (lexical,)
)
if _is_within(resolved, xdg_config_home / "vcspull"):

if any(
cand.parent in {raw_home, real_home} and cand.name in _PROJECT_FILENAMES
for cand in candidates
):
return "user"

xdg_config_dirs = environ.get("XDG_CONFIG_DIRS")
for user_base in user_config_dirs(home=raw_home, environ=environ):
for hop in _follow_hops(user_base, raw_home=raw_home, real_home=real_home):
if any(_is_within(cand, hop) for cand in candidates):
return "user"

for dotfile_name in _HOME_DOTFILE_NAMES:
dotfile = raw_home / dotfile_name
if dotfile.is_file():
hops = _follow_hops(dotfile, raw_home=raw_home, real_home=real_home)
if any(cand in hops for cand in candidates):
return "user"

active_user_dir = get_config_dir(home=raw_home, environ=environ)
for user_file in _config_files_in(active_user_dir):
hops = _follow_hops(user_file, raw_home=raw_home, real_home=real_home)
if any(cand in hops for cand in candidates):
return "user"

xdg_config_dirs = environ.get("XDG_CONFIG_DIRS", "").strip()
system_bases = [SYSTEM_CONFIG_DIR]
if xdg_config_dirs:
system_bases.extend(
pathlib.Path(entry) / "vcspull"
expand_home(pathlib.Path(entry.strip()), raw_home) / "vcspull"
for entry in xdg_config_dirs.split(os.pathsep)
if entry
if entry.strip()
)
else:
system_bases.append(pathlib.Path("/etc/xdg/vcspull"))
for base in system_bases:
if _is_within(resolved, _real(base)):
return "system"

return "project" if _is_within(resolved, cwd) else "external"
for hop in _follow_hops(base, raw_home=raw_home, real_home=real_home):
if any(_is_within(cand, hop) for cand in candidates):
return "system"
for sys_file in _config_files_in(base):
hops = _follow_hops(sys_file, raw_home=raw_home, real_home=real_home)
if any(cand in hops for cand in candidates):
return "system"

real_cwd = _real(lexical_cwd)
entry_dir = _real(expanded.parent)
ceilings = ceiling_paths(environ, home=real_home)
active_project_dirs = {
_real(directory) for directory in project_dirs(lexical_cwd, ceilings=ceilings)
}
is_ancestor_project_config = (
expanded.name in _PROJECT_FILENAMES and entry_dir in active_project_dirs
)
return (
"project"
if is_ancestor_project_config
or _is_within(entry_dir, real_cwd)
or _is_within(lexical.parent, lexical_cwd)
else "external"
)


def _is_within(path: pathlib.Path, directory: pathlib.Path) -> bool:
Expand Down
7 changes: 4 additions & 3 deletions src/vcspull/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2246,18 +2246,19 @@ def ensure_config_trusted(
True
"""
cwd = cwd or pathlib.Path.cwd()
if explicit or not config_path.is_file():
anchor = config_path if config_path.is_absolute() else cwd / config_path
if explicit or not anchor.is_file():
return True

scope = scopes.classify_scope(
config_path,
anchor,
cwd=cwd,
home=pathlib.Path.home(),
)
if scope != "project":
return True

source = scopes.ConfigSource("project", config_path, config_path.parent)
source = scopes.ConfigSource("project", anchor, anchor.parent)
escaping = source_escapes(source, cwd=cwd)
if not escaping:
return True
Expand Down
Loading