repos: submodule links must stay in the repo; import checks before it moves

* A submodule's working-tree `.git` names the git directory git uses for
  it.  Before any submodule command (update, or deinit when submodules
  are turned off), and before an import, every `.git` below the top of
  the working tree must be a file pointing inside the repo's own
  .git/modules; a link, a directory, or a file pointing elsewhere could
  make git use a config that was never checked.
* Import validates and sanitizes the clone where it is, then moves it,
  and moves it back if marking it fails, so a refused or failed import
  never leaves a clone moved without a Repo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-10-06 19:03:07 -06:00
parent d0ea6ab313
commit 4b034ae3ee
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
4 changed files with 141 additions and 14 deletions

View file

@ -207,3 +207,33 @@ async def test_import_refuses(test_session, test_directory, async_client, test_w
repo = await lib.import_repository(test_session, 'code/ssh-clone', url='https://git.example.com/owner/library',
confirm=True)
assert repo.url == 'https://git.example.com/owner/library'
@pytest.mark.asyncio
async def test_import_checks_before_moving(test_session, test_directory, async_client, test_wrolpi_config,
user_clone):
"""Everything which can refuse an import is checked before the clone is moved."""
directory, _ = user_clone()
(directory / 'other').mkdir()
(directory / 'other/.git').write_text(f'gitdir: {test_directory}\n')
with pytest.raises(InvalidRepo, match='outside'):
await lib.import_repository(test_session, 'code/kiwix-tools', confirm=True)
test_session.rollback()
assert (directory / '.git').is_dir()
assert not (test_directory / 'repos/kiwix-tools').exists()
assert test_session.query(Repository).count() == 0
@pytest.mark.asyncio
async def test_import_moves_back_on_failure(test_session, test_directory, async_client, test_wrolpi_config,
user_clone):
"""If the import fails after the move, the clone is moved back."""
from mock import mock
directory, _ = user_clone()
with mock.patch('modules.repos.lib.mark_clone', side_effect=OSError('disk full')):
with pytest.raises(OSError):
await lib.import_repository(test_session, 'code/kiwix-tools', confirm=True)
test_session.rollback()
assert (directory / '.git').is_dir()
assert not (test_directory / 'repos/kiwix-tools').exists()
assert test_session.query(Repository).count() == 0