@ -63,6 +63,12 @@ examples:
import re
import re
import tempfile
import tempfile
def _run(args):
cmd = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate()
rc = cmd.returncode
return (rc, out, err)
def get_version(dest):
def get_version(dest):
''' samples the version of the git repo '''
''' samples the version of the git repo '''
os.chdir(dest)
os.chdir(dest)
@ -77,12 +83,7 @@ def clone(repo, dest):
os.makedirs(os.path.dirname(dest))
os.makedirs(os.path.dirname(dest))
except:
except:
pass
pass
cmd = "git clone %s %s" % (repo, dest)
return _run("git clone %s %s" % (repo, dest))
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=tempfile.gettempdir())
(out, err) = cmd.communicate()
rc = cmd.returncode
return (rc, out, err)
def has_local_mods(dest):
def has_local_mods(dest):
os.chdir(dest)
os.chdir(dest)
@ -100,19 +101,13 @@ def reset(module,dest,force):
os.chdir(dest)
os.chdir(dest)
if not force and has_local_mods(dest):
if not force and has_local_mods(dest):
module.fail_json(msg="Local modifications exist in repository (force=no).")
module.fail_json(msg="Local modifications exist in repository (force=no).")
cmd = "git reset --hard HEAD"
return _run("git reset --hard HEAD")
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = cmd.communicate()
rc = cmd.returncode
return (rc, out, err)
def get_branches(module, dest):
def get_branches(module, dest):
os.chdir(dest)
os.chdir(dest)
branches = []
branches = []
cmd = "git branch -a"
(rc, out, err) = _run("git branch -a")
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if rc != 0:
out, err = cmd.communicate()
if cmd.returncode != 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())
@ -156,39 +151,25 @@ def is_not_a_branch(module, dest):
def get_head_branch(module, dest, remote):
def get_head_branch(module, dest, remote):
os.chdir(dest)
os.chdir(dest)
head = ''
head = ''
cmd = "git remote show %s" % remote
(rc, out, err) = _run("git remote show %s" % remote)
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if rc != 0:
out, err = cmd.communicate()
if cmd.returncode != 0:
module.fail_json(msg="Could not determine HEAD branch via git remote show")
module.fail_json(msg="Could not determine HEAD branch via git remote show")
for line in out.split('\n'):
for line in out.split('\n'):
if 'HEAD branch' in line:
if 'HEAD branch' in line:
head = line.split()[-1].strip()
head = line.split()[-1].strip()
return head
return head
def pull (module, repo, dest, version, remote):
def fetch (module, repo, dest, version, remote):
''' updates repo from remote sources '''
''' updates repo from remote sources '''
os.chdir(dest)
os.chdir(dest)
branches = get_branches(module, dest)
(rc, out1, err1) = _run("git fetch %s" % remote)
cur_branch = ''
for b in branches:
if b.startswith('* '):
cur_branch = b
if is_local_branch(module, dest, version) and not is_current_branch(module, dest, version):
(rc, out, err) = switch_version(module, dest, remote, version)
if rc != 0:
if rc != 0:
module.fail_json(msg=err)
module.fail_json(msg="Failed to download remote objects and refs")
if is_not_a_branch(module, dest):
head_branch = get_head_branch(module, dest, remote)
(rc, out, err) = switch_version(module, dest, remote, head_branch)
if rc != 0:
module.fail_json(msg=err)
cmd = "git pull -u %s" % remote
(rc, out2, err2) = _run("git fetch --tags %s" % remote)
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if rc != 0:
out, err = cmd.communicate()
module.fail_json(msg="Failed to download remote objects and refs")
rc = cmd.returncode
return (rc, out1 + out2, err1 + err2)
return (rc, out, err)
def switch_version(module, dest, remote, version):
def switch_version(module, dest, remote, version):
''' once pulled, switch to a particular SHA, tag, or branch '''
''' once pulled, switch to a particular SHA, tag, or branch '''
@ -202,10 +183,7 @@ def switch_version(module, dest, remote, version):
else:
else:
# is there a better way to do this?
# is there a better way to do this?
cmd = "git rebase %s" % remote
cmd = "git rebase %s" % remote
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return _run(cmd)
(out, err) = cmd.communicate()
rc = cmd.returncode
return (rc, out, err)
# ===========================================
# ===========================================
@ -245,7 +223,7 @@ def main():
(rc, out, err) = reset(module,dest,force)
(rc, out, err) = reset(module,dest,force)
if rc != 0:
if rc != 0:
module.fail_json(msg=err)
module.fail_json(msg=err)
(rc, out, err) = pull (module, repo, dest, version, remote)
(rc, out, err) = fetch (module, repo, dest, version, remote)
if rc != 0:
if rc != 0:
module.fail_json(msg=err)
module.fail_json(msg=err)