From 201ff02d1fbcb8095544b2c0decf26e1da92c926 Mon Sep 17 00:00:00 2001 From: Yap Sok Ann Date: Wed, 16 Oct 2013 14:16:25 +0800 Subject: [PATCH 1/6] git: Full length SHA-1 in, full length SHA-1 out. This removes ambiguity at the expense of slight backward incompatibility. --- source_control/git | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/source_control/git b/source_control/git index b839540e842..cbc005f5395 100644 --- a/source_control/git +++ b/source_control/git @@ -41,7 +41,8 @@ options: default: "HEAD" description: - What version of the repository to check out. This can be the - git I(SHA), the literal string C(HEAD), a branch name, or a tag name. + full 40-character I(SHA-1) hash, the literal string C(HEAD), a + branch name, or a tag name. remote: required: false default: "origin" @@ -107,7 +108,7 @@ import tempfile def get_version(git_path, dest): ''' samples the version of the git repo ''' os.chdir(dest) - cmd = "%s show --abbrev-commit" % (git_path,) + cmd = "%s show" % (git_path,) sha = os.popen(cmd).read().split("\n") sha = sha[0].split()[1] return sha @@ -354,8 +355,7 @@ def main(): if rc != 0: module.fail_json(msg=err) # exit if already at desired sha version - # abbreviate version in case full sha is given - if before == str(version)[:7]: + if before == version: module.exit_json(changed=False) # check or get changes from remote remote_head = get_remote_head(git_path, module, dest, version, remote) @@ -370,7 +370,6 @@ def main(): else: changed = True else: - remote_head = remote_head[0:7] if before != remote_head: changed = True else: From 3ff0c6d50d94b15e72c5217d8c862f8dbcc3eac8 Mon Sep 17 00:00:00 2001 From: Yap Sok Ann Date: Wed, 16 Oct 2013 18:51:36 +0800 Subject: [PATCH 2/6] git: Remove unnecessary return code checking. The functions either return rc=0 or call fail_json themselves. --- source_control/git | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/source_control/git b/source_control/git index cbc005f5395..64450428b7c 100644 --- a/source_control/git +++ b/source_control/git @@ -337,7 +337,7 @@ def main(): if not os.path.exists(gitconfig): if module.check_mode: module.exit_json(changed=True) - (rc, out, err) = clone(git_path, module, repo, dest, remote, depth, version) + clone(git_path, module, repo, dest, remote, depth, version) elif not update: # Just return having found a repo already in the dest path # this does no checking that the repo is the actual repo @@ -351,9 +351,7 @@ def main(): # if force, do a reset if local_mods and module.check_mode: module.exit_json(changed=True, msg='Local modifications exist') - (rc, out, err) = reset(git_path, module, dest, force) - if rc != 0: - module.fail_json(msg=err) + reset(git_path, module, dest, force) # exit if already at desired sha version if before == version: module.exit_json(changed=False) @@ -375,15 +373,11 @@ def main(): else: changed = False module.exit_json(changed=changed, before=before, after=remote_head) - (rc, out, err) = fetch(git_path, module, repo, dest, version, remote) - if rc != 0: - module.fail_json(msg=err) + fetch(git_path, module, repo, dest, version, remote) # switch to version specified regardless of whether # we cloned or pulled - (rc, out, err) = switch_version(git_path, module, dest, remote, version) - if rc != 0: - module.fail_json(msg=err) + switch_version(git_path, module, dest, remote, version) # determine if we changed anything after = get_version(git_path, dest) From 12c4bf51b877430b364cc3c2543393b85220a9ca Mon Sep 17 00:00:00 2001 From: Yap Sok Ann Date: Wed, 16 Oct 2013 19:02:11 +0800 Subject: [PATCH 3/6] 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 From 633438bfbb097ba0b05c74b31f0cbcaa690fb8a0 Mon Sep 17 00:00:00 2001 From: Yap Sok Ann Date: Wed, 16 Oct 2013 20:08:52 +0800 Subject: [PATCH 4/6] git: Always return the before/after revisions, even in check mode. The return values from check mode and non-check mode should match in all cases, except when a SHA-1 hash is used as version, as there is no way to check if it is a valid hash using `git ls-remote`. Also, to accomodate this change, the force flag for the reset function has been removed so that we can do the checking in main. --- source_control/git | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/source_control/git b/source_control/git index 9488283a7b8..569a26875bd 100644 --- a/source_control/git +++ b/source_control/git @@ -136,15 +136,13 @@ def has_local_mods(git_path, dest): lines = filter(lambda c: not re.search('^\\?\\?.*$', c), lines) return len(lines) > 0 -def reset(git_path, module, dest, force): +def reset(git_path, module, dest): ''' Resets the index and working tree to HEAD. Discards any changes to tracked files in working tree since that commit. ''' os.chdir(dest) - if not force and has_local_mods(git_path, dest): - module.fail_json(msg="Local modifications exist in repository (force=no).") cmd = "%s reset --hard HEAD" % (git_path,) return module.run_command(cmd, check_rc=True) @@ -343,7 +341,8 @@ def main(): local_mods = False if not os.path.exists(gitconfig): if module.check_mode: - module.exit_json(changed=True) + remote_head = get_remote_head(git_path, module, dest, version, repo) + module.exit_json(changed=True, before=before, after=remote_head) clone(git_path, module, repo, dest, remote, depth, version) elif not update: # Just return having found a repo already in the dest path @@ -355,31 +354,23 @@ def main(): # else do a pull local_mods = has_local_mods(git_path, dest) before = get_version(git_path, dest) - # if force, do a reset - if local_mods and module.check_mode: - module.exit_json(changed=True, msg='Local modifications exist') - reset(git_path, module, dest, force) - # exit if already at desired sha version - if before == version: - module.exit_json(changed=False) - # check or get changes from remote remote_head = get_remote_head(git_path, module, dest, version, remote) - if module.check_mode: - changed = False - if remote_head == version: - # get_remote_head returned version as-is - # were given a sha1 object, see if it is present - (rc, out, err) = module.run_command("%s show %s" % (git_path, version)) - if version in out: - changed = False - else: - changed = True + if local_mods: + # failure should happen regardless of check mode + if not force: + module.fail_json(msg="Local modifications exist in repository (force=no).") + # if force and in non-check mode, do a reset + if not module.check_mode: + reset(git_path, module, dest) + # exit if already at desired sha version + if before == remote_head: + if local_mods: + module.exit_json(changed=True, before=before, after=remote_head, + msg="Local modifications exist") else: - if before != remote_head: - changed = True - else: - changed = False - module.exit_json(changed=changed, before=before, after=remote_head) + module.exit_json(changed=False, before=before, after=remote_head) + if module.check_mode: + module.exit_json(changed=True, before=before, after=remote_head) fetch(git_path, module, repo, dest, version, remote) # switch to version specified regardless of whether From d77276380f276b2720bc5fc386eda4c0d19e818c Mon Sep 17 00:00:00 2001 From: Yap Sok Ann Date: Wed, 16 Oct 2013 20:18:01 +0800 Subject: [PATCH 5/6] git: Handle network problem when running `git ls-remote`. --- source_control/git | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source_control/git b/source_control/git index 569a26875bd..a22c9cabdfa 100644 --- a/source_control/git +++ b/source_control/git @@ -175,7 +175,7 @@ def get_remote_head(git_path, module, dest, version, remote): def is_remote_tag(git_path, module, dest, 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, check_rc=True) if version in out: return True else: @@ -194,7 +194,7 @@ def get_branches(git_path, module, dest): 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) + (rc, out, err) = module.run_command(cmd, check_rc=True) if version in out: return True else: From e568c6310860cb028517edb6d19eedc55b9ce5d3 Mon Sep 17 00:00:00 2001 From: Yap Sok Ann Date: Wed, 16 Oct 2013 20:31:25 +0800 Subject: [PATCH 6/6] git: Don't add --branch while cloning if the version is a SHA-1 hash. --- source_control/git | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source_control/git b/source_control/git index a22c9cabdfa..c241b8b5e61 100644 --- a/source_control/git +++ b/source_control/git @@ -122,8 +122,9 @@ def clone(git_path, module, repo, dest, remote, depth, version): pass os.chdir(dest_dirname) cmd = [ git_path, 'clone', '-o', remote, '--recursive' ] - if version and version != 'HEAD': - cmd.extend([ '--branch', str(version) ]) + if is_remote_branch(git_path, module, dest, repo, version) \ + or is_remote_tag(git_path, module, dest, repo, version): + cmd.extend([ '--branch', version ]) if depth: cmd.extend([ '--depth', str(depth) ]) cmd.extend([ repo, dest ])