root URL must be accompanied with branch name
authorTimo Savola <tsavola@movial.fi>
Fri, 16 May 2008 12:23:42 +0000 (15:23 +0300)
committerTimo Savola <tsavola@movial.fi>
Fri, 16 May 2008 12:26:03 +0000 (15:26 +0300)
'URL' -> ('URL', 'branch')

also, Component() no longer takes tag/tags parameters but a branch
parameter which overrides root's branch.  the specified branch will be
checked out after cloning.

matrix/components.py
matrix/git.py
matrix/repositories.py

index f4c835a46f1b5e316219ddb67daebaf6b66e7290..973e6f8fb3b348024705dfc941cdd6cc05e5185a 100644 (file)
@@ -21,23 +21,19 @@ class Component(object):
        weight = None
        state = None
 
-       def __init__(self, name, tag='master', tags={}, flags=[]):
-               commits = {}
-               commits.update(tags)
-               commits[None] = tag
-
+       def __init__(self, name, branch=None, flags=[]):
                self.name = name
 
                self.source = Repository(
                        'source/%s' % name,
                        os.path.join(config.top_dir, 'src', name),
-                       commits,
+                       branch,
                        exclude=['meta'])
 
                self.meta = Repository(
                        'meta/%s' % name,
                        os.path.join(config.top_dir, 'src', name, 'meta'),
-                       commits)
+                       branch)
 
                self.flags = flags
 
index a781b5ba63f191a877537158eb4d43debd72e041..a705b97c598347448f7422ce239ba62ea12cc35a 100644 (file)
@@ -134,6 +134,20 @@ def clone(name, url, checkout=True):
 
        call(['clone'] + options + [url, name], fail=True)
 
+def branch_active(name):
+       lines = call_output(['symbolic-ref', 'HEAD'], workdir=name)
+       if lines:
+               return lines[0].replace('refs/heads/', '')
+       else:
+               return None
+
+def branch_create(name, branch, start=None):
+       cmd = ['branch', '--track', branch]
+       if start:
+               cmd.append(start)
+
+       call(cmd, workdir=name, fail=True)
+
 def remote_update(name):
        call(['remote', 'update'], workdir=name)
 
@@ -175,9 +189,9 @@ def rev_parse(name, arg):
        else:
                return None
 
-def getvar(name,var):
-       res=call_output(['config',var],workdir=name)
-       if res and len(res)==1:
+def config_get(name, var):
+       res = call_output(['config', '--get', var], workdir=name)
+       if res and len(res) == 1:
                return res[0]
        return None
 
index 7abc9687e5ec7ebbafcfa93f288e291dfd41a616..dd071715a6b471ad43ff011e96ad1c3689318aea 100644 (file)
@@ -10,68 +10,91 @@ from config import config
 Error = RuntimeError
 
 class Repository(object):
-       def __init__(self, name, path, commits, exclude=None):
+       def __init__(self, name, path, branch, exclude=None):
                self.name = name
                self.path = path
-               self.commits = commits
+               self.branch = branch
                self.exclude = exclude
-               self.__url = None
+               self.__url_branch = None
                self.__hash = None
 
        def __str__(self):
                return self.path
 
-       def get_commit(self):
-               return self.commits.get(config.board) or self.commits.get(None)
+       def exists(self):
+               return git.contains_database(self.path)
 
-       def get_url(self):
-               if self.__url is None:
-                       self.__url = self.__get_url()
-               return self.__url
+       def __get_url_branch(self):
+               if not self.__url_branch:
+                       if self.exists():
+                               self.__url_branch = self.__old_url_branch()
 
-       def __get_url(self):
-               if git.contains_database(self.path):
-                       url = git.getvar(self.path, 'remote.origin.url')
-                       if url:
-                               if config.debug:
-                                       print 'Using', url
-                               return url
+                       if not self.__url_branch:
+                               self.__url_branch = self.__new_url_branch()
+
+                       if config.debug:
+                               url, branch = self.__url_branch
+                               print 'Using URL:', url
+                               print 'Using branch:', branch
 
-               for root in config.roots:
+               return self.__url_branch
+
+       def __new_url_branch(self):
+               for root, branch in config.roots:
                        for suffix in ('.git', ''):
                                url = '%s/%s%s' % (root, self.name, suffix)
+
                                if config.debug:
                                        print 'Trying', url
 
-                               if not git.peek_remote(url, quiet=True):
-                                       continue
-
-                               if config.debug:
-                                       print 'Found', url
-                               return url
+                               if git.peek_remote(url, quiet=True):
+                                       if self.branch:
+                                               branch = self.branch
+                                       return url, branch
 
                raise Error('Failed to locate repository under any root: ' + \
                            '%s.git' % self.name)
 
-       def exists(self):
-               return git.contains_database(self.path)
+       def __old_url_branch(self):
+               branch = git.branch_active(self.path)
+               if not branch:
+                       return None
+
+               remote = git.config_get(self.path, 'branch.%s.remote' % branch)
+               if not remote:
+                       remote = 'origin'
+                       if config.debug:
+                               print 'Branch "%s" has no configured remote,' \
+                                     ' assuming "%s"' % (branch, remote)
+
+               url = git.config_get(self.path, 'remote.%s.url' % remote)
+               if not url:
+                       return None
+
+               return url, branch
 
        def clone(self):
                print 'Cloning', self
 
+               url, branch = self.__get_url_branch()
+
                if os.path.exists(self.path):
-                       self.__clone_in_place()
+                       self.__clone_in_place(url)
                else:
-                       git.clone(self.path, self.get_url(), checkout=False)
+                       git.clone(self.path, url, checkout=False)
 
                if self.exclude:
                        git.exclude(self.path, self.exclude)
 
-               git.reset(self.path, self.get_commit(), hard=True)
+               if branch != git.branch_active(self.path):
+                       remote = 'origin/%s' % branch
+                       git.branch_create(self.path, branch, remote)
+
+               git.checkout(self.path, branch)
 
-       def __clone_in_place(self):
+       def __clone_in_place(self, url):
                tmp = os.path.join(self.path, 'tmp')
-               git.clone(tmp, self.get_url(), checkout=False)
+               git.clone(tmp, url, checkout=False)
 
                try:
                        tmpdb = git.database_path(tmp)
@@ -129,16 +152,20 @@ class Repository(object):
        def archive(self, name, path):
                print 'Archiving', self
 
+               url, branch = self.__get_url_branch()
+
                git.archive(self.path, path,
                            prefix=name + os.path.sep,
-                           treeish=self.get_commit())
+                           treeish=branch)
 
        def dump_log(self, path):
                print 'Generating change log for', self
 
+               url, branch = self.__get_url_branch()
+
                fd = os.open(path, os.O_WRONLY | os.O_CREAT, 0644)
                try:
-                       git.log(self.path, [self.get_commit()], fd=fd)
+                       git.log(self.path, [branch], fd=fd)
                finally:
                        os.close(fd)