| 1 | # -*- mode: python; coding: utf-8 -*- |
|---|
| 2 | # :Progetto: vcpx -- Git target (using cogito) |
|---|
| 3 | # :Creato: Wed 24 ago 2005 18:34:27 EDT |
|---|
| 4 | # :Autore: Todd Mokros <tmokros@tmokros.net> |
|---|
| 5 | # :Licenza: GNU General Public License |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | """ |
|---|
| 9 | This module implements the backend for Git by using Cogito. |
|---|
| 10 | """ |
|---|
| 11 | |
|---|
| 12 | __docformat__ = 'reStructuredText' |
|---|
| 13 | |
|---|
| 14 | from shwrap import ExternalCommand, ReopenableNamedTemporaryFile |
|---|
| 15 | from target import SyncronizableTargetWorkingDir, TargetInitializationFailure |
|---|
| 16 | from source import ChangesetApplicationFailure |
|---|
| 17 | |
|---|
| 18 | class CgWorkingDir(SyncronizableTargetWorkingDir): |
|---|
| 19 | |
|---|
| 20 | ## SyncronizableTargetWorkingDir |
|---|
| 21 | |
|---|
| 22 | def _addPathnames(self, names): |
|---|
| 23 | """ |
|---|
| 24 | Add some new filesystem objects. |
|---|
| 25 | """ |
|---|
| 26 | |
|---|
| 27 | from os.path import join, isdir |
|---|
| 28 | |
|---|
| 29 | # Currently git/cogito does not handle directories at all, so filter |
|---|
| 30 | # them out. |
|---|
| 31 | |
|---|
| 32 | notdirs = [n for n in names if not isdir(join(self.basedir, n))] |
|---|
| 33 | if notdirs: |
|---|
| 34 | cmd = [self.repository.CG_CMD, "add"] |
|---|
| 35 | ExternalCommand(cwd=self.basedir, command=cmd).execute(notdirs) |
|---|
| 36 | |
|---|
| 37 | def __parse_author(self, author): |
|---|
| 38 | """ |
|---|
| 39 | Parse the author field, returning (name, email) |
|---|
| 40 | """ |
|---|
| 41 | from email.Utils import parseaddr |
|---|
| 42 | from target import AUTHOR, HOST |
|---|
| 43 | |
|---|
| 44 | if author.find('@') > -1: |
|---|
| 45 | name, email = parseaddr(author) |
|---|
| 46 | else: |
|---|
| 47 | name, email = author, '' |
|---|
| 48 | name = name.strip() |
|---|
| 49 | email = email.strip() |
|---|
| 50 | if not name: |
|---|
| 51 | name = AUTHOR |
|---|
| 52 | if not email: |
|---|
| 53 | email = "%s@%s" % (AUTHOR, HOST) |
|---|
| 54 | return (name, email) |
|---|
| 55 | |
|---|
| 56 | def _commit(self, date, author, patchname, changelog=None, entries=None): |
|---|
| 57 | """ |
|---|
| 58 | Commit the changeset. |
|---|
| 59 | """ |
|---|
| 60 | |
|---|
| 61 | from time import mktime |
|---|
| 62 | from sys import getdefaultencoding |
|---|
| 63 | from os import environ |
|---|
| 64 | |
|---|
| 65 | encoding = ExternalCommand.FORCE_ENCODING or getdefaultencoding() |
|---|
| 66 | |
|---|
| 67 | logmessage = [] |
|---|
| 68 | if patchname: |
|---|
| 69 | logmessage.append(patchname.encode(encoding)) |
|---|
| 70 | if changelog: |
|---|
| 71 | logmessage.append(changelog.encode(encoding)) |
|---|
| 72 | |
|---|
| 73 | env = {} |
|---|
| 74 | env.update(environ) |
|---|
| 75 | |
|---|
| 76 | (name, email) = self.__parse_author(author) |
|---|
| 77 | if name: |
|---|
| 78 | env['GIT_AUTHOR_NAME']=name |
|---|
| 79 | if email: |
|---|
| 80 | env['GIT_AUTHOR_EMAIL']=email |
|---|
| 81 | if date: |
|---|
| 82 | env['GIT_AUTHOR_DATE']=str(date) |
|---|
| 83 | # '-f' flag means we can get empty commits, which |
|---|
| 84 | # shouldn't be a problem. |
|---|
| 85 | cmd = [self.repository.CG_CMD, "commit", "-f"] |
|---|
| 86 | c = ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 87 | |
|---|
| 88 | c.execute(env=env, input='\n'.join(logmessage)) |
|---|
| 89 | if c.exit_status: |
|---|
| 90 | raise ChangesetApplicationFailure("%s returned status %d" % |
|---|
| 91 | (str(c), c.exit_status)) |
|---|
| 92 | |
|---|
| 93 | def _removePathnames(self, names): |
|---|
| 94 | """ |
|---|
| 95 | Remove some filesystem object. |
|---|
| 96 | """ |
|---|
| 97 | |
|---|
| 98 | from os.path import join, isdir |
|---|
| 99 | # Currently git does not handle directories at all, so filter |
|---|
| 100 | # them out. |
|---|
| 101 | |
|---|
| 102 | notdirs = [n for n in names if not isdir(join(self.basedir, n))] |
|---|
| 103 | if notdirs: |
|---|
| 104 | cmd = [self.repository.CG_CMD, "rm"] |
|---|
| 105 | c=ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 106 | c.execute(notdirs) |
|---|
| 107 | |
|---|
| 108 | def _renamePathname(self, oldname, newname): |
|---|
| 109 | """ |
|---|
| 110 | Rename a filesystem object. |
|---|
| 111 | """ |
|---|
| 112 | # In the future, we may want to switch to using |
|---|
| 113 | # git rename, in case renames ever get more support |
|---|
| 114 | # in git. It currently just does and add and remove. |
|---|
| 115 | from os.path import join, isdir |
|---|
| 116 | from os import walk |
|---|
| 117 | from dualwd import IGNORED_METADIRS |
|---|
| 118 | |
|---|
| 119 | if isdir(join(self.basedir, newname)): |
|---|
| 120 | # Given lack of support for directories in current Git, |
|---|
| 121 | # loop over all files under the new directory and |
|---|
| 122 | # do a add/remove on them. |
|---|
| 123 | skip = len(self.basedir)+len(newname)+2 |
|---|
| 124 | for dir, subdirs, files in walk(join(self.basedir, newname)): |
|---|
| 125 | prefix = dir[skip:] |
|---|
| 126 | |
|---|
| 127 | for excd in IGNORED_METADIRS: |
|---|
| 128 | if excd in subdirs: |
|---|
| 129 | subdirs.remove(excd) |
|---|
| 130 | |
|---|
| 131 | for f in files: |
|---|
| 132 | self._removePathnames([join(oldname, prefix, f)]) |
|---|
| 133 | self._addPathnames([join(newname, prefix, f)]) |
|---|
| 134 | else: |
|---|
| 135 | self._removePathnames([oldname]) |
|---|
| 136 | self._addPathnames([newname]) |
|---|
| 137 | |
|---|
| 138 | def _prepareTargetRepository(self): |
|---|
| 139 | """ |
|---|
| 140 | Execute ``cg init``. |
|---|
| 141 | """ |
|---|
| 142 | |
|---|
| 143 | from os import makedirs |
|---|
| 144 | from os.path import join, exists |
|---|
| 145 | |
|---|
| 146 | if not exists(self.basedir): |
|---|
| 147 | makedirs(self.basedir) |
|---|
| 148 | |
|---|
| 149 | if not exists(join(self.basedir, self.repository.METADIR)): |
|---|
| 150 | init = ExternalCommand(cwd=self.basedir, |
|---|
| 151 | command=[self.repository.CG_CMD, |
|---|
| 152 | "init", "-I"]) |
|---|
| 153 | init.execute() |
|---|
| 154 | |
|---|
| 155 | if init.exit_status: |
|---|
| 156 | raise TargetInitializationFailure( |
|---|
| 157 | "%s returned status %s" % (str(init), init.exit_status)) |
|---|
| 158 | |
|---|
| 159 | def _prepareWorkingDirectory(self, source_repo): |
|---|
| 160 | """ |
|---|
| 161 | Create the .gitignore. |
|---|
| 162 | """ |
|---|
| 163 | |
|---|
| 164 | from os.path import join |
|---|
| 165 | from re import escape |
|---|
| 166 | from dualwd import IGNORED_METADIRS |
|---|
| 167 | |
|---|
| 168 | # Create the .gitignore file, that contains an fnmatch per line |
|---|
| 169 | # with all known VCs metadirs to be skipped. |
|---|
| 170 | # .gitignore is per directory, and does not recurse. |
|---|
| 171 | # We could change to cogito specific .git/ignore or |
|---|
| 172 | # once/if upstream cogito is updated to use .git/info/exclude, |
|---|
| 173 | # use it instead of .gitignore |
|---|
| 174 | ignore = open(join(self.basedir, '.gitignore'), 'w') |
|---|
| 175 | ignore.write('\n'.join(['%s' % md |
|---|
| 176 | for md in IGNORED_METADIRS])) |
|---|
| 177 | ignore.write('\n') |
|---|
| 178 | if self.logfile.startswith(self.basedir): |
|---|
| 179 | ignore.write(self.logfile[len(self.basedir)+1:]) |
|---|
| 180 | ignore.write('\n') |
|---|
| 181 | if self.state_file.filename.startswith(self.basedir): |
|---|
| 182 | sfrelname = self.state_file.filename[len(self.basedir)+1:] |
|---|
| 183 | ignore.write(sfrelname) |
|---|
| 184 | ignore.write('\n') |
|---|
| 185 | ignore.write(sfrelname+'.journal') |
|---|
| 186 | ignore.write('\n') |
|---|
| 187 | ignore.close() |
|---|
| 188 | |
|---|