Skip to content

Commit 54dc1c7

Browse files
author
Vasileios Karakasis
authored
Merge pull request #1107 from teojgo/feat/git_commit_log
[feat] Print repository's revision hash when ReFrame's version is requested
2 parents a9b0bce + dbabfb5 commit 54dc1c7

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

reframe/core/logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def __init__(self, logger=None, check=None):
384384
'osuser': os_ext.osuser() or '<unknown>',
385385
'osgroup': os_ext.osgroup() or '<unknown>',
386386
'check_tags': None,
387-
'version': reframe.VERSION,
387+
'version': os_ext.reframe_version(),
388388
}
389389
)
390390
self.check = check

reframe/frontend/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def main():
246246
'(default format "%%FT%%T")'
247247
)
248248
misc_options.add_argument('-V', '--version', action='version',
249-
version=reframe.VERSION)
249+
version=os_ext.reframe_version())
250250
misc_options.add_argument('-v', '--verbose', action='count', default=0,
251251
help='Increase verbosity level of output')
252252

reframe/utility/os_ext.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import shlex
1212
import shutil
1313
import signal
14+
import sys
1415
import subprocess
1516
import tempfile
1617
from urllib.parse import urlparse
1718

19+
import reframe
1820
from reframe.core.exceptions import (ReframeError, SpawnedProcessError,
1921
SpawnedProcessTimeout)
2022

@@ -68,7 +70,6 @@ def run_command_async(cmd,
6870
shell=False,
6971
log=True,
7072
**popen_args):
71-
# Import logger here to avoid unnecessary circular dependencies
7273
if log:
7374
from reframe.core.logging import getlogger
7475
getlogger().debug('executing OS command: ' + cmd)
@@ -315,6 +316,53 @@ def git_repo_exists(url, timeout=5):
315316
return True
316317

317318

319+
def git_repo_hash(branch='HEAD', short=True, wd=None):
320+
'''Return the SHA1 hash of a git repository.
321+
322+
:arg branch: The branch to look at.
323+
:arg short: Return a short hash. This always corresponds to the first 8
324+
characters of the long hash. We don't rely on Git for the short hash,
325+
since depending on the version it might return either 7 or 8
326+
characters.
327+
:arg wd: Change to this directory before retrieving the hash. If ``None``,
328+
ReFrame's install prefix will be used.
329+
:returns: The repository has or ``None`` if the hash could not be
330+
retrieved.
331+
332+
'''
333+
try:
334+
wd = wd or reframe.INSTALL_PREFIX
335+
with change_dir(wd):
336+
# Do not log this command, since we need to call this function
337+
# from the logger
338+
completed = run_command('git rev-parse %s' % branch,
339+
check=True, log=False)
340+
341+
except SpawnedProcessError:
342+
return None
343+
344+
hash = completed.stdout.strip()
345+
if hash:
346+
return hash[:8] if short else hash
347+
else:
348+
return None
349+
350+
351+
def reframe_version():
352+
'''Return ReFrame version.
353+
354+
If ReFrame's installation contains the repository metadata, the
355+
repository's hash will be appended to the actual version.
356+
357+
'''
358+
version = reframe.VERSION
359+
repo_hash = git_repo_hash()
360+
if repo_hash:
361+
return '%s (rev: %s)' % (version, repo_hash)
362+
else:
363+
return version
364+
365+
318366
def expandvars(path):
319367
'''Expand environment variables in ``path`` and
320368
perform any command substitution

unittests/test_utility.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ def test_is_url(self):
174174
self.assertTrue(os_ext.is_url(repo_https))
175175
self.assertFalse(os_ext.is_url(repo_ssh))
176176

177+
def test_git_repo_hash(self):
178+
# A git branch hash consists of 8(short) or 40 characters.
179+
assert len(os_ext.git_repo_hash()) == 8
180+
assert len(os_ext.git_repo_hash(short=False)) == 40
181+
assert os_ext.git_repo_hash(branch='invalid') is None
182+
assert os_ext.git_repo_hash(branch='') is None
183+
177184
def test_git_repo_exists(self):
178185
self.assertTrue(os_ext.git_repo_exists(
179186
'https://github.com/eth-cscs/reframe.git', timeout=3))

0 commit comments

Comments
 (0)