-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Worktree and directory functions to separate files
- Loading branch information
Showing
6 changed files
with
97 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,23 @@ | ||
from infrahub.git.directory import initialize_repositories_directory | ||
from infrahub.git.repository import ( | ||
BRANCHES_DIRECTORY_NAME, | ||
COMMITS_DIRECTORY_NAME, | ||
TEMPORARY_DIRECTORY_NAME, | ||
ArtifactGenerateResult, | ||
CheckDefinitionInformation, | ||
GraphQLQueryInformation, | ||
InfrahubReadOnlyRepository, | ||
InfrahubRepository, | ||
RepoFileInformation, | ||
TransformPythonInformation, | ||
Worktree, | ||
extract_repo_file_information, | ||
initialize_repositories_directory, | ||
) | ||
|
||
__all__ = [ | ||
"BRANCHES_DIRECTORY_NAME", | ||
"COMMITS_DIRECTORY_NAME", | ||
"TEMPORARY_DIRECTORY_NAME", | ||
"ArtifactGenerateResult", | ||
"InfrahubReadOnlyRepository", | ||
"InfrahubRepository", | ||
"TransformPythonInformation", | ||
"CheckDefinitionInformation", | ||
"RepoFileInformation", | ||
"GraphQLQueryInformation", | ||
"Worktree", | ||
"extract_repo_file_information", | ||
"initialize_repositories_directory", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
COMMITS_DIRECTORY_NAME = "commits" | ||
BRANCHES_DIRECTORY_NAME = "branches" | ||
TEMPORARY_DIRECTORY_NAME = "temp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
|
||
from infrahub import config | ||
from infrahub.log import get_logger | ||
|
||
log = get_logger() | ||
|
||
|
||
def get_repositories_directory() -> str: | ||
"""Return the absolute path to the main directory used for the repositories.""" | ||
repos_dir = config.SETTINGS.git.repositories_directory | ||
if not os.path.isabs(repos_dir): | ||
current_dir = os.getcwd() | ||
repos_dir = os.path.join(current_dir, config.SETTINGS.git.repositories_directory) | ||
|
||
return str(repos_dir) | ||
|
||
|
||
def initialize_repositories_directory() -> bool: | ||
"""Check if the main repositories_directory already exist, if not create it. | ||
Return | ||
True if the directory has been created, | ||
False if the directory was already present. | ||
""" | ||
repos_dir = get_repositories_directory() | ||
if not os.path.isdir(repos_dir): | ||
os.makedirs(repos_dir) | ||
log.debug(f"Initialized the repositories_directory at {repos_dir}") | ||
return True | ||
|
||
log.debug(f"Repositories_directory already present at {repos_dir}") | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
from infrahub.exceptions import Error | ||
from infrahub.git.constants import BRANCHES_DIRECTORY_NAME, COMMITS_DIRECTORY_NAME | ||
from infrahub.git.directory import get_repositories_directory | ||
|
||
|
||
class Worktree(BaseModel): | ||
identifier: str | ||
directory: str | ||
commit: str | ||
branch: Optional[str] = None | ||
|
||
@classmethod | ||
def init(cls, text: str) -> Worktree: | ||
lines = text.split("\n") | ||
|
||
full_directory = lines[0].replace("worktree ", "") | ||
|
||
# Extract the identifier from the directory name | ||
# We first need to substract the main repository_directory to get the relative path. | ||
repo_directory = get_repositories_directory() | ||
relative_directory = full_directory.replace(repo_directory, "") | ||
relative_paths = relative_directory.split("/") | ||
|
||
identifier = None | ||
if len(relative_paths) == 3: | ||
# this is the main worktree for the main branch | ||
identifier = relative_paths[2] | ||
|
||
elif len(relative_paths) == 4 and relative_paths[2] == COMMITS_DIRECTORY_NAME: | ||
# this is the either a commit or a branch worktree | ||
identifier = relative_paths[3] | ||
elif len(relative_paths) == 4 and relative_paths[2] == BRANCHES_DIRECTORY_NAME and lines[2] != "detached": | ||
identifier = lines[2].replace("branch refs/heads/", "") | ||
else: | ||
raise Error("Unexpected path for a worktree.") | ||
|
||
item = cls( | ||
identifier=identifier, | ||
directory=full_directory, | ||
commit=lines[1].replace("HEAD ", ""), | ||
) | ||
|
||
if lines[2] != "detached": | ||
item.branch = lines[2].replace("branch refs/heads/", "") | ||
|
||
return item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters