| 1 | # -*- mode: python; coding: utf-8 -*- |
|---|
| 2 | # :Progetto: vcpx -- Mercurial stuff |
|---|
| 3 | # :Creato: ven 24 giu 2005 20:42:46 CEST |
|---|
| 4 | # :Autore: Lele Gaifax <lele@nautilus.homeip.net> |
|---|
| 5 | # :Licenza: GNU General Public License |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | """ |
|---|
| 9 | This module implements the backends for Mercurial. |
|---|
| 10 | """ |
|---|
| 11 | |
|---|
| 12 | __docformat__ = 'reStructuredText' |
|---|
| 13 | |
|---|
| 14 | from shwrap import ExternalCommand, ReopenableNamedTemporaryFile |
|---|
| 15 | from target import SyncronizableTargetWorkingDir, TargetInitializationFailure, \ |
|---|
| 16 | ChangesetReplayFailure |
|---|
| 17 | from source import ChangesetApplicationFailure |
|---|
| 18 | |
|---|
| 19 | class HgWorkingDir(SyncronizableTargetWorkingDir): |
|---|
| 20 | |
|---|
| 21 | ## SyncronizableTargetWorkingDir |
|---|
| 22 | |
|---|
| 23 | def _addPathnames(self, names): |
|---|
| 24 | """ |
|---|
| 25 | Add some new filesystem objects. |
|---|
| 26 | """ |
|---|
| 27 | |
|---|
| 28 | from os.path import join, isdir |
|---|
| 29 | |
|---|
| 30 | # Currently hg does not handle directories at all, so filter |
|---|
| 31 | # them out. |
|---|
| 32 | |
|---|
| 33 | notdirs = [n for n in names if not isdir(join(self.basedir, n))] |
|---|
| 34 | if notdirs: |
|---|
| 35 | cmd = self.repository.command("add") |
|---|
| 36 | ExternalCommand(cwd=self.basedir, command=cmd).execute(notdirs) |
|---|
| 37 | |
|---|
| 38 | def _commit(self, date, author, patchname, changelog=None, entries=None): |
|---|
| 39 | """ |
|---|
| 40 | Commit the changeset. |
|---|
| 41 | """ |
|---|
| 42 | |
|---|
| 43 | from time import mktime |
|---|
| 44 | from sys import getdefaultencoding |
|---|
| 45 | |
|---|
| 46 | encoding = ExternalCommand.FORCE_ENCODING or getdefaultencoding() |
|---|
| 47 | |
|---|
| 48 | logmessage = [] |
|---|
| 49 | if patchname: |
|---|
| 50 | logmessage.append(patchname.encode(encoding)) |
|---|
| 51 | if changelog: |
|---|
| 52 | logmessage.append(changelog.encode(encoding)) |
|---|
| 53 | |
|---|
| 54 | cmd = self.repository.command("commit", "-u", author, |
|---|
| 55 | "-l", "%(logfile)s", |
|---|
| 56 | "-d", "%(time)d 0") |
|---|
| 57 | c = ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 58 | |
|---|
| 59 | rontf = ReopenableNamedTemporaryFile('hg', 'tailor') |
|---|
| 60 | log = open(rontf.name, "w") |
|---|
| 61 | log.write('\n'.join(logmessage)) |
|---|
| 62 | log.close() |
|---|
| 63 | |
|---|
| 64 | c.execute(logfile=rontf.name, time=mktime(date.timetuple())) |
|---|
| 65 | |
|---|
| 66 | if c.exit_status: |
|---|
| 67 | raise ChangesetApplicationFailure("%s returned status %d" % |
|---|
| 68 | (str(c), c.exit_status)) |
|---|
| 69 | |
|---|
| 70 | def _removePathnames(self, names): |
|---|
| 71 | """ |
|---|
| 72 | Remove some filesystem object. |
|---|
| 73 | """ |
|---|
| 74 | |
|---|
| 75 | from os.path import join, isdir |
|---|
| 76 | |
|---|
| 77 | # Currently hg does not handle directories at all, so filter |
|---|
| 78 | # them out. |
|---|
| 79 | |
|---|
| 80 | notdirs = [n for n in names if not isdir(join(self.basedir, n))] |
|---|
| 81 | if notdirs: |
|---|
| 82 | cmd = self.repository.command("remove") |
|---|
| 83 | ExternalCommand(cwd=self.basedir, command=cmd).execute(notdirs) |
|---|
| 84 | |
|---|
| 85 | def _renamePathname(self, oldname, newname): |
|---|
| 86 | """ |
|---|
| 87 | Rename a filesystem object. |
|---|
| 88 | """ |
|---|
| 89 | |
|---|
| 90 | from os.path import join, isdir |
|---|
| 91 | from os import walk |
|---|
| 92 | from dualwd import IGNORED_METADIRS |
|---|
| 93 | |
|---|
| 94 | cmd = self.repository.command("rename", "--after") |
|---|
| 95 | copy = ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 96 | if isdir(join(self.basedir, newname)): |
|---|
| 97 | # Given lack of support for directories in current HG, |
|---|
| 98 | # loop over all files under the new directory and |
|---|
| 99 | # do a copy on them. |
|---|
| 100 | skip = len(self.basedir)+len(newname)+2 |
|---|
| 101 | for dir, subdirs, files in walk(join(self.basedir, newname)): |
|---|
| 102 | prefix = dir[skip:] |
|---|
| 103 | |
|---|
| 104 | for excd in IGNORED_METADIRS: |
|---|
| 105 | if excd in subdirs: |
|---|
| 106 | subdirs.remove(excd) |
|---|
| 107 | |
|---|
| 108 | for f in files: |
|---|
| 109 | copy.execute(join(oldname, prefix, f), |
|---|
| 110 | join(newname, prefix, f)) |
|---|
| 111 | if copy.exit_status: |
|---|
| 112 | raise ChangesetReplayFailure("Could not rename %r " |
|---|
| 113 | "into %r: maybe using a " |
|---|
| 114 | "pre 0.7 mercurial?") |
|---|
| 115 | else: |
|---|
| 116 | copy.execute(oldname, newname) |
|---|
| 117 | if copy.exit_status: |
|---|
| 118 | raise ChangesetReplayFailure("Could not rename %r " |
|---|
| 119 | "into %r: maybe using a " |
|---|
| 120 | "pre 0.7 mercurial?") |
|---|
| 121 | |
|---|
| 122 | def _prepareTargetRepository(self): |
|---|
| 123 | """ |
|---|
| 124 | Execute ``hg init``. |
|---|
| 125 | """ |
|---|
| 126 | |
|---|
| 127 | from os.path import join, exists |
|---|
| 128 | |
|---|
| 129 | if not exists(join(self.basedir, self.repository.METADIR)): |
|---|
| 130 | init = ExternalCommand(cwd=self.basedir, |
|---|
| 131 | command=self.repository.command("init")) |
|---|
| 132 | init.execute() |
|---|
| 133 | |
|---|
| 134 | if init.exit_status: |
|---|
| 135 | raise TargetInitializationFailure( |
|---|
| 136 | "%s returned status %s" % (str(init), init.exit_status)) |
|---|
| 137 | |
|---|
| 138 | def _prepareWorkingDirectory(self, source_repo): |
|---|
| 139 | """ |
|---|
| 140 | Create the .hgignore. |
|---|
| 141 | """ |
|---|
| 142 | |
|---|
| 143 | from os.path import join |
|---|
| 144 | from re import escape |
|---|
| 145 | from dualwd import IGNORED_METADIRS |
|---|
| 146 | |
|---|
| 147 | # Create the .hgignore file, that contains a regexp per line |
|---|
| 148 | # with all known VCs metadirs to be skipped. |
|---|
| 149 | ignore = open(join(self.basedir, '.hgignore'), 'w') |
|---|
| 150 | ignore.write('\n'.join(['(^|/)%s($|/)' % escape(md) |
|---|
| 151 | for md in IGNORED_METADIRS])) |
|---|
| 152 | ignore.write('\n') |
|---|
| 153 | if self.logfile.startswith(self.basedir): |
|---|
| 154 | ignore.write('^') |
|---|
| 155 | ignore.write(self.logfile[len(self.basedir)+1:]) |
|---|
| 156 | ignore.write('$\n') |
|---|
| 157 | if self.state_file.filename.startswith(self.basedir): |
|---|
| 158 | sfrelname = self.state_file.filename[len(self.basedir)+1:] |
|---|
| 159 | ignore.write('^') |
|---|
| 160 | ignore.write(sfrelname) |
|---|
| 161 | ignore.write('$\n') |
|---|
| 162 | ignore.write('^') |
|---|
| 163 | ignore.write(sfrelname+'.journal') |
|---|
| 164 | ignore.write('$\n') |
|---|
| 165 | ignore.close() |
|---|
| 166 | |
|---|
| 167 | def _initializeWorkingDir(self): |
|---|
| 168 | """ |
|---|
| 169 | Use ``hg addremove`` to import initial version of the tree. |
|---|
| 170 | """ |
|---|
| 171 | |
|---|
| 172 | ExternalCommand(cwd=self.basedir, |
|---|
| 173 | command=self.repository.command("addremove")).execute() |
|---|