WorkSpace.py revision 2b5878de2735cb61d008168e1f27e390d2edf915
#
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
#
# Theory:
#
# All important operations apply to the changes between the two.
#
# However, for the sake of remote operation, the 'parent' of a
# workspace is not seen as a literal entity, instead the figurative
# parent contains the last changeset common to both parent and child,
# as such the 'parent tip' is actually nothing of the sort, but instead a
# convenient imitation.
#
# Any change made to a workspace is a change to a file therein, such
# changes can be represented briefly as whether the file was
# the file has a different name in the parent and if so, whether it
# was renamed or merely copied. Each changed file has an
# associated ActiveEntry.
#
# The ActiveList being a list ActiveEntrys can thus present the entire
# change in workspace state between a parent and its child, and is the
# important bit here (in that if it is incorrect, everything else will
# be as incorrect, or more)
#
import cStringIO
import os
class ActiveEntry(object):
'''Representation of the changes made to a single file.
MODIFIED - Contents changed, but no other changes were made
ADDED - File is newly created
REMOVED - File is being removed
Copies are represented by an Entry whose .parentname is non-nil
Truly copied files have non-nil .parentname and .renamed = False
Renames have non-nil .parentname and .renamed = True
Do not access any of this information directly, do so via the
.is_<change>() methods.'''
MODIFIED = 1
ADDED = 2
REMOVED = 3
self.parentname = None
# As opposed to copied (or neither)
#
# ActiveEntrys sort by the name of the file they represent.
#
def is_modified(self):
def is_removed(self):
def is_renamed(self):
class ActiveList(object):
'''Complete representation of workspace change.
In practice, a container for ActiveEntrys, and methods to build them,
update them, and deal with them en masse.'''
#
# If we couldn't find a parenttip, the two repositories must
# be unrelated (Hg catches most of this, but this case is valid for it
# but invalid for us)
#
if revs:
if not revs:
return
files = []
#
# When a file is renamed, two operations actually occur.
# A file copy from source to dest and a removal of source.
#
# These are represented as two distinct entries in the
# changectx and status (one on the dest file for the
# copy, one on the source file for the remove).
#
# Since these are unconnected in both the context and
# status we can only make the association by explicitly
# looking for it.
#
# We deal with this thusly:
#
# We maintain a dict dest -> source of all copies
# (updating dest as appropriate, but leaving source alone).
#
# After all other processing, we mark as renamed any pair
# where source is on the removed list.
#
copies = {}
#
# Walk revs looking for renames and adding files that
# are in both change context and status to the active
# list.
#
#
# We store comments per-entry as well, for the sake of
# webrev and similar. We store twice to avoid the problems
# of uniquifying comments for the general list (and possibly
# destroying multi-line entities in the process).
#
try:
except revlog.LookupError:
continue
#
# NB: .renamed() is a misnomer, this actually checks
# for copies.
#
if rn:
#
# If the source file is a known copy we know its
# ancestry leads us to the parent.
# Otherwise make sure the source file is known to
# be in the parent, we need not care otherwise.
#
# We detect cycles at a later point. There is no
# reason to continuously handle them.
#
#
# Walk the copy list marking as copied any non-cyclic pair
# where the destination file is still present in the local
# tip (to avoid ephemeral changes)
#
# Where source is removed, mark as renamed, and remove the
# AL entry for the source file
#
continue
#
# Walk the active list setting the change type for each active
# file.
#
# In the case of modified files that are not renames or
# copies, we do a content comparison, and drop entries that
# are not actually modified.
#
# We walk a copy of the AL such that we can drop entries
# within the loop.
#
continue
#
# There are cases during a merge where a file will be in
# the status return as modified, but in reality be an
# addition (ie, not in the parenttip).
#
# We need to check whether the file is actually present
# in the parenttip, and set it as an add, if not.
#
continue
yield entry
#
# Return list of files represented in this AL,
# including the parent file of a rename
#
return ret
#
# It's not uncommon for a child workspace to itself contain the
# merge of several other children, with initial branch points in
# the parent (possibly from the cset a project gate was created
# from, for instance).
#
# Immediately after recommit, this leaves us looking like this:
#
# * <- recommitted changeset (real tip)
# |
# | * <- Local tip
# |/|
# * | <- parent tip
# | |\
# | | |
# | | |\
# | | | |
# | * | | <- Base
# |/_/__/
#
# [left-most is parent, next is child, right two being
# branches in child, intermediate merges parent->child
# omitted]
#
# Obviously stripping base (the first child-specific delta on the
# main child workspace line) doesn't remove the vestigial branches
# from other workspaces (or in-workspace branches, or whatever)
#
# In reality, what we need to strip in a recommit is any
# child-specific branch descended from the parent (rather than
# another part of the child). Note that this by its very nature
# includes the branch representing the 'main' child workspace.
#
# We calculate these by walking from base (which is guaranteed to
# be the oldest child-local cset) to localtip searching for
# changesets with only one parent cset, and where that cset is not
# part of the active list (and is therefore outgoing).
#
'''Find the bases that in combination define the "old"
side of a recommitted set of changes, based on AL'''
# We don't rebuild the AL So the AL local tip is the old tip
hold = []
ret = []
if st == 'add':
elif st == 'iter':
if n not in hold:
continue
continue
return ret
'''Find tags that refer to a changeset in the ActiveList,
returning a list of 3-tuples (tag, node, is_local) for each.
We return all instances of a tag that refer to such a node,
not just that which takes precedence.'''
else:
ltags = []
# We want to use the tags file from the localtip
else:
rtags = []
tags = []
return tags
'''Compare two revisions of two files
Return True if file changed, False otherwise.
The fast path compares file metadata, slow path is a
real comparison of file content.'''
#
# NB: Keep these ordered such as to make every attempt
# to short-circuit the more time consuming checks.
#
return True
return True
return True
if parent == 'default':
parent = None
self.activecache = {}
self.outgoingcache = {}
'''Return canonical workspace parent, either SPEC if passed,
or default parent otherwise'''
'''Return a tuple (changectx, workingctx) representing the most
representative head to act as the local tip.
If the working directory is modified, the changectx is its
tipmost local parent (or tipmost parent, if neither is
local), and the workingctx is non-null.
If the working directory is clean, the workingctx is null.
The changectx is the tip-most local head on the current branch.
If this can't be determined for some reason (e.g., the parent
repo is inacessible), changectx is the tip-most head on the
current branch.
If the workingctx is non-null it is the actual local tip (and would
be the local tip in any generated ActiveList, for instance),
the better parent revision is returned also to aid callers needing
a real changeset to act as a surrogate for an uncommitted change.'''
def tipmost_of(nodes):
#
# We need a full set of outgoing nodes such that we can limit
# local branch heads to those which are outgoing
#
#
# A modified working context is seen as a proto-branch, where
# the 'heads' from our view are the parent revisions of that
# context.
# (and the working head is it)
#
else:
wctx = None
'''Find the closest approximation of the parents tip, as best
as we can.
In parent-less workspaces returns our tip (given the best
we can do is deal with uncommitted changes)'''
'''Return the tipmost node on the same branch as head that is not
in outnodes.
We walk from head to the bottom of the workspace (revision
0) collecting nodes not in outnodes during the add phase
and return the first node we see in the iter phase that
was previously collected.
See the docstring of mercurial.cmdutil.walkchangerevs()
for the phased approach to the iterator returned. The
important part to note is that the 'add' phase gathers
nodes, which the 'iter' phase then iterates through.'''
seen = []
if st == 'add':
if n not in outnodes:
elif st == 'iter':
if n in seen:
return rev
return None
outgoing = None
if parent:
if wctx:
else:
'''Translate from the hg 6-tuple status format to a hash keyed
on change-type'''
'ignored']
base = '.'
#
# Cache findoutgoing results
#
ret = []
else:
try:
"Warning: Parent workspace %s is not accessible\n" % parent)
return ret
'''Return a list of files modified in the workspace'''
'''Return boolean indicating whether the workspace has an uncommitted
merge'''
'''Return boolean indicating whether the workspace has an
uncommitted named branch'''
'''Return an ActiveList describing changes between workspace
and parent workspace (including uncommitted changes).
If workspace has no parent ActiveList will still describe any
uncommitted changes'''
if parent:
else:
outgoing = [] # No parent, no outgoing nodes
if workinghead:
[h.node() for h in
parents])[0]]
else:
ctxs)
return act
'Return diffs relative to PARENT, as best as we can make out'
#
# act.localtip maybe nil, in the case of uncommitted local
# changes.
#
return
#
# Theory:
#
# We wish to go from a single series of consecutive changesets
# (possibly including merges with the parent) to a single
# changeset faithfully representing contents and copy history.
#
# We achieve this in a somewhat confusing fashion.
#
# - Sanity check the workspace
# - Update the workspace to tip
# - Enter into the dirstate the sum total of file contents in the
# to-be-squished changesets
# - Commit this in-progress change (which has no changes at all,
# in reality) On top of the effective parent tip.
# - Strip the child-local branch(es) (see ActiveList.bases())
#
'''Create a single conglomerate changeset, with log message MESSAGE
containing the changes from ACTIVE. USER, if set, is used
as the author name.
The old changes are removed.'''
def strip_tags(nodes):
'''Remove any tags referring to the specified nodes.'''
#
# The files involved need to be present in the workspace and
# not otherwise molested, rather than the workspace not being
# modified we also need to prevent files being deleted (but
# left versioned) too.
#
# The easiest way to achieve this is to update the working
# copy to tip.
#
try:
except EnvironmentError, e:
#
# For copied files, we need to enter the copy into the
# dirstate before we force the commit such that the
# file logs of both branches (old and new) contain
# representation of the copy.
#
continue
("parentname '%s' (of '%s') not in parent" %
#
# If the source file exists, and used to be versioned
# this will cause this to become a true copy
# (re-introducing the source file)
#
# We bandaid this, by removing the source file in this
# case. If we're here, the user has already agreed to this
# from above.
#
if (entry.is_renamed() and
wsstate = "recommitted changeset"
else:
#
# If all we're doing is stripping the old nodes, we want to
# update the working copy such that we're not at a revision
# that's about to go away.
#
wsstate = "tip changeset"
# Silence all the strip and update fun
#
# We must strip away the old representation of the child
# branch(es). This may involve stripping a theoretically
# large number of branches in certain cases
#
try:
try:
except:
#
# If this fails, it may leave us in a surprising place in
# the history.
#
# We want to warn the user that something went wrong,
# and what will happen next, re-raise the exception, and
# bring the working copy back into a consistent state
# (which the finally block will do)
#
"superfluous heads.\n"
"your workspace has been updated to the "
"%s.\n" % wsstate)
raise # Re-raise the exception
finally:
#
# We need to remove Hg's undo information (used for rollback),
# since it refers to data that will probably not exist after
# the strip.
#
try:
except EnvironmentError, e:
'Return the full path to a workspace file.'
'''Bring workspace up to REV (or tip) forcefully (discarding in
progress changes)'''
if rev != None:
else:
def mq_applied(self):
'''True if the workspace has Mq patches applied'''
return q.applied
def workingctx(self):
else:
def workingctx(self):