Backport can handle conflicts in bootstrapbase css. Fixes #76
authorFrederic Massart <fred@moodle.com>
Mon, 10 Mar 2014 06:01:08 +0000 (14:01 +0800)
committerFrederic Massart <fred@moodle.com>
Mon, 10 Mar 2014 06:04:11 +0000 (14:04 +0800)
lib/commands/backport.py
lib/git.py

index 153e55e..648a0c7 100644 (file)
@@ -23,7 +23,7 @@ http://github.com/FMCorz/mdk
 """
 
 import logging
-from lib import tools
+from lib import tools, css
 from lib.command import Command
 from lib.tools import yesOrNo
 
@@ -191,16 +191,40 @@ class BackportCommand(Command):
             logging.info('Cherry-picking %s' % (cherry))
             result = M2.git().pick(hashes)
             if result[0] != 0:
-                logging.error('Error while cherry-picking %s in %s.' % (cherry, name))
-                logging.debug(result[2])
-                if yesOrNo('The cherry-pick might still be in progress, would you like to abort it?'):
-                    result = M2.git().pick(abort=True)
-                    if result[0] > 0 and result[0] != 128:
-                        logging.error('Could not abort the cherry-pick!')
+
+                # Try to resolve the conflicts if any.
+                resolveConflicts = True
+                conflictsResolved = False
+                while resolveConflicts:
+
+                    # Check the list of possible conflicting files.
+                    conflictingFiles = M2.git().conflictingFiles()
+                    if conflictingFiles and len(conflictingFiles) == 1 and 'theme/bootstrapbase/style/moodle.css' in conflictingFiles:
+                        logging.info('Conflicts found in bootstrapbase moodle CSS, trying to auto resolve...')
+                        cssCompiler = css.Css(M2)
+                        if cssCompiler.compile(theme='bootstrapbase', sheets=['moodle']):
+                            M2.git().add('theme/bootstrapbase/style/moodle.css')
+                            # We need to commit manually to prevent the editor to open.
+                            M2.git().commit(filepath='.git/MERGE_MSG')
+                            result = M2.git().pick(continu=True)
+                            if result[0] == 0:
+                                resolveConflicts = False
+                                conflictsResolved = True
                     else:
-                        stashPop(stash)
-                logging.info('')
-                continue
+                        resolveConflicts = False
+
+                # We still have a dirty repository.
+                if not conflictsResolved:
+                    logging.error('Error while cherry-picking %s in %s.' % (cherry, name))
+                    logging.debug(result[2])
+                    if yesOrNo('The cherry-pick might still be in progress, would you like to abort it?'):
+                        result = M2.git().pick(abort=True)
+                        if result[0] > 0 and result[0] != 128:
+                            logging.error('Could not abort the cherry-pick!')
+                        else:
+                            stashPop(stash)
+                    logging.info('')
+                    continue
 
             # Pushing branch
             if args.push:
index 7a69d2a..1bea328 100644 (file)
@@ -37,6 +37,12 @@ class Git(object):
         self.setPath(path)
         self.setBin(bin)
 
+    def add(self, path):
+        """Add a file/path"""
+        cmd = 'add %s' % (path)
+        result = self.execute(cmd)
+        return result[0] == 0
+
     def addRemote(self, name, remote):
         cmd = 'remote add %s %s' % (name, remote)
         result = self.execute(cmd)
@@ -60,6 +66,22 @@ class Git(object):
         result = self.execute(cmd)
         return result[0] == 0
 
+    def commit(self, filepath=None):
+        """Wrapper for the commit command"""
+        cmd = 'commit'
+        if filepath:
+            cmd += ' -F %s' % (filepath)
+        result = self.execute(cmd)
+        return result[0] == 0
+
+    def conflictingFiles(self):
+        cmd = 'diff --name-only --diff-filter=U'
+        result = self.execute(cmd)
+        if result[0] == 0:
+            return result[1].strip().split('\n')
+        else:
+            return False
+
     def createBranch(self, branch, track=None):
         if track != None:
             cmd = 'branch --track %s %s' % (branch, track)
@@ -182,7 +204,11 @@ class Git(object):
         messages = self.log(count=count, since=since, path=path, format='%s')
         return messages.split('\n')[:-1]
 
-    def pick(self, refs=None, abort=None):
+    def pick(self, refs=None, abort=None, continu=None):
+        """Wrapper for the cherry-pick command
+
+        The typo in the argument 'continu' is voluntarily there, continue is a reserved word.
+        """
         args = ''
         if refs != None:
             if type(refs) == list:
@@ -190,6 +216,8 @@ class Git(object):
             args = refs
         elif abort != None:
             args = '--abort'
+        elif continu != None:
+            args = '--continue'
         cmd = 'cherry-pick %s' % (args)
         return self.execute(cmd)