Fix git.latest failure when rev is not the default branch

This commit is contained in:
Erik Johnson 2018-01-11 15:26:35 -06:00
parent 0959ae4ea3
commit 0cbc6767bf
No known key found for this signature in database
GPG Key ID: 5E5583C437808F3F
2 changed files with 41 additions and 1 deletions

View File

@ -802,6 +802,13 @@ def latest(name,
)
remote_loc = None
if depth is not None and remote_rev_type != 'branch':
return _fail(
ret,
'When \'depth\' is used, \'rev\' must be set to the name of a '
'branch on the remote repository'
)
if remote_rev is None and not bare:
if rev != 'HEAD':
# A specific rev is desired, but that rev doesn't exist on the
@ -1679,7 +1686,7 @@ def latest(name,
if remote != 'origin':
clone_opts.extend(['--origin', remote])
if depth is not None:
clone_opts.extend(['--depth', str(depth)])
clone_opts.extend(['--depth', str(depth), '--branch', rev])
# We're cloning a fresh repo, there is no local branch or revision
local_branch = local_rev = None

View File

@ -401,6 +401,39 @@ class GitTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
if exc.errno != errno.ENOENT:
raise exc
def test_latest_depth(self):
'''
Test running git.latest state using the "depth" argument to limit the
history. See #45394.
'''
name = os.path.join(integration.TMP, 'salt_repo')
try:
ret = self.run_state(
'git.latest',
name='https://{0}/saltstack/salt-test-repo.git'.format(self.__domain),
rev='HEAD',
target=name,
depth=1
)
# HEAD is not a branch, this should fail
self.assertSaltFalseReturn(ret)
self.assertIn(
'must be set to the name of a branch',
ret[next(iter(ret))]['comment']
)
ret = self.run_state(
'git.latest',
name='https://{0}/saltstack/salt-test-repo.git'.format(self.__domain),
rev='non-default-branch',
target=name,
depth=1
)
self.assertSaltTrueReturn(ret)
self.assertTrue(os.path.isdir(os.path.join(name, '.git')))
finally:
shutil.rmtree(name, ignore_errors=True)
def test_present(self):
'''
git.present