From e7fcfa981e7d7a17ff0c8e3c8ceee02f8b7885d0 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Fri, 21 Oct 2016 08:30:43 -0700 Subject: [PATCH] Remove the yaml dep from the git module --- lib/ansible/modules/source_control/git.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/ansible/modules/source_control/git.py b/lib/ansible/modules/source_control/git.py index 15dda627cd8..a9952172272 100644 --- a/lib/ansible/modules/source_control/git.py +++ b/lib/ansible/modules/source_control/git.py @@ -222,7 +222,7 @@ from distutils.version import LooseVersion from ansible.module_utils.basic import AnsibleModule, get_module_path from ansible.module_utils.known_hosts import add_git_host_key -from ansible.module_utils.six import string_types +from ansible.module_utils.six import b, string_types from ansible.module_utils._text import to_bytes, to_native @@ -536,8 +536,8 @@ def is_local_branch(git_path, module, dest, branch): def is_not_a_branch(git_path, module, dest): branches = get_branches(git_path, module, dest) - for b in branches: - if b.startswith('* ') and ('no branch' in b or 'detached from' in b): + for branch in branches: + if branch.startswith('* ') and ('no branch' in branch or 'detached from' in branch): return True return False @@ -556,17 +556,23 @@ def get_head_branch(git_path, module, dest, remote, bare=False): # Check if the .git is a file. If it is a file, it means that we are in a submodule structure. if os.path.isfile(repo_path): try: - ### FIXME: This introduces another dep that we don't want. We - # probably need to take a look at the format of this file and do - # our own parsing. - import yaml - gitdir = yaml.safe_load(open(repo_path)).get('gitdir') + git_conf = open(repo_path, 'rb') + for line in git_conf: + config_val = line.split(b(':'), 1) + if config_val[0].strip() == b('gitdir'): + gitdir = to_native(config_val[1].strip(), errors='surrogate_or_strict') + break + else: + # No repo path found + return '' + # There is a possibility the .git file to have an absolute path. if os.path.isabs(gitdir): repo_path = gitdir else: repo_path = os.path.join(repo_path.split('.git')[0], gitdir) except (IOError, AttributeError): + # No repo path found return '' # Read .git/HEAD for the name of the branch. # If we're in a detached HEAD state, look up the branch associated with