From a6078a729c01ec0ecce282d6979a54ae5121686d Mon Sep 17 00:00:00 2001 From: Matthew Cengia Date: Mon, 20 Mar 2023 14:25:41 +1100 Subject: [PATCH 1/2] get_default_branch: Use HEAD instead of guessing --- klaus/repo.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/klaus/repo.py b/klaus/repo.py index 5033607b..e00a506b 100644 --- a/klaus/repo.py +++ b/klaus/repo.py @@ -137,20 +137,13 @@ def get_commit(self, rev): raise KeyError(rev) def get_default_branch(self): - """Tries to guess the default repo branch name.""" - for candidate in ["master", "main", "trunk", "default", "gh-pages"]: - try: - self.get_commit(candidate) - return candidate - except InaccessibleRef: - pass - for name in self.get_branch_names(): - try: - self.get_commit(name) - return name - except InaccessibleRef: - pass - else: + """Retrieves the default branch name from HEAD""" + try: + self.head() + for branch in self.get_branch_names(): + if self.get_commit(branch) == self.get_commit(self.head().decode()): + return branch + except InaccessibleRef: return None def get_ref_names_ordered_by_last_commit(self, prefix, exclude=None): From 9a2197d4afbe6957166b733a6362a19f1af1a381 Mon Sep 17 00:00:00 2001 From: Matthew Cengia Date: Thu, 23 Mar 2023 12:50:29 +1100 Subject: [PATCH 2/2] Use refs.follow to get HEAD refs --- klaus/repo.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/klaus/repo.py b/klaus/repo.py index e00a506b..9dc269be 100644 --- a/klaus/repo.py +++ b/klaus/repo.py @@ -139,10 +139,8 @@ def get_commit(self, rev): def get_default_branch(self): """Retrieves the default branch name from HEAD""" try: - self.head() - for branch in self.get_branch_names(): - if self.get_commit(branch) == self.get_commit(self.head().decode()): - return branch + heads = [ head.split(b'/')[-1] for head in self.refs.follow(b'HEAD')[0] if head.startswith(b'refs/head') ] + return(heads[0].decode()) except InaccessibleRef: return None