|
2 | 2 |
|
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | | -from dulwich.objects import Tree |
| 5 | +from dulwich import porcelain |
6 | 6 | from dulwich.repo import Repo |
7 | 7 |
|
8 | 8 |
|
9 | 9 | class GitRepoManager: |
10 | 10 | def __init__(self, root_directory: str, branch: str = "main"): |
11 | 11 | self.root_directory = root_directory |
12 | | - self.git: Repo | None = None |
13 | 12 | self.branch = branch |
| 13 | + self.git: Repo = self.initialize_repo() |
14 | 14 |
|
15 | | - self.initialize_repo() |
16 | | - |
17 | | - def initialize_repo(self) -> None: |
| 15 | + def initialize_repo(self) -> Repo: |
18 | 16 | # Check if the directory already has a repository |
19 | 17 |
|
20 | 18 | root_path = Path(self.root_directory) |
21 | 19 |
|
22 | 20 | 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 |
28 | 22 | 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 | + |
30 | 25 | 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.") |
34 | 26 |
|
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.") |
38 | 29 |
|
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 |
43 | 31 |
|
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]>" |
46 | 34 | 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) |
54 | 36 |
|
55 | 37 | @property |
56 | 38 | 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