Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gitbranch option #37

Merged
merged 2 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ optional arguments:
-m, --machine prints system and machine info
-g, --githash prints current Git commit hash
-r, --gitrepo prints current Git remote address
-b, --gitbranch prints the current Git branch (new in v1.6)
-iv, --iversion print name and version of all imported packages
-w, --watermark prints the current version of watermark
```
Expand Down
2 changes: 1 addition & 1 deletion watermark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys


__version__ = '1.5.0'
__version__ = '1.6.0dev'

if sys.version_info >= (3, 0):
from watermark.watermark import *
Expand Down
16 changes: 16 additions & 0 deletions watermark/watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class WaterMark(Magics):
help='prints current Git commit hash')
@argument('-r', '--gitrepo', action='store_true',
help='prints current Git remote address')
@argument('-b', '--gitbranch', action='store_true',
help='prints current Git branch')
@argument('-w', '--watermark', action='store_true',
help='prints the current version of watermark')
@argument('-iv', '--iversions', action='store_true',
Expand Down Expand Up @@ -127,6 +129,8 @@ def watermark(self, line):
self._get_commit_hash(bool(args.machine))
if args.gitrepo:
self._get_git_remote_origin(bool(args.machine))
if args.gitbranch:
self._get_git_branch(bool(args.machine))
if args.iversions:
self._print_all_import_versions(self.shell.user_ns)
if args.watermark:
Expand Down Expand Up @@ -209,6 +213,18 @@ def _get_git_remote_origin(self, machine):
self.out += '\nGit repo%s: %s' % (space,
git_remote_origin.decode("utf-8"))

def _get_git_branch(self, machine):
process = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref',
'HEAD'],
shell=False,
stdout=subprocess.PIPE)
git_branch = process.communicate()[0].strip()
space = ''
if machine:
space = ' '
self.out += '\nGit branch%s: %s' % (space,
git_branch.decode("utf-8"))

@staticmethod
def _print_all_import_versions(vars):
for val in list(vars.values()):
Expand Down