From 12c4bf51b877430b364cc3c2543393b85220a9ca Mon Sep 17 00:00:00 2001 From: Yap Sok Ann Date: Wed, 16 Oct 2013 19:02:11 +0800 Subject: [PATCH] git: Make function get_remote_head usable when cloning. This allows the module to return the before/after revisions in all cases. --- source_control/git | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/source_control/git b/source_control/git index 64450428b7c..9488283a7b8 100644 --- a/source_control/git +++ b/source_control/git @@ -149,11 +149,19 @@ def reset(git_path, module, dest, force): return module.run_command(cmd, check_rc=True) def get_remote_head(git_path, module, dest, version, remote): - cmd = '' - os.chdir(dest) + cloning = False + if remote == module.params['repo']: + cloning = True + else: + os.chdir(dest) if version == 'HEAD': - version = get_head_branch(git_path, module, dest, remote) - if is_remote_branch(git_path, module, dest, remote, version): + if cloning: + # cloning the repo, just get the remote's HEAD version + cmd = '%s ls-remote %s -h HEAD' % (git_path, remote) + else: + head_branch = get_head_branch(git_path, module, dest, remote) + cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, head_branch) + elif is_remote_branch(git_path, module, dest, remote, version): cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version) elif is_remote_tag(git_path, module, dest, remote, version): cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version) @@ -168,7 +176,6 @@ def get_remote_head(git_path, module, dest, version, remote): return rev def is_remote_tag(git_path, module, dest, remote, version): - os.chdir(dest) cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version) (rc, out, err) = module.run_command(cmd) if version in out: @@ -187,10 +194,10 @@ def get_branches(git_path, module, dest): branches.append(line.strip()) return branches -def is_remote_branch(git_path, module, dest, remote, branch): - branches = get_branches(git_path, module, dest) - rbranch = 'remotes/%s/%s' % (remote, branch) - if rbranch in branches: +def is_remote_branch(git_path, module, dest, remote, version): + cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version) + (rc, out, err) = module.run_command(cmd) + if version in out: return True else: return False