Skip to content

Commit 0a4ff88

Browse files
akinnanedetnon
and
detnon
committed
Use hexsha instead of active_branch to work around detached head
When concourse checks out a repo it leaves it in a detached head state. This was causing errors as python git has slightly undefined behaviour in this case. ``` File "detect_checker.py", line 125, in _load_repo self.parent_branch = self.repo.active_branch File "/home/a/venv/local/lib/python3.7/site-packages/git/repo/base.py", line 703, in active_branch return self.head.reference File "/home/a/venv/local/lib/python3.7/site-packages/git/refs/symbolic.py", line 272, in _get_reference raise TypeError("%s is a detached symbolic reference as it points to %r" % (self, sha)) TypeError: HEAD is a detached symbolic reference as it points to '6e589de2864ee310a46a343ae9d656405e6b8c84' ``` gitpython-developers/GitPython#633 This PR changes it use the hexsha instead which works correctly with detached heads and normal branches. Co-authored-by: detnon <[email protected]> Co-authored-by: Alex Kinnane <[email protected]>
1 parent 6e589de commit 0a4ff88

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

test-detection/detect_checker.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import fire
1111
import git
1212

13-
1413
def get_paged_ssm_params(path: str) -> list:
1514
""" Get SSM parameters into single array from 10 item pages """
1615
ssm_client = boto3.client("ssm")
@@ -121,8 +120,8 @@ def _build_commitable_temp_files(self):
121120

122121
def _load_repo(self):
123122
self.repo = git.repo.base.Repo("..")
124-
self.parent_branch = self.repo.active_branch
125-
print(f"Currently on branch: {self.parent_branch.name}")
123+
self.parent_branch = self.repo.head.object.hexsha
124+
print(f"Currently on branch: {self.parent_branch}")
126125
return self.repo
127126

128127
def _checkout_test_branch(self):
@@ -138,7 +137,7 @@ def _checkout_test_branch(self):
138137
def _delete_test_branch(self):
139138
""" Delete local test branch """
140139
self._restore_ignore_file()
141-
self.parent_branch.checkout()
140+
self.repo.head.reference = self.parent_branch
142141
self.repo.delete_head(self.branch_name, force=True)
143142

144143
def _test_commit(self, example_file: str) -> bool:
@@ -192,7 +191,7 @@ def check(self):
192191
test["outcome"] = outcome
193192

194193
self._delete_test_branch()
195-
print(f"Reset to parent branch: {self.repo.active_branch.name}")
194+
print(f"Reset to parent branch: {self.repo.head.object.hexsha}")
196195

197196
language_stats = defaultdict(dict)
198197
secret_stats = defaultdict(dict)

0 commit comments

Comments
 (0)