Doctor command can check the value of the master branch
authorFrederic Massart <fred@moodle.com>
Wed, 5 Nov 2014 04:54:54 +0000 (12:54 +0800)
committerFrederic Massart <fred@moodle.com>
Wed, 5 Nov 2014 04:58:20 +0000 (12:58 +0800)
extra/bash_completion
mdk/commands/doctor.py
mdk/git.py
mdk/workplace.py

index 1cb8f1d..3b7e32c 100644 (file)
@@ -108,7 +108,7 @@ function _mdk() {
                 OPTS="--compile --debug --sheets --theme --watch"
                 ;;
             doctor)
-                OPTS="--all --branch --cached --dependencies --directories --remotes --symlink --wwwroot"
+                OPTS="--all --branch --cached --dependencies --directories --masterbranch --remotes --symlink --wwwroot"
                 ;;
             fix)
                 if [[ "$PREV" == "-n" || "$PREV" == "--name" ]]; then
index cb3da5c..0f75cd4 100644 (file)
@@ -23,6 +23,7 @@ http://github.com/FMCorz/mdk
 """
 
 import os
+import re
 import shutil
 import subprocess
 from .. import git
@@ -84,6 +85,13 @@ class DoctorCommand(Command):
             }
         ),
         (
+            ['--masterbranch'],
+            {
+                'action': 'store_true',
+                'help': 'Check the status of the master branch'
+            }
+        ),
+        (
             ['--remotes'],
             {
                 'action': 'store_true',
@@ -145,6 +153,10 @@ class DoctorCommand(Command):
         if args.branch or allChecks:
             self.branch(args)
 
+        # Check the master branch
+        if args.masterbranch or allChecks:
+            self.masterbranch(args)
+
         # Check what you see is what you get
         if args.hi:
             self.hi(args)
@@ -274,6 +286,46 @@ class DoctorCommand(Command):
                     print '    Creating %s' % d
                     mkdir(d, 0777)
 
+    def masterbranch(self, args):
+        """Checks the current master branch and the value set in config."""
+
+        print 'Checking master branch'
+
+        if not self._checkWorkplace():
+            return
+
+        repoPath = self.Wp.getCachedRemote()
+        if not os.path.isdir(repoPath):
+            return
+
+        try:
+            self.Wp.updateCachedClones(verbose=False)
+        except Exception:
+            print '  Could not update clone, please try again.'
+            return
+
+        repo = git.Git(repoPath, self.C.get('git'))
+        result = repo.execute(['show', 'master:version.php'])
+        if result[0] != 0:
+            print '  Could not read the master version.php'
+            return
+
+        reBranch = re.compile(r'^\s*\$branch\s*=\s*(?P<brackets>[\'"])?([0-9]+)(?P=brackets)\s*;')
+        latestBranch = None
+        for line in result[1].split('\n'):
+            if reBranch.search(line):
+                latestBranch = int(reBranch.search(line).group(2))
+
+        masterBranch = int(self.C.get('masterBranch'))
+        if not latestBranch:
+            print '  Oops, could not identify the mater branch'
+        elif masterBranch != latestBranch:
+            print '  The config masterBranch is set to %d, expecting %d' % (masterBranch, latestBranch)
+            if args.fix:
+                print '    Setting masterBranch to %d' % (latestBranch)
+                self.C.set('masterBranch', latestBranch)
+
+
     def hi(self, args):
         """I wonder what is the purpose of this...
 
index ac89158..8a2bff7 100644 (file)
@@ -123,7 +123,7 @@ class Git(object):
         if not self.isRepository(path):
             raise Exception('This is not a Git repository')
 
-        if not type(cmd) == 'list':
+        if not type(cmd) == list:
             cmd = shlex.split(str(cmd))
         cmd.insert(0, self.getBin())
 
index a9dff86..f16bdab 100644 (file)
@@ -426,7 +426,8 @@ class Workplace(object):
 
             repo = git.Git(cache, C.get('git'))
 
-            logging.info('Fetching cached repository %s...' % os.path.basename(cache))
+            if verbose:
+                logging.info('Fetching cached repository %s...', os.path.basename(cache))
             if not repo.fetch():
                 raise Exception('Could not fetch in repository %s' % (cache))