From 7225839befa149cf1fc9daf73ab974c305138894 Mon Sep 17 00:00:00 2001 From: Maarten Bezemer Date: Thu, 8 Feb 2018 00:07:02 +0100 Subject: [PATCH] [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 --- lib/ansible/modules/source_control/git.py | 4 +- test/integration/targets/git/tasks/depth.yml | 51 +++++++++++++++++++ .../targets/git/tasks/setup-local-repos.yml | 10 ++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/source_control/git.py b/lib/ansible/modules/source_control/git.py index 2d5bba6801..039744227a 100644 --- a/lib/ansible/modules/source_control/git.py +++ b/lib/ansible/modules/source_control/git.py @@ -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: diff --git a/test/integration/targets/git/tasks/depth.yml b/test/integration/targets/git/tasks/depth.yml index 77a0e0cc29..769a73c944 100644 --- a/test/integration/targets/git/tasks/depth.yml +++ b/test/integration/targets/git/tasks/depth.yml @@ -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 }}" diff --git a/test/integration/targets/git/tasks/setup-local-repos.yml b/test/integration/targets/git/tasks/setup-local-repos.yml index bf92b168e6..5426fe8a6c 100644 --- a/test/integration/targets/git/tasks/setup-local-repos.yml +++ b/test/integration/targets/git/tasks/setup-local-repos.yml @@ -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"