Repository: ansible/ansible-modules-core

Issue:
git module: fatal: A branch named 'STABLE' already exists.
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

Module: git
##### ANSIBLE VERSION

```
ansible 2.2.0 (devel b427818f02) last updated 2016/09/30 19:29:28 (GMT +200)
  lib/ansible/modules/core: (detached HEAD 537a7eb924) last updated 2016/09/30 19:29:49 (GMT +200)
  lib/ansible/modules/extras: (detached HEAD a58e1d59c0) last updated 2016/09/30 19:29:49 (GMT +200)
  config file = 
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

No customisation.
##### OS / ENVIRONMENT

N/A
##### SUMMARY

"msg": "Failed to checkout STABLE"
"stderr": "fatal: A branch named 'STABLE' already exists.\n"

On first run only.
##### STEPS TO REPRODUCE

Run:

```
hacking/test-module -m lib/ansible/modules/core/source_control/git.py -a 'repo=https://github.com/phpmyadmin/phpmyadmin.git dest=/tmp/pma depth=1 version=STABLE force=yes'
```

Output:

```
PARSED OUTPUT
{
    "cmd": "/usr/bin/git checkout --track -b STABLE origin/STABLE", 
    "failed": true, 
    "invocation": {
        "module_args": {
            "accept_hostkey": false, 
            "bare": false, 
            "clone": true, 
            "depth": 1, 
            "dest": "/tmp/pma", 
            "executable": null, 
            "force": true, 
            "key_file": null, 
            "recursive": true, 
            "reference": null, 
            "refspec": null, 
            "remote": "origin", 
            "repo": "https://github.com/phpmyadmin/phpmyadmin.git", 
            "ssh_opts": null, 
            "track_submodules": false, 
            "umask": null, 
            "update": true, 
            "verify_commit": false, 
            "version": "STABLE"
        }
    }, 
    "msg": "Failed to checkout STABLE", 
    "rc": 128, 
    "stderr": "fatal: A branch named 'STABLE' already exists.\n", 
    "stdout": ""
}
```

On second run the task succeeds, so to reproduce again one needs to delete the repo:

```
rm -rf /tpm/pma
```
##### EXPECTED RESULTS

Task succeeds on first run.
##### ACTUAL RESULTS

Task fails on first run:

```
* including generated source, if any, saving to: /home/riaan/.ansible_module_generated
* ansiballz module detected; extracted module source to: /home/riaan/debug_dir
***********************************
RAW OUTPUT

{"stdout": "", "cmd": "/usr/bin/git checkout --track -b STABLE origin/STABLE", "failed": true, "stderr": "fatal: A branch named 'STABLE' already exists.\n", "rc": 128, "invocation": {"module_args": {"executable": null, "force": true, "refspec": null, "reference": null, "dest": "/tmp/pma", "verify_commit": false, "clone": true, "accept_hostkey": false, "update": true, "umask": null, "repo": "https://github.com/phpmyadmin/phpmyadmin.git", "depth": 1, "version": "STABLE", "bare": false, "recursive": true, "remote": "origin", "key_file": null, "ssh_opts": null, "track_submodules": false}}, "msg": "Failed to checkout STABLE"}


***********************************
PARSED OUTPUT
{
    "cmd": "/usr/bin/git checkout --track -b STABLE origin/STABLE", 
    "failed": true, 
    "invocation": {
        "module_args": {
            "accept_hostkey": false, 
            "bare": false, 
            "clone": true, 
            "depth": 1, 
            "dest": "/tmp/pma", 
            "executable": null, 
            "force": true, 
            "key_file": null, 
            "recursive": true, 
            "reference": null, 
            "refspec": null, 
            "remote": "origin", 
            "repo": "https://github.com/phpmyadmin/phpmyadmin.git", 
            "ssh_opts": null, 
            "track_submodules": false, 
            "umask": null, 
            "update": true, 
            "verify_commit": false, 
            "version": "STABLE"
        }
    }, 
    "msg": "Failed to checkout STABLE", 
    "rc": 128, 
    "stderr": "fatal: A branch named 'STABLE' already exists.\n", 
    "stdout": ""
}

```



Discussion:
<!-- @RiaanBurger: Thanks for filing this bug report! -->

@ansible ping, this issue is waiting for your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_bug_new --->


Resolving patch:
diff --git a/source_control/git.py b/source_control/git.py
--- a/source_control/git.py
+++ b/source_control/git.py
@@ -756,11 +756,12 @@ def switch_version(git_path, module, dest, remote, version, verify_commit, depth
     else:
         # FIXME check for local_branch first, should have been fetched already
         if is_remote_branch(git_path, module, dest, remote, version):
+            if depth and not is_local_branch(git_path, module, dest, version):
+                # git clone --depth implies --single-branch, which makes
+                # the checkout fail if the version changes
+                # fetch the remote branch, to be able to check it out next
+                set_remote_branch(git_path, module, dest, remote, version, depth)
             if not is_local_branch(git_path, module, dest, version):
-                if depth:
-                    # git clone --depth implies --single-branch, which makes
-                    # the checkout fail if the version changes
-                    set_remote_branch(git_path, module, dest, remote, version, depth)
                 cmd = "%s checkout --track -b %s %s/%s" % (git_path, version, remote, version)
             else:
                 (rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, version), cwd=dest)


Test patch: