fix git switch branches in combination with depth on old git versions (#3492)
* fix git switch branches in combination with depth * the old implementation is correct, but relies on git working correctly (as only newer versions do) * with some older git version (e.g. 1.8.3), git fetch remote branch does not work, if the branch does not yet exist locally * this patch works around that without explicitly checking the git version * future refactoring is needed to reduce the number of fetches to a minimum; but this patch makes the code "correct" * add git version requirements to doc * replace set-branches with git 1.7.1 compatible version
This commit is contained in:
parent
a90fd67c7e
commit
281398fdc2
1 changed files with 21 additions and 8 deletions
|
@ -110,7 +110,7 @@ options:
|
||||||
description:
|
description:
|
||||||
- Create a shallow clone with a history truncated to the specified
|
- Create a shallow clone with a history truncated to the specified
|
||||||
number or revisions. The minimum possible value is C(1), otherwise
|
number or revisions. The minimum possible value is C(1), otherwise
|
||||||
ignored.
|
ignored. Needs I(git>=1.8.3) to work correctly.
|
||||||
clone:
|
clone:
|
||||||
required: false
|
required: false
|
||||||
default: "yes"
|
default: "yes"
|
||||||
|
@ -174,7 +174,8 @@ options:
|
||||||
be trusted in the GPG trustdb.
|
be trusted in the GPG trustdb.
|
||||||
|
|
||||||
requirements:
|
requirements:
|
||||||
- git (the command line tool)
|
- git>=1.7.1 (the command line tool)
|
||||||
|
|
||||||
notes:
|
notes:
|
||||||
- "If the task seems to be hanging, first verify remote host is in C(known_hosts).
|
- "If the task seems to be hanging, first verify remote host is in C(known_hosts).
|
||||||
SSH will prompt user to authorize the first contact with a remote host. To avoid this prompt,
|
SSH will prompt user to authorize the first contact with a remote host. To avoid this prompt,
|
||||||
|
@ -526,6 +527,14 @@ def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec):
|
||||||
elif version == 'HEAD':
|
elif version == 'HEAD':
|
||||||
refspecs.append('HEAD')
|
refspecs.append('HEAD')
|
||||||
elif is_remote_branch(git_path, module, dest, repo, version):
|
elif is_remote_branch(git_path, module, dest, repo, version):
|
||||||
|
currenthead = get_head_branch(git_path, module, dest, remote)
|
||||||
|
if currenthead != version:
|
||||||
|
# this workaroung is only needed for older git versions
|
||||||
|
# 1.8.3 is broken, 1.9.x works
|
||||||
|
# ensure that remote branch is available as both local and remote ref
|
||||||
|
refspecs.append('+refs/heads/%s:refs/heads/%s' % (version, version))
|
||||||
|
refspecs.append('+refs/heads/%s:refs/remotes/%s/%s' % (version, remote, version))
|
||||||
|
else:
|
||||||
refspecs.append(version)
|
refspecs.append(version)
|
||||||
elif is_remote_tag(git_path, module, dest, repo, version):
|
elif is_remote_tag(git_path, module, dest, repo, version):
|
||||||
refspecs.append('+refs/tags/'+version+':refs/tags/'+version)
|
refspecs.append('+refs/tags/'+version+':refs/tags/'+version)
|
||||||
|
@ -632,11 +641,15 @@ def submodule_update(git_path, module, dest, track_submodules):
|
||||||
return (rc, out, err)
|
return (rc, out, err)
|
||||||
|
|
||||||
def set_remote_branch(git_path, module, dest, remote, version, depth):
|
def set_remote_branch(git_path, module, dest, remote, version, depth):
|
||||||
cmd = "%s remote set-branches %s %s" % (git_path, remote, version)
|
"""set refs for the remote branch version
|
||||||
(rc, out, err) = module.run_command(cmd, cwd=dest)
|
|
||||||
if rc != 0:
|
This assumes the branch does not yet exist locally and is therefore also not checked out.
|
||||||
module.fail_json(msg="Failed to set remote branch: %s" % version)
|
Can't use git remote set-branches, as it is not available in git 1.7.1 (centos6)
|
||||||
cmd = "%s fetch --depth=%s %s %s" % (git_path, depth, remote, version)
|
"""
|
||||||
|
|
||||||
|
branchref = "+refs/heads/%s:refs/heads/%s" % (version, version)
|
||||||
|
branchref += ' +refs/heads/%s:refs/remotes/%s/%s' % (version, remote, version)
|
||||||
|
cmd = "%s fetch --depth=%s %s %s" % (git_path, depth, remote, branchref)
|
||||||
(rc, out, err) = module.run_command(cmd, cwd=dest)
|
(rc, out, err) = module.run_command(cmd, cwd=dest)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
module.fail_json(msg="Failed to fetch branch from remote: %s" % version)
|
module.fail_json(msg="Failed to fetch branch from remote: %s" % version)
|
||||||
|
|
Loading…
Reference in a new issue