xref: /titanic_44/usr/src/tools/onbld/Scm/WorkSpace.py (revision ff3124eff995e6cd8ebd8c6543648e0670920034)
1#
2#  This program is free software; you can redistribute it and/or modify
3#  it under the terms of the GNU General Public License version 2
4#  as published by the Free Software Foundation.
5#
6#  This program is distributed in the hope that it will be useful,
7#  but WITHOUT ANY WARRANTY; without even the implied warranty of
8#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9#  GNU General Public License for more details.
10#
11#  You should have received a copy of the GNU General Public License
12#  along with this program; if not, write to the Free Software
13#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
14#
15
16#
17# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
18# Use is subject to license terms.
19#
20# ident	"%Z%%M%	%I%	%E% SMI"
21#
22
23#
24# Theory:
25#
26# Workspaces have a non-binding parent/child relationship.
27# All important operations apply to the changes between the two.
28#
29# However, for the sake of remote operation, the 'parent' of a
30# workspace is not seen as a literal entity, instead the figurative
31# parent contains the last changeset common to both parent and child,
32# as such the 'parent tip' is actually nothing of the sort, but instead a
33# convenient imitation.
34#
35# Any change made to a workspace is a change to a file therein, such
36# changes can be represented briefly as whether the file was
37# modified/added/removed as compared to the parent workspace, whether
38# the file has a different name in the parent and if so, whether it
39# was renamed or merely copied.  Each changed file has an
40# associated ActiveEntry.
41#
42# The ActiveList being a list ActiveEntrys can thus present the entire
43# change in workspace state between a parent and its child, and is the
44# important bit here (in that if it is incorrect, everything else will
45# be as incorrect, or more)
46#
47
48import cStringIO
49import os
50from mercurial import hg, patch, cmdutil, util, node, repo
51from mercurial import revlog, repair
52from hgext import mq
53
54
55class ActiveEntry(object):
56    '''Representation of the changes made to a single file.
57
58    MODIFIED   - Contents changed, but no other changes were made
59    ADDED      - File is newly created
60    REMOVED    - File is being removed
61
62    Copies are represented by an Entry whose .parentname is non-nil
63
64    Truly copied files have non-nil .parentname and .renamed = False
65    Renames have non-nil .parentname and .renamed = True
66
67    Do not access any of this information directly, do so via the
68
69    .is_<change>() methods.'''
70
71    MODIFIED = 1
72    ADDED = 2
73    REMOVED = 3
74
75    def __init__(self, name):
76        self.name = name
77        self.change = None
78        self.parentname = None
79        # As opposed to copied (or neither)
80        self.renamed = False
81        self.comments = []
82
83    #
84    # ActiveEntrys sort by the name of the file they represent.
85    #
86    def __cmp__(self, other):
87        return cmp(self.name, other.name)
88
89    def is_added(self):
90        return self.change == self.ADDED
91
92    def is_modified(self):
93        return self.change == self.MODIFIED
94
95    def is_removed(self):
96        return self.change == self.REMOVED
97
98    def is_renamed(self):
99        return self.parentname and self.renamed
100
101    def is_copied(self):
102        return self.parentname and not self.renamed
103
104
105class ActiveList(object):
106    '''Complete representation of workspace change.
107
108    In practice, a container for ActiveEntrys, and methods to build them,
109    update them, and deal with them en masse.'''
110
111    def __init__(self, ws, parenttip, revs=None):
112        self._active = {}
113        self.ws = ws
114
115        self.revs = revs
116
117        self.base = None
118        self.parenttip = parenttip
119
120        #
121        # If we couldn't find a parenttip, the two repositories must
122        # be unrelated (Hg catches most of this, but this case is valid for it
123        # but invalid for us)
124        #
125        if self.parenttip == None:
126            raise util.Abort('repository is unrelated')
127        self.localtip = None
128
129        if revs:
130            self.base = revs[0]
131            self.localtip = revs[-1]
132
133        self._comments = []
134
135        self._build(revs)
136
137    def _build(self, revs):
138        if not revs:
139            return
140
141        status = self.ws.status(self.parenttip.node(), self.localtip.node())
142
143        files = []
144        for ctype in status.values():
145            files.extend(ctype)
146
147        #
148        # When a file is renamed, two operations actually occur.
149        # A file copy from source to dest and a removal of source.
150        #
151        # These are represented as two distinct entries in the
152        # changectx and status (one on the dest file for the
153        # copy, one on the source file for the remove).
154        #
155        # Since these are unconnected in both the context and
156        # status we can only make the association by explicitly
157        # looking for it.
158        #
159        # We deal with this thusly:
160        #
161        # We maintain a dict dest -> source of all copies
162        # (updating dest as appropriate, but leaving source alone).
163        #
164        # After all other processing, we mark as renamed any pair
165        # where source is on the removed list.
166        #
167        copies = {}
168
169        #
170        # Walk revs looking for renames and adding files that
171        # are in both change context and status to the active
172        # list.
173        #
174        for ctx in revs:
175            desc = ctx.description().splitlines()
176
177            self._comments.extend(desc)
178
179            for fname in ctx.files():
180                #
181                # We store comments per-entry as well, for the sake of
182                # webrev and similar.  We store twice to avoid the problems
183                # of uniquifying comments for the general list (and possibly
184                # destroying multi-line entities in the process).
185                #
186                if fname not in self:
187                    self._addentry(fname)
188                self[fname].comments.extend(desc)
189
190                try:
191                    fctx = ctx.filectx(fname)
192                except revlog.LookupError:
193                    continue
194
195                #
196                # NB: .renamed() is a misnomer, this actually checks
197                #     for copies.
198                #
199                rn = fctx.renamed()
200                if rn:
201                    #
202                    # If the source file is a known copy we know its
203                    # ancestry leads us to the parent.
204                    # Otherwise make sure the source file is known to
205                    # be in the parent, we need not care otherwise.
206                    #
207                    # We detect cycles at a later point.  There is no
208                    # reason to continuously handle them.
209                    #
210                    if rn[0] in copies:
211                        copies[fname] = copies[rn[0]]
212                    elif rn[0] in self.parenttip.manifest():
213                        copies[fname] = rn[0]
214
215        #
216        # Walk the copy list marking as copied any non-cyclic pair
217        # where the destination file is still present in the local
218        # tip (to avoid ephemeral changes)
219        #
220        # Where source is removed, mark as renamed, and remove the
221        # AL entry for the source file
222        #
223        for fname, oldname in copies.iteritems():
224            if fname == oldname or fname not in self.localtip.manifest():
225                continue
226
227            self[fname].parentname = oldname
228
229            if oldname in status['removed']:
230                self[fname].renamed = True
231                if oldname in self:
232                    del self[oldname]
233
234        #
235        # Walk the active list setting the change type for each active
236        # file.
237        #
238        # In the case of modified files that are not renames or
239        # copies, we do a content comparison, and drop entries that
240        # are not actually modified.
241        #
242        # We walk a copy of the AL such that we can drop entries
243        # within the loop.
244        #
245        for entry in self._active.values():
246            if entry.name not in files:
247                del self[entry.name]
248                continue
249
250            if entry.name in status['added']:
251                entry.change = ActiveEntry.ADDED
252            elif entry.name in status['removed']:
253                entry.change = ActiveEntry.REMOVED
254            elif entry.name in status['modified']:
255                entry.change = ActiveEntry.MODIFIED
256
257            #
258            # There are cases during a merge where a file will be in
259            # the status return as modified, but in reality be an
260            # addition (ie, not in the parenttip).
261            #
262            # We need to check whether the file is actually present
263            # in the parenttip, and set it as an add, if not.
264            #
265            if entry.name not in self.parenttip.manifest():
266                entry.change = ActiveEntry.ADDED
267            elif entry.is_modified() and not entry.parentname:
268                if not self.filecmp(entry):
269                    del self[entry.name]
270                    continue
271
272            assert entry.change
273
274    def __contains__(self, fname):
275        return fname in self._active
276
277    def __getitem__(self, key):
278        return self._active[key]
279
280    def __setitem__(self, key, value):
281        self._active[key] = value
282
283    def __delitem__(self, key):
284        del self._active[key]
285
286    def __iter__(self):
287        for entry in self._active.values():
288            yield entry
289
290    def _addentry(self, fname):
291        if fname not in self:
292            self[fname] = ActiveEntry(fname)
293
294    #
295    # Return list of files represented in this AL,
296    # including the parent file of a rename
297    #
298    def files(self):
299        ret = self._active.keys()
300
301        ret.extend([x.parentname for x in self
302                    if x.parentname and x.parentname not in ret])
303        return ret
304
305    def comments(self):
306        return self._comments
307
308    #
309    # It's not uncommon for a child workspace to itself contain the
310    # merge of several other children, with initial branch points in
311    # the parent (possibly from the cset a project gate was created
312    # from, for instance).
313    #
314    # Immediately after recommit, this leaves us looking like this:
315    #
316    #     *   <- recommitted changeset (real tip)
317    #     |
318    #     | *  <- Local tip
319    #     |/|
320    #     * |  <- parent tip
321    #     | |\
322    #     | | |
323    #     | | |\
324    #     | | | |
325    #     | * | |  <- Base
326    #     |/_/__/
327    #
328    #     [left-most is parent, next is child, right two being
329    #     branches in child, intermediate merges parent->child
330    #     omitted]
331    #
332    # Obviously stripping base (the first child-specific delta on the
333    # main child workspace line) doesn't remove the vestigial branches
334    # from other workspaces (or in-workspace branches, or whatever)
335    #
336    # In reality, what we need to strip in a recommit is any
337    # child-specific branch descended from the parent (rather than
338    # another part of the child).  Note that this by its very nature
339    # includes the branch representing the 'main' child workspace.
340    #
341    # We calculate these by walking from base (which is guaranteed to
342    # be the oldest child-local cset) to localtip searching for
343    # changesets with only one parent cset, and where that cset is not
344    # part of the active list (and is therefore outgoing).
345    #
346    def bases(self):
347        '''Find the bases that in combination define the "old"
348        side of a recommitted set of changes, based on AL'''
349
350        get = util.cachefunc(lambda r: self.ws.repo.changectx(r).changeset())
351
352        # We don't rebuild the AL So the AL local tip is the old tip
353        revrange = "%s:%s" % (self.base.rev(), self.localtip.rev())
354
355        changeiter = cmdutil.walkchangerevs(self.ws.repo.ui, self.ws.repo,
356                                            [], get, {'rev': [revrange]})[0]
357
358        hold = []
359        ret = []
360        alrevs = [x.rev() for x in self.revs]
361        for st, rev, fns in changeiter:
362            n = self.ws.repo.changelog.node(rev)
363            if st == 'add':
364                if rev in alrevs:
365                    hold.append(n)
366            elif st == 'iter':
367                if n not in hold:
368                    continue
369
370                p = self.ws.repo.changelog.parents(n)
371                if p[1] != node.nullid:
372                    continue
373
374                if self.ws.repo.changectx(p[0]).rev() not in alrevs:
375                    ret.append(n)
376        return ret
377
378    def tags(self):
379        '''Find tags that refer to a changeset in the ActiveList,
380           returning a list of 3-tuples (tag, node, is_local) for each.
381
382           We return all instances of a tag that refer to such a node,
383           not just that which takes precedence.'''
384
385        if os.path.exists(self.ws.repo.join('localtags')):
386            l = self.ws.repo.opener('localtags').readlines()
387            ltags = [x.rstrip().split(' ') for x in l]
388        else:
389            ltags = []
390
391        # We want to use the tags file from the localtip
392        if '.hgtags' in self.localtip.manifest():
393            f = self.localtip.filectx('.hgtags')
394            rtags = [x.rstrip().split(' ') for x in f.data().splitlines()]
395        else:
396            rtags = []
397
398        nodes = [node.hex(n.node()) for n in self.revs]
399        tags = []
400
401        for nd, name in rtags:
402            if nd in nodes:
403                tags.append((name, self.ws.repo.lookup(nd), False))
404
405        for nd, name in ltags:
406            if nd in nodes:
407                tags.append((name, self.ws.repo.lookup(nd), True))
408
409        return tags
410
411    def filecmp(self, entry):
412        '''Compare two revisions of two files
413
414        Return True if file changed, False otherwise.
415
416        The fast path compares file metadata, slow path is a
417        real comparison of file content.'''
418
419        parentfile = self.parenttip.filectx(entry.parentname or entry.name)
420        localfile = self.localtip.filectx(entry.name)
421
422        #
423        # NB: Keep these ordered such as to make every attempt
424        #     to short-circuit the more time consuming checks.
425        #
426        if parentfile.size() != localfile.size():
427            return True
428
429        if parentfile.fileflags() != localfile.fileflags():
430            return True
431
432        if parentfile.cmp(localfile.data()):
433            return True
434
435
436class WorkSpace(object):
437
438    def __init__(self, repository):
439        self.repo = repository
440        self.ui = self.repo.ui
441        self.name = self.repo.root
442
443        parent = self.repo.ui.expandpath('default')
444        if parent == 'default':
445            parent = None
446        self.parentrepo = parent
447
448        self.activecache = {}
449        self.outgoingcache = {}
450
451    def parent(self, spec=None):
452        '''Return canonical workspace parent, either SPEC if passed,
453        or default parent otherwise'''
454        return spec or self.parentrepo
455
456    def _localtip(self, bases, heads):
457        '''Return a tuple (changectx, workingctx) representing the most
458        representative head to act as the local tip.
459
460        If the working directory is modified, the changectx is its
461        tipmost local parent (or tipmost parent, if neither is
462        local), and the workingctx is non-null.
463
464        If the working directory is clean, the workingctx is null.
465        The changectx is the tip-most local head on the current branch.
466        If this can't be determined for some reason (e.g., the parent
467        repo is inacessible), changectx is the tip-most head on the
468        current branch.
469
470        If the workingctx is non-null it is the actual local tip (and would
471        be the local tip in any generated ActiveList, for instance),
472        the better parent revision is returned also to aid callers needing
473        a real changeset to act as a surrogate for an uncommitted change.'''
474
475        def tipmost_of(nodes):
476            return sorted(nodes, cmp=lambda x, y: cmp(x.rev(), y.rev()))[-1]
477
478        #
479        # We need a full set of outgoing nodes such that we can limit
480        # local branch heads to those which are outgoing
481        #
482        outnodes = self.repo.changelog.nodesbetween(bases, heads)[0]
483        wctx = self.repo.workingctx()
484
485        #
486        # A modified working context is seen as a proto-branch, where
487        # the 'heads' from our view are the parent revisions of that
488        # context.
489        # (and the working head is it)
490        #
491        if (wctx.files() or len(wctx.parents()) > 1 or
492            wctx.branch() != wctx.parents()[0].branch()):
493            heads = wctx.parents()
494        else:
495            heads = [self.repo.changectx(n) for n in heads]
496            wctx = None
497
498        localchoices = [n for n in heads if n.node() in outnodes]
499        return (tipmost_of(localchoices or heads), wctx)
500
501    def _parenttip(self, localtip, parent=None):
502        '''Find the closest approximation of the parents tip, as best
503        as we can.
504
505        In parent-less workspaces returns our tip (given the best
506        we can do is deal with uncommitted changes)'''
507
508        def tipmost_shared(head, outnodes):
509            '''Return the tipmost node on the same branch as head that is not
510            in outnodes.
511
512            We walk from head to the bottom of the workspace (revision
513            0) collecting nodes not in outnodes during the add phase
514            and return the first node we see in the iter phase that
515            was previously collected.
516
517            See the docstring of mercurial.cmdutil.walkchangerevs()
518            for the phased approach to the iterator returned.  The
519            important part to note is that the 'add' phase gathers
520            nodes, which the 'iter' phase then iterates through.'''
521
522            get = util.cachefunc(lambda r: self.repo.changectx(r).changeset())
523            changeiter = cmdutil.walkchangerevs(self.repo.ui, self.repo, [],
524                                                get, {'rev': ['%s:0' % head],
525                                                      'follow': True})[0]
526            seen = []
527            for st, rev, fns in changeiter:
528                n = self.repo.changelog.node(rev)
529                if st == 'add':
530                    if n not in outnodes:
531                        seen.append(n)
532                elif st == 'iter':
533                    if n in seen:
534                        return rev
535            return None
536
537        tipctx, wctx = localtip
538        parent = self.parent(parent)
539        outgoing = None
540
541        if parent:
542            outgoing = self.findoutgoing(parent)
543
544        if wctx:
545            possible_branches = wctx.parents()
546        else:
547            possible_branches = [tipctx]
548
549        nodes = self.repo.changelog.nodesbetween(outgoing)[0]
550        ptips = map(lambda x: tipmost_shared(x.rev(), nodes), possible_branches)
551        return self.repo.changectx(sorted(ptips)[-1])
552
553    def status(self, base=None, head=None):
554        '''Translate from the hg 6-tuple status format to a hash keyed
555        on change-type'''
556        states = ['modified', 'added', 'removed', 'deleted', 'unknown',
557              'ignored']
558        chngs = self.repo.status(base, head)
559        return dict(zip(states, chngs))
560
561    #
562    # Cache findoutgoing results
563    #
564    def findoutgoing(self, parent):
565        ret = []
566        if parent in self.outgoingcache:
567            ret = self.outgoingcache[parent]
568        else:
569            self.ui.pushbuffer()
570            try:
571                pws = hg.repository(self.ui, parent)
572                ret = self.repo.findoutgoing(pws)
573            except repo.RepoError:
574                self.ui.warn(
575                    "Warning: Parent workspace %s is not accessible\n" % parent)
576                self.ui.warn("active list will be incomplete\n\n")
577
578            self.outgoingcache[parent] = ret
579            self.ui.popbuffer()
580
581        return ret
582
583    def modified(self):
584        '''Return a list of files modified in the workspace'''
585        wctx = self.repo.workingctx()
586        return sorted(wctx.files() + wctx.deleted()) or None
587
588    def merged(self):
589        '''Return boolean indicating whether the workspace has an uncommitted
590        merge'''
591        wctx = self.repo.workingctx()
592        return len(wctx.parents()) > 1
593
594    def branched(self):
595        '''Return boolean indicating whether the workspace has an
596        uncommitted named branch'''
597
598        wctx = self.repo.workingctx()
599        return wctx.branch() != wctx.parents()[0].branch()
600
601    def active(self, parent=None):
602        '''Return an ActiveList describing changes between workspace
603        and parent workspace (including uncommitted changes).
604        If workspace has no parent ActiveList will still describe any
605        uncommitted changes'''
606
607        parent = self.parent(parent)
608        if parent in self.activecache:
609            return self.activecache[parent]
610
611        if parent:
612            outgoing = self.findoutgoing(parent)
613        else:
614            outgoing = []       # No parent, no outgoing nodes
615
616        branchheads = self.repo.heads(start=self.repo.dirstate.parents()[0])
617        ourhead, workinghead = self._localtip(outgoing, branchheads)
618
619        if len(branchheads) > 1:
620            self.ui.warn('The current branch has more than one head, '
621                         'using %s\n' % ourhead.rev())
622
623        if workinghead:
624            parents = workinghead.parents()
625            ctxs = [self.repo.changectx(n) for n in
626                    self.repo.changelog.nodesbetween(outgoing,
627                                                     [h.node() for h in
628                                                      parents])[0]]
629            ctxs.append(workinghead)
630        else:
631            ctxs = [self.repo.changectx(n) for n in
632                    self.repo.changelog.nodesbetween(outgoing,
633                                                     [ourhead.node()])[0]]
634
635        act = ActiveList(self, self._parenttip((ourhead, workinghead), parent),
636                         ctxs)
637
638        self.activecache[parent] = act
639        return act
640
641    def pdiff(self, parent=None):
642        'Return diffs relative to PARENT, as best as we can make out'
643
644        parent = self.parent(parent)
645        act = self.active(parent)
646
647        #
648        # act.localtip maybe nil, in the case of uncommitted local
649        # changes.
650        #
651        if not act.revs:
652            return
653
654        ret = cStringIO.StringIO()
655        patch.diff(self.repo, act.parenttip.node(), act.localtip.node(),
656                   fp=ret)
657        return ret.getvalue()
658
659    #
660    # Theory:
661    #
662    # We wish to go from a single series of consecutive changesets
663    # (possibly including merges with the parent) to a single
664    # changeset faithfully representing contents and copy history.
665    #
666    # We achieve this in a somewhat confusing fashion.
667    #
668    # - Sanity check the workspace
669    # - Update the workspace to tip
670    # - Enter into the dirstate the sum total of file contents in the
671    #   to-be-squished changesets
672    # - Commit this in-progress change (which has no changes at all,
673    #   in reality) On top of the effective parent tip.
674    # - Strip the child-local branch(es) (see ActiveList.bases())
675    #
676    def squishdeltas(self, active, message, user=None):
677        '''Create a single conglomerate changeset, with log message MESSAGE
678        containing the changes from ACTIVE.  USER, if set, is used
679        as the author name.
680
681        The old changes are removed.'''
682
683        def strip_tags(nodes):
684            '''Remove any tags referring to the specified nodes.'''
685
686            if os.path.exists(self.repo.join('localtags')):
687                fh = self.repo.opener('localtags').readlines()
688                tags = [t for t in fh if t.split(' ')[0] not in nodes]
689                fh = self.repo.opener('localtags', 'w', atomictemp=True)
690                fh.writelines(tags)
691                fh.rename()
692
693            if os.path.exists(self.repo.wjoin('.hgtags')):
694                fh = self.repo.wopener('.hgtags', 'rb').readlines()
695                tags = [t for t in fh if t.split(' ')[0] not in nodes]
696                fh = self.repo.wopener('.hgtags', 'wb', atomictemp=True)
697                fh.writelines(tags)
698                fh.rename()
699
700        wlock = self.repo.wlock()
701        lock = self.repo.lock()
702
703        #
704        # The files involved need to be present in the workspace and
705        # not otherwise molested, rather than the workspace not being
706        # modified we also need to prevent files being deleted (but
707        # left versioned) too.
708        #
709        # The easiest way to achieve this is to update the working
710        # copy to tip.
711        #
712        self.clean()
713
714        try:
715            strip_tags([node.hex(ctx.node()) for ctx in active.revs])
716        except EnvironmentError, e:
717            raise util.Abort('Could not recommit tags: %s\n' % e)
718
719        #
720        # For copied files, we need to enter the copy into the
721        # dirstate before we force the commit such that the
722        # file logs of both branches (old and new) contain
723        # representation of the copy.
724        #
725        parentman = active.parenttip.manifest()
726        for entry in active:
727            if not entry.is_renamed() and not entry.is_copied():
728                continue
729
730            assert entry.parentname in parentman, \
731                ("parentname '%s' (of '%s') not in parent" %
732                 (entry.parentname, entry.name))
733
734            #
735            # If the source file exists, and used to be versioned
736            # this will cause this to become a true copy
737            # (re-introducing the source file)
738            #
739            # We bandaid this, by removing the source file in this
740            # case.  If we're here, the user has already agreed to this
741            # from above.
742            #
743            if (entry.is_renamed() and
744                os.path.exists(self.repo.wjoin(entry.parentname))):
745                os.unlink(self.repo.wjoin(entry.parentname))
746
747            self.repo.copy(entry.parentname, entry.name)
748
749        if active.files():
750            extra = {'branch': active.localtip.branch()}
751            self.repo.commit(files=active.files(), text=message,
752                             user=user, p1=active.parenttip.node(), p2=None,
753                             extra=extra)
754            wsstate = "recommitted changeset"
755            self.clean()
756        else:
757            #
758            # If all we're doing is stripping the old nodes, we want to
759            # update the working copy such that we're not at a revision
760            # that's about to go away.
761            #
762            wsstate = "tip changeset"
763            self.clean(rev=active.parenttip.node())
764
765        # Silence all the strip and update fun
766        self.ui.pushbuffer()
767
768        #
769        # We must strip away the old representation of the child
770        # branch(es).  This may involve stripping a theoretically
771        # large number of branches in certain cases
772        #
773        bases = active.bases()
774        try:
775            try:
776                for basenode in bases:
777                    repair.strip(self.ui, self.repo, basenode, backup=False)
778            except:
779                #
780                # If this fails, it may leave us in a surprising place in
781                # the history.
782                #
783                # We want to warn the user that something went wrong,
784                # and what will happen next, re-raise the exception, and
785                # bring the working copy back into a consistent state
786                # (which the finally block will do)
787                #
788                self.ui.warn("stripping failed, your workspace will have "
789                             "superfluous heads.\n"
790                             "your workspace has been updated to the "
791                             "%s.\n" % wsstate)
792                raise               # Re-raise the exception
793        finally:
794            #
795            # We need to remove Hg's undo information (used for rollback),
796            # since it refers to data that will probably not exist after
797            # the strip.
798            #
799
800            self.clean()
801            self.repo.dirstate.write() # Flush the dirstate
802            self.repo.invalidate()     # Invalidate caches
803
804            if os.path.exists(self.repo.sjoin('undo')):
805                try:
806                    os.unlink(self.repo.sjoin('undo'))
807                except EnvironmentError, e:
808                    raise util.Abort('failed to remove undo data: %s\n' % e)
809
810            self.ui.popbuffer()
811
812    def filepath(self, path):
813        'Return the full path to a workspace file.'
814        return self.repo.pathto(path)
815
816    def clean(self, rev=None):
817        '''Bring workspace up to REV (or tip) forcefully (discarding in
818        progress changes)'''
819        if rev != None:
820            rev = self.repo.lookup(rev)
821        else:
822            rev = self.repo.changelog.tip()
823
824        wlock = self.repo.wlock()
825        hg.clean(self.repo, rev, show_stats=False)
826
827    def mq_applied(self):
828        '''True if the workspace has Mq patches applied'''
829        q = mq.queue(self.ui, self.repo.join(''))
830        return q.applied
831