git: Make function get_remote_head usable when cloning.

This allows the module to return the before/after revisions in all cases.
reviewable/pr18780/r1
Yap Sok Ann 11 years ago
parent 3ff0c6d50d
commit 12c4bf51b8

@ -149,11 +149,19 @@ def reset(git_path, module, dest, force):
return module.run_command(cmd, check_rc=True) return module.run_command(cmd, check_rc=True)
def get_remote_head(git_path, module, dest, version, remote): def get_remote_head(git_path, module, dest, version, remote):
cmd = '' cloning = False
if remote == module.params['repo']:
cloning = True
else:
os.chdir(dest) os.chdir(dest)
if version == 'HEAD': if version == 'HEAD':
version = get_head_branch(git_path, module, dest, remote) if cloning:
if is_remote_branch(git_path, module, dest, remote, version): # 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) cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version)
elif is_remote_tag(git_path, module, dest, 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) 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 return rev
def is_remote_tag(git_path, module, dest, remote, version): 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) cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version)
(rc, out, err) = module.run_command(cmd) (rc, out, err) = module.run_command(cmd)
if version in out: if version in out:
@ -187,10 +194,10 @@ def get_branches(git_path, module, dest):
branches.append(line.strip()) branches.append(line.strip())
return branches return branches
def is_remote_branch(git_path, module, dest, remote, branch): def is_remote_branch(git_path, module, dest, remote, version):
branches = get_branches(git_path, module, dest) cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version)
rbranch = 'remotes/%s/%s' % (remote, branch) (rc, out, err) = module.run_command(cmd)
if rbranch in branches: if version in out:
return True return True
else: else:
return False return False

Loading…
Cancel
Save