Skip to content

Commit 4ff42f6

Browse files
author
Gunther Klessinger
committed
fixf; master/main
1 parent 8004d1d commit 4ff42f6

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/lcdoc/mkdocs/tools.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from mkdocs.config import config_options
2121
from mkdocs.plugins import BasePlugin
2222
from mkdocs.plugins import log as mkdlog
23+
import subprocess
2324

2425
script = lambda s: '<script>\n%s\n</script>' % s
2526
style = lambda s: '<style>\n%s\n</style>' % s
@@ -34,9 +35,22 @@ def add_post_page_func(kw, f, once=False):
3435
p.lp_on_post_page = h
3536

3637

38+
def get_default_branch():
39+
"""Get the default branch name (main or master)"""
40+
try:
41+
l = ['git', 'symbolic-ref', 'refs/remotes/origin/HEAD']
42+
result = subprocess.run(l, capture_output=True, text=True, check=True)
43+
return result.stdout.strip().split('/')[-1]
44+
except Exception:
45+
return 'main'
46+
47+
3748
def srclink(fn, config, line=None, match=None, title=''):
3849
# TODO: allow others:
39-
u = config['repo_url'] + 'blob/master'
50+
u = config['repo_url']
51+
if not u.endswith('/'):
52+
u += '/'
53+
u += f'blob/{get_default_branch()}'
4054
r = project.root(config)
4155
if fn[0] == '/':
4256
fnr = fn.split(r, 1)[1]

0 commit comments

Comments
 (0)