1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #4539 from sayap/git-sha1

git: Always return the before/after revisions, even in check mode.
This commit is contained in:
jctanner 2013-10-17 11:36:03 -07:00
commit 12eaefb7e6

View file

@ -41,7 +41,8 @@ options:
default: "HEAD" default: "HEAD"
description: description:
- What version of the repository to check out. This can be the - What version of the repository to check out. This can be the
git I(SHA), the literal string C(HEAD), a branch name, or a tag name. full 40-character I(SHA-1) hash, the literal string C(HEAD), a
branch name, or a tag name.
remote: remote:
required: false required: false
default: "origin" default: "origin"
@ -107,7 +108,7 @@ import tempfile
def get_version(git_path, dest): def get_version(git_path, dest):
''' samples the version of the git repo ''' ''' samples the version of the git repo '''
os.chdir(dest) os.chdir(dest)
cmd = "%s show --abbrev-commit" % (git_path,) cmd = "%s show" % (git_path,)
sha = os.popen(cmd).read().split("\n") sha = os.popen(cmd).read().split("\n")
sha = sha[0].split()[1] sha = sha[0].split()[1]
return sha return sha
@ -121,8 +122,9 @@ def clone(git_path, module, repo, dest, remote, depth, version):
pass pass
os.chdir(dest_dirname) os.chdir(dest_dirname)
cmd = [ git_path, 'clone', '-o', remote, '--recursive' ] cmd = [ git_path, 'clone', '-o', remote, '--recursive' ]
if version and version != 'HEAD': if is_remote_branch(git_path, module, dest, repo, version) \
cmd.extend([ '--branch', str(version) ]) or is_remote_tag(git_path, module, dest, repo, version):
cmd.extend([ '--branch', version ])
if depth: if depth:
cmd.extend([ '--depth', str(depth) ]) cmd.extend([ '--depth', str(depth) ])
cmd.extend([ repo, dest ]) cmd.extend([ repo, dest ])
@ -135,24 +137,30 @@ def has_local_mods(git_path, dest):
lines = filter(lambda c: not re.search('^\\?\\?.*$', c), lines) lines = filter(lambda c: not re.search('^\\?\\?.*$', c), lines)
return len(lines) > 0 return len(lines) > 0
def reset(git_path, module, dest, force): def reset(git_path, module, dest):
''' '''
Resets the index and working tree to HEAD. Resets the index and working tree to HEAD.
Discards any changes to tracked files in working Discards any changes to tracked files in working
tree since that commit. tree since that commit.
''' '''
os.chdir(dest) os.chdir(dest)
if not force and has_local_mods(git_path, dest):
module.fail_json(msg="Local modifications exist in repository (force=no).")
cmd = "%s reset --hard HEAD" % (git_path,) cmd = "%s reset --hard HEAD" % (git_path,)
return module.run_command(cmd, check_rc=True) return module.run_command(cmd, check_rc=True)
def get_remote_head(git_path, module, dest, version, remote): def get_remote_head(git_path, module, dest, version, remote):
cmd = '' cloning = False
os.chdir(dest) if remote == module.params['repo']:
cloning = True
else:
os.chdir(dest)
if version == 'HEAD': if version == 'HEAD':
version = get_head_branch(git_path, module, dest, remote) if cloning:
if is_remote_branch(git_path, module, dest, remote, version): # cloning the repo, just get the remote's HEAD version
cmd = '%s ls-remote %s -h HEAD' % (git_path, remote)
else:
head_branch = get_head_branch(git_path, module, dest, remote)
cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, head_branch)
elif is_remote_branch(git_path, module, dest, remote, version):
cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version) cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version)
elif is_remote_tag(git_path, module, dest, remote, version): elif is_remote_tag(git_path, module, dest, remote, version):
cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version) cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version)
@ -167,9 +175,8 @@ def get_remote_head(git_path, module, dest, version, remote):
return rev return rev
def is_remote_tag(git_path, module, dest, remote, version): def is_remote_tag(git_path, module, dest, remote, version):
os.chdir(dest)
cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version) cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version)
(rc, out, err) = module.run_command(cmd) (rc, out, err) = module.run_command(cmd, check_rc=True)
if version in out: if version in out:
return True return True
else: else:
@ -186,10 +193,10 @@ def get_branches(git_path, module, dest):
branches.append(line.strip()) branches.append(line.strip())
return branches return branches
def is_remote_branch(git_path, module, dest, remote, branch): def is_remote_branch(git_path, module, dest, remote, version):
branches = get_branches(git_path, module, dest) cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version)
rbranch = 'remotes/%s/%s' % (remote, branch) (rc, out, err) = module.run_command(cmd, check_rc=True)
if rbranch in branches: if version in out:
return True return True
else: else:
return False return False
@ -335,8 +342,9 @@ def main():
local_mods = False local_mods = False
if not os.path.exists(gitconfig): if not os.path.exists(gitconfig):
if module.check_mode: if module.check_mode:
module.exit_json(changed=True) remote_head = get_remote_head(git_path, module, dest, version, repo)
(rc, out, err) = clone(git_path, module, repo, dest, remote, depth, version) module.exit_json(changed=True, before=before, after=remote_head)
clone(git_path, module, repo, dest, remote, depth, version)
elif not update: elif not update:
# Just return having found a repo already in the dest path # Just return having found a repo already in the dest path
# this does no checking that the repo is the actual repo # this does no checking that the repo is the actual repo
@ -347,44 +355,28 @@ def main():
# else do a pull # else do a pull
local_mods = has_local_mods(git_path, dest) local_mods = has_local_mods(git_path, dest)
before = get_version(git_path, dest) before = get_version(git_path, dest)
# if force, do a reset
if local_mods and module.check_mode:
module.exit_json(changed=True, msg='Local modifications exist')
(rc, out, err) = reset(git_path, module, dest, force)
if rc != 0:
module.fail_json(msg=err)
# exit if already at desired sha version
# abbreviate version in case full sha is given
if before == str(version)[:7]:
module.exit_json(changed=False)
# check or get changes from remote
remote_head = get_remote_head(git_path, module, dest, version, remote) remote_head = get_remote_head(git_path, module, dest, version, remote)
if module.check_mode: if local_mods:
changed = False # failure should happen regardless of check mode
if remote_head == version: if not force:
# get_remote_head returned version as-is module.fail_json(msg="Local modifications exist in repository (force=no).")
# were given a sha1 object, see if it is present # if force and in non-check mode, do a reset
(rc, out, err) = module.run_command("%s show %s" % (git_path, version)) if not module.check_mode:
if version in out: reset(git_path, module, dest)
changed = False # exit if already at desired sha version
else: if before == remote_head:
changed = True if local_mods:
module.exit_json(changed=True, before=before, after=remote_head,
msg="Local modifications exist")
else: else:
remote_head = remote_head[0:7] module.exit_json(changed=False, before=before, after=remote_head)
if before != remote_head: if module.check_mode:
changed = True module.exit_json(changed=True, before=before, after=remote_head)
else: fetch(git_path, module, repo, dest, version, remote)
changed = False
module.exit_json(changed=changed, before=before, after=remote_head)
(rc, out, err) = fetch(git_path, module, repo, dest, version, remote)
if rc != 0:
module.fail_json(msg=err)
# switch to version specified regardless of whether # switch to version specified regardless of whether
# we cloned or pulled # we cloned or pulled
(rc, out, err) = switch_version(git_path, module, dest, remote, version) switch_version(git_path, module, dest, remote, version)
if rc != 0:
module.fail_json(msg=err)
# determine if we changed anything # determine if we changed anything
after = get_version(git_path, dest) after = get_version(git_path, dest)