Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/67972-git-fetch-force.yml
@@ -0,0 +1,2 @@
bugfixes:
- git - when force=True, apply --force flag to git fetches as well
8 changes: 6 additions & 2 deletions lib/ansible/modules/source_control/git.py
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 8 additions & 8 deletions test/integration/targets/git/tasks/archive.yml
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions test/integration/targets/git/tasks/forcefully-fetch-tag.yml
@@ -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
1 change: 1 addition & 0 deletions test/integration/targets/git/tasks/main.yml
Expand Up @@ -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
11 changes: 10 additions & 1 deletion test/integration/targets/git/tasks/setup-local-repos.yml
Expand Up @@ -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"
Expand All @@ -26,11 +27,19 @@
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"
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"

# 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"