mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-03 04:34:24 -07:00
Rewrite switch_version() to detect branch
Rewrote switch_version() to read .git/HEAD to find branch associated with HEAD. If in a detached HEAD state, will read .git/refs/remotes/<remote>/HEAD.
This commit is contained in:
parent
49d41da152
commit
6aa51a7cf3
1 changed files with 30 additions and 11 deletions
41
library/git
41
library/git
|
@ -149,15 +149,35 @@ def is_not_a_branch(module, dest):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_head_branch(module, dest, remote):
|
def get_head_branch(module, dest, remote):
|
||||||
os.chdir(dest)
|
'''
|
||||||
head = ''
|
Determine what branch HEAD is associated with. This is partly
|
||||||
(rc, out, err) = _run("git remote show %s" % remote)
|
taken from lib/ansible/utils/__init__.py. It finds the correct
|
||||||
if rc != 0:
|
path to .git/HEAD and reads from that file the branch that HEAD is
|
||||||
module.fail_json(msg="Could not determine HEAD branch via git remote show")
|
associated with. In the case of a detached HEAD, this will look
|
||||||
for line in out.split('\n'):
|
up the branch in .git/refs/remotes/<remote>/HEAD.
|
||||||
if 'HEAD branch' in line:
|
'''
|
||||||
head = line.split()[-1].strip()
|
repo_path = os.path.join(dest, '.git')
|
||||||
return head
|
# 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:
|
||||||
|
gitdir = yaml.load(open(repo_path)).get('gitdir')
|
||||||
|
# There is a posibility 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):
|
||||||
|
return ''
|
||||||
|
# Read .git/HEAD for the name of the branch.
|
||||||
|
# If we're in a detached HEAD state, look up the branch associated with
|
||||||
|
# the remote HEAD in .git/refs/remotes/<remote>/HEAD
|
||||||
|
f = open(os.path.join(repo_path, "HEAD"))
|
||||||
|
if is_not_a_branch(module, dest):
|
||||||
|
f.close()
|
||||||
|
f = open(os.path.join(repo_path, 'refs', 'remotes', remote, 'HEAD'))
|
||||||
|
branch = f.readline().split('/')[-1].rstrip("\n")
|
||||||
|
f.close()
|
||||||
|
return branch
|
||||||
|
|
||||||
def fetch(module, repo, dest, version, remote):
|
def fetch(module, repo, dest, version, remote):
|
||||||
''' updates repo from remote sources '''
|
''' updates repo from remote sources '''
|
||||||
|
@ -181,12 +201,11 @@ def switch_version(module, dest, remote, version):
|
||||||
else:
|
else:
|
||||||
cmd = "git checkout --force %s" % version
|
cmd = "git checkout --force %s" % version
|
||||||
else:
|
else:
|
||||||
# is there a better way to do this?
|
|
||||||
branch = get_head_branch(module, dest, remote)
|
branch = get_head_branch(module, dest, remote)
|
||||||
(rc, out, err) = _run("git checkout --force %s" % branch)
|
(rc, out, err) = _run("git checkout --force %s" % branch)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
module.fail_json(msg="Failed to checkout branch %s" % branch)
|
module.fail_json(msg="Failed to checkout branch %s" % branch)
|
||||||
cmd = "git rebase %s" % remote
|
cmd = "git reset --hard %s" % remote
|
||||||
return _run(cmd)
|
return _run(cmd)
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue