From d1476aeb01497cf2749c65a76792c8b951846157 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Sun, 28 Sep 2014 11:03:47 +1000 Subject: [PATCH] Updated version info to include submodule information `ansible --version` etc. now include information about submodules ``` ansible 1.8 (submodule_ansible_version ffee9a8fe0) last updated 2014/09/28 11:03:14 (GMT +1000) lib/ansible/modules/core: (ec2_snapshot_remove 3a77c31ecb) last updated 2014/09/27 18:23:31 (GMT +1000) lib/ansible/modules/extras: (detached HEAD 110250d344) last updated 2014/09/27 14:33:42 (GMT +1000) ``` Also improved handling of detached HEAD when printing out version information. --- lib/ansible/utils/__init__.py | 41 ++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 9ad1254899d..3f26e0779ff 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -832,11 +832,10 @@ def default(value, function): return function() return value -def _gitinfo(): + +def _gitrepoinfo(repo_path): ''' returns a string containing git branch, commit id and commit date ''' result = None - repo_path = os.path.join(os.path.dirname(__file__), '..', '..', '..', '.git') - if os.path.exists(repo_path): # Check if the .git is a file. If it is a file, it means that we are in a submodule structure. if os.path.isfile(repo_path): @@ -857,17 +856,39 @@ def _gitinfo(): f = open(branch_path) commit = f.readline()[:10] f.close() - date = time.localtime(os.stat(branch_path).st_mtime) - if time.daylight == 0: - offset = time.timezone - else: - offset = time.altzone - result = "({0} {1}) last updated {2} (GMT {3:+04d})".format(branch, commit, - time.strftime("%Y/%m/%d %H:%M:%S", date), offset / -36) + else: + # detached HEAD + commit = branch[:10] + branch = 'detached HEAD' + branch_path = os.path.join(repo_path, "HEAD") + + date = time.localtime(os.stat(branch_path).st_mtime) + if time.daylight == 0: + offset = time.timezone + else: + offset = time.altzone + result = "({0} {1}) last updated {2} (GMT {3:+04d})".format(branch, commit, + time.strftime("%Y/%m/%d %H:%M:%S", date), offset / -36) else: result = '' return result + +def _gitinfo(): + basedir = os.path.join(os.path.dirname(__file__), '..', '..', '..') + repo_path = os.path.join(basedir, '.git') + result = _gitrepoinfo(repo_path) + submodules = os.path.join(basedir, '.gitmodules') + f = open(submodules) + for line in f: + tokens = line.strip().split(' ') + if tokens[0] == 'path': + repo_path = tokens[2] + result += "\n {0}: {1}".format(repo_path, _gitrepoinfo(os.path.join(basedir, repo_path, '.git'))) + f.close() + return result + + def version(prog): result = "{0} {1}".format(prog, __version__) gitinfo = _gitinfo()