From e4193da47edaf41625e88663f6612392d6355511 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 --- source_control/git.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/source_control/git.py b/source_control/git.py index 0cb87304a92..f26e0155d9c 100644 --- a/source_control/git.py +++ b/source_control/git.py @@ -573,12 +573,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): 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)