More work in git module

Rename pull() to fetch().  It does a git fetch and then a
git fetch --tags.
Add _run() method to handle all subprocess.Popen calls.  Change
all previous calls to subprocess.Popen to use _run().
pull/1648/head
Stephen Fromm 12 years ago
parent 75d3b77454
commit ccc04cf891

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

Loading…
Cancel
Save