Don't check local rev/branch until after we know the target dir exists

Also made lint fixes.
This commit is contained in:
Erik Johnson 2015-08-15 01:13:59 -05:00
parent afa435b0a4
commit 21305e7719

View File

@ -21,7 +21,6 @@ import copy
import logging import logging
import os import os
import re import re
import shutil
import string import string
# Import salt libs # Import salt libs
@ -54,8 +53,8 @@ def _parse_fetch(output):
Go through the output from a git fetch and return a dict Go through the output from a git fetch and return a dict
''' '''
update_re = re.compile( update_re = re.compile(
'.*(?:([0-9a-f]+)\.\.([0-9a-f]+)|' r'.*(?:([0-9a-f]+)\.\.([0-9a-f]+)|'
'\[(?:new (tag|branch)|tag update)\])\s+(.+)->' r'\[(?:new (tag|branch)|tag update)\])\s+(.+)->'
) )
ret = {} ret = {}
for line in output.splitlines(): for line in output.splitlines():
@ -63,7 +62,7 @@ def _parse_fetch(output):
if match: if match:
old_sha, new_sha, new_ref_type, ref_name = \ old_sha, new_sha, new_ref_type, ref_name = \
match.groups() match.groups()
ref_name = refname.rstrip() ref_name = ref_name.rstrip()
if new_ref_type is not None: if new_ref_type is not None:
# ref is a new tag/branch # ref is a new tag/branch
ref_key = 'new tags' \ ref_key = 'new tags' \
@ -80,16 +79,29 @@ def _parse_fetch(output):
return ret return ret
def _get_local_rev(target, user): def _get_local_rev_and_branch(target, user):
''' '''
Return the local revision for before/after comparisons Return the local revision for before/after comparisons
''' '''
log.info('Checking local revision for {0}'.format(target)) log.info('Checking local revision for {0}'.format(target))
try: try:
return __salt__['git.revision'](target, user=user, ignore_retcode=True) local_rev = __salt__['git.revision'](target,
user=user,
ignore_retcode=True)
except CommandExecutionError: except CommandExecutionError:
log.info('No local revision for {0}'.format(target)) log.info('No local revision for {0}'.format(target))
return None local_rev = None
log.info('Checking local branch for {0}'.format(target))
try:
local_branch = __salt__['git.current_branch'](target,
user=user,
ignore_retcode=True)
except CommandExecutionError:
log.info('No local branch for {0}'.format(target))
local_branch = None
return local_rev, local_branch
def _strip_exc(exc): def _strip_exc(exc):
@ -391,16 +403,6 @@ def latest(name,
'+refs/tags/*:refs/tags/*' '+refs/tags/*:refs/tags/*'
] if fetch_tags else [] ] if fetch_tags else []
local_rev = _get_local_rev(target, user)
log.info('Checking local branch for {0}'.format(target))
try:
local_branch = __salt__['git.current_branch'](
target, user=user, ignore_retcode=True)
except CommandExecutionError:
log.info('No local branch for {0}'.format(target))
local_branch = None
log.info('Checking remote revision for {0}'.format(name)) log.info('Checking remote revision for {0}'.format(name))
remote_rev_matches = __salt__['git.ls_remote']( remote_rev_matches = __salt__['git.ls_remote'](
None, None,
@ -444,12 +446,6 @@ def latest(name,
'No revision matching \'{0}\' exists in the remote ' 'No revision matching \'{0}\' exists in the remote '
'repository'.format(rev) 'repository'.format(rev)
) )
elif local_rev is not None and remote_rev is None:
return _fail(
ret,
'Remote repository is empty, cannot update from {0} to an '
'empty repository'.format(local_rev[:7])
)
check = 'refs' if bare else '.git' check = 'refs' if bare else '.git'
gitdir = os.path.join(target, check) gitdir = os.path.join(target, check)
@ -461,6 +457,14 @@ def latest(name,
target, user=user) target, user=user)
has_local_branch = local_branch in all_local_branches has_local_branch = local_branch in all_local_branches
all_local_tags = __salt__['git.list_tags'](target, user=user) all_local_tags = __salt__['git.list_tags'](target, user=user)
local_rev, local_branch = _get_local_rev_and_branch(target, user)
if local_rev is not None and remote_rev is None:
return _fail(
ret,
'Remote repository is empty, cannot update from {0} to an '
'empty repository'.format(local_rev[:7])
)
remotes = __salt__['git.remotes'](target, user=user) remotes = __salt__['git.remotes'](target, user=user)
if remote not in remotes \ if remote not in remotes \
@ -505,30 +509,28 @@ def latest(name,
# locally but account for this just in # locally but account for this just in
# case. # case.
local_tag_sha1 = None local_tag_sha1 = None
finally: if local_tag_sha1 != remote_rev \
if local_tag_sha1 != remote_rev \ and fast_forward is False:
and merge_action != 'hard-reset': # Remote tag is different than local tag,
# Remote tag is different than local # unless we're doing a hard reset then we
# tag, unless we're doing a hard reset # don't need to proceed as we know that the
# then we don't need to proceed as we # fetch will update the tag and the only
# know that the fetch will update the # way to make the state succeed is to reset
# tag and the only way to make the # the branch to point at the tag's new rev
# state succeed is to reset the branch return _fail(
# to point at the tag's new rev ret,
return _fail( '\'{0}\' is a tag, but the remote '
ret, 'SHA1 for this tag ({1}) doesn\'t '
'\'{0}\' is a tag, but the remote ' 'match the local SHA1 ({2}). Set '
'SHA1 for this tag ({1}) doesn\'t ' '\'force_reset\' to True to force '
'match the local SHA1 ({2}). Set ' 'this update.'.format(
'\'force_reset\' to True to force ' rev,
'this update.'.format( remote_rev[:7],
rev, local_tag_sha1[:7] if
remote_rev[:7], local_tag_sha1 is not None
local_tag_sha1[:7] if else None
local_tag_sha1 is not None
else None
)
) )
)
pre_rev = None pre_rev = None
if not has_remote_rev or not has_local_branch: if not has_remote_rev or not has_local_branch:
@ -691,7 +693,11 @@ def latest(name,
actions.append( actions.append(
'New branch \'{0}\' would be checked out, ' 'New branch \'{0}\' would be checked out, '
'with {1} ({2}) as a starting point' 'with {1} ({2}) as a starting point'
.format(branch, checkout_rev, remote_rev) .format(
branch,
desired_upstream if desired_upstream
else rev,
remote_rev)
) )
if desired_upstream: if desired_upstream:
actions.append( actions.append(
@ -797,7 +803,7 @@ def latest(name,
target, target,
remote_rev + '^{commit}', remote_rev + '^{commit}',
ignore_retcode=True) ignore_retcode=True)
except CommandExecutionError: except CommandExecutionError as exc:
return _fail( return _fail(
ret, ret,
'Fetch did not successfully retrieve remote rev ' 'Fetch did not successfully retrieve remote rev '
@ -1016,7 +1022,8 @@ def latest(name,
except OSError as exc: except OSError as exc:
return _fail( return _fail(
ret, ret,
'Unable to remove {0}: {1}'.format(target, exc) 'Unable to remove {0}: {1}'.format(target, exc),
comments
) )
else: else:
ret['changes']['forced clone'] = True ret['changes']['forced clone'] = True
@ -1043,8 +1050,6 @@ def latest(name,
) )
) )
try: try:
local_rev = _get_local_rev(target, user)
clone_opts = ['--mirror'] if mirror else ['--bare'] if bare else [] clone_opts = ['--mirror'] if mirror else ['--bare'] if bare else []
if remote != 'origin': if remote != 'origin':
clone_opts.extend(['--origin', remote]) clone_opts.extend(['--origin', remote])
@ -1091,17 +1096,20 @@ def latest(name,
log.error(msg.format(name)) log.error(msg.format(name))
return _fail(ret, msg.format('Repository'), comments) return _fail(ret, msg.format('Repository'), comments)
else: else:
local_rev, local_branch = \
_get_local_rev_and_branch(target, user)
all_local_branches = __salt__['git.list_branches']( all_local_branches = __salt__['git.list_branches'](
target, user=user) target, user=user)
has_local_branch = local_branch in all_local_branches has_local_branch = local_branch in all_local_branches
if remote_rev_type == 'tag' \ if remote_rev_type == 'tag' \
and rev not in __salt__['git.list_tags']( and rev not in __salt__['git.list_tags'](
target, user=user): target, user=user):
return _fail( return _fail(
ret, ret,
'Revision \'{0}\' does not exist in clone', 'Revision \'{0}\' does not exist in clone'
comments .format(rev),
) comments
)
if not has_local_branch: if not has_local_branch:
checkout_rev = desired_upstream if desired_upstream \ checkout_rev = desired_upstream if desired_upstream \
else rev else rev
@ -1255,8 +1263,7 @@ def present(name,
except OSError as exc: except OSError as exc:
return _fail( return _fail(
ret, ret,
'Unable to remove {0}: {1}'.format(name, exc), 'Unable to remove {0}: {1}'.format(name, exc)
comments
) )
else: else:
ret['changes']['forced init'] = True ret['changes']['forced init'] = True
@ -1310,7 +1317,7 @@ def config_unset(name,
repo=None, repo=None,
user=None, user=None,
**kwargs): **kwargs):
''' r'''
.. versionadded:: 2015.8.0 .. versionadded:: 2015.8.0
Ensure that the named config key is not present Ensure that the named config key is not present
@ -1443,7 +1450,6 @@ def config_unset(name,
) )
) )
if __opts__['test']: if __opts__['test']:
ret['changes'] = pre_matches ret['changes'] = pre_matches
return _neutral_test( return _neutral_test(