Skip to content

Commit

Permalink
Updated version info to include submodule information
Browse files Browse the repository at this point in the history
`ansible --version` etc. now include information about submodules

```
ansible 1.8 (submodule_ansible_version ffee9a8) 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 110250d) last updated 2014/09/27 14:33:42 (GMT +1000)
```

Also improved handling of detached HEAD when printing out version
information.
  • Loading branch information
willthames authored and mpdehaan committed Sep 28, 2014
1 parent 4f44a8a commit d1476ae
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions lib/ansible/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down

0 comments on commit d1476ae

Please sign in to comment.