|
|
@ -181,11 +181,12 @@ def set_git_ssh(ssh_wrapper, key_file, ssh_opts):
|
|
|
|
if ssh_opts:
|
|
|
|
if ssh_opts:
|
|
|
|
os.environ["GIT_SSH_OPTS"] = ssh_opts
|
|
|
|
os.environ["GIT_SSH_OPTS"] = ssh_opts
|
|
|
|
|
|
|
|
|
|
|
|
def get_version(git_path, dest, ref="HEAD"):
|
|
|
|
def get_version(module, git_path, dest, ref="HEAD"):
|
|
|
|
''' samples the version of the git repo '''
|
|
|
|
''' samples the version of the git repo '''
|
|
|
|
os.chdir(dest)
|
|
|
|
|
|
|
|
cmd = "%s rev-parse %s" % (git_path, ref)
|
|
|
|
cmd = "%s rev-parse %s" % (git_path, ref)
|
|
|
|
sha = os.popen(cmd).read().rstrip("\n")
|
|
|
|
rc, stdout, stderr = module.run_command(cmd, cwd=dest)
|
|
|
|
|
|
|
|
sha = stdout.rstrip('\n')
|
|
|
|
return sha
|
|
|
|
return sha
|
|
|
|
|
|
|
|
|
|
|
|
def clone(git_path, module, repo, dest, remote, depth, version, bare, reference):
|
|
|
|
def clone(git_path, module, repo, dest, remote, depth, version, bare, reference):
|
|
|
@ -195,7 +196,6 @@ def clone(git_path, module, repo, dest, remote, depth, version, bare, reference)
|
|
|
|
os.makedirs(dest_dirname)
|
|
|
|
os.makedirs(dest_dirname)
|
|
|
|
except:
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
os.chdir(dest_dirname)
|
|
|
|
|
|
|
|
cmd = [ git_path, 'clone' ]
|
|
|
|
cmd = [ git_path, 'clone' ]
|
|
|
|
if bare:
|
|
|
|
if bare:
|
|
|
|
cmd.append('--bare')
|
|
|
|
cmd.append('--bare')
|
|
|
@ -209,19 +209,19 @@ def clone(git_path, module, repo, dest, remote, depth, version, bare, reference)
|
|
|
|
if reference:
|
|
|
|
if reference:
|
|
|
|
cmd.extend([ '--reference', str(reference) ])
|
|
|
|
cmd.extend([ '--reference', str(reference) ])
|
|
|
|
cmd.extend([ repo, dest ])
|
|
|
|
cmd.extend([ repo, dest ])
|
|
|
|
module.run_command(cmd, check_rc=True)
|
|
|
|
module.run_command(cmd, check_rc=True, cwd=dest_dirname)
|
|
|
|
if bare:
|
|
|
|
if bare:
|
|
|
|
os.chdir(dest)
|
|
|
|
|
|
|
|
if remote != 'origin':
|
|
|
|
if remote != 'origin':
|
|
|
|
module.run_command([git_path, 'remote', 'add', remote, repo], check_rc=True)
|
|
|
|
module.run_command([git_path, 'remote', 'add', remote, repo], check_rc=True, cwd=dest)
|
|
|
|
|
|
|
|
|
|
|
|
def has_local_mods(git_path, dest, bare):
|
|
|
|
def has_local_mods(module, git_path, dest, bare):
|
|
|
|
if bare:
|
|
|
|
if bare:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
os.chdir(dest)
|
|
|
|
|
|
|
|
cmd = "%s status -s" % (git_path,)
|
|
|
|
cmd = "%s status -s" % (git_path)
|
|
|
|
lines = os.popen(cmd).read().splitlines()
|
|
|
|
rc, stdout, stderr = module.run_command(cmd, cwd=dest)
|
|
|
|
lines = filter(lambda c: not re.search('^\\?\\?.*$', c), lines)
|
|
|
|
lines = stdout.splitlines()
|
|
|
|
|
|
|
|
|
|
|
|
return len(lines) > 0
|
|
|
|
return len(lines) > 0
|
|
|
|
|
|
|
|
|
|
|
|
def reset(git_path, module, dest):
|
|
|
|
def reset(git_path, module, dest):
|
|
|
@ -230,16 +230,16 @@ def reset(git_path, module, dest):
|
|
|
|
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)
|
|
|
|
|
|
|
|
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, cwd=dest)
|
|
|
|
|
|
|
|
|
|
|
|
def get_remote_head(git_path, module, dest, version, remote, bare):
|
|
|
|
def get_remote_head(git_path, module, dest, version, remote, bare):
|
|
|
|
cloning = False
|
|
|
|
cloning = False
|
|
|
|
|
|
|
|
cwd = None
|
|
|
|
if remote == module.params['repo']:
|
|
|
|
if remote == module.params['repo']:
|
|
|
|
cloning = True
|
|
|
|
cloning = True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
os.chdir(dest)
|
|
|
|
cwd = dest
|
|
|
|
if version == 'HEAD':
|
|
|
|
if version == 'HEAD':
|
|
|
|
if cloning:
|
|
|
|
if cloning:
|
|
|
|
# cloning the repo, just get the remote's HEAD version
|
|
|
|
# cloning the repo, just get the remote's HEAD version
|
|
|
@ -255,7 +255,7 @@ def get_remote_head(git_path, module, dest, version, remote, bare):
|
|
|
|
# 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
|
|
|
|
return version
|
|
|
|
return version
|
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True )
|
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True, cwd=cwd)
|
|
|
|
if len(out) < 1:
|
|
|
|
if len(out) < 1:
|
|
|
|
module.fail_json(msg="Could not determine remote revision for %s" % version)
|
|
|
|
module.fail_json(msg="Could not determine remote revision for %s" % version)
|
|
|
|
rev = out.split()[0]
|
|
|
|
rev = out.split()[0]
|
|
|
@ -270,10 +270,9 @@ def is_remote_tag(git_path, module, dest, remote, version):
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def get_branches(git_path, module, dest):
|
|
|
|
def get_branches(git_path, module, dest):
|
|
|
|
os.chdir(dest)
|
|
|
|
|
|
|
|
branches = []
|
|
|
|
branches = []
|
|
|
|
cmd = '%s branch -a' % (git_path,)
|
|
|
|
cmd = '%s branch -a' % (git_path,)
|
|
|
|
(rc, out, err) = module.run_command(cmd)
|
|
|
|
(rc, out, err) = module.run_command(cmd, cwd=dest)
|
|
|
|
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'):
|
|
|
@ -281,10 +280,9 @@ def get_branches(git_path, module, dest):
|
|
|
|
return branches
|
|
|
|
return branches
|
|
|
|
|
|
|
|
|
|
|
|
def get_tags(git_path, module, dest):
|
|
|
|
def get_tags(git_path, module, dest):
|
|
|
|
os.chdir(dest)
|
|
|
|
|
|
|
|
tags = []
|
|
|
|
tags = []
|
|
|
|
cmd = '%s tag' % (git_path,)
|
|
|
|
cmd = '%s tag' % (git_path,)
|
|
|
|
(rc, out, err) = module.run_command(cmd)
|
|
|
|
(rc, out, err) = module.run_command(cmd, cwd=dest)
|
|
|
|
if rc != 0:
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Could not determine tag data - received %s" % out)
|
|
|
|
module.fail_json(msg="Could not determine tag data - received %s" % out)
|
|
|
|
for line in out.split('\n'):
|
|
|
|
for line in out.split('\n'):
|
|
|
@ -352,18 +350,17 @@ def get_head_branch(git_path, module, dest, remote, bare=False):
|
|
|
|
|
|
|
|
|
|
|
|
def fetch(git_path, module, repo, dest, version, remote, bare):
|
|
|
|
def fetch(git_path, module, repo, dest, version, remote, bare):
|
|
|
|
''' updates repo from remote sources '''
|
|
|
|
''' updates repo from remote sources '''
|
|
|
|
os.chdir(dest)
|
|
|
|
|
|
|
|
if bare:
|
|
|
|
if bare:
|
|
|
|
(rc, out1, err1) = module.run_command([git_path, 'fetch', remote, '+refs/heads/*:refs/heads/*'])
|
|
|
|
(rc, out1, err1) = module.run_command([git_path, 'fetch', remote, '+refs/heads/*:refs/heads/*'], cwd=dest)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
(rc, out1, err1) = module.run_command("%s fetch %s" % (git_path, remote))
|
|
|
|
(rc, out1, err1) = module.run_command("%s fetch %s" % (git_path, remote), cwd=dest)
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
if bare:
|
|
|
|
if bare:
|
|
|
|
(rc, out2, err2) = module.run_command([git_path, 'fetch', remote, '+refs/tags/*:refs/tags/*'])
|
|
|
|
(rc, out2, err2) = module.run_command([git_path, 'fetch', remote, '+refs/tags/*:refs/tags/*'], cwd=dest)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
(rc, out2, err2) = module.run_command("%s fetch --tags %s" % (git_path, remote))
|
|
|
|
(rc, out2, err2) = module.run_command("%s fetch --tags %s" % (git_path, remote), cwd=dest)
|
|
|
|
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, out3, err3) = submodule_update(git_path, module, dest)
|
|
|
|
(rc, out3, err3) = submodule_update(git_path, module, dest)
|
|
|
@ -371,28 +368,26 @@ def fetch(git_path, module, repo, dest, version, remote, bare):
|
|
|
|
|
|
|
|
|
|
|
|
def submodule_update(git_path, module, dest):
|
|
|
|
def submodule_update(git_path, module, dest):
|
|
|
|
''' init and update any submodules '''
|
|
|
|
''' init and update any submodules '''
|
|
|
|
os.chdir(dest)
|
|
|
|
|
|
|
|
# skip submodule commands if .gitmodules is not present
|
|
|
|
# skip submodule commands if .gitmodules is not present
|
|
|
|
if not os.path.exists(os.path.join(dest, '.gitmodules')):
|
|
|
|
if not os.path.exists(os.path.join(dest, '.gitmodules')):
|
|
|
|
return (0, '', '')
|
|
|
|
return (0, '', '')
|
|
|
|
cmd = [ git_path, 'submodule', 'sync' ]
|
|
|
|
cmd = [ git_path, 'submodule', 'sync' ]
|
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True, cwd=dest)
|
|
|
|
cmd = [ git_path, 'submodule', 'update', '--init', '--recursive' ]
|
|
|
|
cmd = [ git_path, 'submodule', 'update', '--init', '--recursive' ]
|
|
|
|
(rc, out, err) = module.run_command(cmd)
|
|
|
|
(rc, out, err) = module.run_command(cmd, cwd=dest)
|
|
|
|
if rc != 0:
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed to init/update submodules")
|
|
|
|
module.fail_json(msg="Failed to init/update submodules")
|
|
|
|
return (rc, out, err)
|
|
|
|
return (rc, out, err)
|
|
|
|
|
|
|
|
|
|
|
|
def switch_version(git_path, 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)
|
|
|
|
|
|
|
|
cmd = ''
|
|
|
|
cmd = ''
|
|
|
|
if version != 'HEAD':
|
|
|
|
if version != 'HEAD':
|
|
|
|
if is_remote_branch(git_path, module, dest, remote, version):
|
|
|
|
if is_remote_branch(git_path, module, dest, remote, version):
|
|
|
|
if not is_local_branch(git_path, module, dest, version):
|
|
|
|
if not is_local_branch(git_path, module, dest, version):
|
|
|
|
cmd = "%s checkout --track -b %s %s/%s" % (git_path, version, remote, version)
|
|
|
|
cmd = "%s checkout --track -b %s %s/%s" % (git_path, version, remote, version)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
(rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, version))
|
|
|
|
(rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, version), cwd=dest)
|
|
|
|
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 = "%s reset --hard %s/%s" % (git_path, remote, version)
|
|
|
|
cmd = "%s reset --hard %s/%s" % (git_path, remote, version)
|
|
|
@ -400,11 +395,11 @@ def switch_version(git_path, module, dest, remote, version):
|
|
|
|
cmd = "%s checkout --force %s" % (git_path, version)
|
|
|
|
cmd = "%s checkout --force %s" % (git_path, version)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
branch = get_head_branch(git_path, module, dest, remote)
|
|
|
|
branch = get_head_branch(git_path, module, dest, remote)
|
|
|
|
(rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, branch))
|
|
|
|
(rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, branch), cwd=dest)
|
|
|
|
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 = "%s reset --hard %s" % (git_path, remote)
|
|
|
|
cmd = "%s reset --hard %s" % (git_path, remote)
|
|
|
|
(rc, out1, err1) = module.run_command(cmd)
|
|
|
|
(rc, out1, err1) = module.run_command(cmd, cwd=dest)
|
|
|
|
if rc != 0:
|
|
|
|
if rc != 0:
|
|
|
|
if version != 'HEAD':
|
|
|
|
if version != 'HEAD':
|
|
|
|
module.fail_json(msg="Failed to checkout %s" % (version))
|
|
|
|
module.fail_json(msg="Failed to checkout %s" % (version))
|
|
|
@ -484,12 +479,12 @@ def main():
|
|
|
|
# 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(git_path, dest)
|
|
|
|
before = get_version(module, 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(git_path, dest, bare)
|
|
|
|
local_mods = has_local_mods(module, git_path, dest, bare)
|
|
|
|
before = get_version(git_path, dest)
|
|
|
|
before = get_version(module, git_path, dest)
|
|
|
|
if local_mods:
|
|
|
|
if local_mods:
|
|
|
|
# failure should happen regardless of check mode
|
|
|
|
# failure should happen regardless of check mode
|
|
|
|
if not force:
|
|
|
|
if not force:
|
|
|
@ -519,7 +514,7 @@ def main():
|
|
|
|
switch_version(git_path, module, dest, remote, version)
|
|
|
|
switch_version(git_path, module, dest, remote, version)
|
|
|
|
|
|
|
|
|
|
|
|
# determine if we changed anything
|
|
|
|
# determine if we changed anything
|
|
|
|
after = get_version(git_path, dest)
|
|
|
|
after = get_version(module, git_path, dest)
|
|
|
|
changed = False
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
|
|
|
|
if before != after or local_mods:
|
|
|
|
if before != after or local_mods:
|
|
|
|