Skip to content

Commit

Permalink
Fix git clone tag with depth=1
Browse files Browse the repository at this point in the history
* Fixes ansible#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
  • Loading branch information
Robin Roth authored and abadger committed Mar 2, 2017
1 parent fc0ae5e commit 3afc993
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/ansible/modules/source_control/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ])
Expand Down
22 changes: 22 additions & 0 deletions test/integration/targets/git/tasks/depth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3afc993

Please sign in to comment.