[jhbuild/wip/hadess/default-branch-name: 1/2] Get default branch name from repository



commit 77192689d33d54a43289130c5d7edf85e89f9f3d
Author: Bastien Nocera <hadess hadess net>
Date:   Mon Jun 15 18:36:39 2020 +0200

    Get default branch name from repository
    
    Instead of hardcoding the 'master' branch name.

 jhbuild/versioncontrol/git.py | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
---
diff --git a/jhbuild/versioncontrol/git.py b/jhbuild/versioncontrol/git.py
index 5ad51c70..806a79ba 100644
--- a/jhbuild/versioncontrol/git.py
+++ b/jhbuild/versioncontrol/git.py
@@ -236,9 +236,19 @@ class GitBranch(Branch):
                 extra_env=get_git_extra_env())
         return self.execute_git_predicate( ['git', 'show-ref', wanted_ref])
 
+    def get_default_branch_name(self):
+        try:
+            default_branch = get_output(['git', 'symbolic-ref', '--short',
+                               'refs/remotes/origin/HEAD'],
+                               cwd=self.get_checkoutdir(),
+                               extra_env=get_git_extra_env()).strip()
+        except CommandError:
+            return 'master'
+        return default_branch.replace('origin/', '')
+
     def get_branch_switch_destination(self):
         current_branch = self.get_current_branch()
-        wanted_branch = self.branch or 'master'
+        wanted_branch = self.branch or self.get_default_branch_name()
 
         # Always switch away from a detached head.
         if not current_branch:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]