2012-02-27 03:17:31 +00:00
|
|
|
#!/usr/bin/python
|
2012-08-03 01:29:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-02-27 03:17:31 +00:00
|
|
|
|
2012-02-29 00:08:09 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-05-08 20:20:08 +00:00
|
|
|
import re
|
2012-08-28 09:28:50 +00:00
|
|
|
import tempfile
|
2012-02-27 03:17:31 +00:00
|
|
|
|
|
|
|
def get_version(dest):
|
2012-07-30 05:33:16 +00:00
|
|
|
''' samples the version of the git repo '''
|
|
|
|
os.chdir(dest)
|
|
|
|
cmd = "git show --abbrev-commit"
|
|
|
|
sha = os.popen(cmd).read().split("\n")
|
|
|
|
sha = sha[0].split()[1]
|
|
|
|
return sha
|
2012-02-27 03:17:31 +00:00
|
|
|
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
def clone(repo, dest):
|
2012-07-30 05:33:16 +00:00
|
|
|
''' makes a new git repo if it does not already exist '''
|
|
|
|
try:
|
|
|
|
os.makedirs(os.path.dirname(dest))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
cmd = "git clone %s %s" % (repo, dest)
|
2012-08-28 09:28:50 +00:00
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=tempfile.gettempdir())
|
2012-07-30 05:33:16 +00:00
|
|
|
(out, err) = cmd.communicate()
|
|
|
|
rc = cmd.returncode
|
|
|
|
return (rc, out, err)
|
2012-02-27 03:17:31 +00:00
|
|
|
|
2012-08-23 04:07:14 +00:00
|
|
|
|
|
|
|
def has_local_mods(dest):
|
|
|
|
os.chdir(dest)
|
|
|
|
cmd = "git status -s"
|
|
|
|
lines = os.popen(cmd).read().splitlines()
|
|
|
|
lines = filter(lambda c: re.search('^\\?\\?.*$',c) == None,lines)
|
|
|
|
return len(lines) > 0
|
|
|
|
|
|
|
|
def reset(module,dest,force):
|
2012-07-30 05:33:16 +00:00
|
|
|
'''
|
|
|
|
Resets the index and working tree to HEAD.
|
|
|
|
Discards any changes to tracked files in working
|
|
|
|
tree since that commit.
|
|
|
|
'''
|
|
|
|
os.chdir(dest)
|
2012-08-23 04:07:14 +00:00
|
|
|
if not force and has_local_mods(dest):
|
|
|
|
module.fail_json(msg="Local modifications exist in repository (force=no).")
|
2012-07-30 05:33:16 +00:00
|
|
|
cmd = "git reset --hard HEAD"
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(out, err) = cmd.communicate()
|
|
|
|
rc = cmd.returncode
|
|
|
|
return (rc, out, err)
|
2012-05-02 04:53:20 +00:00
|
|
|
|
2012-07-30 05:33:16 +00:00
|
|
|
def get_branches(module, dest):
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
os.chdir(dest)
|
|
|
|
branches = []
|
|
|
|
cmd = "git branch -a"
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
out, err = cmd.communicate()
|
|
|
|
if cmd.returncode != 0:
|
2012-07-30 05:33:16 +00:00
|
|
|
module.fail_json(msg="Could not determine branch data - received %s" % out)
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
for line in out.split('\n'):
|
|
|
|
branches.append(line.strip())
|
|
|
|
return branches
|
|
|
|
|
2012-07-30 05:33:16 +00:00
|
|
|
def is_remote_branch(module, dest, remote, branch):
|
|
|
|
branches = get_branches(module, dest)
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
rbranch = 'remotes/%s/%s' % (remote, branch)
|
|
|
|
if rbranch in branches:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-07-30 05:33:16 +00:00
|
|
|
def is_local_branch(module, dest, branch):
|
|
|
|
branches = get_branches(module, dest)
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
lbranch = '%s' % branch
|
|
|
|
if lbranch in branches:
|
|
|
|
return True
|
2012-07-30 05:33:16 +00:00
|
|
|
elif '* %s' % branch in branches:
|
|
|
|
return True
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-07-30 05:33:16 +00:00
|
|
|
def is_current_branch(module, dest, branch):
|
|
|
|
branches = get_branches(module, dest)
|
|
|
|
for b in branches:
|
|
|
|
if b.startswith('* '):
|
|
|
|
cur_branch = b
|
|
|
|
if branch == cur_branch or '* %s' % branch == cur_branch:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def pull(module, repo, dest, version):
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
''' updates repo from remote sources '''
|
|
|
|
os.chdir(dest)
|
2012-07-30 05:33:16 +00:00
|
|
|
branches = get_branches(module, dest)
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
cur_branch = ''
|
|
|
|
for b in branches:
|
|
|
|
if b.startswith('* '):
|
|
|
|
cur_branch = b
|
2012-07-30 05:33:16 +00:00
|
|
|
if is_local_branch(module, dest, version) and not is_current_branch(module, dest, version):
|
|
|
|
(out, err) = switch_version(module, dest, remote, version)
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
|
|
|
|
cmd = "git pull -u origin"
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
out, err = cmd.communicate()
|
|
|
|
rc = cmd.returncode
|
|
|
|
return (rc, out, err)
|
|
|
|
|
2012-07-30 05:33:16 +00:00
|
|
|
def switch_version(module, dest, remote, version):
|
|
|
|
''' once pulled, switch to a particular SHA, tag, or branch '''
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
os.chdir(dest)
|
|
|
|
cmd = ''
|
|
|
|
if version != 'HEAD':
|
2012-07-30 05:33:16 +00:00
|
|
|
if not is_local_branch(module, dest, version) and is_remote_branch(module, dest, remote, version):
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 07:22:30 +00:00
|
|
|
cmd = "git checkout --track -b %s %s/%s" % (version, remote, version)
|
|
|
|
else:
|
|
|
|
cmd = "git checkout --force %s" % version
|
|
|
|
else:
|
|
|
|
# is there a better way to do this?
|
|
|
|
cmd = "git rebase origin"
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(out, err) = cmd.communicate()
|
|
|
|
rc = cmd.returncode
|
|
|
|
return (rc, out, err)
|
2012-02-27 03:17:31 +00:00
|
|
|
|
2012-07-30 05:33:16 +00:00
|
|
|
# ===========================================
|
2012-02-27 03:17:31 +00:00
|
|
|
|
2012-07-30 05:33:16 +00:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
dest=dict(required=True),
|
2012-08-01 04:21:36 +00:00
|
|
|
repo=dict(required=True, aliases=['name']),
|
2012-07-30 05:33:16 +00:00
|
|
|
version=dict(default='HEAD'),
|
2012-08-23 04:07:14 +00:00
|
|
|
remote=dict(default='origin'),
|
|
|
|
force=dict(default='yes', choices=['yes', 'no'], aliases=['force'])
|
2012-07-30 05:33:16 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2012-09-21 17:20:05 +00:00
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
2012-07-30 05:33:16 +00:00
|
|
|
repo = module.params['repo']
|
|
|
|
version = module.params['version']
|
|
|
|
remote = module.params['remote']
|
2012-08-23 04:07:14 +00:00
|
|
|
force = module.boolean(module.params['force'])
|
2012-07-30 05:33:16 +00:00
|
|
|
|
|
|
|
gitconfig = os.path.join(dest, '.git', 'config')
|
|
|
|
|
2012-08-07 18:20:14 +00:00
|
|
|
rc, out, err, status = (0, None, None, None)
|
2012-07-30 05:33:16 +00:00
|
|
|
|
|
|
|
# if there is no git configuration, do a clone operation
|
|
|
|
# else pull and switch the version
|
|
|
|
before = None
|
2012-08-23 04:07:14 +00:00
|
|
|
local_mods = False
|
2012-07-30 05:33:16 +00:00
|
|
|
if not os.path.exists(gitconfig):
|
|
|
|
(rc, out, err) = clone(repo, dest)
|
|
|
|
if rc != 0:
|
2012-07-31 02:51:26 +00:00
|
|
|
module.fail_json(msg=err)
|
2012-07-30 05:33:16 +00:00
|
|
|
else:
|
2012-08-07 00:07:02 +00:00
|
|
|
# else do a pull
|
2012-08-23 04:07:14 +00:00
|
|
|
local_mods = has_local_mods(dest)
|
2012-07-30 05:33:16 +00:00
|
|
|
before = get_version(dest)
|
2012-08-23 04:07:14 +00:00
|
|
|
(rc, out, err) = reset(module,dest,force)
|
2012-07-30 05:33:16 +00:00
|
|
|
if rc != 0:
|
2012-07-31 02:51:26 +00:00
|
|
|
module.fail_json(msg=err)
|
2012-07-30 05:33:16 +00:00
|
|
|
(rc, out, err) = pull(module, repo, dest, version)
|
|
|
|
|
|
|
|
# handle errors from clone or pull
|
|
|
|
if out.find('error') != -1 or err.find('ERROR') != -1:
|
2012-07-31 02:51:26 +00:00
|
|
|
module.fail_json(msg=err)
|
2012-07-30 05:33:16 +00:00
|
|
|
|
|
|
|
# switch to version specified regardless of whether
|
|
|
|
# we cloned or pulled
|
|
|
|
(rc, out, err) = switch_version(module, dest, remote, version)
|
2012-08-07 18:20:14 +00:00
|
|
|
if rc != 0:
|
2012-07-31 02:51:26 +00:00
|
|
|
module.fail_json(msg=err)
|
2012-07-30 05:33:16 +00:00
|
|
|
|
|
|
|
# determine if we changed anything
|
|
|
|
after = get_version(dest)
|
|
|
|
changed = False
|
|
|
|
|
2012-08-23 04:07:14 +00:00
|
|
|
if before != after or local_mods:
|
2012-07-30 05:33:16 +00:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, before=before, after=after)
|
|
|
|
|
|
|
|
# include magic from lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|