From 39cff29f4b11d94430a3d70a0708c66df7f6946b Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Wed, 23 Aug 2023 19:02:43 +0100 Subject: [PATCH] Support main branches other than "master" This patch adds support for having the main branch be named different than "master". It will use "master" or "main" if available, and fall back to the first branch name if they're both missing. --- git.py | 11 +++++++++++ views/summary.html | 14 ++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/git.py b/git.py index bf30e83..8402c8c 100644 --- a/git.py +++ b/git.py @@ -223,6 +223,17 @@ class Repo: refs = self._for_each_ref(pattern="refs/heads/", sort="-authordate") return [ref[len("refs/heads/") :] for _, _, ref in refs] + @functools.cache + def main_branch(self): + """Get the name of the main branch.""" + bs = self.branch_names() + for branch in ["master", "main"]: + if branch in bs: + return branch + if bs: + return bs[0] + return None + @functools.cache def tags(self, sort="-taggerdate"): """Get the (name, obj_id) of the tags.""" diff --git a/views/summary.html b/views/summary.html index d3388e6..9b180b8 100644 --- a/views/summary.html +++ b/views/summary.html @@ -35,20 +35,22 @@
% end -% if "master" in repo.branch_names(): +% if repo.main_branch():
- commits (master) + commits ({{repo.main_branch()}})
-% kwargs = dict(repo = repo, start_ref = "refs/heads/master", +% kwargs = dict(repo = repo, +% start_ref = "refs/heads/" + repo.main_branch(), % limit = repo.info.commits_in_summary, % shorten = shorten, repo_root = ".", offset = 0) % include commit-list **kwargs
- tree (master) + tree ({{repo.main_branch()}})
-% kwargs = dict(repo = repo, tree=repo.tree("master"), -% treeroot="b/master/t", dirname=smstr.from_url("")) +% kwargs = dict(repo = repo, tree=repo.tree(repo.main_branch()), +% treeroot="b/" + repo.main_branch() + "/t", +% dirname=smstr.from_url("")) % include tree-list **kwargs
% end