Skip to content

Commit

Permalink
Merge pull request #1286 from effigies/fix/missing-git
Browse files Browse the repository at this point in the history
FIX: Tolerate missing git
  • Loading branch information
effigies authored Dec 26, 2023
2 parents 4098598 + 46a765d commit 1436a6e
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions nibabel/pkg_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import sys
from contextlib import suppress
from subprocess import run

from packaging.version import Version
Expand Down Expand Up @@ -102,14 +103,16 @@ def pkg_commit_hash(pkg_path: str | None = None) -> tuple[str, str]:
ver = Version(__version__)
if ver.local is not None and ver.local.startswith('g'):
return 'installation', ver.local[1:8]
# maybe we are in a repository
proc = run(
('git', 'rev-parse', '--short', 'HEAD'),
capture_output=True,
cwd=pkg_path,
)
if proc.stdout:
return 'repository', proc.stdout.decode().strip()
# maybe we are in a repository, but consider that we may not have git
with suppress(FileNotFoundError):
proc = run(
('git', 'rev-parse', '--short', 'HEAD'),
capture_output=True,
cwd=pkg_path,
)
if proc.stdout:
return 'repository', proc.stdout.decode().strip()

return '(none found)', '<not found>'


Expand Down

0 comments on commit 1436a6e

Please sign in to comment.