From 99ef1f3a9fa2bc16f47ce97c353c66da2efa8336 Mon Sep 17 00:00:00 2001 From: Chulki Lee Date: Mon, 3 Apr 2017 22:19:46 -0700 Subject: [PATCH] git: git reset with branch `git reset ` can be ambiguous and fail to switch to the correct branch. To avoid it, specify branch as well. --- lib/ansible/modules/source_control/git.py | 2 +- .../targets/git/tasks/ambiguous-ref.yml | 31 +++++++++++++++++++ test/integration/targets/git/tasks/main.yml | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/integration/targets/git/tasks/ambiguous-ref.yml diff --git a/lib/ansible/modules/source_control/git.py b/lib/ansible/modules/source_control/git.py index 2a58f0d0f6d..e36ca2a3d5c 100644 --- a/lib/ansible/modules/source_control/git.py +++ b/lib/ansible/modules/source_control/git.py @@ -816,7 +816,7 @@ def switch_version(git_path, module, dest, remote, version, verify_commit, depth if rc != 0: module.fail_json(msg="Failed to checkout branch %s" % branch, stdout=out, stderr=err, rc=rc) - cmd = "%s reset --hard %s --" % (git_path, remote) + cmd = "%s reset --hard %s/%s --" % (git_path, remote, branch) else: # FIXME check for local_branch first, should have been fetched already if is_remote_branch(git_path, module, dest, remote, version): diff --git a/test/integration/targets/git/tasks/ambiguous-ref.yml b/test/integration/targets/git/tasks/ambiguous-ref.yml new file mode 100644 index 00000000000..614e3e2f598 --- /dev/null +++ b/test/integration/targets/git/tasks/ambiguous-ref.yml @@ -0,0 +1,31 @@ +--- + +# test for https://github.com/ansible/ansible-modules-core/pull/3386 + +- name: clone repo + git: + repo: '{{ repo_format1 }}' + dest: '{{ checkout_dir }}' + +- name: rename remote to be ambiguous + command: git remote rename origin v0.1 chdir="{{ checkout_dir }}" + +- name: switch to HEAD + git: + repo: '{{ repo_format1 }}' + dest: '{{ checkout_dir }}' + remote: v0.1 + +- name: rev-parse remote HEAD + command: git rev-parse v0.1/HEAD chdir="{{ checkout_dir }}" + register: git_remote_head + +- name: rev-parse local HEAD + command: git rev-parse HEAD chdir="{{ checkout_dir }}" + register: git_local_head + +- assert: + that: git_remote_head.stdout == git_local_head.stdout + +- name: clear checkout_dir + file: state=absent path={{ checkout_dir }} diff --git a/test/integration/targets/git/tasks/main.yml b/test/integration/targets/git/tasks/main.yml index 0f3687f21e8..bc87aeb810c 100644 --- a/test/integration/targets/git/tasks/main.yml +++ b/test/integration/targets/git/tasks/main.yml @@ -30,3 +30,4 @@ - include: tag-verification.yml - include: localmods.yml - include: reset-origin.yml +- include: ambiguous-ref.yml