|
8 | 8 | from typing import Any, Never, cast |
9 | 9 |
|
10 | 10 | import git |
11 | | -from git import GitCommandError, GitError, InvalidGitRepositoryError, Tree |
| 11 | +from git import HEAD, GitCommandError, GitError, Head, InvalidGitRepositoryError, Tree |
12 | 12 | from git import Repo as GitRepo |
13 | 13 | from git.cmd import _AutoInterrupt as GitCmd |
14 | 14 | from loguru import logger |
@@ -132,9 +132,13 @@ def _clone(cls, remote: Remote, local: AbsDir) -> None: |
132 | 132 | @classmethod |
133 | 133 | def _sync(cls, local: GitDir) -> None: |
134 | 134 | with describe(f"Pulling {cls.repo(local).remote().url} into {local}", error_level="DEBUG"): |
135 | | - repo = cls.repo(local) |
136 | | - [fetch_info] = repo.remote().fetch() |
137 | | - repo.head.reset(fetch_info.commit, working_tree=True, index=True) |
| 135 | + commit = cls._fetch(local) |
| 136 | + cls.head(local).reset(commit.sha, working_tree=True, index=True) |
| 137 | + |
| 138 | + @classmethod |
| 139 | + def _fetch(cls, local: GitDir) -> Commit: |
| 140 | + cls.repo(local).remote().fetch() |
| 141 | + return Commit(strict_not_none(cls.branch(local).tracking_branch()).commit.hexsha) |
138 | 142 |
|
139 | 143 | @classmethod |
140 | 144 | def fresh_diff(cls, local: GitDir, file: RelFile) -> str: |
@@ -167,9 +171,17 @@ def apply_patch(cls, local: GitDir, patch: str) -> None: |
167 | 171 | def hash_object(cls, local: GitDir, blob: bytes) -> None: |
168 | 172 | cls.run_command(local, "hash-object", "--stdin", "-w", stdin=blob) |
169 | 173 |
|
| 174 | + @classmethod |
| 175 | + def head(cls, local: GitDir) -> HEAD: |
| 176 | + return cls.repo(local).head |
| 177 | + |
| 178 | + @classmethod |
| 179 | + def branch(cls, local: GitDir) -> Head: |
| 180 | + return cls.repo(local).active_branch |
| 181 | + |
170 | 182 | @classmethod |
171 | 183 | def commit(cls, local: GitDir) -> str: |
172 | | - return cls.repo(local).head.commit.hexsha |
| 184 | + return cls.head(local).commit.hexsha |
173 | 185 |
|
174 | 186 | @classmethod |
175 | 187 | def tree(cls, local: GitDir, commit: Commit | None = None) -> Tree: |
|
0 commit comments