Skip to content

Commit 9dc2f8d

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 9dc2f8d

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

test-detection/detect_checker.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import boto3
1010
import fire
1111
import git
12-
12+
from pprint import pprint
1313

1414
def get_paged_ssm_params(path: str) -> list:
1515
""" Get SSM parameters into single array from 10 item pages """
@@ -121,8 +121,8 @@ def _build_commitable_temp_files(self):
121121

122122
def _load_repo(self):
123123
self.repo = git.repo.base.Repo("..")
124-
self.parent_branch = self.repo.active_branch
125-
print(f"Currently on branch: {self.parent_branch.name}")
124+
self.parent_branch = self.repo.head.object.hexsha
125+
print(f"Currentlsy on branch: {self.parent_branch}")
126126
return self.repo
127127

128128
def _checkout_test_branch(self):
@@ -138,7 +138,7 @@ def _checkout_test_branch(self):
138138
def _delete_test_branch(self):
139139
""" Delete local test branch """
140140
self._restore_ignore_file()
141-
self.parent_branch.checkout()
141+
self.repo.head.reference = self.parent_branch
142142
self.repo.delete_head(self.branch_name, force=True)
143143

144144
def _test_commit(self, example_file: str) -> bool:
@@ -192,7 +192,7 @@ def check(self):
192192
test["outcome"] = outcome
193193

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

197197
language_stats = defaultdict(dict)
198198
secret_stats = defaultdict(dict)

0 commit comments

Comments
 (0)