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 - If C(yes), any modified files in the working
repository will be discarded. Prior to 0.7, this was always repository will be discarded. Prior to 0.7, this was always
'yes' and could not be disabled. '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: examples:
- code: "git: repo=git://foosball.example.org/path/to/repo.git dest=/srv/checkout version=release-0.22" - 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 description: Example git checkout from Ansible Playbooks
@ -74,7 +82,7 @@ def get_version(dest):
sha = sha[0].split()[1] sha = sha[0].split()[1]
return sha 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 ''' ''' 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:
@ -82,8 +90,11 @@ def clone(module, repo, dest, remote):
except: except:
pass pass
os.chdir(dest_dirname) os.chdir(dest_dirname)
return module.run_command("git clone -o %s %s %s" % (remote, repo, dest), cmd = [ module.get_bin_path('git', True), 'clone', '-o', remote ]
check_rc=True) if depth:
cmd.extend([ '--depth', str(depth) ])
cmd.extend([ repo, dest ])
return module.run_command(cmd, check_rc=True)
def has_local_mods(dest): def has_local_mods(dest):
os.chdir(dest) os.chdir(dest)
@ -92,7 +103,7 @@ def has_local_mods(dest):
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(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
@ -251,7 +262,8 @@ def main():
repo=dict(required=True, aliases=['name']), repo=dict(required=True, aliases=['name']),
version=dict(default='HEAD'), version=dict(default='HEAD'),
remote=dict(default='origin'), 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 supports_check_mode=True
) )
@ -261,6 +273,7 @@ def main():
version = module.params['version'] version = module.params['version']
remote = module.params['remote'] remote = module.params['remote']
force = module.params['force'] force = module.params['force']
depth = module.params['depth']
gitconfig = os.path.join(dest, '.git', 'config') gitconfig = os.path.join(dest, '.git', 'config')
@ -273,7 +286,7 @@ 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) (rc, out, err) = clone(module, repo, dest, remote, depth)
else: else:
# else do a pull # else do a pull
local_mods = has_local_mods(dest) local_mods = has_local_mods(dest)