Skip to content

Commit

Permalink
Merge pull request #2410 from Nodd/git-revision
Browse files Browse the repository at this point in the history
Print git revision and branch in bootstrap.py instead of mercurial
  • Loading branch information
ccordoba12 committed Jun 1, 2015
2 parents 792372f + 0126ba0 commit 133cbf3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@

# --- Continue

from spyderlib.utils.vcs import get_hg_revision
print("Revision %s:%s, Branch: %s" % get_hg_revision(DEVPATH))
from spyderlib.utils.vcs import get_git_revision
print("Revision %s, Branch: %s" % get_git_revision(DEVPATH))

sys.path.insert(0, DEVPATH)
print("01. Patched sys.path with %s" % DEVPATH)
Expand Down
2 changes: 1 addition & 1 deletion spyderlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_versions(reporev=True):
revision = None
if reporev:
from spyderlib.utils import vcs
revision = vcs.get_git_revision(os.path.dirname(__dir__))
revision, branch = vcs.get_git_revision(os.path.dirname(__dir__))

if not sys.platform == 'darwin': # To avoid a crash with our Mac app
system = platform.system()
Expand Down
31 changes: 24 additions & 7 deletions spyderlib/utils/vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from __future__ import print_function

import sys
import os.path as osp
import subprocess

Expand Down Expand Up @@ -38,7 +39,7 @@
class ActionToolNotFound(RuntimeError):
"""Exception to transmit information about supported tools for
failed attempt to execute given action"""

def __init__(self, vcsname, action, tools):
RuntimeError.__init__(self)
self.vcsname = vcsname
Expand Down Expand Up @@ -113,22 +114,38 @@ def get_hg_revision(repopath):

def get_git_revision(repopath):
"""Return Git revision for the repository located at repopath
Result is the latest commit hash, with None on error
Result is a tuple (latest commit hash, branch), with None values on
error
"""
try:
git = programs.find_program('git')
assert git is not None and osp.isdir(osp.join(repopath, '.git'))

# Revision
commit = subprocess.Popen([git, 'rev-parse', '--short', 'HEAD'],
stdout=subprocess.PIPE,
cwd=repopath).communicate()
commit = commit[0].strip()
if PY3:
commit = str(commit[0][:-1])
commit = commit[2:-1]
commit = commit.decode(sys.getdefaultencoding())

# Branch
branches = subprocess.Popen([git, 'branch'],
stdout=subprocess.PIPE,
cwd=repopath).communicate()
branches = branches[0]
if PY3:
branches = branches.decode(sys.getdefaultencoding())
branches = branches.split('\n')
active_branch = [b for b in branches if b.startswith('*')]
if len(active_branch) != 1:
branch = None
else:
commit = commit[0][:-1]
return commit
branch = active_branch[0].split(None, 1)[1]

return commit, branch
except (subprocess.CalledProcessError, AssertionError, AttributeError):
return None
return None, None


if __name__ == '__main__':
Expand Down

0 comments on commit 133cbf3

Please sign in to comment.