From 848fa89224e5e32b83f7e90813ab21b7a12cafa3 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Tue, 7 Aug 2012 17:25:00 +0800 Subject: [PATCH] Moodle command to rebase issues --- lib/git.py | 16 ++++++++-- moodle-rebase.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100755 moodle-rebase.py diff --git a/lib/git.py b/lib/git.py index d49ca42..bbd85c1 100644 --- a/lib/git.py +++ b/lib/git.py @@ -95,12 +95,18 @@ class Git(object): cmd = 'pull %s %s' % (remote, ref) return self.execute(cmd) - def push(self, toremote = '', tobranch = '', force = None): + def push(self, remote = '', branch = '', force = None): if force: force = '--force ' else: force = '' - cmd = 'push %s%s %s' % (force, toremote, tobranch) + cmd = 'push %s%s %s' % (force, remote, branch) + return self.execute(cmd) + + def rebase(self, branch, onto = ''): + if onto != '': + onto = '--onto %s ' % (onto) + cmd = 'rebase %s%s' % (onto, branch) return self.execute(cmd) def reset(self, to, hard = False): @@ -111,6 +117,12 @@ class Git(object): result = self.execute(cmd) return result[0] == 0 + def stash(self, command = 'save', untracked = False): + cmd = 'stash %s' % command + if untracked: + cmd += ' --include-untracked' + return self.execute(cmd) + def status(self): return self.execute('status') diff --git a/moodle-rebase.py b/moodle-rebase.py new file mode 100755 index 0000000..e135973 --- /dev/null +++ b/moodle-rebase.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys +import argparse +import re +from lib import config, workplace, moodle, tools +from lib.tools import debug + +C = config.Conf().get +Wp = workplace.Workplace() + +# Arguments +parser = argparse.ArgumentParser(description="Backports a branch") +parser.add_argument('-i', '--issues', metavar='issues', required=True, nargs='+', help='issues to be rebased') +parser.add_argument('-s', '--suffix', metavar='suffix', help='the suffix of the branch of those issues') +parser.add_argument('-v', '--versions', metavar='version', nargs='+', choices=[ str(x) for x in range(13, C('masterBranch')) ] + ['master'], help='versions to rebase the issues on. Ignored if name is set.') +parser.add_argument('-p', '--push', action='store_true', help='push the branch after successful rebase') +parser.add_argument('-r', '--remote', metavar='remote', help='the remote to push the branch to. Default is %s.' % C('mineRepo')) +parser.add_argument('-f', '--force-push', action='store_true', help='Force the push', dest='forcepush') +parser.add_argument('name', metavar='name', default=None, nargs='?', help='name of the instance to work on') +args = parser.parse_args() + +instances = [] +if args.versions == None: + M = Wp.resolve(args.name) + if not M: + debug('This is not a Moodle instance') + sys.exit(1) + instances.append(M) +else: + for v in args.versions: + name = Wp.generateInstanceName(v) + if Wp.isMoodle(name): + instances.append(Wp.get(name)) + +# Loops over instances to rebase +for M in instances: + debug('Working on %s' % (M.get('identifier'))) + M.git().fetch('origin') + + # Stash + stash = M.git().stash(untracked=True) + if stash[0] != 0: + debug('Error while trying to stash your changes. Skipping %s.' % M.get('identifier')) + debug(stash[2]) + continue + elif not stash[1].startswith('No local changes'): + debug('Stashed your local changes') + + # Looping over each issue to rebase + for issue in args.issues: + branch = M.generateBranchName(issue, suffix=args.suffix) + if not M.git().hasBranch(branch): + debug('Could not find branch %s' % (branch)) + continue + + # Rebase + debug('> Rebasing %s...' % (branch)) + onto = 'origin/%s' % M.get('stablebranch') + result = M.git().rebase(branch, onto=onto) + if result[0] != 0: + debug('Error while rebasing branch %s onto %s' % (branch, onto)) + debug(result[2]) + continue + + # Pushing branch + if args.push: + remote = args.remote + if remote == None: + remote = C('mineRepo') + debug('Pushing %s to %s' % (branch, remote)) + result = M.git().push(remote=remote, branch=branch, force=args.forcepush) + if result[0] != 0: + debug('Error while pushing to remote') + debug(result[2]) + continue + + # Stash pop + if not stash[1].startswith('No local changes'): + pop = M.git().stash(command='pop') + if pop == False: + debug('An error ocured while unstashing your changes') + else: + debug('Popped the stash') + + debug('') + +debug('Done.') -- 2.11.0