[git] make force=True apply to git fetches (#68691)

Fixes #67972

Signed-off-by: Rick Elrod <rick@elrod.me>
This commit is contained in:
Rick Elrod 2020-04-06 15:25:24 -05:00 committed by GitHub
parent 123c624b28
commit 4916be24fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 11 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- git - when force=True, apply --force flag to git fetches as well

View file

@ -749,7 +749,7 @@ def set_remote_url(git_path, module, repo, dest, remote):
return remote_url is not None
def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, git_version_used):
def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, git_version_used, force=False):
''' updates repo from remote sources '''
set_remote_url(git_path, module, repo, dest, remote)
commands = []
@ -797,6 +797,10 @@ def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, g
refspecs = ['+refs/tags/*:refs/tags/*']
if refspec:
refspecs.append(refspec)
if force:
fetch_cmd.append('--force')
fetch_cmd.extend([remote])
commands.append((fetch_str, fetch_cmd + refspecs))
@ -1230,7 +1234,7 @@ def main():
result['diff'] = diff
module.exit_json(**result)
else:
fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, git_version_used)
fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, git_version_used, force=force)
result['after'] = get_version(module, git_path, dest)

View file

@ -33,14 +33,14 @@
- "ansible_os_family == 'RedHat'"
- ansible_distribution_major_version is version('7', '>=')
- name: ARCHIVE | Clear checkout_dir
file:
state: absent
path: "{{ checkout_dir }}"
- name: ARCHIVE | Clone clean repo
git:
repo: '{{ repo_format1 }}'
- name: ARCHIVE | Clear checkout_dir
file:
state: absent
path: "{{ checkout_dir }}"
- name: ARCHIVE | Clone clean repo
git:
repo: '{{ repo_format1 }}'
dest: '{{ checkout_dir }}'
# Check git archive functionality without update

View file

@ -0,0 +1,38 @@
# Tests against https://github.com/ansible/ansible/issues/67972
# Do our first clone manually; there are no commits yet and Ansible doesn't like
# that.
- name: FORCEFULLY-FETCH-TAG | Clone the bare repo in a non-bare clone
shell: git clone {{ repo_dir }}/tag_force_push {{ repo_dir }}/tag_force_push_clone1
- name: FORCEFULLY-FETCH-TAG | Prepare repo with a tag
shell: |
echo 1337 > leet;
git add leet;
git commit -m uh-oh;
git tag -f herewego;
git push --tags origin master
args:
chdir: "{{ repo_dir }}/tag_force_push_clone1"
- name: FORCEFULLY-FETCH-TAG | clone the repo for the second time
git:
repo: "{{ repo_dir }}/tag_force_push"
dest: "{{ repo_dir }}/tag_force_push_clone2"
- name: FORCEFULLY-FETCH-TAG | Forcefully overwrite the tag in clone1
shell: |
echo 1338 > leet;
git add leet;
git commit -m uh-oh;
git tag -f herewego;
git push -f --tags origin master
args:
chdir: "{{ repo_dir }}/tag_force_push_clone1"
- name: FORCEFULLY-FETCH-TAG | Try to update the second clone
git:
repo: "{{ repo_dir }}/tag_force_push"
dest: "{{ repo_dir }}/tag_force_push_clone2"
force: yes
register: git_res

View file

@ -37,3 +37,4 @@
- include_tasks: ambiguous-ref.yml
- include_tasks: archive.yml
- include_tasks: separate-git-dir.yml
- include_tasks: forcefully-fetch-tag.yml

View file

@ -6,6 +6,7 @@
- "{{ repo_dir }}/minimal"
- "{{ repo_dir }}/shallow"
- "{{ repo_dir }}/shallow_branches"
- "{{ repo_dir }}/tag_force_push"
- name: SETUP-LOCAL-REPOS | prepare minimal git repo
shell: git init; echo "1" > a; git add a; git commit -m "1"
@ -26,7 +27,7 @@
args:
chdir: "{{ repo_dir }}/shallow"
- name: prepare tmp git repo with two branches
- name: SETUP-LOCAL-REPOS | prepare tmp git repo with two branches
shell: |
git init
echo "1" > a; git add a; git commit -m "1"
@ -34,3 +35,11 @@
git checkout -b new_branch; echo "3" > a; git commit -m "3 on new branch" a
args:
chdir: "{{ repo_dir }}/shallow_branches"
# Make this a bare one, we need to be able to push to it from clones
# We make the repo here for consistency with the other repos,
# but we finish setting it up in forcefully-fetch-tag.yml.
- name: SETUP-LOCAL-REPOS | prepare tag_force_push git repo
shell: git init --bare
args:
chdir: "{{ repo_dir }}/tag_force_push"