1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Added commit time information to the --version output

Now prints date of commit (and timezone offset of commit)
Hopefully resolves #783
This commit is contained in:
willthames 2012-08-09 11:05:58 +10:00
parent 183fce6d99
commit 208f2b66ed

View file

@ -28,6 +28,7 @@ from ansible import errors
from ansible import color from ansible import color
from ansible import __version__ from ansible import __version__
import ansible.constants as C import ansible.constants as C
import time
try: try:
import json import json
@ -288,16 +289,19 @@ def default(value, function):
return value return value
def _gitinfo(): def _gitinfo():
''' returns a string containing git branch and commit id ''' returns a string containing git branch, commit id and commit date '''
using gitpython if installed and native file operations if not '''
result = None result = None
repo_path = os.path.join(os.path.dirname(__file__), '..', '..', '.git') repo_path = os.path.join(os.path.dirname(__file__), '..', '..', '.git')
if os.path.exists(repo_path): if os.path.exists(repo_path):
with open(os.path.join(repo_path, "HEAD")) as f: with open(os.path.join(repo_path, "HEAD")) as f:
branch = f.readline().split('/')[-1].rstrip("\n") branch = f.readline().split('/')[-1].rstrip("\n")
with open(os.path.join(repo_path, "refs", "heads", branch)) as f: branch_path = os.path.join(repo_path, "refs", "heads", branch)
with open(branch_path) as f:
commit = f.readline()[:10] commit = f.readline()[:10]
result = "({0} {1})".format(branch, commit) date = time.localtime(os.stat(branch_path).st_mtime)
offset = time.timezone if (time.daylight == 0) else time.altzone
result = "({0}) [{1}] {2} ({3:+04d})".format(branch, commit,
time.strftime("%Y%m%d-%H%M%S", date), offset / -36)
return result return result
def version(prog): def version(prog):