Merge pull request #3069 from benjixx/consistently-get-bin-path-for-git

consistently use module.get_bin_path('git', True) to get git path before...
reviewable/pr18780/r1
Michael DeHaan 12 years ago
commit 6de520f996

@ -85,15 +85,15 @@ examples:
import re import re
import tempfile import tempfile
def get_version(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 = "git show --abbrev-commit" cmd = "%s show --abbrev-commit" % (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
def clone(module, repo, dest, remote, depth): def clone(git_path, module, repo, dest, remote, depth):
''' makes a new git repo if it does not already exist ''' ''' makes a new git repo if it does not already exist '''
dest_dirname = os.path.dirname(dest) dest_dirname = os.path.dirname(dest)
try: try:
@ -101,39 +101,40 @@ def clone(module, repo, dest, remote, depth):
except: except:
pass pass
os.chdir(dest_dirname) os.chdir(dest_dirname)
cmd = [ module.get_bin_path('git', True), 'clone', '-o', remote ] cmd = [ git_path, 'clone', '-o', remote ]
if depth: if depth:
cmd.extend([ '--depth', str(depth) ]) cmd.extend([ '--depth', str(depth) ])
cmd.extend([ repo, dest ]) cmd.extend([ repo, dest ])
return module.run_command(cmd, check_rc=True) return module.run_command(cmd, check_rc=True)
def has_local_mods(dest): def has_local_mods(git_path, dest):
os.chdir(dest) os.chdir(dest)
cmd = "git status -s" cmd = "%s status -s" % (git_path,)
lines = os.popen(cmd).read().splitlines() lines = os.popen(cmd).read().splitlines()
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(module, dest, force): def reset(git_path, module, dest, force):
''' '''
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(dest): if not force and has_local_mods(git_path, dest):
module.fail_json(msg="Local modifications exist in repository (force=no).") module.fail_json(msg="Local modifications exist in repository (force=no).")
return module.run_command("git reset --hard HEAD", check_rc=True) cmd = "%s reset --hard HEAD" % (git_path,)
return module.run_command(cmd, check_rc=True)
def get_remote_head(module, dest, version, remote): def get_remote_head(git_path, module, dest, version, remote):
cmd = '' cmd = ''
os.chdir(dest) os.chdir(dest)
if version == 'HEAD': if version == 'HEAD':
version = get_head_branch(module, dest, remote) version = get_head_branch(git_path, module, dest, remote)
if is_remote_branch(module, dest, remote, version): if is_remote_branch(git_path, module, dest, remote, version):
cmd = 'git ls-remote %s -h refs/heads/%s' % (remote, version) cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version)
elif is_remote_tag(module, dest, remote, version): elif is_remote_tag(git_path, module, dest, remote, version):
cmd = 'git ls-remote %s -t refs/tags/%s' % (remote, version) cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version)
else: else:
# appears to be a sha1. return as-is since it appears # appears to be a sha1. return as-is since it appears
# cannot check for a specific sha1 on remote # cannot check for a specific sha1 on remote
@ -144,35 +145,36 @@ def get_remote_head(module, dest, version, remote):
rev = out.split()[0] rev = out.split()[0]
return rev return rev
def is_remote_tag(module, dest, remote, version): def is_remote_tag(git_path, module, dest, remote, version):
os.chdir(dest) os.chdir(dest)
cmd = 'git ls-remote %s -t refs/tags/%s' % (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)
if version in out: if version in out:
return True return True
else: else:
return False return False
def get_branches(module, dest): def get_branches(git_path, module, dest):
os.chdir(dest) os.chdir(dest)
branches = [] branches = []
(rc, out, err) = module.run_command("git branch -a") cmd = '%s branch -a' % (git_path,)
(rc, out, err) = module.run_command(cmd)
if rc != 0: if rc != 0:
module.fail_json(msg="Could not determine branch data - received %s" % out) module.fail_json(msg="Could not determine branch data - received %s" % out)
for line in out.split('\n'): for line in out.split('\n'):
branches.append(line.strip()) branches.append(line.strip())
return branches return branches
def is_remote_branch(module, dest, remote, branch): def is_remote_branch(git_path, module, dest, remote, branch):
branches = get_branches(module, dest) branches = get_branches(git_path, module, dest)
rbranch = 'remotes/%s/%s' % (remote, branch) rbranch = 'remotes/%s/%s' % (remote, branch)
if rbranch in branches: if rbranch in branches:
return True return True
else: else:
return False return False
def is_local_branch(module, dest, branch): def is_local_branch(git_path, module, dest, branch):
branches = get_branches(module, dest) branches = get_branches(git_path, module, dest)
lbranch = '%s' % branch lbranch = '%s' % branch
if lbranch in branches: if lbranch in branches:
return True return True
@ -181,8 +183,8 @@ def is_local_branch(module, dest, branch):
else: else:
return False return False
def is_current_branch(module, dest, branch): def is_current_branch(git_path, module, dest, branch):
branches = get_branches(module, dest) branches = get_branches(git_path, module, dest)
for b in branches: for b in branches:
if b.startswith('* '): if b.startswith('* '):
cur_branch = b cur_branch = b
@ -191,14 +193,14 @@ def is_current_branch(module, dest, branch):
else: else:
return True return True
def is_not_a_branch(module, dest): def is_not_a_branch(git_path, module, dest):
branches = get_branches(module, dest) branches = get_branches(git_path, module, dest)
for b in branches: for b in branches:
if b.startswith('* ') and 'no branch' in b: if b.startswith('* ') and 'no branch' in b:
return True return True
return False return False
def get_head_branch(module, dest, remote): def get_head_branch(git_path, module, dest, remote):
''' '''
Determine what branch HEAD is associated with. This is partly Determine what branch HEAD is associated with. This is partly
taken from lib/ansible/utils/__init__.py. It finds the correct taken from lib/ansible/utils/__init__.py. It finds the correct
@ -222,46 +224,46 @@ def get_head_branch(module, dest, remote):
# If we're in a detached HEAD state, look up the branch associated with # If we're in a detached HEAD state, look up the branch associated with
# the remote HEAD in .git/refs/remotes/<remote>/HEAD # the remote HEAD in .git/refs/remotes/<remote>/HEAD
f = open(os.path.join(repo_path, "HEAD")) f = open(os.path.join(repo_path, "HEAD"))
if is_not_a_branch(module, dest): if is_not_a_branch(git_path, module, dest):
f.close() f.close()
f = open(os.path.join(repo_path, 'refs', 'remotes', remote, 'HEAD')) f = open(os.path.join(repo_path, 'refs', 'remotes', remote, 'HEAD'))
branch = f.readline().split('/')[-1].rstrip("\n") branch = f.readline().split('/')[-1].rstrip("\n")
f.close() f.close()
return branch return branch
def fetch(module, repo, dest, version, remote): def fetch(git_path, module, repo, dest, version, remote):
''' updates repo from remote sources ''' ''' updates repo from remote sources '''
os.chdir(dest) os.chdir(dest)
(rc, out1, err1) = module.run_command("git fetch %s" % remote) (rc, out1, err1) = module.run_command("%s fetch %s" % (git_path, remote))
if rc != 0: if rc != 0:
module.fail_json(msg="Failed to download remote objects and refs") module.fail_json(msg="Failed to download remote objects and refs")
(rc, out2, err2) = module.run_command("git fetch --tags %s" % remote) (rc, out2, err2) = module.run_command("%s fetch --tags %s" % (git_path, remote))
if rc != 0: if rc != 0:
module.fail_json(msg="Failed to download remote objects and refs") module.fail_json(msg="Failed to download remote objects and refs")
return (rc, out1 + out2, err1 + err2) return (rc, out1 + out2, err1 + err2)
def switch_version(module, dest, remote, version): def switch_version(git_path, module, dest, remote, version):
''' once pulled, switch to a particular SHA, tag, or branch ''' ''' once pulled, switch to a particular SHA, tag, or branch '''
os.chdir(dest) os.chdir(dest)
cmd = '' cmd = ''
if version != 'HEAD': if version != 'HEAD':
if is_remote_branch(module, dest, remote, version): if is_remote_branch(git_path, module, dest, remote, version):
if not is_local_branch(module, dest, version): if not is_local_branch(git_path, module, dest, version):
cmd = "git checkout --track -b %s %s/%s" % (version, remote, version) cmd = "%s checkout --track -b %s %s/%s" % (git_path, version, remote, version)
else: else:
(rc, out, err) = module.run_command("git checkout --force %s" % version) (rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, version))
if rc != 0: if rc != 0:
module.fail_json(msg="Failed to checkout branch %s" % version) module.fail_json(msg="Failed to checkout branch %s" % version)
cmd = "git reset --hard %s/%s" % (remote, version) cmd = "%s reset --hard %s/%s" % (git_path, remote, version)
else: else:
cmd = "git checkout --force %s" % version cmd = "%s checkout --force %s" % (git_path, version)
else: else:
branch = get_head_branch(module, dest, remote) branch = get_head_branch(git_path, module, dest, remote)
(rc, out, err) = module.run_command("git checkout --force %s" % branch) (rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, branch))
if rc != 0: if rc != 0:
module.fail_json(msg="Failed to checkout branch %s" % branch) module.fail_json(msg="Failed to checkout branch %s" % branch)
cmd = "git reset --hard %s" % remote cmd = "%s reset --hard %s" % (git_path, remote)
return module.run_command(cmd, check_rc=True) return module.run_command(cmd, check_rc=True)
# =========================================== # ===========================================
@ -288,6 +290,7 @@ def main():
depth = module.params['depth'] depth = module.params['depth']
update = module.params['update'] update = module.params['update']
git_path = module.get_bin_path('git', True)
gitconfig = os.path.join(dest, '.git', 'config') gitconfig = os.path.join(dest, '.git', 'config')
rc, out, err, status = (0, None, None, None) rc, out, err, status = (0, None, None, None)
@ -299,31 +302,31 @@ def main():
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) module.exit_json(changed=True)
(rc, out, err) = clone(module, repo, dest, remote, depth) (rc, out, err) = clone(git_path, module, repo, dest, remote, depth)
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
# requested. # requested.
before = get_version(dest) before = get_version(git_path, dest)
module.exit_json(changed=False, before=before, after=before) module.exit_json(changed=False, before=before, after=before)
else: else:
# else do a pull # else do a pull
local_mods = has_local_mods(dest) local_mods = has_local_mods(git_path, dest)
before = get_version(dest) before = get_version(git_path, dest)
# if force, do a reset # if force, do a reset
if local_mods and module.check_mode: if local_mods and module.check_mode:
module.exit_json(changed=True, msg='Local modifications exist') module.exit_json(changed=True, msg='Local modifications exist')
(rc, out, err) = reset(module,dest,force) (rc, out, err) = reset(git_path, module, dest, force)
if rc != 0: if rc != 0:
module.fail_json(msg=err) module.fail_json(msg=err)
# check or get changes from remote # check or get changes from remote
remote_head = get_remote_head(module, dest, version, remote) remote_head = get_remote_head(git_path, module, dest, version, remote)
if module.check_mode: if module.check_mode:
changed = False changed = False
if remote_head == version: if remote_head == version:
# get_remote_head returned version as-is # get_remote_head returned version as-is
# were given a sha1 object, see if it is present # were given a sha1 object, see if it is present
(rc, out, err) = module.run_command("git show %s" % version) (rc, out, err) = module.run_command("%s show %s" % (git_path, version))
if version in out: if version in out:
changed = False changed = False
else: else:
@ -335,16 +338,16 @@ def main():
else: else:
changed = False changed = False
module.exit_json(changed=changed, before=before, after=remote_head) module.exit_json(changed=changed, before=before, after=remote_head)
(rc, out, err) = fetch(module, repo, dest, version, remote) (rc, out, err) = fetch(git_path, module, repo, dest, version, remote)
if rc != 0: if rc != 0:
module.fail_json(msg=err) 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(module, dest, remote, version) (rc, out, err) = switch_version(git_path, module, dest, remote, version)
# determine if we changed anything # determine if we changed anything
after = get_version(dest) after = get_version(git_path, dest)
changed = False changed = False
if before != after or local_mods: if before != after or local_mods:

Loading…
Cancel
Save