mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
repos: hold submodule git directories to the same rules as the repo's
The checks covered .git but not the submodules' git directories in .git/modules: a linked submodule config was skipped (and still read by git), and a submodule's commondir or alternates were not refused. Every git directory now needs a regular config and must not borrow another directory, and nothing under .git/modules may be a link; otherwise the update fails. A git directory is one with HEAD and objects (a ref named HEAD is not one). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d3cf1b7282
commit
9e8947767b
2 changed files with 73 additions and 5 deletions
|
|
@ -426,15 +426,29 @@ def _sanitize_config_file(config_file: pathlib.Path, worktree_root: Optional[pat
|
|||
|
||||
|
||||
def sanitize_git_config(directory: pathlib.Path):
|
||||
"""Keep only the settings WROLPi needs in the configs of the repo in `directory` (and its submodules)."""
|
||||
"""Keep only the settings WROLPi needs in the configs of the repo in `directory`, and of its submodules.
|
||||
|
||||
Every git directory (the repo's, and each submodule's in .git/modules) is held to the same rules: no links, a
|
||||
regular config file, and nothing which makes git use another directory. Raises ValueError otherwise."""
|
||||
git_dir = directory / '.git'
|
||||
root = directory.resolve()
|
||||
_sanitize_config_file(git_dir / 'config')
|
||||
|
||||
modules = git_dir / 'modules'
|
||||
if modules.is_dir() and not modules.is_symlink():
|
||||
for config_file in modules.rglob('config'):
|
||||
if config_file.is_file() and not config_file.is_symlink() and (config_file.parent / 'HEAD').exists():
|
||||
_sanitize_config_file(config_file, worktree_root=root)
|
||||
if modules.is_symlink():
|
||||
raise ValueError(f'{modules} is a link')
|
||||
if not modules.is_dir():
|
||||
return
|
||||
for path in modules.rglob('*'):
|
||||
if path.is_symlink():
|
||||
# git would follow it out of the repo.
|
||||
raise ValueError(f'{path} is a link')
|
||||
for submodule_git_dir in sorted({i.parent for i in modules.rglob('HEAD')}):
|
||||
if not (submodule_git_dir / 'objects').is_dir():
|
||||
continue # e.g. refs/remotes/origin/HEAD, a ref, not a git directory.
|
||||
if borrowed := _borrowing_git_file(submodule_git_dir):
|
||||
raise ValueError(f'{submodule_git_dir} uses another git directory ({borrowed})')
|
||||
_sanitize_config_file(submodule_git_dir / 'config', worktree_root=root)
|
||||
|
||||
|
||||
def find_readme(directory: pathlib.Path) -> Optional[str]:
|
||||
|
|
|
|||
|
|
@ -125,6 +125,7 @@ def test_sanitize_git_config_submodules(tmp_path):
|
|||
(evil / 'config').write_text('[core]\n\tworktree = /etc\n')
|
||||
for gitdir in (module, evil):
|
||||
(gitdir / 'HEAD').write_text('ref: refs/heads/main\n')
|
||||
(gitdir / 'objects').mkdir()
|
||||
|
||||
lib.sanitize_git_config(repo)
|
||||
assert 'worktree = ../../../vendor/library' in (module / 'config').read_text()
|
||||
|
|
@ -254,3 +255,56 @@ async def test_clone_cannot_borrow_another_git_directory(test_session, test_dire
|
|||
download = await update(test_session, repos_download_manager)
|
||||
assert download.status == DownloadStatus.deferred
|
||||
assert 'another git directory' in download.error
|
||||
|
||||
|
||||
@pytest.mark.parametrize('plant', ['config-link', 'commondir', 'alternates', 'modules-link', 'gitdir-link'])
|
||||
def test_submodule_git_directories_are_checked_too(tmp_path, plant):
|
||||
"""A submodule's git directory is held to the same rules as the repo's: no linked config, no borrowing."""
|
||||
repo = tmp_path / 'repo'
|
||||
git_dir = repo / '.git'
|
||||
git_dir.mkdir(parents=True)
|
||||
(git_dir / 'config').write_text('[core]\n\tbare = false\n')
|
||||
victim = tmp_path / 'victim'
|
||||
victim.mkdir()
|
||||
(victim / 'config').write_text('[core]\n\tfsmonitor = evil\n')
|
||||
|
||||
module = git_dir / 'modules/library'
|
||||
(module / 'objects').mkdir(parents=True)
|
||||
(module / 'HEAD').write_text('ref: refs/heads/main\n')
|
||||
(module / 'config').write_text('[core]\n\tbare = false\n')
|
||||
# A ref named HEAD is not a git directory.
|
||||
(module / 'refs/remotes/origin').mkdir(parents=True)
|
||||
(module / 'refs/remotes/origin/HEAD').write_text('ref: refs/remotes/origin/main\n')
|
||||
|
||||
if plant == 'config-link':
|
||||
(module / 'config').unlink()
|
||||
(module / 'config').symlink_to(victim / 'config')
|
||||
elif plant == 'commondir':
|
||||
(module / 'commondir').write_text(f'{victim}\n')
|
||||
elif plant == 'alternates':
|
||||
(module / 'objects/info').mkdir()
|
||||
(module / 'objects/info/alternates').write_text(f'{victim}\n')
|
||||
elif plant == 'modules-link':
|
||||
import shutil
|
||||
shutil.rmtree(git_dir / 'modules')
|
||||
(git_dir / 'modules').symlink_to(victim)
|
||||
elif plant == 'gitdir-link':
|
||||
(git_dir / 'modules/other').symlink_to(victim)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
lib.sanitize_git_config(repo)
|
||||
assert (victim / 'config').read_text() == '[core]\n\tfsmonitor = evil\n'
|
||||
|
||||
|
||||
def test_submodule_git_directories_pass(tmp_path):
|
||||
repo = tmp_path / 'repo'
|
||||
git_dir = repo / '.git'
|
||||
module = git_dir / 'modules/library'
|
||||
(module / 'objects').mkdir(parents=True)
|
||||
(module / 'HEAD').write_text('ref: refs/heads/main\n')
|
||||
(module / 'config').write_text('[core]\n\tbare = false\n[http]\n\tproxy = evil\n')
|
||||
(module / 'refs/remotes/origin').mkdir(parents=True)
|
||||
(module / 'refs/remotes/origin/HEAD').write_text('ref: refs/remotes/origin/main\n')
|
||||
(git_dir / 'config').write_text('[core]\n\tbare = false\n')
|
||||
lib.sanitize_git_config(repo)
|
||||
assert 'proxy' not in (module / 'config').read_text()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue