From 3afc993f3a5774f61b6a9b6ed4ea1136821f91ef Mon Sep 17 00:00:00 2001 From: Robin Roth Date: Tue, 21 Feb 2017 14:22:29 +0100 Subject: [PATCH] Fix git clone tag with depth=1 * Fixes #21316, add testcase based on this * Add option `--branch NAME` to git clone command in case of branch or tag in combination with depth=1 * This option should work back to at least git 1.8 and thus on all supported distributions * Provide better warning if depth is dropped --- lib/ansible/modules/source_control/git.py | 15 ++++++++----- test/integration/targets/git/tasks/depth.yml | 22 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/source_control/git.py b/lib/ansible/modules/source_control/git.py index a940d6ef688..fc1f2ee906e 100644 --- a/lib/ansible/modules/source_control/git.py +++ b/lib/ansible/modules/source_control/git.py @@ -423,12 +423,17 @@ def clone(git_path, module, repo, dest, remote, depth, version, bare, else: cmd.extend([ '--origin', remote ]) if depth: - if version == 'HEAD' \ - or refspec \ - or is_remote_branch(git_path, module, dest, repo, version) \ - or is_remote_tag(git_path, module, dest, repo, version): - # only use depth if the remote object is branch or tag (i.e. fetchable) + if version == 'HEAD' or refspec: cmd.extend([ '--depth', str(depth) ]) + elif is_remote_branch(git_path, module, dest, repo, version) \ + or is_remote_tag(git_path, module, dest, repo, version): + cmd.extend([ '--depth', str(depth) ]) + cmd.extend(['--branch', version]) + else: + # only use depth if the remote object is branch or tag (i.e. fetchable) + module.warn("Ignoring depth argument. " + "Shallow clones are only available for " + "HEAD, branches, tags or in combination with refspec.") if reference: cmd.extend([ '--reference', str(reference) ]) cmd.extend([ repo, dest ]) diff --git a/test/integration/targets/git/tasks/depth.yml b/test/integration/targets/git/tasks/depth.yml index a60299f3e22..e08f08b5fb5 100644 --- a/test/integration/targets/git/tasks/depth.yml +++ b/test/integration/targets/git/tasks/depth.yml @@ -64,6 +64,28 @@ args: chdir: '{{ checkout_dir }}' +- name: clear checkout_dir + file: + state: absent + path: "{{ checkout_dir }}" + +# Test for https://github.com/ansible/ansible/issues/21316 +- name: Shallow clone with tag + git: + repo: 'file://{{ repo_dir|expanduser }}/shallow' + dest: '{{ checkout_dir }}' + depth: 1 + version: earlytag + register: cloneold + +- assert: + that: "cloneold|success" + +- name: clear checkout_dir + file: + state: absent + path: "{{ checkout_dir }}" + # Test for https://github.com/ansible/ansible-modules-core/issues/3456 # clone a repo with depth and version specified