Skip to content

Commit

Permalink
version: retrieve commit hash for git worktrees
Browse files Browse the repository at this point in the history
  • Loading branch information
SnoopJ committed Dec 16, 2023
1 parent 6fc9eee commit 7a0ee69
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions sopel/builtins/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,53 @@
GIT_DIR = os.path.join(PROJECT_DIR, '.git')


def git_info():
head = os.path.join(GIT_DIR, 'HEAD')
def _read_commit(gitdir: str, head: str) -> str | None:

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns Note

Mixing implicit and explicit returns may indicate an error as implicit returns always return None.
"""
Given paths to ``.git/`` and ``HEAD``, determine the associated commit hash
"""
if os.path.isfile(head):
with open(head) as h:
head_loc = h.readline()[5:-1] # strip ref: and \n
head_file = os.path.join(GIT_DIR, head_loc)
head_file = os.path.join(gitdir, head_loc)
if os.path.isfile(head_file):
with open(head_file) as h:
sha = h.readline()
if sha:
return sha


def _resolve_git_dirs(pth: str) -> tuple[str, str]:
"""
Resolve a ``.git`` path to its 'true' ``.git/`` and `HEAD`
This helper is useful for dealing with the ``.git`` file stored in a
git worktree.
"""
# default to the old behavior: assume `pth` is a valid .git/ to begin with
gitdir = pth
head = os.path.join(pth, "HEAD")

if os.path.isfile(pth):
# this may be a worktree, let's override the result properly if so
with open(pth, 'r') as f:
first, rest = next(f).strip().split(maxsplit=1)
if first == "gitdir:":
# line is "gitdir: /path/to/parentrepo/.git/worktrees/thispath"
gitdir = os.path.dirname(os.path.dirname(rest))
head = os.path.join(rest, "HEAD")
# else: we can't make sense of this file, stick to the default

return gitdir, head


def git_info() -> str | None:
"""
Determine the git commit hash of this Sopel, if applicable
"""
gitdir, head = _resolve_git_dirs(GIT_DIR)
return _read_commit(gitdir, head)


@plugin.command('version')
@plugin.example('.version [plugin_name]')
@plugin.output_prefix('[version] ')
Expand Down

0 comments on commit 7a0ee69

Please sign in to comment.