Merge pull request #812 from willthames/gitversion

Added commit time information to the --version output
This commit is contained in:
Michael DeHaan 2012-08-08 18:50:04 -07:00
commit e654f881a8

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
VERBOSITY=0 VERBOSITY=0
@ -290,16 +291,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):