Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version: retrieve commit hash for git worktrees #2586

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions sopel/builtins/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,55 @@
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:
Fixed Show fixed Hide fixed
"""
Given paths to ``.git/`` and ``HEAD``, determine the associated commit hash
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
Given paths to ``.git/`` and ``HEAD``, determine the associated commit hash
"""
"""Determine the associated commit hash from the git dir and its HEAD"""

If you want, you can detail each parameter with :param gitdir: <desc> stanza in the docstring's body.

result = None

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
result = sha

return result


def _resolve_git_dirs(pth: str) -> tuple[str, str]:
SnoopJ marked this conversation as resolved.
Show resolved Hide resolved
"""
Resolve a ``.git`` path to its 'true' ``.git/`` and `HEAD`
SnoopJ marked this conversation as resolved.
Show resolved Hide resolved

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
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
Determine the git commit hash of this Sopel, if applicable
"""
"""Determine the git commit hash of this Sopel, if applicable"""

gitdir, head = _resolve_git_dirs(GIT_DIR)
return _read_commit(gitdir, head)
SnoopJ marked this conversation as resolved.
Show resolved Hide resolved


@plugin.command('version')
Expand Down