[jhbuild/ls_remote_main_branch: 1/2] get default branch name from remote repository
- From: Nelson Ben <nbenitez src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [jhbuild/ls_remote_main_branch: 1/2] get default branch name from remote repository
- Date: Fri, 21 Jan 2022 16:07:57 +0000 (UTC)
commit 2d12a60461a2625c78559516a9305e843ad5afcb
Author: Nelson Benítez León <nbenitezl gmail com>
Date: Thu Jan 20 16:01:03 2022 -0400
get default branch name from remote repository
commit 77192689d retrieves the branch name from
the local repository, so it worked fine for new
cloned repositories, but if you have an already
cloned repository then you'll still be getting
the old default branch name.
Fix this by using command:
git ls-remote --symref origin HEAD
which will retrieve the default branch name
from the remote repository itself.
As a plus 'git ls-remote' is a plumbing command
in git which means it has stable output well
suited for scripts.
jhbuild/versioncontrol/git.py | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
---
diff --git a/jhbuild/versioncontrol/git.py b/jhbuild/versioncontrol/git.py
index c2d21738..460dbe5b 100644
--- a/jhbuild/versioncontrol/git.py
+++ b/jhbuild/versioncontrol/git.py
@@ -238,13 +238,26 @@ class GitBranch(Branch):
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()
+ out = get_output(['git', 'ls-remote', '--symref', 'origin', 'HEAD'],
+ cwd=self.get_checkoutdir(),
+ extra_env=get_git_extra_env()).strip()
except CommandError:
- return 'master'
- return default_branch.replace('origin/', '')
+ logging.warning('get_default_branch_name() command error, so defaulting to \'main\'')
+ return 'main'
+
+ ind = out.find("ref: ")
+ if ind == -1:
+ logging.warning('Unexpected get_default_branch_name() output, so defaulting to \'main\'')
+ return 'main'
+
+ tmp = out[ind:].split("\t", maxsplit=1)
+ if len(tmp) == 2 and tmp[1][0:4] == "HEAD":
+ default_branch = tmp[0].split("/")[-1]
+ else:
+ logging.warning('Unexpected get_default_branch_name() output, so defaulting to \'main\'')
+ default_branch = 'main'
+
+ return default_branch
def get_branch_switch_destination(self):
current_branch = self.get_current_branch()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]