Backup.py revision 605a716e6d38b3af09034c254382d0ae3b7d5f70
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# This program is free software; you can redistribute it and/or modify
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# it under the terms of the GNU General Public License version 2
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# as published by the Free Software Foundation.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# This program is distributed in the hope that it will be useful,
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# but WITHOUT ANY WARRANTY; without even the implied warranty of
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# GNU General Public License for more details.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# You should have received a copy of the GNU General Public License
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# along with this program; if not, write to the Free Software
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# Use is subject to license terms.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe# Copyright 2008, 2011, Richard Lowe
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonWorkspace backup
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonBackup format is:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson generation#/
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson dirstate (handled by CdmUncommittedBackup)
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe File containing dirstate nodeid (the changeset we need
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe to update the workspace to after applying the bundle).
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe This is the node to which the working copy changes
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe (see 'diff', below) will be applied if applicable.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson bundle (handled by CdmCommittedBackup)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson An Hg bundle containing outgoing committed changes.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson nodes (handled by CdmCommittedBackup)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson A text file listing the full (hex) nodeid of all nodes in
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson bundle, used by need_backup.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson diff (handled by CdmUncommittedBackup)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson A Git-formatted diff containing uncommitted changes.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson renames (handled by CdmUncommittedBackup)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson A list of renames in the working copy that have to be
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson applied manually, rather than by the diff.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson metadata.tar.gz (handled by CdmMetadataBackup)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson $CODEMGR_WS/.hg/hgrc
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson $CODEMGR_WS/.hg/localtags
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson $CODEMGR_WS/.hg/patches (Mq data)
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe clear.tar.gz (handled by CdmClearBackup)
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe <short node>/
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe copies of each modified or added file, as it is in
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe ... for each outgoing head
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe copies of each modified or added file in the
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe working copy if any.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson latest -> generation#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson Newest backup generation.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonAll files in a given backup generation, with the exception of
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsondirstate, are optional.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Loweimport grp, os, pwd, shutil, tarfile, time, traceback
8703921742d9c7d4d3724f89a39ff0e2725cbe7bRichard Lowefrom mercurial import changegroup, error, node, patch, util
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe '''a required node is not present in the destination workspace.
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe This may occur both in the case where the bundle contains a
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe changeset which is a child of a node not present in the
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe destination workspace (because the destination workspace is not as
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe up-to-date as the source), or because the source and destination
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe workspace are not related.
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe It may also happen in cases where the uncommitted changes need to
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe be applied onto a node that the workspace does not possess even
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe after application of the bundle (on a branch not present
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe in the bundle or destination workspace, for instance)'''
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe # If e.name is a string 20 characters long, it is
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe # assumed to be a node. (Mercurial makes this
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe # same assumption, when creating a LookupError)
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe util.Abort.__init__(self, "%s: changeset '%s' is missing\n"
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe "Your workspace is either not "
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe "sufficiently up to date,\n"
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe "or is unrelated to the workspace from "
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Tar file access + simple comparison to the filesystem, and
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe creation addition of files from Mercurial filectx objects.'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe tarfile.TarFile.__init__(self, *args, **kwargs)
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Compare the contents of the tar archive to the directory
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe specified by rootpath. Return False if they differ.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe Every file in the archive must match the equivalent file in
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe the filesystem.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe The existence, modification time, and size of each file are
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe compared, content is not.'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Compare a single member to its filesystem counterpart'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe elif ((os.path.isfile(fpath) != member.isfile()) or
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # The filesystem may return a modification time with a
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # fractional component (as a float), whereas the tar format
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # only stores it to the whole second, perform the comparison
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # using integers (truncated, not rounded)
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe elif member.mtime != int(os.path.getmtime(fpath)):
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe elif not member.isdir() and member.size != os.path.getsize(fpath):
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Add a filectx object to the archive.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe Use the path specified by the filectx object or, if specified,
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe the PATH argument.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe The size, modification time, type and permissions of the tar
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe member are taken from the filectx object, user and group id
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe are those of the invoking user, user and group name are those
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe of the invoking user if information is available, or "unknown"
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe if it is not.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # Mercurial versions symlinks by setting a flag and storing
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # the destination path in place of the file content. The
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # actual contents (in the tar), should be empty.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe t.mode = 'x' in filectx.flags() and 0755 or 0644
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Backup of committed changes'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Return a list of all outgoing nodes in hex format'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson nodes = self.ws.repo.changelog.nodesbetween(outgoing)[0]
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Backup committed changes'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson self.ws.ui.warn('Workspace has no parent, committed changes will '
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson 'not be backed up\n')
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson changegroup.writebundle(cg, self.bu.backupfile('bundle'), 'HG10BZ')
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe raise util.Abort("couldn't store outgoing nodes: %s" % e)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Restore committed changes from backup'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe raise util.Abort("couldn't restore committed changes: %s\n"
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe raise CdmNodeMissing("couldn't restore committed changes",
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe if f and not f.closed:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Compare backup of committed changes to workspace'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe bnodes = set(line.rstrip('\r\n') for line in f.readlines())
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't open backup node list: %s" % e)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if f and not f.closed:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson outnodes = set(self._outgoing_nodes(self.ws.parent()))
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # If there are outgoing nodes not in the prior backup we need
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # to take a new backup; it's fine if there are nodes in the
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # old backup which are no longer outgoing, however.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Remove backed up committed changes'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Backup of uncommitted changes'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Return a list of pairs of files representing renames/copies
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe that clobber already versioned files. [(old-name new-name)...]
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # Note that this doesn't handle uncommitted merges
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # as CdmUncommittedBackup itself doesn't.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe for fname in self.wctx.added() + self.wctx.modified():
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Backup uncommitted changes'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("Unable to backup an uncommitted merge.\n"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson "Please complete your merge and commit")
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe dirstate = node.hex(self.wctx.parents()[0].node())
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't save working copy parent: %s" % e)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't save clobbering copies: %s" % e)
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe match = self.ws.matcher(files=self.wctx.files())
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe fp.write(self.ws.diff(opts={'git': True}, match=match))
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't save working copy diff: %s" % e)
2b5878de2735cb61d008168e1f27e390d2edf915Rich Lowe '''Return the desired working copy node from the backup'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't read saved parent: %s" % e)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Restore uncommitted changes'''
2b5878de2735cb61d008168e1f27e390d2edf915Rich Lowe # Check that the patch's parent changeset exists.
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe raise CdmNodeMissing("couldn't restore uncommitted changes",
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't update to saved node: %s" % e)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # There's a race here whereby if the patch (or part thereof)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # is applied within the same second as the clean above (such
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # that modification time doesn't change) and if the size of
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # that file does not change, Hg may not see the change.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # We sleep a full second to avoid this, as sleeping merely
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # until the next second begins would require very close clock
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # synchronization on network filesystems.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort('working copy diff applied with fuzz')
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't apply working copy diff: %s\n"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # We need to re-apply name changes where the new name
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # (rename/copy destination) is an already versioned file, as
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # Hg would otherwise ignore them.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort('unable to open renames file: %s' % e)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Compare backup of uncommitted changes to workspace'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe match = self.ws.matcher(files=self.wctx.files())
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe curdiff = self.ws.diff(opts={'git': True}, match=match)
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe bakrenamed = [tuple(line.strip().split(' ')) for line in fd]
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't open renames file %s: %s\n" %
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Remove backed up uncommitted changes'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Backup of workspace metadata'''
9a70fc3be3b1e966bf78825cdb8d509963a6f0a1Mark J. Nelson self.files = ('hgrc', 'localtags', 'patches', 'cdm')
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Backup workspace metadata'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe tarpath = self.bu.backupfile('metadata.tar.gz')
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # Files is a list of tuples (name, path), where name is as in
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # self.files, and path is the absolute path.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe files = filter(lambda (name, path): os.path.exists(path),
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe zip(self.files, map(self.ws.repo.join, self.files)))
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe except (EnvironmentError, tarfile.TarError), e:
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe raise util.Abort("couldn't open %s for writing: %s" %
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe except (EnvironmentError, tarfile.TarError), e:
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # tarfile.TarError doesn't include the tar member or file
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe # in question, so we have to do so ourselves.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe raise util.Abort("couldn't backup metadata to %s:\n"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Restore workspace metadata from an pre-tar backup'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't restore metadata from %s:\n"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Restore workspace metadata (from a tar-style backup)'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe tarpath = self.bu.backupfile('metadata.tar.gz')
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe except (EnvironmentError, tarfile.TarError), e:
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe raise util.Abort("couldn't open %s: %s" % (tarpath, e))
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # Make sure the member name is in the exception message.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't restore metadata from %s:\n"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Restore workspace metadata'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Yield the repo-relative path to each file we operate on,
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe including each file within any affected directory'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe for root, dirs, files in os.walk(path, topdown=True):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Compare backed up workspace metadata to workspace'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Remove a possible trailing path separator from PATHNAME'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe return pathname.endswith('/') and pathname[:-1] or pathname
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe tarpath = self.bu.backupfile('metadata.tar.gz')
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("couldn't open metadata tarball: %s\n"
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe if not tar.members_match_fs(self.ws.repo.path):
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe tarnames = map(strip_trailing_pathsep, tar.getnames())
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe if path.replace(repopath, '', 1) not in tarnames:
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Remove backed up workspace metadata'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''A backup (in tar format) of complete source files from every
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe workspace head.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe Paths in the tarball are prefixed by the revision and node of the
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe head, or "working" for the working directory.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe This is done purely for the benefit of the user, and as such takes
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe no part in restore or need_backup checking, restore always
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe succeeds, need_backup always returns False
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Return a list of tuples (parenttip, localtip) for each
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe outgoing head. If the working copy contains modified files,
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe it is a head, and neither of its parents are.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe outnodes = set(self.ws.repo.changelog.nodesbetween(outgoing)[0])
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe heads = [self.ws.repo.changectx(n) for n in self.ws.repo.heads()
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe if wctx.files(): # We only care about file changes.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe heads = filter(lambda x: x not in wctx.parents(), heads) + [wctx]
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe pairs.append((self.ws.parenttip(c, outnodes), head))
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Save a clear copy of each source file modified between each
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe head and that head's parenttip (see WorkSpace.parenttip).
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe except (EnvironmentError, tarfile.TarError), e:
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe raise util.Abort("Could not open %s for writing: %s" %
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe tpath = child.node() and node.short(child.node()) or "working"
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe for fname, change in self.ws.status(parent, child).iteritems():
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe raise util.Abort("Could not backup clear file %s "
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Cleanup a failed Clear backup.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe Remove the clear tarball from the backup directory.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Clear backups are never restored, do nothing'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Clear backups are never compared, return False (no backup needed).
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe Should a backup actually be needed, one of the other
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe implementation classes would notice in any situation we would.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''A backup of a given workspace'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # The order of instances here controls the order the various operations
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # There's some inherent dependence, in that on restore we need
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # to restore committed changes prior to uncommitted changes
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # (as the parent revision of any uncommitted changes is quite
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # likely to not exist until committed changes are restored).
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # Metadata restore can happen at any point, but happens last
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # as a matter of convention.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson self.modules = [x(self, ws) for x in [CdmCommittedBackup,
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if os.path.exists(os.path.join(self.backupdir, 'latest')):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson generation = os.readlink(os.path.join(self.backupdir, 'latest'))
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson self.generation = int(os.path.split(generation)[1])
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Find the path to an appropriate backup directory based on NAME'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson backupbase = os.path.expanduser(self.ui.config('cdm', 'backupdir'))
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson home = os.getenv('HOME') or pwd.getpwuid(os.getuid()).pw_dir
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson pass # Handled anyway
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort('Could not determine your HOME directory to '
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson 'find backup path')
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # If backupdir exists, it must be a directory.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if (os.path.exists(backupdir) and not os.path.isdir(backupdir)):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort('%s exists but is not a directory' % backupdir)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Update latest symlink to point to the current generation'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Create a new backup generation'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson os.makedirs(os.path.join(self.backupdir, str(gen)))
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort("Couldn't create backup generation %s: %s" %
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''return full path to backup file FILE at GEN'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe return os.path.join(self.backupdir, str(self.generation), path)
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Unlink the specified path from the backup directory.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe A no-op if the path does not exist.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Open the specified file in the backup directory'''
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe '''Return boolean indicating wether a given file exists in the
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe backup directory.'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Compare backed up changes to workspace'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # If there's no current backup generation, or the last backup was
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # invalid (lacking the dirstate file), we need a backup regardless
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # of anything else.
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe if not self.generation or not self.exists('dirstate'):
c959a081a8aebb76386b6d8ea3afa850e328f6c7Richard Lowe '''Take a backup of the current workspace
c959a081a8aebb76386b6d8ea3afa850e328f6c7Richard Lowe Calling code is expected to hold both the working copy lock
c959a081a8aebb76386b6d8ea3afa850e328f6c7Richard Lowe and repository lock.'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort('Could not create backup directory %s: %s' %
8703921742d9c7d4d3724f89a39ff0e2725cbe7bRichard Lowe show_traceback = self.ws.ui.configbool('ui', 'traceback',
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # If it's not a 'normal' error, we want to print a stack
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # trace now in case the attempt to remove the partial
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # backup also fails, and raises a second exception.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if (not isinstance(e, (EnvironmentError, util.Abort))
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson os.rmdir(os.path.join(self.backupdir, str(self.generation)))
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '''Restore workspace from backup
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson Restores from backup generation GEN (defaulting to the latest)
c959a081a8aebb76386b6d8ea3afa850e328f6c7Richard Lowe into workspace WS.
c959a081a8aebb76386b6d8ea3afa850e328f6c7Richard Lowe Calling code is expected to hold both the working copy lock
c959a081a8aebb76386b6d8ea3afa850e328f6c7Richard Lowe and repository lock of the destination workspace.'''
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort('Backup directory does not exist: %s' %
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if not os.path.exists(os.path.join(self.backupdir, str(gen))):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort('Backup generation does not exist: %s' %
605a716e6d38b3af09034c254382d0ae3b7d5f70Richard Lowe if not self.generation: # This is OK, 0 is not a valid generation
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort('Backup has no generations: %s' % self.backupdir)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson raise util.Abort('Backup %s/%s is incomplete (dirstate missing)' %
12203c7149a0ba35c436436cd2400dc7aaba1e62Richard Lowe 'Workspace may be partially restored' % e)