Add --depth support in the git module

This commit is contained in:
Jesús García Crespo 2013-04-11 09:40:15 -07:00
parent 0be04d2b57
commit 62d7e6b006

View file

@ -56,6 +56,14 @@ options:
- If C(yes), any modified files in the working
repository will be discarded. Prior to 0.7, this was always
'yes' and could not be disabled.
depth:
required: false
default: null
version_added: "1.2"
description:
- Create a shallow clone with a history truncated to the specified
number or revisions. The minimum possible value is C(1), otherwise
ignored.
examples:
- code: "git: repo=git://foosball.example.org/path/to/repo.git dest=/srv/checkout version=release-0.22"
description: Example git checkout from Ansible Playbooks
@ -74,7 +82,7 @@ def get_version(dest):
sha = sha[0].split()[1]
return sha
def clone(module, repo, dest, remote):
def clone(module, repo, dest, remote, depth):
''' makes a new git repo if it does not already exist '''
dest_dirname = os.path.dirname(dest)
try:
@ -82,8 +90,11 @@ def clone(module, repo, dest, remote):
except:
pass
os.chdir(dest_dirname)
return module.run_command("git clone -o %s %s %s" % (remote, repo, dest),
check_rc=True)
cmd = [ module.get_bin_path('git', True), 'clone', '-o', remote ]
if depth:
cmd.extend([ '--depth', str(depth) ])
cmd.extend([ repo, dest ])
return module.run_command(cmd, check_rc=True)
def has_local_mods(dest):
os.chdir(dest)
@ -251,7 +262,8 @@ def main():
repo=dict(required=True, aliases=['name']),
version=dict(default='HEAD'),
remote=dict(default='origin'),
force=dict(default='yes', type='bool')
force=dict(default='yes', type='bool'),
depth=dict(default=None, type='int'),
),
supports_check_mode=True
)
@ -261,6 +273,7 @@ def main():
version = module.params['version']
remote = module.params['remote']
force = module.params['force']
depth = module.params['depth']
gitconfig = os.path.join(dest, '.git', 'config')
@ -273,7 +286,7 @@ def main():
if not os.path.exists(gitconfig):
if module.check_mode:
module.exit_json(changed=True)
(rc, out, err) = clone(module, repo, dest, remote)
(rc, out, err) = clone(module, repo, dest, remote, depth)
else:
# else do a pull
local_mods = has_local_mods(dest)