Skip to content

Commit

Permalink
fix(git): Always use worktree-local git hooks for remote repos
Browse files Browse the repository at this point in the history
Since v0.5.67, the work tree is fully checked out into a local
directory and the LFS hooks installed. This works fine in most cases,
however if `core.hooksPath` was set globally to a shared directory,
the LFS hooks would be installed into that directory, silently
overwriting whatever was there before. This commit addresses the issue
by configuring `core.hooksPath` in the checked out work tree before
trying to install any hooks.
  • Loading branch information
Wuestengecko committed Aug 29, 2024
1 parent fbcd213 commit 6b9070c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
21 changes: 18 additions & 3 deletions capellambse/filehandler/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ class GitFileHandler(abc.FileHandler):
__has_lfs: bool
__lfsfiles: dict[pathlib.PurePosixPath, bool]
__repo: pathlib.Path
__lockfile: pathlib.Path

def __init__(
self,
Expand Down Expand Up @@ -623,10 +624,14 @@ def __init_cache_dir_local(self) -> None:
path = pathlib.Path(urlpath[1:] if windows else urlpath)
else:
path = pathlib.Path(self.path)
self.cache_dir = path.resolve()
self.__repo = self.cache_dir = path.resolve()
gitdir = self.__git_nolock("rev-parse", "--git-dir", encoding="utf-8")
assert isinstance(gitdir, str)
self.__repo = pathlib.Path(self.cache_dir, gitdir.strip()).resolve()
self.__lockfile = (
pathlib.Path(self.cache_dir, gitdir.strip())
.resolve()
.joinpath("capellambse.lock")
)

def __init_cache_dir_remote(self) -> None:
slug_pattern = '[\x00-\x1f\x7f"*/:<>?\\|]+'
Expand All @@ -640,6 +645,7 @@ def __init_cache_dir_remote(self) -> None:
hashpath, digest_size=12, usedforsecurity=False
).hexdigest()
self.__repo = self.cache_dir = CACHEBASE.joinpath(path_hash, path_slug)
self.__lockfile = self.__repo.joinpath("capellambse.lock")

if old_dir.exists():
LOGGER.debug("Moving cache from %s to %s", old_dir, self.cache_dir)
Expand Down Expand Up @@ -746,6 +752,9 @@ def __init_worktree(self) -> None:
os.rmdir(worktree)
raise
self.cache_dir = worktree
gitdir = self.__git_nolock("rev-parse", "--git-dir", encoding="utf-8")
assert isinstance(gitdir, str)
self.__repo = pathlib.Path(self.cache_dir, gitdir.strip()).resolve()

self.__fnz = weakref.finalize(
self, self.__cleanup_worktree, self.__repo, worktree
Expand All @@ -758,6 +767,12 @@ def __init_worktree(self) -> None:
LOGGER.debug("LFS not installed, skipping setup")
else:
LOGGER.debug("LFS support detected, registering filter")
self.__git_nolock(
"config",
"--worktree",
"core.hooksPath",
self.__repo.joinpath("hooks"),
)
self.__git_nolock("lfs", "install", "--worktree", "--force")
self.__git_nolock("config", "--worktree", "core.bare", "false")
self._git("lfs", "pull")
Expand Down Expand Up @@ -787,7 +802,7 @@ def _git(
silent: bool = False,
**kw: t.Any,
) -> bytes | str:
with capellambse.helpers.flock(self.__repo / "capellambse.lock"):
with capellambse.helpers.flock(self.__lockfile):
return self.__git_nolock(*cmd, env=env, silent=silent, **kw)

def __git_nolock(
Expand Down
5 changes: 4 additions & 1 deletion tests/test_model_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ def test_model_loading_with_invalid_entrypoint_fails(suffix: str):

def test_model_loading_via_GitFileHandler():
path = "git+" + pathlib.Path.cwd().as_uri()

capellambse.MelodyModel(
path, entrypoint="tests/data/melodymodel/5_0/Melody Model Test.aird"
)
assert not pathlib.Path.cwd().joinpath("capellambse.lock").exists()

lockfile = pathlib.Path.cwd().joinpath("capellambse.lock")
assert not lockfile.exists()


def test_model_loading_via_GitFileHandler_invalid_uri():
Expand Down

0 comments on commit 6b9070c

Please sign in to comment.