| 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 | from source import ChangesetApplicationFailure |
|---|
| 17 | |
|---|
| 18 | class HgWorkingDir(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 hg 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.command("add") |
|---|
| 35 | ExternalCommand(cwd=self.basedir, command=cmd).execute(notdirs) |
|---|
| 36 | |
|---|
| 37 | def _commit(self, date, author, patchname, changelog=None, entries=None): |
|---|
| 38 | """ |
|---|
| 39 | Commit the changeset. |
|---|
| 40 | """ |
|---|
| 41 | |
|---|
| 42 | from time import mktime |
|---|
| 43 | from sys import getdefaultencoding |
|---|
| 44 | |
|---|
| 45 | encoding = ExternalCommand.FORCE_ENCODING or getdefaultencoding() |
|---|
| 46 | |
|---|
| 47 | logmessage = [] |
|---|
| 48 | if patchname: |
|---|
| 49 | logmessage.append(patchname.encode(encoding)) |
|---|
| 50 | if changelog: |
|---|
| 51 | logmessage.append(changelog.encode(encoding)) |
|---|
| 52 | |
|---|
| 53 | cmd = self.repository.command("commit", "-u", author, |
|---|
| 54 | "-l", "%(logfile)s", |
|---|
| 55 | "-d", "%(time)d 0") |
|---|
| 56 | c = ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 57 | |
|---|
| 58 | rontf = ReopenableNamedTemporaryFile('hg', 'tailor') |
|---|
| 59 | log = open(rontf.name, "w") |
|---|
| 60 | log.write('\n'.join(logmessage)) |
|---|
| 61 | log.close() |
|---|
| 62 | |
|---|
| 63 | c.execute(logfile=rontf.name, time=mktime(date.timetuple())) |
|---|
| 64 | |
|---|
| 65 | if c.exit_status: |
|---|
| 66 | raise ChangesetApplicationFailure("%s returned status %d" % |
|---|
| 67 | (str(c), c.exit_status)) |
|---|
| 68 | |
|---|
| 69 | def _removePathnames(self, names): |
|---|
| 70 | """ |
|---|
| 71 | Remove some filesystem object. |
|---|
| 72 | """ |
|---|
| 73 | |
|---|
| 74 | from os.path import join, isdir |
|---|
| 75 | |
|---|
| 76 | # Currently hg does not handle directories at all, so filter |
|---|
| 77 | # them out. |
|---|
| 78 | |
|---|
| 79 | notdirs = [n for n in names if not isdir(join(self.basedir, n))] |
|---|
| 80 | if notdirs: |
|---|
| 81 | cmd = self.repository.command("remove") |
|---|
| 82 | ExternalCommand(cwd=self.basedir, command=cmd).execute(notdirs) |
|---|
| 83 | |
|---|
| 84 | def _renamePathname(self, oldname, newname): |
|---|
| 85 | """ |
|---|
| 86 | Rename a filesystem object. |
|---|
| 87 | """ |
|---|
| 88 | |
|---|
| 89 | from os.path import join, isdir |
|---|
| 90 | from os import walk |
|---|
| 91 | from dualwd import IGNORED_METADIRS |
|---|
| 92 | |
|---|
| 93 | cmd = self.repository.command("rename", "--after") |
|---|
| 94 | copy = ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 95 | if isdir(join(self.basedir, newname)): |
|---|
| 96 | # Given lack of support for directories in current HG, |
|---|
| 97 | # loop over all files under the new directory and |
|---|
| 98 | # do a copy on them. |
|---|
| 99 | skip = len(self.basedir)+len(newname)+2 |
|---|
| 100 | for dir, subdirs, files in walk(join(self.basedir, newname)): |
|---|
| 101 | prefix = dir[skip:] |
|---|
| 102 | |
|---|
| 103 | for excd in IGNORED_METADIRS: |
|---|
| 104 | if excd in subdirs: |
|---|
| 105 | subdirs.remove(excd) |
|---|
| 106 | |
|---|
| 107 | for f in files: |
|---|
| 108 | copy.execute(join(oldname, prefix, f), |
|---|
| 109 | join(newname, prefix, f)) |
|---|
| 110 | else: |
|---|
| 111 | copy.execute(oldname, newname) |
|---|
| 112 | |
|---|
| 113 | def _prepareTargetRepository(self): |
|---|
| 114 | """ |
|---|
| 115 | Execute ``hg init``. |
|---|
| 116 | """ |
|---|
| 117 | |
|---|
| 118 | from os.path import join, exists |
|---|
| 119 | |
|---|
| 120 | if not exists(join(self.basedir, self.repository.METADIR)): |
|---|
| 121 | init = ExternalCommand(cwd=self.basedir, |
|---|
| 122 | command=self.repository.command("init")) |
|---|
| 123 | init.execute() |
|---|
| 124 | |
|---|
| 125 | if init.exit_status: |
|---|
| 126 | raise TargetInitializationFailure( |
|---|
| 127 | "%s returned status %s" % (str(init), init.exit_status)) |
|---|
| 128 | |
|---|
| 129 | def _prepareWorkingDirectory(self, source_repo): |
|---|
| 130 | """ |
|---|
| 131 | Create the .hgignore. |
|---|
| 132 | """ |
|---|
| 133 | |
|---|
| 134 | from os.path import join |
|---|
| 135 | from re import escape |
|---|
| 136 | from dualwd import IGNORED_METADIRS |
|---|
| 137 | |
|---|
| 138 | # Create the .hgignore file, that contains a regexp per line |
|---|
| 139 | # with all known VCs metadirs to be skipped. |
|---|
| 140 | ignore = open(join(self.basedir, '.hgignore'), 'w') |
|---|
| 141 | ignore.write('\n'.join(['(^|/)%s($|/)' % escape(md) |
|---|
| 142 | for md in IGNORED_METADIRS])) |
|---|
| 143 | ignore.write('\n') |
|---|
| 144 | if self.logfile.startswith(self.basedir): |
|---|
| 145 | ignore.write('^') |
|---|
| 146 | ignore.write(self.logfile[len(self.basedir)+1:]) |
|---|
| 147 | ignore.write('$\n') |
|---|
| 148 | if self.state_file.filename.startswith(self.basedir): |
|---|
| 149 | sfrelname = self.state_file.filename[len(self.basedir)+1:] |
|---|
| 150 | ignore.write('^') |
|---|
| 151 | ignore.write(sfrelname) |
|---|
| 152 | ignore.write('$\n') |
|---|
| 153 | ignore.write('^') |
|---|
| 154 | ignore.write(sfrelname+'.journal') |
|---|
| 155 | ignore.write('$\n') |
|---|
| 156 | ignore.close() |
|---|
| 157 | |
|---|
| 158 | def _initializeWorkingDir(self): |
|---|
| 159 | """ |
|---|
| 160 | Use ``hg addremove`` to import initial version of the tree. |
|---|
| 161 | """ |
|---|
| 162 | |
|---|
| 163 | ExternalCommand(cwd=self.basedir, |
|---|
| 164 | command=self.repository.command("addremove")).execute() |
|---|