Skip to content

Commit d39c85c

Browse files
committed
Refactor RepoManager
1 parent 18f12e5 commit d39c85c

File tree

1 file changed

+14
-47
lines changed

1 file changed

+14
-47
lines changed

infrahub_sdk/repository.py

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,39 @@
22

33
from pathlib import Path
44

5-
from dulwich.objects import Tree
5+
from dulwich import porcelain
66
from dulwich.repo import Repo
77

88

99
class GitRepoManager:
1010
def __init__(self, root_directory: str, branch: str = "main"):
1111
self.root_directory = root_directory
12-
self.git: Repo | None = None
1312
self.branch = branch
13+
self.git: Repo = self.initialize_repo()
1414

15-
self.initialize_repo()
16-
17-
def initialize_repo(self) -> None:
15+
def initialize_repo(self) -> Repo:
1816
# Check if the directory already has a repository
1917

2018
root_path = Path(self.root_directory)
2119

2220
if root_path.exists() and (root_path / ".git").is_dir():
23-
self.git = Repo(self.root_directory) # Open existing repo
24-
try:
25-
self.active_branch # noqa: B018
26-
except KeyError:
27-
raise ValueError("Git repository does not have an active branch.")
21+
repo = Repo(self.root_directory) # Open existing repo
2822
else:
29-
self.git = Repo.init(self.root_directory, default_branch=self.branch.encode("utf-8"))
23+
repo = Repo.init(self.root_directory, default_branch=self.branch.encode("utf-8"))
24+
3025
self.create_initial_commit()
31-
# Ensure the repository is valid
32-
if not self.git:
33-
raise ValueError("Failed to initialize or open a repository.")
3426

35-
def create_initial_commit(self) -> None:
36-
if not self.git:
37-
raise ValueError("Git repository not initialized.")
27+
if not repo:
28+
raise ValueError("Failed to initialize or open a repository.")
3829

39-
"""Create an initial commit if no commits exist."""
40-
# Create an empty tree object
41-
tree = Tree()
42-
self.git.object_store.add_object(tree)
30+
return repo
4331

44-
# Create the initial commit without an index (use empty tree)
45-
author = committer = b"Initial Commit <[email protected]>"
32+
def create_initial_commit(self) -> None:
33+
committer = b"Initial Commit <[email protected]>"
4634
commit_msg = b"Initial commit"
47-
48-
ref = f"refs/heads/{self.branch}".encode()
49-
50-
self.git.do_commit(author=author, committer=committer, message=commit_msg, ref=ref)
51-
52-
# Set HEAD reference to point to the main branch
53-
self.git.refs.set_symbolic_ref(b"HEAD", ref)
35+
porcelain.commit(self.root_directory, message=commit_msg, author=committer)
5436

5537
@property
5638
def active_branch(self) -> str | None:
57-
"""Get the name of the current active branch."""
58-
if not self.git:
59-
raise ValueError("Repository is not initialized.")
60-
61-
# Read the symbolic reference of HEAD to get the current commit SHA
62-
head_ref = self.git.refs[b"HEAD"]
63-
if head_ref:
64-
commit_sha = head_ref.decode("utf-8") # Commit SHA from HEAD
65-
# Now look for the branch that points to this commit SHA
66-
for ref in self.git.refs.as_dict().keys():
67-
if ref.startswith(b"refs/heads/"):
68-
branch_ref = ref.decode("utf-8")
69-
# Check if this branch points to the same commit SHA
70-
if self.git.refs[ref] == commit_sha.encode("utf-8"):
71-
return branch_ref.split("/")[-1]
72-
return None # If no matching branch is found
73-
raise ValueError("No HEAD reference found, cannot determine active branch.")
39+
active_branch = porcelain.active_branch(self.root_directory).decode("utf-8")
40+
return active_branch

0 commit comments

Comments
 (0)