Skip to content
Merged
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
24 changes: 23 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-26.04, macos-latest, windows-latest]
python-version: ['3.10', '3.11', '3.12', '3.13', '3.14']
# A PR runs only the oldest and newest Python. main runs all of them.
python-version: ${{ fromJSON(github.event_name == 'pull_request' && '["3.10", "3.14"]' || '["3.10", "3.11", "3.12", "3.13", "3.14"]') }}

steps:
- name: Checkout code
Expand All @@ -87,6 +88,27 @@ jobs:
# authorship) needs real history, not the default shallow, tagless
# checkout.
fetch-depth: 0
# The tests start many git processes and write many small files.
# On Windows, Defender scans each one, which makes the tests slow.
- name: Turn off Defender scanning
if: runner.os == 'Windows'
shell: pwsh
run: Set-MpPreference -DisableRealtimeMonitoring $true
# A Dev Drive is a fast ReFS drive. TEMP points there, so the test
# repos in tmp_path live on it.
- name: Set up Dev Drive
if: runner.os == 'Windows'
uses: samypr100/setup-dev-drive@v4.1.0
with:
drive-size: 8GB
drive-path: ${{ runner.temp }}/dev_drive.vhdx
trusted-dev-drive: true
env-mapping: |
TEMP,{{ DEV_DRIVE }}/tmp
TMP,{{ DEV_DRIVE }}/tmp
- name: Create temp dir on Dev Drive
if: runner.os == 'Windows'
run: mkdir -p "$TEMP" && echo "TEMP=$TEMP"
- name: Install uv
uses: astral-sh/setup-uv@v10.3.3
with:
Expand Down
6 changes: 3 additions & 3 deletions src/git_nested/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def check_repository(git: GitRunner, command: str, flags: Flags) -> tuple[Path |
return None, None

try:
git.run(['rev-parse', '--git-dir'])
git_dir, inside_work_tree = git.check_output(['rev-parse', '--git-dir', '--is-inside-work-tree']).splitlines()
except GitNestedError:
# A deliberate re-interpretation of git's own message, not an
# incidental failure, so the chain is suppressed -- and since
Expand All @@ -29,14 +29,14 @@ def check_repository(git: GitRunner, command: str, flags: Flags) -> tuple[Path |
raise GitNestedError("not inside a git repository") from None

# Per worktree of this repository, so they do not block each other.
git_tmp = Path(git.check_output(['rev-parse', '--git-dir'])) / 'tmp'
git_tmp = Path(git_dir) / 'tmp'

# Not a nested branch. A detached HEAD is fine: commits go there, as with git commit.
current_branch = git.check_output(['symbolic-ref', '--short', '--quiet', 'HEAD'], may_fail=True)
if current_branch.startswith('nested/'):
raise GitNestedError(f"can't {command} while the nested branch {current_branch} is checked out")

if git.check_output(['rev-parse', '--is-inside-work-tree'], may_fail=True) != 'true':
if inside_work_tree != 'true':
raise GitNestedError("not inside a git working tree")

if _needs_clean_worktree(command, flags):
Expand Down
2 changes: 1 addition & 1 deletion src/git_nested/cli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def setup_command(
tuple: (subdir, gitnested, subref, config)
"""
subdir = resolve_subdir(subdir)
subref = refs.sanitize_subref(git, subdir.as_posix())
subref = refs.sanitize_subref(subdir.as_posix())
gitnested = resolve_gitnested_file(subdir)

if not flags.force:
Expand Down
2 changes: 1 addition & 1 deletion src/git_nested/commands/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

def do_clean(git: GitRunner, flags: Flags, subdir: Path, git_tmp: Path) -> list[str]:
"""Clean nested branches and refs."""
subref = refs.sanitize_subref(git, subdir.as_posix())
subref = refs.sanitize_subref(subdir.as_posix())
items = _delete_branch(git, refs.nested_branch(git, subref), git_tmp)
refs.record_nested_branch(git, subref, None)
if flags.force:
Expand Down
4 changes: 2 additions & 2 deletions src/git_nested/commands/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def _adopt_existing_subdir(
git, config, upstream_commit, upstream_commit, subdir, 'clone'
)
content.commit_gitnested_update(git, flags, subdir, msg)
refs.create_nested_ref(git, refs.sanitize_subref(git, subdir.as_posix()), 'commit', upstream_commit)
refs.create_nested_ref(git, refs.sanitize_subref(subdir.as_posix()), 'commit', upstream_commit)


def _clone_into(
Expand Down Expand Up @@ -192,7 +192,7 @@ def nest_submodules(git: GitRunner, flags: Flags, config: NestedConfig, subdir:
are skipped.
"""
in_index = dict(submodules.gitlinks(git, subdir))
for relative, pinned in sorted(submodules.gitlinks_at(git, content.nested_tree(git, config, subdir)).items()):
for relative, pinned in sorted(submodules.gitlinks_at(git, content.nested_tree(config, subdir)).items()):
path = f'{subdir.as_posix()}/{relative}'
if (Path(path) / GITNESTED_FILENAME).is_file() or ((config.filter or config.exclude) and path not in in_index):
continue
Expand Down
4 changes: 2 additions & 2 deletions src/git_nested/commands/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _rewrite_history_with_prefix(
git: GitRunner, flags: Flags, nested_commit_ref: str, subdir: Path, git_tmp: Path, subref: str
) -> str:
"""Replay `nested_commit_ref` as commits whose trees live under `subdir/`."""
scratch = git_tmp / '.pull-no-squash' / f'{refs.sanitize_subref(git, subref)}-{uuid4().hex}'
scratch = git_tmp / '.pull-no-squash' / f'{refs.sanitize_subref(subref)}-{uuid4().hex}'
scratch.mkdir(parents=True, exist_ok=True)
index_env = {**os.environ, 'GIT_INDEX_FILE': str(scratch / 'index')}
rewritten: dict[str, str] = {}
Expand Down Expand Up @@ -334,7 +334,7 @@ def follow_inner(ctx: CommandContext, config: NestedConfig, subdir: Path) -> Non
If one stops on conflicts, a new pull of subdir does the rest.
"""
git = ctx.git
for relative, pin in sorted(submodules.gitlinks_at(git, content.nested_tree(git, config, subdir)).items()):
for relative, pin in sorted(submodules.gitlinks_at(git, content.nested_tree(config, subdir)).items()):
path = subdir / relative
if not (path / GITNESTED_FILENAME).is_file():
continue
Expand Down
2 changes: 1 addition & 1 deletion src/git_nested/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_status(git: GitRunner, flags: Flags) -> str:

def _status_for_subdir(git: GitRunner, flags: Flags, subdir: Path) -> str:
"""The status report of one nested subdir."""
subref = refs.sanitize_subref(git, subdir.as_posix())
subref = refs.sanitize_subref(subdir.as_posix())
gitnested = subdir / GITNESTED_FILENAME
if not gitnested.is_file():
return f"'{subdir.as_posix()}' is not a nested repository\n"
Expand Down
6 changes: 3 additions & 3 deletions src/git_nested/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def commit_nested_branch(
commit_gitnested_update(git, flags, subdir, msg)

worktree.remove_worktree(git, subdir_worktree)
refs.create_nested_ref(git, refs.sanitize_subref(git, subdir.as_posix()), 'commit', nested_commit_ref)
refs.create_nested_ref(git, refs.sanitize_subref(subdir.as_posix()), 'commit', nested_commit_ref)


def place_with_patches(git: GitRunner, config: NestedConfig, subdir: Path, nested_commit_ref: str) -> list[str] | None:
Expand Down Expand Up @@ -157,9 +157,9 @@ def _drop_gitlink(git: GitRunner, path: Path) -> None:
path.rmdir()


def nested_tree(git: GitRunner, config: NestedConfig, subdir: Path) -> str:
def nested_tree(config: NestedConfig, subdir: Path) -> str:
"""The tree that subdir got its content from in the last clone, pull or commit."""
tree = f'refs/nested/{refs.sanitize_subref(git, subdir.as_posix())}/commit'
tree = f'refs/nested/{refs.sanitize_subref(subdir.as_posix())}/commit'
return f'{tree}:{config.path}' if config.path else tree


Expand Down
21 changes: 19 additions & 2 deletions src/git_nested/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from .constants import REQUIRED_GIT_VERSION
from .errors import GitNestedError

# The version of each git binary, so it is asked only once per process.
# The test suite runs many commands in one process.
_VERSIONS: dict[str, str] = {}


class GitRunner:
"""Simplified git command execution."""
Expand Down Expand Up @@ -56,6 +60,17 @@ def check_output(self, args: Sequence[str | Path], may_fail: bool = False, **kwa
result = self.run(args=args, may_fail=may_fail, **kwargs)
return result.stdout.strip()

def cached_output(self, args: Sequence[str]) -> str:
"""check_output, but git is asked only once per directory.

Only for answers that do not change while a command runs.
"""
cache: dict[tuple[Path, tuple[str, ...]], str] = self.__dict__.setdefault('_cache', {})
key = (Path.cwd(), tuple(args))
if key not in cache:
cache[key] = self.check_output(args)
return cache[key]

def is_tracked(self, path: Path) -> bool:
"""Check if given path is tracked by git.

Expand All @@ -81,15 +96,17 @@ def commit_in_rev_list(self, commit: str, list_head: str) -> bool:

def check(self) -> None:
"""Check that environment is suitable."""
if not shutil.which('git'):
path = shutil.which('git')
if not path:
raise GitNestedError("git is not on PATH")
version = self.get_version()
version = _VERSIONS.get(path) or self.get_version()

def version_tuple(v: str) -> tuple[int, ...]:
return tuple(map(int, (v.split("."))))

if version_tuple(version) < version_tuple(REQUIRED_GIT_VERSION):
raise GitNestedError(f"git {REQUIRED_GIT_VERSION} or newer is required, but this is {version}")
_VERSIONS[path] = version

def get_version(self) -> str:
"""Return the installed git version string (e.g. '2.43.0')."""
Expand Down
17 changes: 11 additions & 6 deletions src/git_nested/refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import re
from pathlib import Path
from urllib.parse import quote

Expand All @@ -26,7 +27,7 @@ def worktree_suffix(git: GitRunner) -> str:

So the worktrees of this repository each get their own nested branches.
"""
git_dir, common_dir = git.check_output(['rev-parse', '--git-dir', '--git-common-dir']).splitlines()
git_dir, common_dir = git.cached_output(['rev-parse', '--git-dir', '--git-common-dir']).splitlines()
if Path(git_dir).resolve() == Path(common_dir).resolve():
return ''
return f'@{Path(git_dir).name}'
Expand Down Expand Up @@ -86,14 +87,18 @@ def guess_subdir(remote: str) -> str:
return Path(remote).name.removesuffix('.git')


def _is_valid_ref(git: GitRunner, ref: str) -> bool:
# The rules of `git check-ref-format`, checked here to save a git call
_BAD_REF = re.compile(r'[\x00-\x20\x7f~^:?*\[\\]|\.\.|@\{|//|^/|/$|\.$|(^|/)\.|\.lock(/|$)')


def _is_valid_ref(ref: str) -> bool:
"""Check whether ref is already a valid git ref name (as a nested/ subref)."""
return git.run(['check-ref-format', f'nested/{ref}'], may_fail=True).returncode == 0
return not _BAD_REF.search(f'nested/{ref}')


def sanitize_subref(git: GitRunner, ref: str) -> str:
def sanitize_subref(ref: str) -> str:
"""Sanitize subref to be a valid git ref."""
if _is_valid_ref(git, ref):
if _is_valid_ref(ref):
return ref

sanitized = quote(ref, safe='/')
Expand All @@ -106,6 +111,6 @@ def sanitize_subref(git: GitRunner, ref: str) -> str:
sanitized = sanitized.removesuffix('.lock') + '_lock'
sanitized = sanitized.removesuffix('.')

if not _is_valid_ref(git, sanitized):
if not _is_valid_ref(sanitized):
raise GitNestedError(f"can't derive a git ref name from {ref}")
return sanitized
86 changes: 48 additions & 38 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,12 @@ def add_new_files(self, *files, cwd=None):
file_path = Path(cwd) / file if cwd else Path(file)
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(f"new file {file}\n")
subprocess.run(['git', 'add', '--force', str(file)], cwd=cwd, check=True)

subprocess.run(['git', 'add', '--force', '--', *map(str, files)], cwd=cwd, check=True)
subprocess.run(['git', 'commit', '--quiet', '-m', f'add new file: {files[-1]}'], cwd=cwd, check=True)

def remove_files(self, *files, cwd=None):
"""Remove files and commit"""
for file in files:
subprocess.run(['git', 'rm', file], cwd=cwd, check=True)

subprocess.run(['git', 'rm', '--', *map(str, files)], cwd=cwd, check=True)
subprocess.run(['git', 'commit', '--quiet', '-m', f'Removed file: {files[-1]}'], cwd=cwd, check=True)

def modify_files(self, *files, text=None, cwd=None):
Expand All @@ -176,8 +173,7 @@ def modify_files(self, *files, text=None, cwd=None):
file_path = Path(cwd) / file if cwd else Path(file)
with file_path.open('a') as f:
f.write(f'{text}\n')
subprocess.run(['git', 'add', str(file)], cwd=cwd, check=True)

subprocess.run(['git', 'add', '--', *map(str, files)], cwd=cwd, check=True)
subprocess.run(['git', 'commit', '-m', f'modified file: {files[-1]}'], cwd=cwd, check=True)


Expand All @@ -188,10 +184,9 @@ def modify_files(self, *files, text=None, cwd=None):

def clone_repo(upstream: str, path: Path):
path.parent.mkdir(parents=True, exist_ok=True)
subprocess.run(['git', 'clone', upstream, path], check=True, capture_output=True)
name = path.name
subprocess.run(['git', 'config', 'user.name', f'{name.capitalize()}User'], cwd=path, check=True)
subprocess.run(['git', 'config', 'user.email', f'{name}@{name}'], cwd=path, check=True)
identity = ['-c', f'user.name={name.capitalize()}User', '-c', f'user.email={name}@{name}']
subprocess.run(['git', 'clone', *identity, upstream, path], check=True, capture_output=True)


def remove_tree(path: Path) -> None:
Expand Down Expand Up @@ -224,8 +219,8 @@ def create_upstream_repo(repo_path: Path) -> Path:
work_dir = repo_path.parent / (repo_path.name + ".tmp")
work_dir.mkdir(exist_ok=True)
subprocess.run(['git', 'init'], cwd=work_dir, check=True, capture_output=True)
subprocess.run(['git', 'config', 'user.name', 'Test'], cwd=work_dir, check=True, capture_output=True)
subprocess.run(['git', 'config', 'user.email', 'test@test'], cwd=work_dir, check=True, capture_output=True)
with (work_dir / '.git' / 'config').open('a') as f:
f.write('[user]\n\tname = Test\n\temail = test@test\n')
return work_dir


Expand Down Expand Up @@ -417,8 +412,42 @@ def git_exec_path(tmp_path_factory) -> Path | None:
return shadow


def _isolate_git(home: Path, git_exec_path: Path | None, path_dirs: list[Path]) -> dict[str, str]:
"""Write a global git config into home and return env vars that use only it"""
(home / '.gitconfig').write_text(
'[user]\n\tname = Test User\n\temail = test@example.com\n[init]\n\tdefaultBranch = master\n'
)
env_vars = {
'HOME': str(home),
'GIT_CONFIG_GLOBAL': str(home / '.gitconfig'),
'GIT_CONFIG_SYSTEM': os.devnull,
# Drops config injected through the caller's environment, such as safe.bareRepository.
'GIT_CONFIG_COUNT': '0',
'PATH': os.pathsep.join([*map(str, path_dirs), os.getenv('PATH', '')]),
}
if git_exec_path:
env_vars['GIT_EXEC_PATH'] = str(git_exec_path)
return env_vars


@pytest.fixture(scope='session')
def upstream_template(tmp_path_factory, git_exec_path) -> Path:
"""The foo and bar upstreams, built once per session

Each test gets a copy. Copying is much faster than building them again,
above all on Windows, where each git call is slow.
"""
template = tmp_path_factory.mktemp('upstream-template')
home = template / 'home'
home.mkdir()
with update_env(_isolate_git(home, git_exec_path, [])):
create_upstream_foo(template / 'upstream' / 'foo')
create_upstream_bar(template / 'upstream' / 'bar')
return template / 'upstream'


@pytest.fixture
def env(tmp_path, git_exec_path):
def env(tmp_path, git_exec_path, upstream_template):
"""Setup isolated test environment with git repos"""
root_dir = Path(__file__).parent.parent
tmp_dir = Path(tmp_path)
Expand All @@ -438,31 +467,12 @@ def env(tmp_path, git_exec_path):
(shim_dir / 'python3').write_text(f'#!/bin/sh\nexec "{Path(sys.executable).as_posix()}" "$@"\n')
path_dirs.insert(0, shim_dir)

# Set up isolated environment variables
env_vars = {
'HOME': str(test_env.test_home),
'GIT_CONFIG_GLOBAL': str(test_env.test_home / '.gitconfig'),
'GIT_CONFIG_SYSTEM': os.devnull,
# Drops config injected through the caller's environment, such as safe.bareRepository.
'GIT_CONFIG_COUNT': '0',
# bin/ holds the 'git-nested' launcher `git nested` dispatches to --
# replaced by the directory of the frozen binary when testing that.
'PATH': os.pathsep.join([*map(str, path_dirs), os.getenv('PATH', '')]),
}
if git_exec_path:
env_vars['GIT_EXEC_PATH'] = str(git_exec_path)

with update_env(env_vars):
# Configure git
subprocess.run(['git', 'config', '--global', 'user.name', 'Test User'], check=True)
subprocess.run(['git', 'config', '--global', 'user.email', 'test@example.com'], check=True)
subprocess.run(['git', 'config', '--global', 'init.defaultBranch', 'master'], check=True)

# Create test repositories. 'init' is deliberately absent -- it is
# built lazily by NestedTestEnvironment.clone_init, see there.
create_upstream_foo(test_env.upstream / 'foo')
create_upstream_bar(test_env.upstream / 'bar')

# bin/ holds the 'git-nested' launcher `git nested` dispatches to --
# replaced by the directory of the frozen binary when testing that.
with update_env(_isolate_git(test_env.test_home, git_exec_path, path_dirs)):
# 'init' is deliberately absent -- it is built lazily by
# NestedTestEnvironment.clone_init, see there.
shutil.copytree(upstream_template, test_env.upstream)
yield test_env


Expand Down
Loading
Loading