|
|
|
|
|
|
|
import contextlib |
|
import gc |
|
import os |
|
import os.path as osp |
|
from pathlib import Path |
|
import shutil |
|
import sys |
|
import tempfile |
|
from unittest import mock, skipUnless |
|
|
|
import pytest |
|
|
|
import git |
|
from git.cmd import Git |
|
from git.config import GitConfigParser, cp |
|
from git.exc import ( |
|
GitCommandError, |
|
InvalidGitRepositoryError, |
|
RepositoryDirtyError, |
|
UnsafeOptionError, |
|
UnsafeProtocolError, |
|
) |
|
from git.objects.submodule.base import Submodule |
|
from git.objects.submodule.root import RootModule, RootUpdateProgress |
|
from git.repo.fun import find_submodule_git_dir, touch |
|
from git.util import HIDE_WINDOWS_KNOWN_ERRORS, join_path_native, to_native_path_linux |
|
|
|
from test.lib import TestBase, with_rw_directory, with_rw_repo |
|
|
|
|
|
@contextlib.contextmanager |
|
def _patch_git_config(name, value): |
|
"""Temporarily add a git config name-value pair, using environment variables.""" |
|
pair_index = int(os.getenv("GIT_CONFIG_COUNT", "0")) |
|
|
|
|
|
|
|
patcher = mock.patch.dict( |
|
os.environ, |
|
{ |
|
"GIT_CONFIG_COUNT": str(pair_index + 1), |
|
f"GIT_CONFIG_KEY_{pair_index}": name, |
|
f"GIT_CONFIG_VALUE_{pair_index}": value, |
|
}, |
|
) |
|
|
|
with patcher: |
|
yield |
|
|
|
|
|
class TestRootProgress(RootUpdateProgress): |
|
"""Just prints messages, for now without checking the correctness of the states""" |
|
|
|
def update(self, op, cur_count, max_count, message=""): |
|
print(op, cur_count, max_count, message) |
|
|
|
|
|
prog = TestRootProgress() |
|
|
|
|
|
class TestSubmodule(TestBase): |
|
def tearDown(self): |
|
gc.collect() |
|
|
|
k_subm_current = "c15a6e1923a14bc760851913858a3942a4193cdb" |
|
k_subm_changed = "394ed7006ee5dc8bddfd132b64001d5dfc0ffdd3" |
|
k_no_subm_tag = "0.1.6" |
|
|
|
def _do_base_tests(self, rwrepo): |
|
"""Perform all tests in the given repository, it may be bare or nonbare""" |
|
|
|
smm = Submodule(rwrepo, "\0" * 20) |
|
|
|
self.assertRaises(AttributeError, getattr, smm, "name") |
|
|
|
|
|
sms = Submodule.list_items(rwrepo, self.k_subm_current) |
|
assert len(sms) == 1 |
|
sm = sms[0] |
|
|
|
|
|
assert len(Submodule.list_items(rwrepo, self.k_no_subm_tag)) == 0 |
|
|
|
assert sm.path == "git/ext/gitdb" |
|
assert sm.path != sm.name |
|
assert sm.url.endswith("github.com/gitpython-developers/gitdb.git") |
|
assert sm.branch_path == "refs/heads/master" |
|
assert sm.branch_name == "master" |
|
assert sm.parent_commit == rwrepo.head.commit |
|
|
|
assert sm.size == 0 |
|
|
|
self.assertRaises(InvalidGitRepositoryError, sm.module) |
|
|
|
|
|
|
|
self.assertRaises(InvalidGitRepositoryError, getattr, sm, "branch") |
|
|
|
|
|
assert isinstance(sm.branch_path, str) |
|
|
|
|
|
|
|
smold = next(Submodule.iter_items(rwrepo, self.k_subm_changed)) |
|
assert smold.binsha != sm.binsha |
|
assert smold != sm |
|
|
|
|
|
del smold._url |
|
smold.url == sm.url |
|
|
|
|
|
sm.config_reader() |
|
new_smclone_path = None |
|
new_csmclone_path = None |
|
if rwrepo.bare: |
|
with self.assertRaises(InvalidGitRepositoryError): |
|
with sm.config_writer() as cw: |
|
pass |
|
else: |
|
with sm.config_writer() as writer: |
|
|
|
new_smclone_path = Git.polish_url(osp.join(self.rorepo.working_tree_dir, sm.path)) |
|
writer.set_value("url", new_smclone_path) |
|
writer.release() |
|
assert sm.config_reader().get_value("url") == new_smclone_path |
|
assert sm.url == new_smclone_path |
|
|
|
smold.config_reader() |
|
|
|
|
|
if not rwrepo.bare: |
|
with self.assertRaises(ValueError): |
|
with smold.config_writer(): |
|
pass |
|
|
|
|
|
|
|
self.assertRaises(ValueError, smold.set_parent_commit, self.k_subm_current) |
|
|
|
smold.set_parent_commit(self.k_subm_changed + "~1") |
|
assert smold.binsha != sm.binsha |
|
|
|
|
|
|
|
self.assertRaises(ValueError, smold.set_parent_commit, self.k_no_subm_tag) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if rwrepo.bare: |
|
self.assertRaises(InvalidGitRepositoryError, sm.module) |
|
self.assertRaises(InvalidGitRepositoryError, sm.remove) |
|
self.assertRaises(InvalidGitRepositoryError, sm.add, rwrepo, "here", "there") |
|
else: |
|
|
|
self.assertRaises(InvalidGitRepositoryError, sm.module) |
|
assert not sm.module_exists() |
|
|
|
|
|
assert len(list(rwrepo.iter_submodules())) == 1 |
|
assert sm.binsha != "\0" * 20 |
|
|
|
|
|
|
|
|
|
|
|
sma = Submodule.add(rwrepo, sm.name, sm.path) |
|
assert sma.path == sm.path |
|
|
|
|
|
self.assertRaises(ValueError, Submodule.add, rwrepo, "newsubm", "pathtorepo", url=None) |
|
|
|
|
|
|
|
|
|
|
|
newdir = osp.join(sm.abspath, "dir") |
|
os.makedirs(newdir) |
|
|
|
|
|
self.assertRaises(OSError, sm.update) |
|
os.rmdir(newdir) |
|
|
|
|
|
sm.update(dry_run=True, progress=prog) |
|
assert not sm.module_exists() |
|
|
|
assert sm.update() is sm |
|
sm_repopath = sm.path |
|
assert sm.module_exists() |
|
assert isinstance(sm.module(), git.Repo) |
|
assert sm.module().working_tree_dir == sm.abspath |
|
|
|
|
|
|
|
|
|
|
|
self.assertRaises( |
|
ValueError, |
|
Submodule.add, |
|
rwrepo, |
|
"newsubm", |
|
sm.path, |
|
"git://someurl/repo.git", |
|
) |
|
|
|
|
|
|
|
|
|
assert sm.module().head.ref.tracking_branch() is not None |
|
|
|
|
|
assert len(sm.children()) != 0 |
|
|
|
sm.remove(force=True, configuration=False) |
|
assert len(sm.children()) == 0 |
|
|
|
sm.update(dry_run=True, recursive=False, progress=prog) |
|
assert len(sm.children()) == 0 |
|
|
|
sm.update(recursive=False) |
|
assert len(list(rwrepo.iter_submodules())) == 2 |
|
assert len(sm.children()) == 1 |
|
csm = sm.children()[0] |
|
assert not csm.module_exists() |
|
csm_repopath = csm.path |
|
|
|
|
|
|
|
new_csmclone_path = Git.polish_url(osp.join(self.rorepo.working_tree_dir, sm.path, csm.path)) |
|
with csm.config_writer() as writer: |
|
writer.set_value("url", new_csmclone_path) |
|
assert csm.url == new_csmclone_path |
|
|
|
|
|
assert not csm.module_exists() |
|
sm.update(recursive=True, dry_run=True, progress=prog) |
|
assert not csm.module_exists() |
|
|
|
|
|
sm.update(recursive=True) |
|
assert csm.module_exists() |
|
|
|
|
|
assert csm.module().head.ref.tracking_branch() is not None |
|
|
|
|
|
assert len(list(rwrepo.iter_submodules())) == 2 |
|
|
|
|
|
|
|
smods = (sm.module(), csm.module()) |
|
for repo in smods: |
|
repo.head.reset("HEAD~2", working_tree=1) |
|
|
|
|
|
|
|
self.assertRaises( |
|
RepositoryDirtyError, |
|
sm.update, |
|
recursive=True, |
|
dry_run=True, |
|
progress=prog, |
|
) |
|
sm.update(recursive=True, dry_run=True, progress=prog, force=True) |
|
for repo in smods: |
|
assert repo.head.commit != repo.head.ref.tracking_branch().commit |
|
|
|
|
|
self.assertRaises(RepositoryDirtyError, sm.update, recursive=True, to_latest_revision=True) |
|
sm.update(recursive=True, to_latest_revision=True, force=True) |
|
for repo in smods: |
|
assert repo.head.commit == repo.head.ref.tracking_branch().commit |
|
|
|
del smods |
|
|
|
|
|
smref = sm.module().head.ref |
|
sm.module().head.ref = "HEAD~1" |
|
|
|
csm_tracking_branch = csm.module().head.ref.tracking_branch() |
|
csm.module().head.ref.set_tracking_branch(None) |
|
sm.update(recursive=True, to_latest_revision=True) |
|
|
|
|
|
|
|
csm.set_parent_commit(csm.repo.head.commit) |
|
|
|
|
|
sm.module().head.ref = smref |
|
csm.module().head.ref.set_tracking_branch(csm_tracking_branch) |
|
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, csm.remove, module=False, configuration=False) |
|
|
|
|
|
|
|
csm.set_parent_commit(csm.repo.head.commit) |
|
with csm.config_writer() as cw: |
|
cw.set_value("url", self._small_repo_url()) |
|
csm.repo.index.commit("adjusted URL to point to local source, instead of the internet") |
|
|
|
|
|
|
|
|
|
|
|
csm.set_parent_commit(csm.repo.head.commit) |
|
with sm.config_writer() as writer: |
|
writer.set_value("somekey", "somevalue") |
|
with csm.config_writer() as writer: |
|
writer.set_value("okey", "ovalue") |
|
self.assertRaises(InvalidGitRepositoryError, sm.remove) |
|
|
|
sm.module().index.reset() |
|
|
|
self.assertRaises(InvalidGitRepositoryError, sm.remove, dry_run=True) |
|
sm.module().index.reset(working_tree=True) |
|
|
|
|
|
csm.update() |
|
assert csm.module_exists() |
|
assert csm.exists() |
|
assert osp.isdir(csm.module().working_tree_dir) |
|
|
|
|
|
assert sm.remove(force=True, dry_run=True) is sm |
|
assert sm.module_exists() |
|
sm.remove(force=True, dry_run=True) |
|
assert sm.module_exists() |
|
|
|
|
|
fn = join_path_native(csm.module().working_tree_dir, "newfile") |
|
with open(fn, "w") as fd: |
|
fd.write("hi") |
|
self.assertRaises(InvalidGitRepositoryError, sm.remove) |
|
|
|
|
|
prev_count = len(sm.children()) |
|
self.assertRaises(ValueError, csm.remove, force=True) |
|
|
|
|
|
csm.set_parent_commit(csm.repo.commit(), check=False) |
|
assert not csm.exists() |
|
assert not csm.module_exists() |
|
assert len(sm.children()) == prev_count |
|
|
|
|
|
sm.module().index.reset(working_tree=True) |
|
|
|
|
|
assert sm.module_exists() |
|
sm.remove(configuration=False, force=True) |
|
assert sm.exists() |
|
assert not sm.module_exists() |
|
assert sm.config_reader().get_value("url") |
|
|
|
|
|
sm_path = sm.path |
|
sm.remove() |
|
assert not sm.exists() |
|
assert not sm.module_exists() |
|
self.assertRaises(ValueError, getattr, sm, "path") |
|
|
|
assert len(rwrepo.submodules) == 0 |
|
|
|
|
|
|
|
|
|
smid = "newsub" |
|
osmid = "othersub" |
|
nsm = Submodule.add( |
|
rwrepo, |
|
smid, |
|
sm_repopath, |
|
new_smclone_path + "/", |
|
None, |
|
no_checkout=True, |
|
) |
|
assert nsm.name == smid |
|
assert nsm.module_exists() |
|
assert nsm.exists() |
|
|
|
assert not osp.isfile(join_path_native(nsm.module().working_tree_dir, Submodule.k_modules_file)) |
|
assert len(rwrepo.submodules) == 1 |
|
|
|
|
|
osm = Submodule.add(rwrepo, osmid, csm_repopath, new_csmclone_path, Submodule.k_head_default) |
|
assert osm != nsm |
|
assert osm.module_exists() |
|
assert osm.exists() |
|
assert osp.isfile(join_path_native(osm.module().working_tree_dir, "setup.py")) |
|
|
|
assert len(rwrepo.submodules) == 2 |
|
|
|
|
|
rwrepo.index.commit("my submod commit") |
|
assert len(rwrepo.submodules) == 2 |
|
|
|
|
|
|
|
nsm.set_parent_commit(rwrepo.head.commit) |
|
osm.set_parent_commit(rwrepo.head.commit) |
|
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, nsm.move, "doesntmatter", module=False, configuration=False) |
|
|
|
|
|
assert nsm.move(sm_path) is nsm |
|
|
|
|
|
nmp = join_path_native("new", "module", "dir") + "/" |
|
pmp = nsm.path |
|
assert nsm.move(nmp) is nsm |
|
nmp = nmp[:-1] |
|
nmpl = to_native_path_linux(nmp) |
|
assert nsm.path == nmpl |
|
assert rwrepo.submodules[0].path == nmpl |
|
|
|
mpath = "newsubmodule" |
|
absmpath = join_path_native(rwrepo.working_tree_dir, mpath) |
|
open(absmpath, "w").write("") |
|
self.assertRaises(ValueError, nsm.move, mpath) |
|
os.remove(absmpath) |
|
|
|
|
|
nsm.move(pmp) |
|
assert nsm.path == pmp |
|
assert rwrepo.submodules[0].path == pmp |
|
|
|
|
|
|
|
|
|
|
|
osmod = osm.module() |
|
|
|
osm.remove(module=False) |
|
for remote in osmod.remotes: |
|
remote.remove(osmod, remote.name) |
|
assert not osm.exists() |
|
self.assertRaises(ValueError, Submodule.add, rwrepo, osmid, csm_repopath, url=None) |
|
|
|
|
|
|
|
self.assertRaises( |
|
IOError, |
|
Submodule._config_parser, |
|
rwrepo, |
|
rwrepo.commit(self.k_no_subm_tag), |
|
True, |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@with_rw_repo(k_subm_current) |
|
def test_base_rw(self, rwrepo): |
|
self._do_base_tests(rwrepo) |
|
|
|
@with_rw_repo(k_subm_current, bare=True) |
|
def test_base_bare(self, rwrepo): |
|
self._do_base_tests(rwrepo) |
|
|
|
@pytest.mark.xfail( |
|
sys.platform == "cygwin", |
|
reason="Cygwin GitPython can't find submodule SHA", |
|
raises=ValueError, |
|
) |
|
@pytest.mark.xfail( |
|
HIDE_WINDOWS_KNOWN_ERRORS, |
|
reason=( |
|
'"The process cannot access the file because it is being used by another process"' |
|
+ " on first call to rm.update" |
|
), |
|
raises=PermissionError, |
|
) |
|
@with_rw_repo(k_subm_current, bare=False) |
|
def test_root_module(self, rwrepo): |
|
|
|
rm = RootModule(self.rorepo) |
|
assert rm.module() is self.rorepo |
|
|
|
|
|
rm.binsha |
|
rm.mode |
|
rm.path |
|
assert rm.name == rm.k_root_name |
|
assert rm.parent_commit == self.rorepo.head.commit |
|
rm.url |
|
rm.branch |
|
|
|
assert len(rm.list_items(rm.module())) == 1 |
|
rm.config_reader() |
|
with rm.config_writer(): |
|
pass |
|
|
|
|
|
rsmsp = [sm.path for sm in rm.traverse()] |
|
assert len(rsmsp) >= 2 |
|
|
|
|
|
self.assertRaises(ValueError, rm.set_parent_commit, "HEAD") |
|
|
|
|
|
|
|
|
|
|
|
rm = RootModule(rwrepo) |
|
assert len(rm.children()) == 1 |
|
|
|
|
|
|
|
|
|
sm = rm.children()[0] |
|
pp = "path/prefix" |
|
fp = join_path_native(pp, sm.path) |
|
prep = sm.path |
|
assert not sm.module_exists() |
|
|
|
|
|
with sm.config_writer() as writer: |
|
writer.set_value("url", Git.polish_url(osp.join(self.rorepo.working_tree_dir, sm.path))) |
|
|
|
|
|
sm.update(recursive=False, dry_run=True, progress=prog) |
|
assert not sm.module_exists() |
|
|
|
sm.update(recursive=False) |
|
assert sm.module_exists() |
|
with sm.config_writer() as writer: |
|
|
|
writer.set_value("path", fp) |
|
|
|
|
|
|
|
rm.update(recursive=False) |
|
|
|
|
|
|
|
self.assertRaises(InvalidGitRepositoryError, sm.move, pp) |
|
|
|
sm.path = prep |
|
sm.move(fp, module=False) |
|
|
|
assert not sm.module_exists() |
|
cpathchange = rwrepo.index.commit("changed sm path") |
|
|
|
|
|
rm.update(recursive=False, progress=prog) |
|
sm.set_parent_commit(cpathchange) |
|
assert sm.module_exists() |
|
|
|
|
|
|
|
nsmn = "newsubmodule" |
|
nsmp = "submrepo" |
|
subrepo_url = Git.polish_url(osp.join(self.rorepo.working_tree_dir, rsmsp[0], rsmsp[1])) |
|
nsm = Submodule.add(rwrepo, nsmn, nsmp, url=subrepo_url) |
|
csmadded = rwrepo.index.commit("Added submodule").hexsha |
|
nsm.set_parent_commit(csmadded) |
|
assert nsm.module_exists() |
|
|
|
|
|
nsm.remove(configuration=False, module=True) |
|
assert not nsm.module_exists() and nsm.exists() |
|
|
|
|
|
rm.update(recursive=False, dry_run=True, progress=prog) |
|
|
|
|
|
rm.update(recursive=False, progress=prog) |
|
assert nsm.module_exists() |
|
|
|
|
|
|
|
sm.set_parent_commit(csmadded) |
|
smp = sm.abspath |
|
assert not sm.remove(module=False).exists() |
|
assert osp.isdir(smp) |
|
csmremoved = rwrepo.index.commit("Removed submodule") |
|
|
|
|
|
|
|
rm.update(recursive=False, dry_run=True, force_remove=True) |
|
assert osp.isdir(smp) |
|
|
|
|
|
|
|
|
|
|
|
self.assertRaises(InvalidGitRepositoryError, rm.update, recursive=False, force_remove=False) |
|
rm.update(recursive=False, force_remove=True) |
|
assert not osp.isdir(smp) |
|
|
|
|
|
|
|
|
|
touch(osp.join(nsm.module().working_tree_dir, "new-file")) |
|
|
|
|
|
assert nsm.module().head.commit.hexsha == nsm.hexsha |
|
nsm.module().index.add([nsm]) |
|
nsm.module().index.commit("added new file") |
|
rm.update(recursive=False, dry_run=True, progress=prog) |
|
|
|
|
|
|
|
|
|
prev_commit = nsm.module().head.commit |
|
rm.update(recursive=False, dry_run=False, progress=prog) |
|
assert prev_commit == nsm.module().head.commit, "head shouldn't change, as it is in future of remote branch" |
|
|
|
|
|
rm.update(recursive=True, progress=prog, force_reset=True) |
|
assert prev_commit != nsm.module().head.commit, "head changed, as the remote url and its commit changed" |
|
|
|
|
|
|
|
|
|
|
|
nsm.set_parent_commit(csmremoved) |
|
nsmurl = Git.polish_url(osp.join(self.rorepo.working_tree_dir, rsmsp[0])) |
|
with nsm.config_writer() as writer: |
|
writer.set_value("url", nsmurl) |
|
csmpathchange = rwrepo.index.commit("changed url") |
|
nsm.set_parent_commit(csmpathchange) |
|
|
|
|
|
prev_commit = nsm.module().head.commit |
|
|
|
rm.update(recursive=False, dry_run=True, progress=prog) |
|
assert nsm.module().remotes.origin.url != nsmurl |
|
|
|
rm.update(recursive=False, progress=prog, force_reset=True) |
|
assert nsm.module().remotes.origin.url == nsmurl |
|
assert prev_commit != nsm.module().head.commit, "Should now point to gitdb" |
|
assert len(rwrepo.submodules) == 1 |
|
assert not rwrepo.submodules[0].children()[0].module_exists(), "nested submodule should not be checked out" |
|
|
|
|
|
|
|
nsm.binsha = nsm.module().head.commit.binsha |
|
rwrepo.index.add([nsm]) |
|
|
|
|
|
|
|
|
|
|
|
cur_branch = nsm.branch |
|
nsmm = nsm.module() |
|
prev_commit = nsmm.head.commit |
|
for branch in ("some_virtual_branch", cur_branch.name): |
|
with nsm.config_writer() as writer: |
|
writer.set_value(Submodule.k_head_option, git.Head.to_full_path(branch)) |
|
csmbranchchange = rwrepo.index.commit("changed branch to %s" % branch) |
|
nsm.set_parent_commit(csmbranchchange) |
|
|
|
|
|
|
|
nsmmh = nsmm.head |
|
assert nsmmh.ref.tracking_branch() is None |
|
assert not nsmmh.is_detached |
|
|
|
|
|
rm.update(recursive=False, dry_run=True, progress=prog) |
|
assert nsmmh.ref.tracking_branch() is None |
|
|
|
|
|
rm.update(recursive=False, progress=prog) |
|
|
|
assert nsmmh.ref.tracking_branch() is not None |
|
assert not nsmmh.is_detached |
|
|
|
|
|
|
|
|
|
|
|
assert len(nsm.children()) >= 1 |
|
assert nsm.exists() and nsm.module_exists() and len(nsm.children()) >= 1 |
|
|
|
nsmc = nsm.children()[0] |
|
with nsmc.config_writer() as writer: |
|
writer.set_value("url", subrepo_url) |
|
rm.update(recursive=True, progress=prog, dry_run=True) |
|
rm.update(recursive=True, progress=prog) |
|
|
|
|
|
assert len(nsm.children()) >= 1 and nsmc.module_exists() |
|
|
|
def test_iter_items_from_nonexistent_hash(self): |
|
it = Submodule.iter_items(self.rorepo, "b4ecbfaa90c8be6ed6d9fb4e57cc824663ae15b4") |
|
with self.assertRaisesRegex(ValueError, r"\bcould not be resolved\b"): |
|
next(it) |
|
|
|
def test_iter_items_from_invalid_hash(self): |
|
"""Check legacy behavaior on BadName (also applies to IOError, i.e. OSError).""" |
|
it = Submodule.iter_items(self.rorepo, "xyz") |
|
with self.assertRaises(StopIteration) as ctx: |
|
next(it) |
|
self.assertIsNone(ctx.exception.value) |
|
|
|
@with_rw_repo(k_no_subm_tag, bare=False) |
|
def test_first_submodule(self, rwrepo): |
|
assert len(list(rwrepo.iter_submodules())) == 0 |
|
|
|
for sm_name, sm_path in ( |
|
("first", "submodules/first"), |
|
("second", osp.join(rwrepo.working_tree_dir, "submodules/second")), |
|
): |
|
sm = rwrepo.create_submodule(sm_name, sm_path, rwrepo.git_dir, no_checkout=True) |
|
assert sm.exists() and sm.module_exists() |
|
rwrepo.index.commit("Added submodule " + sm_name) |
|
|
|
|
|
self.assertRaises(ValueError, rwrepo.create_submodule, "fail", osp.expanduser("~")) |
|
self.assertRaises( |
|
ValueError, |
|
rwrepo.create_submodule, |
|
"fail-too", |
|
rwrepo.working_tree_dir + osp.sep, |
|
) |
|
|
|
@with_rw_directory |
|
def test_add_empty_repo(self, rwdir): |
|
empty_repo_dir = osp.join(rwdir, "empty-repo") |
|
|
|
parent = git.Repo.init(osp.join(rwdir, "parent")) |
|
git.Repo.init(empty_repo_dir) |
|
|
|
for checkout_mode in range(2): |
|
name = "empty" + str(checkout_mode) |
|
self.assertRaises( |
|
ValueError, |
|
parent.create_submodule, |
|
name, |
|
name, |
|
url=empty_repo_dir, |
|
no_checkout=checkout_mode and True or False, |
|
) |
|
|
|
|
|
@with_rw_directory |
|
@_patch_git_config("protocol.file.allow", "always") |
|
def test_list_only_valid_submodules(self, rwdir): |
|
repo_path = osp.join(rwdir, "parent") |
|
repo = git.Repo.init(repo_path) |
|
repo.git.submodule("add", self._small_repo_url(), "module") |
|
repo.index.commit("add submodule") |
|
|
|
assert len(repo.submodules) == 1 |
|
|
|
|
|
submodule_path = osp.join(repo_path, "module") |
|
shutil.rmtree(submodule_path) |
|
repo.git.add([submodule_path]) |
|
repo.index.commit("remove submodule") |
|
|
|
repo = git.Repo(repo_path) |
|
assert len(repo.submodules) == 0 |
|
|
|
@pytest.mark.xfail( |
|
HIDE_WINDOWS_KNOWN_ERRORS, |
|
reason=( |
|
'"The process cannot access the file because it is being used by another process"' |
|
+ " on first call to sm.move" |
|
), |
|
raises=PermissionError, |
|
) |
|
@with_rw_directory |
|
@_patch_git_config("protocol.file.allow", "always") |
|
def test_git_submodules_and_add_sm_with_new_commit(self, rwdir): |
|
parent = git.Repo.init(osp.join(rwdir, "parent")) |
|
parent.git.submodule("add", self._small_repo_url(), "module") |
|
parent.index.commit("added submodule") |
|
|
|
assert len(parent.submodules) == 1 |
|
sm = parent.submodules[0] |
|
|
|
assert sm.exists() and sm.module_exists() |
|
|
|
clone = git.Repo.clone_from( |
|
self._small_repo_url(), |
|
osp.join(parent.working_tree_dir, "existing-subrepository"), |
|
) |
|
sm2 = parent.create_submodule("nongit-file-submodule", clone.working_tree_dir) |
|
assert len(parent.submodules) == 2 |
|
|
|
for _ in range(2): |
|
for init in (False, True): |
|
sm.update(init=init) |
|
sm2.update(init=init) |
|
|
|
|
|
|
|
sm.move(sm.path + "_moved") |
|
sm2.move(sm2.path + "_moved") |
|
|
|
parent.index.commit("moved submodules") |
|
|
|
with sm.config_writer() as writer: |
|
writer.set_value("user.email", "example@example.com") |
|
writer.set_value("user.name", "me") |
|
smm = sm.module() |
|
fp = osp.join(smm.working_tree_dir, "empty-file") |
|
with open(fp, "w"): |
|
pass |
|
smm.git.add(Git.polish_url(fp)) |
|
smm.git.commit(m="new file added") |
|
|
|
|
|
|
|
sm_too = parent.submodules["module_moved"] |
|
assert parent.head.commit.tree[sm.path].binsha == sm.binsha |
|
assert sm_too.binsha == sm.binsha, "cached submodule should point to the same commit as updated one" |
|
|
|
added_bies = parent.index.add([sm]) |
|
assert len(added_bies) == 1 |
|
parent.index.commit("add same submodule entry") |
|
commit_sm = parent.head.commit.tree[sm.path] |
|
assert commit_sm.binsha == added_bies[0].binsha |
|
assert commit_sm.binsha == sm.binsha |
|
|
|
sm_too.binsha = sm_too.module().head.commit.binsha |
|
added_bies = parent.index.add([sm_too]) |
|
assert len(added_bies) == 1 |
|
parent.index.commit("add new submodule entry") |
|
commit_sm = parent.head.commit.tree[sm.path] |
|
assert commit_sm.binsha == added_bies[0].binsha |
|
assert commit_sm.binsha == sm_too.binsha |
|
assert sm_too.binsha != sm.binsha |
|
|
|
@pytest.mark.xfail( |
|
HIDE_WINDOWS_KNOWN_ERRORS, |
|
reason='"The process cannot access the file because it is being used by another process" on call to sm.move', |
|
raises=PermissionError, |
|
) |
|
@with_rw_directory |
|
def test_git_submodule_compatibility(self, rwdir): |
|
parent = git.Repo.init(osp.join(rwdir, "parent")) |
|
sm_path = join_path_native("submodules", "intermediate", "one") |
|
sm = parent.create_submodule("mymodules/myname", sm_path, url=self._small_repo_url()) |
|
parent.index.commit("added submodule") |
|
|
|
def assert_exists(sm, value=True): |
|
assert sm.exists() == value |
|
assert sm.module_exists() == value |
|
|
|
|
|
|
|
|
|
|
|
|
|
assert len(parent.git.submodule().splitlines()) == 1 |
|
|
|
module_repo_path = osp.join(sm.module().working_tree_dir, ".git") |
|
assert module_repo_path.startswith(osp.join(parent.working_tree_dir, sm_path)) |
|
if not sm._need_gitfile_submodules(parent.git): |
|
assert osp.isdir(module_repo_path) |
|
assert not sm.module().has_separate_working_tree() |
|
else: |
|
assert osp.isfile(module_repo_path) |
|
assert sm.module().has_separate_working_tree() |
|
assert find_submodule_git_dir(module_repo_path) is not None, "module pointed to by .git file must be valid" |
|
|
|
|
|
|
|
new_sm_path = join_path_native("submodules", "one") |
|
sm.move(new_sm_path) |
|
assert_exists(sm) |
|
|
|
|
|
csm = sm.module().create_submodule( |
|
"nested-submodule", |
|
join_path_native("nested-submodule", "working-tree"), |
|
url=self._small_repo_url(), |
|
) |
|
sm.module().index.commit("added nested submodule") |
|
sm_head_commit = sm.module().commit() |
|
assert_exists(csm) |
|
|
|
|
|
self.assertRaises(InvalidGitRepositoryError, sm.remove, dry_run=True) |
|
assert_exists(sm) |
|
assert sm.module().commit() == sm_head_commit |
|
assert_exists(csm) |
|
|
|
|
|
|
|
|
|
new_name = csm.name + "/mine" |
|
assert csm.rename(new_name).name == new_name |
|
assert_exists(csm) |
|
assert csm.repo.is_dirty(index=True, working_tree=False), "index must contain changed .gitmodules file" |
|
csm.repo.index.commit("renamed module") |
|
|
|
|
|
rsm = parent.submodule_update() |
|
assert_exists(sm) |
|
assert_exists(csm) |
|
with csm.config_writer().set_value("url", "bar"): |
|
pass |
|
csm.repo.index.commit("Have to commit submodule change for algorithm to pick it up") |
|
assert csm.url == "bar" |
|
|
|
self.assertRaises( |
|
Exception, |
|
rsm.update, |
|
recursive=True, |
|
to_latest_revision=True, |
|
progress=prog, |
|
) |
|
assert_exists(csm) |
|
rsm.update(recursive=True, to_latest_revision=True, progress=prog, keep_going=True) |
|
|
|
|
|
sm_module_path = sm.module().git_dir |
|
|
|
for dry_run in (True, False): |
|
sm.remove(dry_run=dry_run, force=True) |
|
assert_exists(sm, value=dry_run) |
|
assert osp.isdir(sm_module_path) == dry_run |
|
|
|
|
|
@with_rw_directory |
|
def test_ignore_non_submodule_file(self, rwdir): |
|
parent = git.Repo.init(rwdir) |
|
|
|
smp = osp.join(rwdir, "module") |
|
os.mkdir(smp) |
|
|
|
with open(osp.join(smp, "a"), "w", encoding="utf-8") as f: |
|
f.write("test\n") |
|
|
|
with open(osp.join(rwdir, ".gitmodules"), "w", encoding="utf-8") as f: |
|
f.write('[submodule "a"]\n') |
|
f.write(" path = module\n") |
|
f.write(" url = https://github.com/chaconinc/DbConnector\n") |
|
|
|
parent.git.add(Git.polish_url(osp.join(smp, "a"))) |
|
parent.git.add(Git.polish_url(osp.join(rwdir, ".gitmodules"))) |
|
|
|
parent.git.commit(message="test") |
|
|
|
assert len(parent.submodules) == 0 |
|
|
|
@with_rw_directory |
|
def test_remove_norefs(self, rwdir): |
|
parent = git.Repo.init(osp.join(rwdir, "parent")) |
|
sm_name = "mymodules/myname" |
|
sm = parent.create_submodule(sm_name, sm_name, url=self._small_repo_url()) |
|
assert sm.exists() |
|
|
|
parent.index.commit("Added submodule") |
|
|
|
assert sm.repo is parent |
|
|
|
smrepo = git.Repo(osp.join(rwdir, "parent", sm.path)) |
|
|
|
smrepo.create_remote("special", "git@server-shouldnotmatter:repo.git") |
|
|
|
sm.remove() |
|
assert not sm.exists() |
|
|
|
@with_rw_directory |
|
def test_rename(self, rwdir): |
|
parent = git.Repo.init(osp.join(rwdir, "parent")) |
|
sm_name = "mymodules/myname" |
|
sm = parent.create_submodule(sm_name, sm_name, url=self._small_repo_url()) |
|
parent.index.commit("Added submodule") |
|
|
|
assert sm.rename(sm_name) is sm and sm.name == sm_name |
|
assert not sm.repo.is_dirty(index=True, working_tree=False, untracked_files=False) |
|
|
|
|
|
|
|
|
|
if sys.platform == "win32" and sys.version_info >= (3, 12): |
|
gc.collect() |
|
|
|
new_path = "renamed/myname" |
|
assert sm.move(new_path).name == new_path |
|
|
|
new_sm_name = "shortname" |
|
assert sm.rename(new_sm_name) is sm |
|
assert sm.repo.is_dirty(index=True, working_tree=False, untracked_files=False) |
|
assert sm.exists() |
|
|
|
sm_mod = sm.module() |
|
if osp.isfile(osp.join(sm_mod.working_tree_dir, ".git")) == sm._need_gitfile_submodules(parent.git): |
|
assert sm_mod.git_dir.endswith(join_path_native(".git", "modules", new_sm_name)) |
|
|
|
@with_rw_directory |
|
def test_branch_renames(self, rw_dir): |
|
|
|
|
|
source_url = self._small_repo_url() |
|
sm_source_repo = git.Repo.clone_from(source_url, osp.join(rw_dir, "sm-source"), b="master") |
|
parent_repo = git.Repo.init(osp.join(rw_dir, "parent")) |
|
sm = parent_repo.create_submodule( |
|
"mysubmodule", |
|
"subdir/submodule", |
|
sm_source_repo.working_tree_dir, |
|
branch="master", |
|
) |
|
parent_repo.index.commit("added submodule") |
|
assert sm.exists() |
|
|
|
|
|
sm_fb = sm_source_repo.create_head("feature") |
|
sm_fb.checkout() |
|
new_file = touch(osp.join(sm_source_repo.working_tree_dir, "new-file")) |
|
sm_source_repo.index.add([new_file]) |
|
sm.repo.index.commit("added new file") |
|
|
|
|
|
|
|
with sm.config_writer() as smcw: |
|
smcw.set_value("branch", sm_fb.name) |
|
assert sm.repo.is_dirty(index=True, working_tree=False) |
|
sm.repo.index.commit("changed submodule branch to '%s'" % sm_fb) |
|
|
|
|
|
|
|
sm_mod = sm.module() |
|
prev_commit = sm_mod.commit() |
|
assert sm_mod.head.ref.name == "master" |
|
assert parent_repo.submodule_update() |
|
assert sm_mod.head.ref.name == sm_fb.name |
|
assert sm_mod.commit() == prev_commit, "Without to_latest_revision, we don't change the commit" |
|
|
|
assert parent_repo.submodule_update(to_latest_revision=True) |
|
assert sm_mod.head.ref.name == sm_fb.name |
|
assert sm_mod.commit() == sm_fb.commit |
|
|
|
|
|
|
|
|
|
sm_pfb = sm_source_repo.create_head("past-feature", commit="HEAD~20") |
|
sm_pfb.checkout() |
|
sm_source_repo.index.add([touch(osp.join(sm_source_repo.working_tree_dir, "new-file"))]) |
|
sm_source_repo.index.commit("new file added, to past of '%r'" % sm_fb) |
|
|
|
|
|
with sm.config_writer() as smcw: |
|
smcw.set_value("branch", sm_pfb.path) |
|
sm.repo.index.commit("changed submodule branch to '%s'" % sm_pfb) |
|
|
|
|
|
touch(osp.join(sm_mod.working_tree_dir, "unstaged file")) |
|
|
|
|
|
parent_repo.submodule_update(to_latest_revision=False) |
|
assert sm_mod.head.ref.name == sm_pfb.name, "should have been switched to past head" |
|
assert sm_mod.commit() == sm_fb.commit, "Head wasn't reset" |
|
|
|
self.assertRaises(RepositoryDirtyError, parent_repo.submodule_update, to_latest_revision=True) |
|
parent_repo.submodule_update(to_latest_revision=True, force_reset=True) |
|
assert sm_mod.commit() == sm_pfb.commit, "Now head should have been reset" |
|
assert sm_mod.head.ref.name == sm_pfb.name |
|
|
|
@skipUnless(sys.platform == "win32", "Specifically for Windows.") |
|
def test_to_relative_path_with_super_at_root_drive(self): |
|
class Repo: |
|
working_tree_dir = "D:\\" |
|
|
|
super_repo = Repo() |
|
submodule_path = "D:\\submodule_path" |
|
relative_path = Submodule._to_relative_path(super_repo, submodule_path) |
|
msg = '_to_relative_path should be "submodule_path" but was "%s"' % relative_path |
|
assert relative_path == "submodule_path", msg |
|
|
|
@pytest.mark.xfail( |
|
reason="for some unknown reason the assertion fails, even though it in fact is working in more common setup", |
|
raises=AssertionError, |
|
) |
|
@with_rw_directory |
|
def test_depth(self, rwdir): |
|
parent = git.Repo.init(osp.join(rwdir, "test_depth")) |
|
sm_name = "mymodules/myname" |
|
sm_depth = 1 |
|
sm = parent.create_submodule(sm_name, sm_name, url=self._small_repo_url(), depth=sm_depth) |
|
self.assertEqual(len(list(sm.module().iter_commits())), sm_depth) |
|
|
|
@with_rw_directory |
|
def test_update_clone_multi_options_argument(self, rwdir): |
|
|
|
parent = git.Repo.init(osp.join(rwdir, "parent")) |
|
sm_name = "foo" |
|
sm_url = self._small_repo_url() |
|
sm_branch = "refs/heads/master" |
|
sm_hexsha = git.Repo(self._small_repo_url()).head.commit.hexsha |
|
sm = Submodule( |
|
parent, |
|
bytes.fromhex(sm_hexsha), |
|
name=sm_name, |
|
path=sm_name, |
|
url=sm_url, |
|
branch_path=sm_branch, |
|
) |
|
|
|
|
|
sm.update(init=True, clone_multi_options=["--config core.eol=true"], allow_unsafe_options=True) |
|
|
|
|
|
sm_config = GitConfigParser(file_or_files=osp.join(parent.git_dir, "modules", sm_name, "config")) |
|
self.assertTrue(sm_config.get_value("core", "eol")) |
|
|
|
@with_rw_directory |
|
def test_update_no_clone_multi_options_argument(self, rwdir): |
|
|
|
parent = git.Repo.init(osp.join(rwdir, "parent")) |
|
sm_name = "foo" |
|
sm_url = self._small_repo_url() |
|
sm_branch = "refs/heads/master" |
|
sm_hexsha = git.Repo(self._small_repo_url()).head.commit.hexsha |
|
sm = Submodule( |
|
parent, |
|
bytes.fromhex(sm_hexsha), |
|
name=sm_name, |
|
path=sm_name, |
|
url=sm_url, |
|
branch_path=sm_branch, |
|
) |
|
|
|
|
|
sm.update(init=True) |
|
|
|
|
|
sm_config = GitConfigParser(file_or_files=osp.join(parent.git_dir, "modules", sm_name, "config")) |
|
with self.assertRaises(cp.NoOptionError): |
|
sm_config.get_value("core", "eol") |
|
|
|
@with_rw_directory |
|
def test_add_clone_multi_options_argument(self, rwdir): |
|
|
|
parent = git.Repo.init(osp.join(rwdir, "parent")) |
|
sm_name = "foo" |
|
|
|
|
|
Submodule.add( |
|
parent, |
|
sm_name, |
|
sm_name, |
|
url=self._small_repo_url(), |
|
clone_multi_options=["--config core.eol=true"], |
|
allow_unsafe_options=True, |
|
) |
|
|
|
|
|
sm_config = GitConfigParser(file_or_files=osp.join(parent.git_dir, "modules", sm_name, "config")) |
|
self.assertTrue(sm_config.get_value("core", "eol")) |
|
|
|
@with_rw_directory |
|
def test_add_no_clone_multi_options_argument(self, rwdir): |
|
|
|
parent = git.Repo.init(osp.join(rwdir, "parent")) |
|
sm_name = "foo" |
|
|
|
|
|
Submodule.add(parent, sm_name, sm_name, url=self._small_repo_url()) |
|
|
|
|
|
sm_config = GitConfigParser(file_or_files=osp.join(parent.git_dir, "modules", sm_name, "config")) |
|
with self.assertRaises(cp.NoOptionError): |
|
sm_config.get_value("core", "eol") |
|
|
|
@with_rw_repo("HEAD") |
|
def test_submodule_add_unsafe_url(self, rw_repo): |
|
with tempfile.TemporaryDirectory() as tdir: |
|
tmp_dir = Path(tdir) |
|
tmp_file = tmp_dir / "pwn" |
|
urls = [ |
|
f"ext::sh -c touch% {tmp_file}", |
|
"fd::/foo", |
|
] |
|
for url in urls: |
|
with self.assertRaises(UnsafeProtocolError): |
|
Submodule.add(rw_repo, "new", "new", url) |
|
assert not tmp_file.exists() |
|
|
|
@with_rw_repo("HEAD") |
|
def test_submodule_add_unsafe_url_allowed(self, rw_repo): |
|
with tempfile.TemporaryDirectory() as tdir: |
|
tmp_dir = Path(tdir) |
|
tmp_file = tmp_dir / "pwn" |
|
urls = [ |
|
f"ext::sh -c touch% {tmp_file}", |
|
"fd::/foo", |
|
] |
|
for url in urls: |
|
|
|
|
|
with self.assertRaises(GitCommandError): |
|
Submodule.add(rw_repo, "new", "new", url, allow_unsafe_protocols=True) |
|
assert not tmp_file.exists() |
|
|
|
@with_rw_repo("HEAD") |
|
def test_submodule_add_unsafe_options(self, rw_repo): |
|
with tempfile.TemporaryDirectory() as tdir: |
|
tmp_dir = Path(tdir) |
|
tmp_file = tmp_dir / "pwn" |
|
unsafe_options = [ |
|
f"--upload-pack='touch {tmp_file}'", |
|
f"-u 'touch {tmp_file}'", |
|
"--config=protocol.ext.allow=always", |
|
"-c protocol.ext.allow=always", |
|
] |
|
for unsafe_option in unsafe_options: |
|
with self.assertRaises(UnsafeOptionError): |
|
Submodule.add(rw_repo, "new", "new", str(tmp_dir), clone_multi_options=[unsafe_option]) |
|
assert not tmp_file.exists() |
|
|
|
@with_rw_repo("HEAD") |
|
def test_submodule_add_unsafe_options_allowed(self, rw_repo): |
|
with tempfile.TemporaryDirectory() as tdir: |
|
tmp_dir = Path(tdir) |
|
tmp_file = tmp_dir / "pwn" |
|
unsafe_options = [ |
|
f"--upload-pack='touch {tmp_file}'", |
|
f"-u 'touch {tmp_file}'", |
|
] |
|
for unsafe_option in unsafe_options: |
|
|
|
with self.assertRaises(GitCommandError): |
|
Submodule.add( |
|
rw_repo, |
|
"new", |
|
"new", |
|
str(tmp_dir), |
|
clone_multi_options=[unsafe_option], |
|
allow_unsafe_options=True, |
|
) |
|
assert not tmp_file.exists() |
|
|
|
unsafe_options = [ |
|
"--config=protocol.ext.allow=always", |
|
"-c protocol.ext.allow=always", |
|
] |
|
for unsafe_option in unsafe_options: |
|
with self.assertRaises(GitCommandError): |
|
Submodule.add( |
|
rw_repo, |
|
"new", |
|
"new", |
|
str(tmp_dir), |
|
clone_multi_options=[unsafe_option], |
|
allow_unsafe_options=True, |
|
) |
|
|
|
@with_rw_repo("HEAD") |
|
def test_submodule_update_unsafe_url(self, rw_repo): |
|
with tempfile.TemporaryDirectory() as tdir: |
|
tmp_dir = Path(tdir) |
|
tmp_file = tmp_dir / "pwn" |
|
urls = [ |
|
f"ext::sh -c touch% {tmp_file}", |
|
"fd::/foo", |
|
] |
|
for url in urls: |
|
submodule = Submodule(rw_repo, b"\0" * 20, name="new", path="new", url=url) |
|
with self.assertRaises(UnsafeProtocolError): |
|
submodule.update() |
|
assert not tmp_file.exists() |
|
|
|
@with_rw_repo("HEAD") |
|
def test_submodule_update_unsafe_url_allowed(self, rw_repo): |
|
with tempfile.TemporaryDirectory() as tdir: |
|
tmp_dir = Path(tdir) |
|
tmp_file = tmp_dir / "pwn" |
|
urls = [ |
|
f"ext::sh -c touch% {tmp_file}", |
|
"fd::/foo", |
|
] |
|
for url in urls: |
|
submodule = Submodule(rw_repo, b"\0" * 20, name="new", path="new", url=url) |
|
|
|
|
|
with self.assertRaises(GitCommandError): |
|
submodule.update(allow_unsafe_protocols=True) |
|
assert not tmp_file.exists() |
|
|
|
@with_rw_repo("HEAD") |
|
def test_submodule_update_unsafe_options(self, rw_repo): |
|
with tempfile.TemporaryDirectory() as tdir: |
|
tmp_dir = Path(tdir) |
|
tmp_file = tmp_dir / "pwn" |
|
unsafe_options = [ |
|
f"--upload-pack='touch {tmp_file}'", |
|
f"-u 'touch {tmp_file}'", |
|
"--config=protocol.ext.allow=always", |
|
"-c protocol.ext.allow=always", |
|
] |
|
submodule = Submodule(rw_repo, b"\0" * 20, name="new", path="new", url=str(tmp_dir)) |
|
for unsafe_option in unsafe_options: |
|
with self.assertRaises(UnsafeOptionError): |
|
submodule.update(clone_multi_options=[unsafe_option]) |
|
assert not tmp_file.exists() |
|
|
|
@with_rw_repo("HEAD") |
|
def test_submodule_update_unsafe_options_allowed(self, rw_repo): |
|
with tempfile.TemporaryDirectory() as tdir: |
|
tmp_dir = Path(tdir) |
|
tmp_file = tmp_dir / "pwn" |
|
unsafe_options = [ |
|
f"--upload-pack='touch {tmp_file}'", |
|
f"-u 'touch {tmp_file}'", |
|
] |
|
submodule = Submodule(rw_repo, b"\0" * 20, name="new", path="new", url=str(tmp_dir)) |
|
for unsafe_option in unsafe_options: |
|
|
|
with self.assertRaises(GitCommandError): |
|
submodule.update(clone_multi_options=[unsafe_option], allow_unsafe_options=True) |
|
assert not tmp_file.exists() |
|
|
|
unsafe_options = [ |
|
"--config=protocol.ext.allow=always", |
|
"-c protocol.ext.allow=always", |
|
] |
|
submodule = Submodule(rw_repo, b"\0" * 20, name="new", path="new", url=str(tmp_dir)) |
|
for unsafe_option in unsafe_options: |
|
with self.assertRaises(GitCommandError): |
|
submodule.update(clone_multi_options=[unsafe_option], allow_unsafe_options=True) |
|
|