[git] Fix switching branch of shallow clone (#18728)

* [git] Fix fetching branch of shallow clone

* Use absolute file:// paths to make sure git uses shallow clones

* Improve tests

* Fix sanity errors
* Match style according to other (depth) tests

* Improve tests

Now they will fail without the fix of this PR
pull/35874/head
Maarten Bezemer 6 years ago committed by Ryan Brown
parent 098eac076d
commit 7225839bef

@ -715,9 +715,7 @@ def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, g
# 1.8.3 is broken, 1.9.x works
# ensure that remote branch is available as both local and remote ref
refspecs.append('+refs/heads/%s:refs/heads/%s' % (version, version))
refspecs.append('+refs/heads/%s:refs/remotes/%s/%s' % (version, remote, version))
else:
refspecs.append(version)
refspecs.append('+refs/heads/%s:refs/remotes/%s/%s' % (version, remote, version))
elif is_remote_tag(git_path, module, dest, repo, version):
refspecs.append('+refs/tags/' + version + ':refs/tags/' + version)
if refspecs:

@ -171,3 +171,54 @@
file:
state: absent
path: "{{ checkout_dir }}"
#
# Make sure shallow fetch works when switching to (fetching) a new a branch
#
- name: DEPTH | clone from branch with depth specified
git:
repo: 'file://{{ repo_dir|expanduser }}/shallow_branches'
dest: '{{ checkout_dir }}'
depth: 1
version: test_branch
- name: DEPTH | check if clone is shallow
stat: path={{ checkout_dir }}/.git/shallow
register: is_shallow
when: git_version.stdout is version(git_version_supporting_depth, '>=')
- name: DEPTH | assert that clone is shallow
assert:
that:
- is_shallow.stat.exists
when: git_version.stdout is version(git_version_supporting_depth, '>=')
- name: DEPTH | switch to new branch (fetch) with the shallow clone
git:
repo: 'file://{{ repo_dir|expanduser }}/shallow_branches'
dest: '{{ checkout_dir }}'
depth: 1
version: new_branch
register: git_fetch
- name: DEPTH | assert if switching a shallow clone to a new branch worked
assert:
that:
- git_fetch is changed
- name: DEPTH | check if clone is still shallow
stat: path={{ checkout_dir }}/.git/shallow
register: is_shallow
when: git_version.stdout is version(git_version_supporting_depth, '>=')
- name: DEPTH | assert that clone still is shallow
assert:
that:
- is_shallow.stat.exists
when: git_version.stdout is version(git_version_supporting_depth, '>=')
- name: DEPTH | clear checkout_dir
file:
state: absent
path: "{{ checkout_dir }}"

@ -5,6 +5,7 @@
with_items:
- "{{ repo_dir }}/minimal"
- "{{ repo_dir }}/shallow"
- "{{ repo_dir }}/shallow_branches"
- name: SETUP-LOCAL-REPOS | prepare minimal git repo
shell: git init; echo "1" > a; git add a; git commit -m "1"
@ -24,3 +25,12 @@
register: git_shallow_head_1
args:
chdir: "{{ repo_dir }}/shallow"
- name: prepare tmp git repo with two branches
shell: |
git init
echo "1" > a; git add a; git commit -m "1"
git checkout -b test_branch; echo "2" > a; git commit -m "2 on branch" a
git checkout -b new_branch; echo "3" > a; git commit -m "3 on new branch" a
args:
chdir: "{{ repo_dir }}/shallow_branches"

Loading…
Cancel
Save