Repository: ansible/ansible-modules-core

Issue:
Git fetch for ansible-pull broken in devel when using -C
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

source_control/git
##### ANSIBLE VERSION

```
ansible 2.1.0 (devel cfe99b991f) last updated 2016/04/18 12:16:20 (GMT -700)
  lib/ansible/modules/core: (detached HEAD 5409ed1b28) last updated 2016/04/18 12:16:42 (GMT -700)
  lib/ansible/modules/extras: (detached HEAD 3afe117730) last updated 2016/04/18 12:16:57 (GMT -700)
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
```
##### OS / ENVIRONMENT

Ubuntu 14.04
##### SUMMARY

When I try to use ansible-pull with the latest devel commits, it fails when I try to specify the branch
##### STEPS TO REPRODUCE

Use ansible-pull with the -C flag to specify a branch. The first time, it works. The second time, it fails with this error:

```
localhost | FAILED! => {
    "changed": false, 
    "cmd": [
        "/usr/bin/git", 
        "fetch", 
        "--depth", 
        "1", 
        "origin", 
        "+refs/heads/master:refs/heads/master"
    ], 
    "failed": true, 
    "msg": "Failed to download remote objects and refs:  X11 forwarding request failed on channel 0\r\nfatal: Refusing to fetch into current branch refs/heads/master of non-bare repository\n"
}
```

Here's my full, very verbose, output:

```
> ./ansible-pull -C master -o -d /tmp/my_ansible_repo -U git@mygitserver.com:ansible_pull.git -vvvv
EXEC: $HOME/dev/ansible/src/ansible/bin/ansible -i "localhost," -c local  -vvvv -m git -a "name=git@mygitserver.com:ansible_pull.git dest=/tmp/my_ansible_repo version=master depth=1" all -l "localhost,127.0.0.1"
Using /etc/ansible/ansible.cfg as config file
Loaded callback minimal of type stdout, v2.0
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: aaronwilson
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1461007298.1-211073535958840 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1461007298.1-211073535958840 `" )'
<localhost> PUT /tmp/tmp5lhzRk TO $HOME/.ansible/tmp/ansible-tmp-1461007298.1-211073535958840/git
<localhost> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python $HOME/.ansible/tmp/ansible-tmp-1461007298.1-211073535958840/git; rm -rf "$HOME/.ansible/tmp/ansible-tmp-1461007298.1-211073535958840/" > /dev/null 2>&1'
localhost | FAILED! => {
    "changed": false, 
    "cmd": [
        "/usr/bin/git", 
        "fetch", 
        "--depth", 
        "1", 
        "origin", 
        "+refs/heads/master:refs/heads/master"
    ], 
    "failed": true, 
    "invocation": {
        "module_args": {
            "accept_hostkey": false, 
            "bare": false, 
            "clone": true, 
            "depth": 1, 
            "dest": "/tmp/my_ansible_repo", 
            "executable": null, 
            "force": false, 
            "key_file": null, 
            "name": "git@mygitserver:ansible_pull.git", 
            "recursive": true, 
            "reference": null, 
            "refspec": null, 
            "remote": "origin", 
            "repo": "git@mygitserver.com:ansible_pull.git", 
            "ssh_opts": null, 
            "track_submodules": false, 
            "update": true, 
            "verify_commit": false, 
            "version": "master"
        }, 
        "module_name": "git"
    }, 
    "msg": "Failed to download remote objects and refs:  X11 forwarding request failed on channel 0\r\nfatal: Refusing to fetch into current branch refs/heads/master of non-bare repository\n"
}

```



Discussion:
Thanks @AaronWilson for the report.
This might be introduced in #3254.
I'll look into it, but it might take me some time to do so.

Quick workaround: Add `--full` to the ansible-pull arguments.

Does it work without `-C master`? (it should)


Resolving patch:
diff --git a/source_control/git.py b/source_control/git.py
--- a/source_control/git.py
+++ b/source_control/git.py
@@ -522,7 +522,7 @@ def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec):
         elif version == 'HEAD':
             refspecs.append('HEAD')
         elif is_remote_branch(git_path, module, dest, repo, version):
-            refspecs.append('+refs/heads/'+version+':refs/heads/'+version)
+            refspecs.append(version)
         elif is_remote_tag(git_path, module, dest, repo, version):
             refspecs.append('+refs/tags/'+version+':refs/tags/'+version)
         if refspecs:


Test patch: