From b6213ffa8054e4e6b1c91c425dcf5e2887ff3aba Mon Sep 17 00:00:00 2001 From: Jordan Bach Date: Thu, 12 Mar 2015 17:06:44 -0500 Subject: [PATCH] git: set remote branch before switching versions when using depth argument --- lib/ansible/modules/source_control/git.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/ansible/modules/source_control/git.py b/lib/ansible/modules/source_control/git.py index 2d3d1542cb8..f017c4a02d3 100644 --- a/lib/ansible/modules/source_control/git.py +++ b/lib/ansible/modules/source_control/git.py @@ -601,12 +601,26 @@ def submodule_update(git_path, module, dest, track_submodules): module.fail_json(msg="Failed to init/update submodules: %s" % out + err) return (rc, out, err) +def set_remote_branch(git_path, module, dest, remote, version, depth): + cmd = "%s remote set-branches %s %s" % (git_path, remote, version) + (rc, out, err) = module.run_command(cmd, cwd=dest) + if rc != 0: + module.fail_json(msg="Failed to set remote branch: %s" % version) + cmd = "%s fetch --depth=%s %s %s" % (git_path, depth, remote, version) + (rc, out, err) = module.run_command(cmd, cwd=dest) + if rc != 0: + module.fail_json(msg="Failed to fetch branch from remote: %s" % version) def switch_version(git_path, module, dest, remote, version, verify_commit): cmd = '' if version != 'HEAD': if is_remote_branch(git_path, module, dest, remote, version): if not is_local_branch(git_path, module, dest, version): + depth = module.params['depth'] + if depth: + # git clone --depth implies --single-branch, which makes + # the checkout fail if the version changes + set_remote_branch(git_path, module, dest, remote, version, depth) cmd = "%s checkout --track -b %s %s/%s" % (git_path, version, remote, version) else: (rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, version), cwd=dest)