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