Skip to content

Commit

Permalink
Merge pull request #1978 from strictdoc-project/stanislaw/cache_sourc…
Browse files Browse the repository at this point in the history
…e_files

git_client: use the cache dir from project config
  • Loading branch information
stanislaw authored Nov 2, 2024
2 parents b76e1fe + 3224e35 commit 40b99ce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
1 change: 0 additions & 1 deletion strictdoc/export/html/html_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import hashlib
import os.path
import shutil
import tempfile
from pathlib import Path
from typing import Any, List, Optional

Expand Down
41 changes: 19 additions & 22 deletions strictdoc/git/git_client.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
# mypy: disable-error-code="no-untyped-call,no-untyped-def"
import os.path
import shutil
import subprocess
import tempfile
from pathlib import Path
from typing import Optional

from strictdoc.core.project_config import ProjectConfig
from strictdoc.helpers.timing import measure_performance

PATH_TO_TMP_DIR = tempfile.gettempdir()
PATH_TO_SANDBOX_DIR = os.path.join(
PATH_TO_TMP_DIR, "strictdoc_cache", "git_sandbox"
)


class GitClient:
def __init__(self, path_to_git_root: str):
def __init__(self, path_to_git_root: str) -> None:
assert os.path.isdir(path_to_git_root)
self.path_to_git_root: str = path_to_git_root

@staticmethod
def create_repo_from_local_copy(revision: str):
def create_repo_from_local_copy(
revision: str, project_config: ProjectConfig
) -> "GitClient":
with measure_performance(f"Copy Git repo: {revision}"):
path_to_cwd = os.getcwd()
if revision == "HEAD+":
path_to_project_git_dir = os.path.join(path_to_cwd, ".git")
assert os.path.isdir(path_to_project_git_dir)
return GitClient(path_to_cwd)

path_to_sandbox_git_repo = os.path.join(
PATH_TO_SANDBOX_DIR, revision
path_to_sandbox = os.path.join(
project_config.get_path_to_cache_dir(), "git"
)
path_to_sandbox_git_repo = os.path.join(path_to_sandbox, revision)
path_to_sandbox_git_repo_git = os.path.join(
path_to_sandbox_git_repo, ".git"
)
Expand All @@ -42,7 +39,7 @@ def create_repo_from_local_copy(revision: str):
if git_client.is_clean_branch():
return git_client

Path(PATH_TO_SANDBOX_DIR).mkdir(parents=True, exist_ok=True)
Path(path_to_sandbox).mkdir(parents=True, exist_ok=True)

# Running git worktree add ... below results with "path already
# exists" error if the destination folder already exists, even if
Expand Down Expand Up @@ -74,7 +71,7 @@ def create_repo_from_local_copy(revision: str):

return git_client

def is_clean_branch(self):
def is_clean_branch(self) -> bool:
"""
https://unix.stackexchange.com/a/155077/77389
"""
Expand All @@ -89,7 +86,7 @@ def is_clean_branch(self):
return False
return result.stdout == ""

def add_file(self, path_to_file):
def add_file(self, path_to_file: str) -> None:
result = subprocess.run(
["git", "add", path_to_file],
cwd=self.path_to_git_root,
Expand All @@ -99,7 +96,7 @@ def add_file(self, path_to_file):
)
assert result.returncode == 0, result

def add_all(self):
def add_all(self) -> None:
result = subprocess.run(
["git", "add", "."],
cwd=self.path_to_git_root,
Expand All @@ -109,7 +106,7 @@ def add_all(self):
)
assert result.returncode == 0, result

def commit(self, message: str):
def commit(self, message: str) -> None:
result = subprocess.run(
["git", "commit", "-m", message],
cwd=self.path_to_git_root,
Expand All @@ -119,7 +116,7 @@ def commit(self, message: str):
)
assert result.returncode == 0, result

def check_revision(self, revision: str):
def check_revision(self, revision: str) -> str:
assert isinstance(revision, str)
assert len(revision) > 0
result = subprocess.run(
Expand All @@ -133,7 +130,7 @@ def check_revision(self, revision: str):
return result.stdout.strip()
raise LookupError(f"Non-existing revision: {revision}.")

def commit_all(self, message: str):
def commit_all(self, message: str) -> None:
result = subprocess.run(
["git", "commit", "-a", "-m", message],
cwd=self.path_to_git_root,
Expand All @@ -143,7 +140,7 @@ def commit_all(self, message: str):
)
assert result.returncode == 0, result

def rebase_from_main(self):
def rebase_from_main(self) -> None:
result = subprocess.run(
["git", "fetch", "origin"],
cwd=self.path_to_git_root,
Expand All @@ -161,7 +158,7 @@ def rebase_from_main(self):
)
assert result.returncode == 0, result

def push(self):
def push(self) -> None:
result = subprocess.run(
["git", "push", "origin"],
cwd=self.path_to_git_root,
Expand All @@ -171,7 +168,7 @@ def push(self):
)
assert result.returncode == 0, result

def hard_reset(self, revision: Optional[str] = None):
def hard_reset(self, revision: Optional[str] = None) -> None:
reset_args = ["git", "reset", "--hard"]
if revision is not None:
reset_args.append(revision)
Expand All @@ -184,7 +181,7 @@ def hard_reset(self, revision: Optional[str] = None):
)
assert result.returncode == 0, result

def clean(self):
def clean(self) -> None:
result = subprocess.run(
["git", "clean", "-fd"],
cwd=self.path_to_git_root,
Expand Down
4 changes: 2 additions & 2 deletions strictdoc/server/routers/other_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def get_git_diff_result(
assert right_revision_resolved is not None

git_client_lhs = GitClient.create_repo_from_local_copy(
left_revision_resolved
left_revision_resolved, project_config
)

project_config_copy_lhs: ProjectConfig = deepcopy(project_config)
Expand All @@ -223,7 +223,7 @@ def get_git_diff_result(
project_config_copy_lhs.input_paths = [export_input_abs_path]

git_client_rhs = GitClient.create_repo_from_local_copy(
right_revision_resolved
right_revision_resolved, project_config
)

export_input_rel_path = os.path.relpath(
Expand Down

0 comments on commit 40b99ce

Please sign in to comment.