Changeset 1195 in tailor for vcpx/repository/git.py


Ignore:
Timestamp:
06/29/06 01:25:02 (7 years ago)
Author:
ydirson@…
Hash name:
20060628232502-130f5-9fddd5dda8c17309c7837d5307fbbe24b112c9de
Message:

[git] add support for "repository" parameter

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vcpx/repository/git.py

    r1194 r1195  
    44# :Autore:   Todd Mokros <tmokros@tmokros.net> 
    55#            Brendan Cully <brendan@kublai.com> 
     6#            Yann Dirson <ydirson@altern.org> 
    67# :Licenza:  GNU General Public License 
    78# 
     
    1516from vcpx.repository import Repository 
    1617from vcpx.shwrap import ExternalCommand, PIPE 
     18from vcpx.config import ConfigurationError 
    1719from vcpx.source import UpdatableSourceWorkingDir, GetUpstreamChangesetsFailure 
    1820from vcpx.source import ChangesetApplicationFailure 
     
    3032        self.PARENT_REPO = project.config.get(self.name, 'parent-repo') 
    3133        self.BRANCHPOINT = project.config.get(self.name, 'branchpoint', 'HEAD') 
    32  
     34        self.BRANCHNAME = project.config.get(self.name, 'branch') 
     35        if self.BRANCHNAME: 
     36            self.BRANCHNAME = 'refs/heads/' + self.BRANCHNAME 
     37 
     38        if self.repository and self.PARENT_REPO: 
     39            self.log.critical('Cannot make sense of both "repository" and "parent-repo" parameters') 
     40            raise ConfigurationError ('Must specify only one of "repository" and "parent-repo"') 
     41 
     42        if self.BRANCHNAME and not self.repository: 
     43            self.log.critical('Cannot make sense of "branch" if "repository" is not set') 
     44            raise ConfigurationError ('Missing "repository" to make use o "branch"') 
     45 
     46        self.env = {} 
     47 
     48        if self.repository: 
     49            self.storagedir = self.repository 
     50            self.env['GIT_DIR'] = self.storagedir 
     51            self.env['GIT_INDEX_FILE'] = self.METADIR + '/index' 
     52        else: 
     53            self.storagedir = self.METADIR 
     54 
     55class GitExternalCommand(ExternalCommand): 
     56    def __init__(self, repo, command=None, cwd=None): 
     57        """ 
     58        Initialize an ExternalCommand instance tied to a GitRepository 
     59        from which it inherits a set of environment variables to use 
     60        fo each execute(). 
     61        """ 
     62 
     63        self.repo = repo 
     64        return ExternalCommand.__init__(self, command, cwd) 
     65 
     66    def execute(self, *args, **kwargs): 
     67        """Execute the command, with controlled environment.""" 
     68 
     69        if not kwargs.has_key('env'): 
     70            kwargs['env'] = {} 
     71 
     72        kwargs['env'].update(self.repo.env) 
     73 
     74        return ExternalCommand.execute(self, *args, **kwargs) 
    3375 
    3476class GitWorkingDir(UpdatableSourceWorkingDir, SynchronizableTargetWorkingDir): 
    3577 
    3678    def _tryCommand(self, cmd, exception=Exception, pipe=True): 
    37         c = ExternalCommand(command = self.repository.command(*cmd), cwd = self.basedir) 
     79        c = GitExternalCommand(self.repository, 
     80                               command = self.repository.command(*cmd), cwd = self.basedir) 
    3881        if pipe: 
    3982            output = c.execute(stdout=PIPE)[0] 
     
    241284        treeid = self._tryCommand(['write-tree'])[0] 
    242285 
    243         # find the parent commit if any 
    244         c = ExternalCommand(cwd=self.basedir, 
    245                          command=self.repository.command('rev-parse', 'HEAD')) 
     286        # in single-repository mode, only update the relevant branch 
     287        if self.repository.BRANCHNAME: 
     288            refname = self.repository.BRANCHNAME 
     289        else: 
     290            refname = 'HEAD' 
     291 
     292        # find the previous commit on the branch if any 
     293        c = GitExternalCommand(self.repository, cwd=self.basedir, 
     294                               command=self.repository.command('rev-parse', refname)) 
    246295        (out, err) = c.execute(stdout=PIPE, stderr=PIPE) 
    247296        if c.exit_status: 
     
    267316        else: 
    268317            cmd = self.repository.command('commit-tree', treeid) 
    269         c = ExternalCommand(cwd=self.basedir, command=cmd) 
     318        c = GitExternalCommand(self.repository, cwd=self.basedir, command=cmd) 
    270319 
    271320        logmessage = encode('\n'.join(logmessage)) 
     
    286335        else: 
    287336            commitid=out.read().split('\n')[0] 
     337 
    288338            if parent: 
    289                 self._tryCommand(['update-ref', 'HEAD', commitid, parent]) 
     339                self._tryCommand(['update-ref', refname, commitid, parent]) 
    290340            else: 
    291                 self._tryCommand(['update-ref', 'HEAD', commitid]) 
     341                self._tryCommand(['update-ref', refname, commitid]) 
    292342 
    293343    def _tag(self, tag): 
    294344        # Allow a new tag to overwrite an older one with -f 
    295345        cmd = self.repository.command("tag", "-f", tag) 
    296         c = ExternalCommand(cwd=self.basedir, command=cmd) 
     346        c = GitExternalCommand(self.repository, cwd=self.basedir, command=cmd) 
    297347        c.execute() 
    298348 
     
    350400        """ 
    351401 
    352         from os import renames 
     402        from os import renames, mkdir 
    353403        from os.path import join, exists 
    354404 
    355405        if not exists(join(self.basedir, self.repository.METADIR)): 
    356             if not self.repository.PARENT_REPO: 
    357                 cmd = self.repository.command("init-db") 
    358                 init = ExternalCommand(cwd=self.basedir, command=cmd) 
    359                 init.execute() 
    360                 if init.exit_status: 
    361                     raise TargetInitializationFailure( 
    362                         "%s returned status %s" % (str(init), init.exit_status)) 
    363             else: 
     406            if self.repository.PARENT_REPO: 
    364407                cmd = self.repository.command("clone", "--shared", "-n", 
    365408                                              self.repository.PARENT_REPO, 'tmp') 
    366                 clone = ExternalCommand(cwd=self.basedir, command=cmd) 
     409                clone = GitExternalCommand(self.repository, cwd=self.basedir, command=cmd) 
    367410                clone.execute() 
    368411                if clone.exit_status: 
     
    374417                 
    375418                cmd = self.repository.command("reset", "--soft", self.repository.BRANCHPOINT) 
    376                 reset = ExternalCommand(cwd=self.basedir, command=cmd) 
     419                reset = GitExternalCommand(self.repository, cwd=self.basedir, command=cmd) 
    377420                reset.execute() 
    378421                if reset.exit_status: 
     
    380423                        "%s returned status %s" % (str(reset), reset.exit_status)) 
    381424 
     425            elif self.repository.repository and self.repository.BRANCHNAME: 
     426                # ...and exists(self.repository.storagedir) ? 
     427 
     428                # initialization of a new branch in single-repository mode 
     429                mkdir(join(self.basedir, self.repository.METADIR)) 
     430 
     431                bp = self._tryCommand(['rev-parse', self.repository.BRANCHPOINT])[0] 
     432                self._tryCommand(['read-tree', bp]) 
     433                self._tryCommand(['update-ref', self.repository.BRANCHNAME, bp]) 
     434                #self._tryCommand(['checkout-index']) 
     435 
     436            else: 
     437                self._tryCommand(['init-db']) 
     438                if self.repository.repository: 
     439                    # in this mode, the db is not stored in working dir, so we 
     440                    # have to create .git ourselves 
     441                    mkdir(join(self.basedir, self.repository.METADIR)) 
    382442 
    383443    def _prepareWorkingDirectory(self, source_repo): 
     
    390450        from vcpx.dualwd import IGNORED_METADIRS 
    391451 
    392         infodir = join(self.basedir, self.repository.METADIR, 'info') 
     452        # create info/excludes in storagedir 
     453        infodir = join(self.basedir, self.repository.storagedir, 'info') 
    393454        if not exists(infodir): 
    394455            mkdir(infodir) 
Note: See TracChangeset for help on using the changeset viewer.