Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from bloom.util import check_output
check_call('git init .', shell=True, cwd=orig_dir, stdout=PIPE)
check_call('touch example.txt', shell=True, cwd=orig_dir, stdout=PIPE)
check_call('git add *', shell=True, cwd=orig_dir, stdout=PIPE)
check_call('git commit -m "Init"', shell=True, cwd=orig_dir, stdout=PIPE)
check_call('git branch bloom', shell=True, cwd=orig_dir, stdout=PIPE)
check_call('git branch upstream', shell=True, cwd=orig_dir, stdout=PIPE)
check_call('git branch refactor', shell=True, cwd=orig_dir, stdout=PIPE)
from vcstools import VcsClient
clone = VcsClient('git', clone_dir)
clone.checkout('file://{0}'.format(orig_dir), 'master')
output = check_output('git branch --no-color', shell=True, cwd=clone_dir)
assert output == '* master\n'
from bloom.git import track_branches
track_branches(['bloom', 'upstream'], clone_dir)
output = check_output('git branch --no-color', shell=True, cwd=clone_dir)
assert output == ' bloom\n* master\n upstream\n', \
'\n' + str(output) + '\n == \n' + ' bloom\n* master\n upstream\n'
track_branches(directory=clone_dir)
output = check_output('git branch --no-color', shell=True, cwd=clone_dir)
assert output == ' bloom\n* master\n refactor\n upstream\n', \
output + ' == ` bloom\n* master\n refactor\n upstream\n`'
track_branches(['fake'], clone_dir)
output = check_output('git branch', shell=True, cwd=clone_dir)
assert output.count('fake') == 0
rmtree(tmp_dir)
def get_commit_hash(reference, directory=None):
"""
Returns the SHA-1 commit hash for the given reference.
:param reference: any git reference (branch or tag) to resolve to SHA-1
:param directory: directory in which to preform this action
:returns: SHA-1 commit hash for the given reference
:raises: subprocess.CalledProcessError if any git calls fail
"""
# Track remote branch
if branch_exists(reference, local_only=False, directory=directory):
if not branch_exists(reference, local_only=True, directory=directory):
track_branches(reference, directory)
cmd = 'git show-branch --sha1-name ' + reference
out = check_output(cmd, shell=True, cwd=directory)
return out.split('[')[1].split(']')[0]
def convert_old_bloom_conf(prefix=None):
prefix = prefix if prefix is not None else 'convert'
tracks_dict = get_tracks_dict_raw()
track = prefix
track_count = 0
while track in tracks_dict['tracks']:
track_count += 1
track = prefix + str(track_count)
track_dict = copy.copy(DEFAULT_TEMPLATE)
cmd = 'git config -f bloom.conf bloom.upstream'
upstream_repo = check_output(cmd, shell=True).strip()
cmd = 'git config -f bloom.conf bloom.upstreamtype'
upstream_type = check_output(cmd, shell=True).strip()
try:
cmd = 'git config -f bloom.conf bloom.upstreambranch'
upstream_branch = check_output(cmd, shell=True).strip()
except subprocess.CalledProcessError:
upstream_branch = ''
for key in template_entry_order:
if key == 'vcs_uri':
track_dict[key] = upstream_repo
continue
if key == 'vcs_type':
track_dict[key] = upstream_type
continue
if key == 'vcs_uri':
track_dict[key] = upstream_branch or None
continue
track_dict[key] = track_dict[key].default
debug('Converted bloom.conf:')
with open('bloom.conf', 'r') as f:
debug(f.read())
def get_tags(directory=None):
"""
Returns a list of tags in the git repository.
:param directory: directory in which to preform this action
:returns: list of tags
:raises: subprocess.CalledProcessError if any git calls fail
"""
out = check_output('git tag -l', shell=True, cwd=directory)
return [l.strip() for l in out.splitlines()]
def get_root(directory=None):
"""
Returns the git root directory above the given dir.
If the given dir is not in a git repository, None is returned.
:param directory: directory to query from, if None the cwd is used
:returns: root of git repository or None if not a git repository
"""
cmd = 'git rev-parse --show-toplevel'
try:
output = check_output(cmd, shell=True, cwd=directory, stderr=PIPE)
except CalledProcessError:
return None
return output.strip()
def convert_old_bloom_conf(prefix=None):
prefix = prefix if prefix is not None else 'convert'
tracks_dict = get_tracks_dict_raw()
track = prefix
track_count = 0
while track in tracks_dict['tracks']:
track_count += 1
track = prefix + str(track_count)
track_dict = copy.copy(DEFAULT_TEMPLATE)
cmd = 'git config -f bloom.conf bloom.upstream'
upstream_repo = check_output(cmd, shell=True).strip()
cmd = 'git config -f bloom.conf bloom.upstreamtype'
upstream_type = check_output(cmd, shell=True).strip()
try:
cmd = 'git config -f bloom.conf bloom.upstreambranch'
upstream_branch = check_output(cmd, shell=True).strip()
except subprocess.CalledProcessError:
upstream_branch = ''
for key in template_entry_order:
if key == 'vcs_uri':
track_dict[key] = upstream_repo
continue
if key == 'vcs_type':
track_dict[key] = upstream_type
continue
if key == 'vcs_uri':
track_dict[key] = upstream_branch or None
def get_current_branch(directory=None):
"""
Returns the current git branch by parsing the output of `git branch`
This will raise a RuntimeError if the current working directory is not
a git repository. If no branch could be determined it will return None,
i.e. (no branch) will return None.
:param directory: directory in which to run the command
:returns: current git branch or None if none can be determined, (no branch)
:raises: subprocess.CalledProcessError if git command fails
"""
cmd = 'git branch --no-color'
output = check_output(cmd, shell=True, cwd=directory)
output = output.splitlines()
for token in output:
if token.strip().startswith('*'):
token = token[2:]
if token == '(no branch)':
return None
return token
return None