New setting to set up set the name of the upstream remote
authorFrederic Massart <fred@moodle.com>
Fri, 12 Oct 2012 13:00:47 +0000 (21:00 +0800)
committerFrederic Massart <fred@moodle.com>
Fri, 12 Oct 2012 13:00:47 +0000 (21:00 +0800)
README.md
config-dist.json
lib/git.py
lib/moodle.py
lib/workplace.py
moodle-backport.py
moodle-fix.py
moodle-rebase.py

index 7973dae..df02dda 100644 (file)
--- a/README.md
+++ b/README.md
@@ -139,7 +139,7 @@ Create a branch from an issue number on the tracker (MDL-12345) and sets it to t
 
 **Examples**
 
-In a Moodle 2.2 instance, this will create a branch named MDL-12345-22 which will track origin/MOODLE_22_STABLE.
+In a Moodle 2.2 instance, this will create a branch named MDL-12345-22 which will track upstream/MOODLE_22_STABLE.
 
     moodle fix MDL-12345
     moodle fix 12345
@@ -202,7 +202,7 @@ Force a push of the branch MDL-12345-22 from the instance stable_22 to your remo
 
 ### - rebase
 
-Fetch the latest branches from origin and rebase your local branches.
+Fetch the latest branches from the upstream remote and rebase your local branches.
 
 **Examples**
 
@@ -234,7 +234,7 @@ Set the instance stable_master ready for development
 
 ### - update
 
-Fetch the latest stables branches from the origin and pull the changes into the local stable branch.
+Fetch the latest stables branches from the upstream remote and pull the changes into the local stable branch.
 
 **Examples**
 
index c0000c9..a821a5a 100644 (file)
         "pgsql": "PostgreSQL"
     },
 
-
     // The host name to set during an install
     "host": "localhost",
     // Moodle admin login
     "login": "admin",
     // Moodle admin password
     "passwd": "test",
-    // What to call your remote, do not call that origin!
-    "myRemote": "github",
+    // What to call your remote, the one pointing to remotes.mine
+    "myRemote": "origin",
+    // What to call the upstream remote, the one pointing to the official repositoriy (stable or integration)
+    "upstreamRemote": "upstream",
 
     // The name of the data directory
     "dataDir": "moodledata",
@@ -86,7 +87,7 @@
     // Path to PHP
     "php": "/usr/bin/php",
 
-    // Experimental setting to use a local cache as your origin remotes.
+    // Experimental setting to use a local cache as your upstream remotes.
     // Can be useful to prevent fetch from a slow remote.
     "useCacheAsRemote": false,
 
index 783e7d1..34494ff 100644 (file)
@@ -23,6 +23,7 @@ http://github.com/FMCorz/mdk
 """
 
 import os
+import re
 import shlex
 import subprocess
 
@@ -64,6 +65,11 @@ class Git(object):
                else:
                        return result[1].replace('refs/heads/', '').strip()
 
+       def delRemote(self, remote):
+               cmd = 'remote rm %s' % remote
+               result = self.execute(cmd)
+               return result[0] == 0
+
        def execute(self, cmd, path = None):
                if path == None:
                        path = self.getPath()
@@ -95,6 +101,24 @@ class Git(object):
                else:
                        return None
 
+       def getRemote(self, remote):
+               remotes = self.getRemotes()
+               return remotes.get(remote, None)
+
+       def getRemotes(self):
+               """Return the remotes"""
+               cmd = 'remote -v'
+               result = self.execute(cmd)
+               remotes = None
+               if result[0] == 0:
+                       remotes = {}
+                       for line in result[1].split('\n'):
+                               if not line: continue
+                               (remote, repo) = re.split('\s+', line, 1)
+                               repo = re.sub(' \(push\)|\(fetch\)$', '', repo)
+                               remotes[remote] = repo
+               return remotes
+
        def hasBranch(self, branch, remote = ''):
                if remote != '':
                        cmd = 'show-ref --verify --quiet "refs/remotes/%s/%s"' % (remote, branch)
@@ -173,6 +197,14 @@ class Git(object):
                result = self.execute(cmd)
                return result[0] == 0
 
+       def setRemote(self, remote, url):
+               if not self.getRemote(remote):
+                       cmd = 'remote add %s %s' % (remote, url)
+               else:
+                       cmd = 'remote set-url %s %s' % (remote, url)
+               result = self.execute(cmd)
+               return result[0] == 0
+
        def stash(self, command = 'save', untracked = False):
                cmd = 'stash %s' % command
                if untracked:
index fd47398..870796c 100644 (file)
@@ -308,7 +308,7 @@ class Moodle(object):
 
     def isIntegration(self):
         """Returns whether an instance is an integration one or not"""
-        remote = self.git().getConfig('remote.origin.url')
+        remote = self.git().getConfig('remote.%s.url' % C.get('upstreamRemote'))
         if remote != None and remote.endswith('integration.git'):
             return True
         return False
@@ -437,9 +437,12 @@ class Moodle(object):
             os.remove(dest)
             return result[0]
 
-    def update(self, remote = 'origin'):
+    def update(self, remote = None):
         """Update the instance from the remote"""
 
+        if remote == None:
+            remote = C.get('upstreamRemote')
+
         # Fetch
         if not self.git().fetch(remote):
             raise Exception('Could not fetch remote %s' % remote)
index 24f674f..c9bdc39 100644 (file)
@@ -92,13 +92,16 @@ class Workplace(object):
 
         if integration:
             repository = os.path.join(self.cache, 'integration.git')
+            upstreamRepository = C.get('remotes.integration')
         else:
             repository = os.path.join(self.cache, 'moodle.git')
+            upstreamRepository = C.get('remotes.stable')
 
         # Clone the instances
         debug('Cloning repository...')
         if useCacheAsRemote:
             result = process('%s clone %s %s' % (C.get('git'), repository, wwwDir))
+            upstreamRepository = repository
         else:
             copy_tree(repository, wwwDir)
 
@@ -117,21 +120,25 @@ class Workplace(object):
             if not os.path.isfile(linkDataDir) and not os.path.isdir(linkDataDir) and not os.path.islink(linkDataDir):
                 os.symlink(dataDir, linkDataDir)
 
-        # Creating, fetch, pulling branches
         debug('Checking out branch...')
         repo = git.Git(wwwDir, C.get('git'))
-        result = repo.fetch('origin')
+
+        # Setting up the correct remote names
+        repo.setRemote(C.get('myRemote'), C.get('remotes.mine'))
+        repo.setRemote(C.get('upstreamRemote'), upstreamRepository)
+
+        # Creating, fetch, pulling branches
+        result = repo.fetch(C.get('upstreamRemote'))
         if version == 'master':
             repo.checkout('master')
         else:
-            track = 'origin/MOODLE_%s_STABLE' % version
+            track = '%s/MOODLE_%s_STABLE' % (C.get('upstreamRemote'), version)
             branch = 'MOODLE_%s_STABLE' % version
             if not repo.createBranch(branch, track):
                 debug('Could not create branch %s tracking %s' % (branch, track))
             else:
                 repo.checkout(branch)
-        repo.pull()
-        repo.addRemote(C.get('myRemote'), C.get('remotes.mine'))
+        repo.pull(remote = C.get('upstreamRemote'))
 
         M = self.get(name)
         return M
@@ -312,7 +319,7 @@ class Workplace(object):
                     raise Exception('Could not create branch %s tracking %s in repository %s' % (branch, track, cache))
 
                 if not repo.checkout(branch):
-                    raise exception('error while checking out branch %s in repository %s' % (branch, cache))
+                    raise Exception('Error while checking out branch %s in repository %s' % (branch, cache))
 
                 if not repo.reset(to = track, hard = True):
                     raise Exception('Could not hard reset to %s in repository %s' % (branch, cache))
index b632a07..d4f5983 100755 (executable)
@@ -112,7 +112,7 @@ for v in versions:
 
        # Creates a new branch if necessary
        newbranch = M2.generateBranchName(issue, suffix=suffix)
-       track = 'origin/%s' % M2.get('stablebranch')
+       track = '%s/%s' % (C.get('upstreamRemote'), M2.get('stablebranch'))
        if not M2.git().hasBranch(newbranch):
                debug('Creating branch %s' % newbranch)
                if not M2.git().createBranch(newbranch, track=track):
@@ -124,8 +124,8 @@ for v in versions:
                debug('Hard reset to %s' % (track))
                M2.git().reset(to=track, hard=True)
 
-       # Picking the diff origin/MOODLE_23_STABLE..github/MDL-12345-master
-       cherry = 'origin/%s..%s/%s' % (originaltrack, remote, branch)
+       # Picking the diff upstream/MOODLE_23_STABLE..github/MDL-12345-master
+       cherry = '%s/%s..%s/%s' % (C.get('upstreamRemote'), originaltrack, remote, branch)
        debug('Cherry-picking %s' % (cherry))
        result = M2.git().pick(cherry)
        if result[0] != 0:
index f12719c..22f00bb 100755 (executable)
@@ -50,7 +50,7 @@ if not M:
 branch = M.generateBranchName(args.issue, suffix=args.suffix)
 
 # Track
-track = 'origin/%s' % M.get('stablebranch')
+track = '%s/%s' % (C.get('upstreamRemote'), M.get('stablebranch'))
 
 # Git repo
 repo = M.git()
index 36423be..0927c6e 100755 (executable)
@@ -63,7 +63,7 @@ Mlist = Wp.resolveMultiple(names)
 # Loops over instances to rebase
 for M in Mlist:
        debug('Working on %s' % (M.get('identifier')))
-       M.git().fetch('origin')
+       M.git().fetch(C.get('upstreamRemote'))
 
        # Test if currently in a detached branch
        if M.git().currentBranch() == 'HEAD':
@@ -91,7 +91,7 @@ for M in Mlist:
 
                # Rebase
                debug('> Rebasing %s...' % (branch))
-               base = 'origin/%s' % M.get('stablebranch')
+               base = '%s/%s' % (C.get('upstreamRemote'), M.get('stablebranch'))
                result = M.git().rebase(branch=branch, base=base)
                if result[0] != 0:
                        debug('Error while rebasing branch %s on top of %s' % (branch, base))