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.
This commit is contained in:
Will Thames 2014-09-28 11:03:47 +10:00 committed by Michael DeHaan
parent 4f44a8ab75
commit d1476aeb01

View file

@ -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,6 +856,12 @@ def _gitinfo():
f = open(branch_path)
commit = f.readline()[:10]
f.close()
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
@ -868,6 +873,22 @@ def _gitinfo():
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()