mercurial/context.py
author Dirkjan Ochtman <dirkjan@ochtman.nl>
Mon, 21 Jul 2008 13:26:42 +0200
changeset 6846 54b7c79575fa
parent 6845 835a01a0cdb3
child 6876 077f1e637cd8
permissions -rw-r--r--
context: get rid of unused changectx.filectxs() method
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
# context.py - changeset and file context objects for mercurial
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
#
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4417
diff changeset
     3
# Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
#
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     7
6763
403682f1c678 context: add __int__ and hex methods
Matt Mackall <mpm@selenic.com>
parents: 6762
diff changeset
     8
from node import nullid, nullrev, short, hex
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 3877
diff changeset
     9
from i18n import _
6212
e75aab656f46 Remove unused imports
Joel Rosdahl <joel@rosdahl.net>
parents: 6211
diff changeset
    10
import ancestor, bdiff, revlog, util, os, errno
3122
da85145d4571 filectx: add rename traversal for parents()
Matt Mackall <mpm@selenic.com>
parents: 2859
diff changeset
    11
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    12
class changectx(object):
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    13
    """A changecontext object makes access to data related to a particular
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    14
    changeset convenient."""
6741
5918e2b79859 context: simplify changeid logic
Matt Mackall <mpm@selenic.com>
parents: 6737
diff changeset
    15
    def __init__(self, repo, changeid=''):
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    16
        """changeid is a revision number, node, or tag"""
6741
5918e2b79859 context: simplify changeid logic
Matt Mackall <mpm@selenic.com>
parents: 6737
diff changeset
    17
        if changeid == '':
5918e2b79859 context: simplify changeid logic
Matt Mackall <mpm@selenic.com>
parents: 6737
diff changeset
    18
            changeid = '.'
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    19
        self._repo = repo
2643
f23973ea3107 fix filectxt to really work
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 2629
diff changeset
    20
        self._node = self._repo.lookup(changeid)
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    21
        self._rev = self._repo.changelog.rev(self._node)
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    22
3166
ebdb3f616bc0 Add str methods to contexts
Matt Mackall <mpm@selenic.com>
parents: 3165
diff changeset
    23
    def __str__(self):
ebdb3f616bc0 Add str methods to contexts
Matt Mackall <mpm@selenic.com>
parents: 3165
diff changeset
    24
        return short(self.node())
ebdb3f616bc0 Add str methods to contexts
Matt Mackall <mpm@selenic.com>
parents: 3165
diff changeset
    25
6763
403682f1c678 context: add __int__ and hex methods
Matt Mackall <mpm@selenic.com>
parents: 6762
diff changeset
    26
    def __int__(self):
403682f1c678 context: add __int__ and hex methods
Matt Mackall <mpm@selenic.com>
parents: 6762
diff changeset
    27
        return self.rev()
403682f1c678 context: add __int__ and hex methods
Matt Mackall <mpm@selenic.com>
parents: 6762
diff changeset
    28
3151
6719b3dd7d50 context: add __repr__ methods
Matt Mackall <mpm@selenic.com>
parents: 3150
diff changeset
    29
    def __repr__(self):
3216
d865390c1781 context: simplify repr methods
Matt Mackall <mpm@selenic.com>
parents: 3215
diff changeset
    30
        return "<changectx %s>" % str(self)
3151
6719b3dd7d50 context: add __repr__ methods
Matt Mackall <mpm@selenic.com>
parents: 3150
diff changeset
    31
6469
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
    32
    def __hash__(self):
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
    33
        try:
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
    34
            return hash(self._rev)
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
    35
        except AttributeError:
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
    36
            return id(self)
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
    37
3165
e78185746554 Add equality operators to changectx and filectx
Matt Mackall <mpm@selenic.com>
parents: 3152
diff changeset
    38
    def __eq__(self, other):
3715
6cb3aca69cdc Make context __eq__ handle arbitrary RHS values
Brendan Cully <brendan@kublai.com>
parents: 3712
diff changeset
    39
        try:
6cb3aca69cdc Make context __eq__ handle arbitrary RHS values
Brendan Cully <brendan@kublai.com>
parents: 3712
diff changeset
    40
            return self._rev == other._rev
6cb3aca69cdc Make context __eq__ handle arbitrary RHS values
Brendan Cully <brendan@kublai.com>
parents: 3712
diff changeset
    41
        except AttributeError:
6cb3aca69cdc Make context __eq__ handle arbitrary RHS values
Brendan Cully <brendan@kublai.com>
parents: 3712
diff changeset
    42
            return False
3165
e78185746554 Add equality operators to changectx and filectx
Matt Mackall <mpm@selenic.com>
parents: 3152
diff changeset
    43
4748
8808ea7da86b merge: make test for fast-forward merge stricter (issue619)
Matt Mackall <mpm@selenic.com>
parents: 4663
diff changeset
    44
    def __ne__(self, other):
8808ea7da86b merge: make test for fast-forward merge stricter (issue619)
Matt Mackall <mpm@selenic.com>
parents: 4663
diff changeset
    45
        return not (self == other)
8808ea7da86b merge: make test for fast-forward merge stricter (issue619)
Matt Mackall <mpm@selenic.com>
parents: 4663
diff changeset
    46
3168
05c588e1803d context: add __nonzero__ methods
Matt Mackall <mpm@selenic.com>
parents: 3166
diff changeset
    47
    def __nonzero__(self):
3578
3b4e00cba57a Define and use nullrev (revision of nullid) instead of -1.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3454
diff changeset
    48
        return self._rev != nullrev
3168
05c588e1803d context: add __nonzero__ methods
Matt Mackall <mpm@selenic.com>
parents: 3166
diff changeset
    49
3215
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    50
    def __getattr__(self, name):
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    51
        if name == '_changeset':
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    52
            self._changeset = self._repo.changelog.read(self.node())
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    53
            return self._changeset
3215
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    54
        elif name == '_manifest':
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    55
            self._manifest = self._repo.manifest.read(self._changeset[0])
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    56
            return self._manifest
3337
b02e60097bbe changectx: search manifest delta for filenode
Brendan Cully <brendan@kublai.com>
parents: 3336
diff changeset
    57
        elif name == '_manifestdelta':
b02e60097bbe changectx: search manifest delta for filenode
Brendan Cully <brendan@kublai.com>
parents: 3336
diff changeset
    58
            md = self._repo.manifest.readdelta(self._changeset[0])
b02e60097bbe changectx: search manifest delta for filenode
Brendan Cully <brendan@kublai.com>
parents: 3336
diff changeset
    59
            self._manifestdelta = md
b02e60097bbe changectx: search manifest delta for filenode
Brendan Cully <brendan@kublai.com>
parents: 3336
diff changeset
    60
            return self._manifestdelta
6742
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
    61
        elif name == '_parents':
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
    62
            p = self._repo.changelog.parents(self._node)
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
    63
            if p[1] == nullid:
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
    64
                p = p[:-1]
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
    65
            self._parents = [changectx(self._repo, x) for x in p]
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
    66
            return self._parents
3215
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    67
        else:
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    68
            raise AttributeError, name
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    69
4909
1fd7a99d98f6 context: add __contains__, __getitem__, and __iter__
Matt Mackall <mpm@selenic.com>
parents: 4889
diff changeset
    70
    def __contains__(self, key):
1fd7a99d98f6 context: add __contains__, __getitem__, and __iter__
Matt Mackall <mpm@selenic.com>
parents: 4889
diff changeset
    71
        return key in self._manifest
1fd7a99d98f6 context: add __contains__, __getitem__, and __iter__
Matt Mackall <mpm@selenic.com>
parents: 4889
diff changeset
    72
1fd7a99d98f6 context: add __contains__, __getitem__, and __iter__
Matt Mackall <mpm@selenic.com>
parents: 4889
diff changeset
    73
    def __getitem__(self, key):
1fd7a99d98f6 context: add __contains__, __getitem__, and __iter__
Matt Mackall <mpm@selenic.com>
parents: 4889
diff changeset
    74
        return self.filectx(key)
1fd7a99d98f6 context: add __contains__, __getitem__, and __iter__
Matt Mackall <mpm@selenic.com>
parents: 4889
diff changeset
    75
1fd7a99d98f6 context: add __contains__, __getitem__, and __iter__
Matt Mackall <mpm@selenic.com>
parents: 4889
diff changeset
    76
    def __iter__(self):
6762
f67d1468ac50 util: add sort helper
Matt Mackall <mpm@selenic.com>
parents: 6760
diff changeset
    77
        for f in util.sort(self._manifest):
5485
8c0756f7b18b Fix context iterator.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5439
diff changeset
    78
            yield f
4909
1fd7a99d98f6 context: add __contains__, __getitem__, and __iter__
Matt Mackall <mpm@selenic.com>
parents: 4889
diff changeset
    79
3215
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    80
    def changeset(self): return self._changeset
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    81
    def manifest(self): return self._manifest
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    82
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    83
    def rev(self): return self._rev
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    84
    def node(self): return self._node
6763
403682f1c678 context: add __int__ and hex methods
Matt Mackall <mpm@selenic.com>
parents: 6762
diff changeset
    85
    def hex(self): return hex(self._node)
3215
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    86
    def user(self): return self._changeset[1]
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    87
    def date(self): return self._changeset[2]
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    88
    def files(self): return self._changeset[3]
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
    89
    def description(self): return self._changeset[4]
4178
0b48e3985765 Minor default branch cleanups
Matt Mackall <mpm@selenic.com>
parents: 4176
diff changeset
    90
    def branch(self): return self._changeset[5].get("branch")
5439
d0c67b52ac01 convert: make contents of "extra" dict available from sources, for sinks.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5407
diff changeset
    91
    def extra(self): return self._changeset[5]
4663
6b2e8cb39583 context: add tags() method
Matt Mackall <mpm@selenic.com>
parents: 4640
diff changeset
    92
    def tags(self): return self._repo.nodetags(self._node)
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    93
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    94
    def parents(self):
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    95
        """return contexts for each parent changeset"""
6742
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
    96
        return self._parents
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    97
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    98
    def children(self):
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    99
        """return contexts for each child changeset"""
2627
b779319a532b context.py: self.repo is not defined, change to self._repo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 2566
diff changeset
   100
        c = self._repo.changelog.children(self._node)
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   101
        return [changectx(self._repo, x) for x in c]
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   102
5389
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   103
    def _fileinfo(self, path):
3336
e44eadc92ec4 context: check self.__dict__ instead of using hasattr
Brendan Cully <brendan@kublai.com>
parents: 3313
diff changeset
   104
        if '_manifest' in self.__dict__:
3242
1539f788e913 Make changectx.filenode raise repo.LookupError on failure
Brendan Cully <brendan@kublai.com>
parents: 3241
diff changeset
   105
            try:
5389
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   106
                return self._manifest[path], self._manifest.flags(path)
3242
1539f788e913 Make changectx.filenode raise repo.LookupError on failure
Brendan Cully <brendan@kublai.com>
parents: 3241
diff changeset
   107
            except KeyError:
6228
c0c4c7b1e8d3 revlog: report node and file when lookup fails
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   108
                raise revlog.LookupError(self._node, path,
c0c4c7b1e8d3 revlog: report node and file when lookup fails
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   109
                                         _('not found in manifest'))
3337
b02e60097bbe changectx: search manifest delta for filenode
Brendan Cully <brendan@kublai.com>
parents: 3336
diff changeset
   110
        if '_manifestdelta' in self.__dict__ or path in self.files():
b02e60097bbe changectx: search manifest delta for filenode
Brendan Cully <brendan@kublai.com>
parents: 3336
diff changeset
   111
            if path in self._manifestdelta:
5389
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   112
                return self._manifestdelta[path], self._manifestdelta.flags(path)
3215
931288cf58a7 contexts: use __getattr__ rather than try/except in changectx
Matt Mackall <mpm@selenic.com>
parents: 3214
diff changeset
   113
        node, flag = self._repo.manifest.find(self._changeset[0], path)
3242
1539f788e913 Make changectx.filenode raise repo.LookupError on failure
Brendan Cully <brendan@kublai.com>
parents: 3241
diff changeset
   114
        if not node:
6228
c0c4c7b1e8d3 revlog: report node and file when lookup fails
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   115
            raise revlog.LookupError(self._node, path,
c0c4c7b1e8d3 revlog: report node and file when lookup fails
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   116
                                     _('not found in manifest'))
3242
1539f788e913 Make changectx.filenode raise repo.LookupError on failure
Brendan Cully <brendan@kublai.com>
parents: 3241
diff changeset
   117
5389
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   118
        return node, flag
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   119
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   120
    def filenode(self, path):
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   121
        return self._fileinfo(path)[0]
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   122
6743
86e8187b721a simplify flag handling
Matt Mackall <mpm@selenic.com>
parents: 6742
diff changeset
   123
    def flags(self, path):
5389
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   124
        try:
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   125
            return self._fileinfo(path)[1]
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   126
        except revlog.LookupError:
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   127
            return ''
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   128
3966
b4eaa68dea1b context: create a filectxt with filelog reuse
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3965
diff changeset
   129
    def filectx(self, path, fileid=None, filelog=None):
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   130
        """get a file context from this changeset"""
2628
9999a796d389 context.py: filectxs was using a keyword arg, add it to filectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 2627
diff changeset
   131
        if fileid is None:
9999a796d389 context.py: filectxs was using a keyword arg, add it to filectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 2627
diff changeset
   132
            fileid = self.filenode(path)
3966
b4eaa68dea1b context: create a filectxt with filelog reuse
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3965
diff changeset
   133
        return filectx(self._repo, path, fileid=fileid,
b4eaa68dea1b context: create a filectxt with filelog reuse
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3965
diff changeset
   134
                       changectx=self, filelog=filelog)
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   135
3125
02b22fefc01f changectx: add ancestor function
Matt Mackall <mpm@selenic.com>
parents: 3124
diff changeset
   136
    def ancestor(self, c2):
02b22fefc01f changectx: add ancestor function
Matt Mackall <mpm@selenic.com>
parents: 3124
diff changeset
   137
        """
02b22fefc01f changectx: add ancestor function
Matt Mackall <mpm@selenic.com>
parents: 3124
diff changeset
   138
        return the ancestor context of self and c2
02b22fefc01f changectx: add ancestor function
Matt Mackall <mpm@selenic.com>
parents: 3124
diff changeset
   139
        """
02b22fefc01f changectx: add ancestor function
Matt Mackall <mpm@selenic.com>
parents: 3124
diff changeset
   140
        n = self._repo.changelog.ancestor(self._node, c2._node)
02b22fefc01f changectx: add ancestor function
Matt Mackall <mpm@selenic.com>
parents: 3124
diff changeset
   141
        return changectx(self._repo, n)
02b22fefc01f changectx: add ancestor function
Matt Mackall <mpm@selenic.com>
parents: 3124
diff changeset
   142
6764
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   143
    def walk(self, match):
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   144
        fdict = dict.fromkeys(match.files())
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   145
        # for dirstate.walk, files=['.'] means "walk the whole tree".
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   146
        # follow that here, too
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   147
        fdict.pop('.', None)
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   148
        for fn in self:
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   149
            for ffn in fdict:
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   150
                # match if the file is the exact name or a directory
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   151
                if ffn == fn or fn.startswith("%s/" % ffn):
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   152
                    del fdict[ffn]
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   153
                    break
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   154
            if match(fn):
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   155
                yield fn
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   156
        for fn in util.sort(fdict):
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   157
            if match.bad(fn, 'No such file in rev ' + str(self)) and match(fn):
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   158
                yield fn
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   159
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   160
class filectx(object):
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   161
    """A filecontext object makes access to data related to a particular
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   162
       filerevision convenient."""
3214
696c656202a0 context: make filectx remember changectx in changectx.filectx
Matt Mackall <mpm@selenic.com>
parents: 3213
diff changeset
   163
    def __init__(self, repo, path, changeid=None, fileid=None,
696c656202a0 context: make filectx remember changectx in changectx.filectx
Matt Mackall <mpm@selenic.com>
parents: 3213
diff changeset
   164
                 filelog=None, changectx=None):
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   165
        """changeid can be a changeset revision, node, or tag.
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   166
           fileid can be a file revision or node."""
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   167
        self._repo = repo
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   168
        self._path = path
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   169
3964
2308c39b9521 make it possible to use changectx to create a filectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3962
diff changeset
   170
        assert (changeid is not None
2308c39b9521 make it possible to use changectx to create a filectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3962
diff changeset
   171
                or fileid is not None
2308c39b9521 make it possible to use changectx to create a filectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3962
diff changeset
   172
                or changectx is not None)
2643
f23973ea3107 fix filectxt to really work
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 2629
diff changeset
   173
3124
4d021b91cb26 filectx: allow passing filelog in init to avoid opening new filelogs
Matt Mackall <mpm@selenic.com>
parents: 3123
diff changeset
   174
        if filelog:
4d021b91cb26 filectx: allow passing filelog in init to avoid opening new filelogs
Matt Mackall <mpm@selenic.com>
parents: 3123
diff changeset
   175
            self._filelog = filelog
4d021b91cb26 filectx: allow passing filelog in init to avoid opening new filelogs
Matt Mackall <mpm@selenic.com>
parents: 3123
diff changeset
   176
5810
124577de40a7 context: preserve changeset in filectx if we have one
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   177
        if changeid is not None:
124577de40a7 context: preserve changeset in filectx if we have one
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   178
            self._changeid = changeid
124577de40a7 context: preserve changeset in filectx if we have one
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   179
        if changectx is not None:
124577de40a7 context: preserve changeset in filectx if we have one
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   180
            self._changectx = changectx
124577de40a7 context: preserve changeset in filectx if we have one
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   181
        if fileid is not None:
3213
e8199702cf4e Make filectx lazier
Matt Mackall <mpm@selenic.com>
parents: 3209
diff changeset
   182
            self._fileid = fileid
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   183
3144
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   184
    def __getattr__(self, name):
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   185
        if name == '_changectx':
2643
f23973ea3107 fix filectxt to really work
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 2629
diff changeset
   186
            self._changectx = changectx(self._repo, self._changeid)
f23973ea3107 fix filectxt to really work
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 2629
diff changeset
   187
            return self._changectx
3213
e8199702cf4e Make filectx lazier
Matt Mackall <mpm@selenic.com>
parents: 3209
diff changeset
   188
        elif name == '_filelog':
e8199702cf4e Make filectx lazier
Matt Mackall <mpm@selenic.com>
parents: 3209
diff changeset
   189
            self._filelog = self._repo.file(self._path)
e8199702cf4e Make filectx lazier
Matt Mackall <mpm@selenic.com>
parents: 3209
diff changeset
   190
            return self._filelog
e8199702cf4e Make filectx lazier
Matt Mackall <mpm@selenic.com>
parents: 3209
diff changeset
   191
        elif name == '_changeid':
5810
124577de40a7 context: preserve changeset in filectx if we have one
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   192
            if '_changectx' in self.__dict__:
124577de40a7 context: preserve changeset in filectx if we have one
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   193
                self._changeid = self._changectx.rev()
124577de40a7 context: preserve changeset in filectx if we have one
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   194
            else:
124577de40a7 context: preserve changeset in filectx if we have one
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   195
                self._changeid = self._filelog.linkrev(self._filenode)
3213
e8199702cf4e Make filectx lazier
Matt Mackall <mpm@selenic.com>
parents: 3209
diff changeset
   196
            return self._changeid
3144
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   197
        elif name == '_filenode':
3961
a4edadd807dd fix tab vs space
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3930
diff changeset
   198
            if '_fileid' in self.__dict__:
a4edadd807dd fix tab vs space
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3930
diff changeset
   199
                self._filenode = self._filelog.lookup(self._fileid)
a4edadd807dd fix tab vs space
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3930
diff changeset
   200
            else:
a4edadd807dd fix tab vs space
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3930
diff changeset
   201
                self._filenode = self._changectx.filenode(self._path)
3144
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   202
            return self._filenode
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   203
        elif name == '_filerev':
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   204
            self._filerev = self._filelog.rev(self._filenode)
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   205
            return self._filerev
6286
90a4329a6b4a filectx.ancestor: use fctx._repopath to cache filelogs (issue1035)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 6228
diff changeset
   206
        elif name == '_repopath':
90a4329a6b4a filectx.ancestor: use fctx._repopath to cache filelogs (issue1035)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 6228
diff changeset
   207
            self._repopath = self._path
90a4329a6b4a filectx.ancestor: use fctx._repopath to cache filelogs (issue1035)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 6228
diff changeset
   208
            return self._repopath
3144
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   209
        else:
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   210
            raise AttributeError, name
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   211
3168
05c588e1803d context: add __nonzero__ methods
Matt Mackall <mpm@selenic.com>
parents: 3166
diff changeset
   212
    def __nonzero__(self):
3712
1bd70d40ec57 context: None is not a valid filenode (revert from 23ede9e7ad4d)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3673
diff changeset
   213
        try:
1bd70d40ec57 context: None is not a valid filenode (revert from 23ede9e7ad4d)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3673
diff changeset
   214
            n = self._filenode
1bd70d40ec57 context: None is not a valid filenode (revert from 23ede9e7ad4d)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3673
diff changeset
   215
            return True
3930
01d98d68d697 Add revlog.LookupError exception, and use it instead of RevlogError.
Brendan Cully <brendan@kublai.com>
parents: 3891
diff changeset
   216
        except revlog.LookupError:
3712
1bd70d40ec57 context: None is not a valid filenode (revert from 23ede9e7ad4d)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3673
diff changeset
   217
            # file is missing
1bd70d40ec57 context: None is not a valid filenode (revert from 23ede9e7ad4d)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3673
diff changeset
   218
            return False
3168
05c588e1803d context: add __nonzero__ methods
Matt Mackall <mpm@selenic.com>
parents: 3166
diff changeset
   219
3166
ebdb3f616bc0 Add str methods to contexts
Matt Mackall <mpm@selenic.com>
parents: 3165
diff changeset
   220
    def __str__(self):
ebdb3f616bc0 Add str methods to contexts
Matt Mackall <mpm@selenic.com>
parents: 3165
diff changeset
   221
        return "%s@%s" % (self.path(), short(self.node()))
ebdb3f616bc0 Add str methods to contexts
Matt Mackall <mpm@selenic.com>
parents: 3165
diff changeset
   222
3151
6719b3dd7d50 context: add __repr__ methods
Matt Mackall <mpm@selenic.com>
parents: 3150
diff changeset
   223
    def __repr__(self):
3216
d865390c1781 context: simplify repr methods
Matt Mackall <mpm@selenic.com>
parents: 3215
diff changeset
   224
        return "<filectx %s>" % str(self)
3151
6719b3dd7d50 context: add __repr__ methods
Matt Mackall <mpm@selenic.com>
parents: 3150
diff changeset
   225
6469
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
   226
    def __hash__(self):
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
   227
        try:
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
   228
            return hash((self._path, self._fileid))
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
   229
        except AttributeError:
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
   230
            return id(self)
fb502719c75c python 2.6 compatibility: add __hash__ to classes that have __eq__
Paul Moore <p.f.moore@gmail.com>
parents: 6286
diff changeset
   231
3165
e78185746554 Add equality operators to changectx and filectx
Matt Mackall <mpm@selenic.com>
parents: 3152
diff changeset
   232
    def __eq__(self, other):
3715
6cb3aca69cdc Make context __eq__ handle arbitrary RHS values
Brendan Cully <brendan@kublai.com>
parents: 3712
diff changeset
   233
        try:
6cb3aca69cdc Make context __eq__ handle arbitrary RHS values
Brendan Cully <brendan@kublai.com>
parents: 3712
diff changeset
   234
            return (self._path == other._path
4889
3b081f2a77b2 contexts: improve filectx eq test
Matt Mackall <mpm@selenic.com>
parents: 4856
diff changeset
   235
                    and self._fileid == other._fileid)
3715
6cb3aca69cdc Make context __eq__ handle arbitrary RHS values
Brendan Cully <brendan@kublai.com>
parents: 3712
diff changeset
   236
        except AttributeError:
6cb3aca69cdc Make context __eq__ handle arbitrary RHS values
Brendan Cully <brendan@kublai.com>
parents: 3712
diff changeset
   237
            return False
3165
e78185746554 Add equality operators to changectx and filectx
Matt Mackall <mpm@selenic.com>
parents: 3152
diff changeset
   238
4748
8808ea7da86b merge: make test for fast-forward merge stricter (issue619)
Matt Mackall <mpm@selenic.com>
parents: 4663
diff changeset
   239
    def __ne__(self, other):
8808ea7da86b merge: make test for fast-forward merge stricter (issue619)
Matt Mackall <mpm@selenic.com>
parents: 4663
diff changeset
   240
        return not (self == other)
8808ea7da86b merge: make test for fast-forward merge stricter (issue619)
Matt Mackall <mpm@selenic.com>
parents: 4663
diff changeset
   241
3207
0790dce2f3a8 Add lookup method to filectx
Brendan Cully <brendan@kublai.com>
parents: 3172
diff changeset
   242
    def filectx(self, fileid):
0790dce2f3a8 Add lookup method to filectx
Brendan Cully <brendan@kublai.com>
parents: 3172
diff changeset
   243
        '''opens an arbitrary revision of the file without
0790dce2f3a8 Add lookup method to filectx
Brendan Cully <brendan@kublai.com>
parents: 3172
diff changeset
   244
        opening a new filelog'''
0790dce2f3a8 Add lookup method to filectx
Brendan Cully <brendan@kublai.com>
parents: 3172
diff changeset
   245
        return filectx(self._repo, self._path, fileid=fileid,
0790dce2f3a8 Add lookup method to filectx
Brendan Cully <brendan@kublai.com>
parents: 3172
diff changeset
   246
                       filelog=self._filelog)
0790dce2f3a8 Add lookup method to filectx
Brendan Cully <brendan@kublai.com>
parents: 3172
diff changeset
   247
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   248
    def filerev(self): return self._filerev
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   249
    def filenode(self): return self._filenode
6743
86e8187b721a simplify flag handling
Matt Mackall <mpm@selenic.com>
parents: 6742
diff changeset
   250
    def flags(self): return self._changectx.flags(self._path)
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   251
    def filelog(self): return self._filelog
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   252
3150
a5e4c8172ace filectx: lazy linkrev usage
Matt Mackall <mpm@selenic.com>
parents: 3149
diff changeset
   253
    def rev(self):
3336
e44eadc92ec4 context: check self.__dict__ instead of using hasattr
Brendan Cully <brendan@kublai.com>
parents: 3313
diff changeset
   254
        if '_changectx' in self.__dict__:
3150
a5e4c8172ace filectx: lazy linkrev usage
Matt Mackall <mpm@selenic.com>
parents: 3149
diff changeset
   255
            return self._changectx.rev()
5810
124577de40a7 context: preserve changeset in filectx if we have one
Maxim Dounin <mdounin@mdounin.ru>
parents: 5760
diff changeset
   256
        if '_changeid' in self.__dict__:
6210
942287cb1f57 Removed trailing spaces from everything except test output
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5813
diff changeset
   257
            return self._changectx.rev()
3403
372999405787 Back out d8eba1c3ce9b and a004164dbeef
Brendan Cully <brendan@kublai.com>
parents: 3401
diff changeset
   258
        return self._filelog.linkrev(self._filenode)
3150
a5e4c8172ace filectx: lazy linkrev usage
Matt Mackall <mpm@selenic.com>
parents: 3149
diff changeset
   259
5811
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   260
    def linkrev(self): return self._filelog.linkrev(self._filenode)
3144
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   261
    def node(self): return self._changectx.node()
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   262
    def user(self): return self._changectx.user()
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   263
    def date(self): return self._changectx.date()
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   264
    def files(self): return self._changectx.files()
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   265
    def description(self): return self._changectx.description()
3413
cc9c31b07c2c Add branch method to contexts
Matt Mackall <mpm@selenic.com>
parents: 3352
diff changeset
   266
    def branch(self): return self._changectx.branch()
3144
8342ad5abe0b Make filectx lazier - some users never use filenode
Brendan Cully <brendan@kublai.com>
parents: 3143
diff changeset
   267
    def manifest(self): return self._changectx.manifest()
3149
ff1ab08e6732 restore filectx.changectx() method
Matt Mackall <mpm@selenic.com>
parents: 3146
diff changeset
   268
    def changectx(self): return self._changectx
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   269
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   270
    def data(self): return self._filelog.read(self._filenode)
3122
da85145d4571 filectx: add rename traversal for parents()
Matt Mackall <mpm@selenic.com>
parents: 2859
diff changeset
   271
    def path(self): return self._path
3302
192085505f6f filectx: add size method
Matt Mackall <mpm@selenic.com>
parents: 3298
diff changeset
   272
    def size(self): return self._filelog.size(self._filerev)
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   273
3310
0e370798eebf context: add cmp for filectxs
Matt Mackall <mpm@selenic.com>
parents: 3302
diff changeset
   274
    def cmp(self, text): return self._filelog.cmp(self._filenode, text)
0e370798eebf context: add cmp for filectxs
Matt Mackall <mpm@selenic.com>
parents: 3302
diff changeset
   275
5811
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   276
    def renamed(self):
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   277
        """check if file was actually renamed in this changeset revision
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   278
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   279
        If rename logged in file revision, we report copy for changeset only
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   280
        if file revisions linkrev points back to the changeset in question
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   281
        or both changeset parents contain different file revisions.
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   282
        """
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   283
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   284
        renamed = self._filelog.renamed(self._filenode)
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   285
        if not renamed:
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   286
            return renamed
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   287
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   288
        if self.rev() == self.linkrev():
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   289
            return renamed
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   290
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   291
        name = self.path()
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   292
        fnode = self._filenode
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   293
        for p in self._changectx.parents():
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   294
            try:
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   295
                if fnode == p.filenode(name):
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   296
                    return None
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   297
            except revlog.LookupError:
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   298
                pass
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   299
        return renamed
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   300
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   301
    def parents(self):
3124
4d021b91cb26 filectx: allow passing filelog in init to avoid opening new filelogs
Matt Mackall <mpm@selenic.com>
parents: 3123
diff changeset
   302
        p = self._path
4d021b91cb26 filectx: allow passing filelog in init to avoid opening new filelogs
Matt Mackall <mpm@selenic.com>
parents: 3123
diff changeset
   303
        fl = self._filelog
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   304
        pl = [(p, n, fl) for n in self._filelog.parents(self._filenode)]
3123
4ea58eb3f0c9 filelog: make metadata method private
Matt Mackall <mpm@selenic.com>
parents: 3122
diff changeset
   305
5813
3ef279074c77 context: fix filectx.parents() bug introduced when editing 180a3eee4b75
Patrick Mezard <pmezard@gmail.com>
parents: 5811
diff changeset
   306
        r = self._filelog.renamed(self._filenode)
3122
da85145d4571 filectx: add rename traversal for parents()
Matt Mackall <mpm@selenic.com>
parents: 2859
diff changeset
   307
        if r:
3124
4d021b91cb26 filectx: allow passing filelog in init to avoid opening new filelogs
Matt Mackall <mpm@selenic.com>
parents: 3123
diff changeset
   308
            pl[0] = (r[0], r[1], None)
4d021b91cb26 filectx: allow passing filelog in init to avoid opening new filelogs
Matt Mackall <mpm@selenic.com>
parents: 3123
diff changeset
   309
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   310
        return [filectx(self._repo, p, fileid=n, filelog=l)
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   311
                for p,n,l in pl if n != nullid]
2563
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   312
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   313
    def children(self):
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   314
        # hard for renames
482c524dd9ab Add context.py: changeset and file revision contexts
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   315
        c = self._filelog.children(self._filenode)
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   316
        return [filectx(self._repo, self._path, fileid=x,
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   317
                        filelog=self._filelog) for x in c]
2566
d8560b458f76 Convert hg annotate to context api
Matt Mackall <mpm@selenic.com>
parents: 2563
diff changeset
   318
4856
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   319
    def annotate(self, follow=False, linenumber=None):
3172
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   320
        '''returns a list of tuples of (ctx, line) for each line
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   321
        in the file, where ctx is the filectx of the node where
4856
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   322
        that line was last changed.
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   323
        This returns tuples of ((ctx, linenumber), line) for each line,
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   324
        if "linenumber" parameter is NOT "None".
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   325
        In such tuples, linenumber means one at the first appearance
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   326
        in the managed file.
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   327
        To reduce annotation cost,
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   328
        this returns fixed value(False is used) as linenumber,
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   329
        if "linenumber" parameter is "False".'''
3172
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   330
4856
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   331
        def decorate_compat(text, rev):
3172
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   332
            return ([rev] * len(text.splitlines()), text)
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   333
4856
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   334
        def without_linenumber(text, rev):
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   335
            return ([(rev, False)] * len(text.splitlines()), text)
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   336
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   337
        def with_linenumber(text, rev):
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   338
            size = len(text.splitlines())
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   339
            return ([(rev, i) for i in xrange(1, size + 1)], text)
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   340
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   341
        decorate = (((linenumber is None) and decorate_compat) or
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   342
                    (linenumber and with_linenumber) or
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   343
                    without_linenumber)
e45c5120ca27 Allow filectx.annotate to return the line number of first appearance.
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 4748
diff changeset
   344
3172
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   345
        def pair(parent, child):
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   346
            for a1, a2, b1, b2 in bdiff.blocks(parent[1], child[1]):
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   347
                child[0][b1:b2] = parent[0][a1:a2]
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   348
            return child
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   349
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   350
        getlog = util.cachefunc(lambda x: self._repo.file(x))
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   351
        def getctx(path, fileid):
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   352
            log = path == self._path and self._filelog or getlog(path)
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   353
            return filectx(self._repo, path, fileid=fileid, filelog=log)
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   354
        getctx = util.cachefunc(getctx)
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   355
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   356
        def parents(f):
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   357
            # we want to reuse filectx objects as much as possible
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   358
            p = f._path
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   359
            if f._filerev is None: # working dir
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   360
                pl = [(n.path(), n.filerev()) for n in f.parents()]
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   361
            else:
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   362
                pl = [(p, n) for n in f._filelog.parentrevs(f._filerev)]
3172
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   363
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   364
            if follow:
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   365
                r = f.renamed()
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   366
                if r:
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   367
                    pl[0] = (r[0], getlog(r[0]).rev(r[1]))
3146
e69a0cbe268e filectx.annotate: return filectx for each line instead of rev
Brendan Cully <brendan@kublai.com>
parents: 3144
diff changeset
   368
3578
3b4e00cba57a Define and use nullrev (revision of nullid) instead of -1.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3454
diff changeset
   369
            return [getctx(p, n) for p, n in pl if n != nullrev]
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   370
3404
1a437b0f4902 Fix annotate where linkrev != rev without exporting linkrev
Brendan Cully <brendan@kublai.com>
parents: 3403
diff changeset
   371
        # use linkrev to find the first changeset where self appeared
5811
180a3eee4b75 Fix copies reporting in log and convert.
Maxim Dounin <mdounin@mdounin.ru>
parents: 5810
diff changeset
   372
        if self.rev() != self.linkrev():
3404
1a437b0f4902 Fix annotate where linkrev != rev without exporting linkrev
Brendan Cully <brendan@kublai.com>
parents: 3403
diff changeset
   373
            base = self.filectx(self.filerev())
1a437b0f4902 Fix annotate where linkrev != rev without exporting linkrev
Brendan Cully <brendan@kublai.com>
parents: 3403
diff changeset
   374
        else:
1a437b0f4902 Fix annotate where linkrev != rev without exporting linkrev
Brendan Cully <brendan@kublai.com>
parents: 3403
diff changeset
   375
            base = self
1a437b0f4902 Fix annotate where linkrev != rev without exporting linkrev
Brendan Cully <brendan@kublai.com>
parents: 3403
diff changeset
   376
3172
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   377
        # find all ancestors
3404
1a437b0f4902 Fix annotate where linkrev != rev without exporting linkrev
Brendan Cully <brendan@kublai.com>
parents: 3403
diff changeset
   378
        needed = {base: 1}
1a437b0f4902 Fix annotate where linkrev != rev without exporting linkrev
Brendan Cully <brendan@kublai.com>
parents: 3403
diff changeset
   379
        visit = [base]
1a437b0f4902 Fix annotate where linkrev != rev without exporting linkrev
Brendan Cully <brendan@kublai.com>
parents: 3403
diff changeset
   380
        files = [base._path]
3172
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   381
        while visit:
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   382
            f = visit.pop(0)
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   383
            for p in parents(f):
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   384
                if p not in needed:
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   385
                    needed[p] = 1
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   386
                    visit.append(p)
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   387
                    if p._path not in files:
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   388
                        files.append(p._path)
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   389
                else:
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   390
                    # count how many times we'll use this
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   391
                    needed[p] += 1
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   392
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   393
        # sort by revision (per file) which is a topological order
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   394
        visit = []
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   395
        for f in files:
6762
f67d1468ac50 util: add sort helper
Matt Mackall <mpm@selenic.com>
parents: 6760
diff changeset
   396
            fn = [(n.rev(), n) for n in needed if n._path == f]
3172
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   397
            visit.extend(fn)
6762
f67d1468ac50 util: add sort helper
Matt Mackall <mpm@selenic.com>
parents: 6760
diff changeset
   398
3172
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   399
        hist = {}
6762
f67d1468ac50 util: add sort helper
Matt Mackall <mpm@selenic.com>
parents: 6760
diff changeset
   400
        for r, f in util.sort(visit):
3172
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   401
            curr = decorate(f.data(), f)
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   402
            for p in parents(f):
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   403
                if p != nullid:
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   404
                    curr = pair(hist[p], curr)
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   405
                    # trim the history of unneeded revs
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   406
                    needed[p] -= 1
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   407
                    if not needed[p]:
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   408
                        del hist[p]
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   409
            hist[f] = curr
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   410
5c93dd0ae413 Refactor annotate copy support.
Brendan Cully <brendan@kublai.com>
parents: 3152
diff changeset
   411
        return zip(hist[f][0], hist[f][1].splitlines(1))
3124
4d021b91cb26 filectx: allow passing filelog in init to avoid opening new filelogs
Matt Mackall <mpm@selenic.com>
parents: 3123
diff changeset
   412
3126
4bf2e895cf86 filectx: add rename-aware ancestor algorithm
Matt Mackall <mpm@selenic.com>
parents: 3125
diff changeset
   413
    def ancestor(self, fc2):
4bf2e895cf86 filectx: add rename-aware ancestor algorithm
Matt Mackall <mpm@selenic.com>
parents: 3125
diff changeset
   414
        """
4bf2e895cf86 filectx: add rename-aware ancestor algorithm
Matt Mackall <mpm@selenic.com>
parents: 3125
diff changeset
   415
        find the common ancestor file context, if any, of self, and fc2
4bf2e895cf86 filectx: add rename-aware ancestor algorithm
Matt Mackall <mpm@selenic.com>
parents: 3125
diff changeset
   416
        """
4bf2e895cf86 filectx: add rename-aware ancestor algorithm
Matt Mackall <mpm@selenic.com>
parents: 3125
diff changeset
   417
3135
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   418
        acache = {}
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   419
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   420
        # prime the ancestor cache for the working directory
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   421
        for c in (self, fc2):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   422
            if c._filerev == None:
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   423
                pl = [(n.path(), n.filenode()) for n in c.parents()]
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   424
                acache[(c._path, None)] = pl
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   425
6286
90a4329a6b4a filectx.ancestor: use fctx._repopath to cache filelogs (issue1035)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 6228
diff changeset
   426
        flcache = {self._repopath:self._filelog, fc2._repopath:fc2._filelog}
3135
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   427
        def parents(vertex):
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   428
            if vertex in acache:
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   429
                return acache[vertex]
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   430
            f, n = vertex
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   431
            if f not in flcache:
3126
4bf2e895cf86 filectx: add rename-aware ancestor algorithm
Matt Mackall <mpm@selenic.com>
parents: 3125
diff changeset
   432
                flcache[f] = self._repo.file(f)
3135
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   433
            fl = flcache[f]
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   434
            pl = [(f, p) for p in fl.parents(n) if p != nullid]
3135
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   435
            re = fl.renamed(n)
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   436
            if re:
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   437
                pl.append(re)
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   438
            acache[vertex] = pl
3135
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   439
            return pl
3126
4bf2e895cf86 filectx: add rename-aware ancestor algorithm
Matt Mackall <mpm@selenic.com>
parents: 3125
diff changeset
   440
3135
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   441
        a, b = (self._path, self._filenode), (fc2._path, fc2._filenode)
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   442
        v = ancestor.ancestor(a, b, parents)
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   443
        if v:
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   444
            f, n = v
3135
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   445
            return filectx(self._repo, f, fileid=n, filelog=flcache[f])
3126
4bf2e895cf86 filectx: add rename-aware ancestor algorithm
Matt Mackall <mpm@selenic.com>
parents: 3125
diff changeset
   446
3135
b1db258e875c Abstract ancestor algorithm into generic function
Matt Mackall <mpm@selenic.com>
parents: 3134
diff changeset
   447
        return None
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   448
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   449
class workingctx(changectx):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   450
    """A workingctx object makes access to data related to
6705
fec6bc978843 context: let workingctx parents be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6685
diff changeset
   451
    the current working directory convenient.
fec6bc978843 context: let workingctx parents be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6685
diff changeset
   452
    parents - a pair of parent nodeids, or None to use the dirstate.
6709
f84f507c53d3 context: let workingctx.date(), .user() and description() be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6708
diff changeset
   453
    date - any valid date string or (unixtime, offset), or None.
f84f507c53d3 context: let workingctx.date(), .user() and description() be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6708
diff changeset
   454
    user - username string, or None.
6708
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   455
    extra - a dictionary of extra values, or None.
6707
02bad34230a2 localrepo: hide commit() file selection behind workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6705
diff changeset
   456
    changes - a list of file lists as returned by localrepo.status()
02bad34230a2 localrepo: hide commit() file selection behind workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6705
diff changeset
   457
               or None to use the repository status.
6705
fec6bc978843 context: let workingctx parents be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6685
diff changeset
   458
    """
6721
521c6c6f3b9b kill some trailing spaces
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6718
diff changeset
   459
    def __init__(self, repo, parents=None, text="", user=None, date=None,
6709
f84f507c53d3 context: let workingctx.date(), .user() and description() be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6708
diff changeset
   460
                 extra=None, changes=None):
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   461
        self._repo = repo
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   462
        self._rev = None
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   463
        self._node = None
6709
f84f507c53d3 context: let workingctx.date(), .user() and description() be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6708
diff changeset
   464
        self._text = text
6718
4386a7706828 Fix commit date (issue1193)
Christian Ebert <blacktrash@gmx.net>
parents: 6715
diff changeset
   465
        if date:
6709
f84f507c53d3 context: let workingctx.date(), .user() and description() be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6708
diff changeset
   466
            self._date = util.parsedate(date)
6817
cf319797d61c minor status fixups
Matt Mackall <mpm@selenic.com>
parents: 6809
diff changeset
   467
        if user:
cf319797d61c minor status fixups
Matt Mackall <mpm@selenic.com>
parents: 6809
diff changeset
   468
            self._user = user
6705
fec6bc978843 context: let workingctx parents be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6685
diff changeset
   469
        if parents:
6772
84b53eef9964 context: simplify parents code
Matt Mackall <mpm@selenic.com>
parents: 6771
diff changeset
   470
            self._parents = [changectx(self._repo, p) for p in parents]
6707
02bad34230a2 localrepo: hide commit() file selection behind workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6705
diff changeset
   471
        if changes:
02bad34230a2 localrepo: hide commit() file selection behind workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6705
diff changeset
   472
            self._status = list(changes)
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   473
6708
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   474
        self._extra = {}
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   475
        if extra:
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   476
            self._extra = extra.copy()
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   477
        if 'branch' not in self._extra:
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   478
            branch = self._repo.dirstate.branch()
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   479
            try:
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   480
                branch = branch.decode('UTF-8').encode('UTF-8')
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   481
            except UnicodeDecodeError:
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   482
                raise util.Abort(_('branch name not in UTF-8!'))
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   483
            self._extra['branch'] = branch
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   484
        if self._extra['branch'] == '':
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   485
            self._extra['branch'] = 'default'
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   486
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   487
    def __str__(self):
3313
6c68bc1e7873 context: change workingctx str() from . to <node>+
Matt Mackall <mpm@selenic.com>
parents: 3310
diff changeset
   488
        return str(self._parents[0]) + "+"
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   489
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   490
    def __nonzero__(self):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   491
        return True
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   492
6771
f5d7cfcbb4d3 workingctx: add __contains__ method
Matt Mackall <mpm@selenic.com>
parents: 6764
diff changeset
   493
    def __contains__(self, key):
6845
835a01a0cdb3 context: fix workingctx's __contains__ method
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6829
diff changeset
   494
        return self._dirstate[key] not in "?r"
6771
f5d7cfcbb4d3 workingctx: add __contains__ method
Matt Mackall <mpm@selenic.com>
parents: 6764
diff changeset
   495
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   496
    def __getattr__(self, name):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   497
        if name == '_status':
6760
4faaa0535ea7 status: clean up all users for unknown files
Matt Mackall <mpm@selenic.com>
parents: 6747
diff changeset
   498
            self._status = self._repo.status(unknown=True)
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   499
            return self._status
6817
cf319797d61c minor status fixups
Matt Mackall <mpm@selenic.com>
parents: 6809
diff changeset
   500
        elif name == '_user':
cf319797d61c minor status fixups
Matt Mackall <mpm@selenic.com>
parents: 6809
diff changeset
   501
            self._user = self._repo.ui.username()
cf319797d61c minor status fixups
Matt Mackall <mpm@selenic.com>
parents: 6809
diff changeset
   502
            return self._user
cf319797d61c minor status fixups
Matt Mackall <mpm@selenic.com>
parents: 6809
diff changeset
   503
        elif name == '_date':
cf319797d61c minor status fixups
Matt Mackall <mpm@selenic.com>
parents: 6809
diff changeset
   504
            self._date = util.makedate()
cf319797d61c minor status fixups
Matt Mackall <mpm@selenic.com>
parents: 6809
diff changeset
   505
            return self._date
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   506
        if name == '_manifest':
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   507
            self._buildmanifest()
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   508
            return self._manifest
6742
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
   509
        elif name == '_parents':
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
   510
            p = self._repo.dirstate.parents()
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
   511
            if p[1] == nullid:
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
   512
                p = p[:-1]
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
   513
            self._parents = [changectx(self._repo, x) for x in p]
2d54e7c1e69d context: clean up parents()
Matt Mackall <mpm@selenic.com>
parents: 6741
diff changeset
   514
            return self._parents
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   515
        else:
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   516
            raise AttributeError, name
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   517
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   518
    def _buildmanifest(self):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   519
        """generate a manifest corresponding to the working directory"""
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   520
3218
8d4855fd9d7b merge: use new working context object in update
Matt Mackall <mpm@selenic.com>
parents: 3217
diff changeset
   521
        man = self._parents[0].manifest().copy()
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   522
        copied = self._repo.dirstate.copies()
6743
86e8187b721a simplify flag handling
Matt Mackall <mpm@selenic.com>
parents: 6742
diff changeset
   523
        cf = lambda x: man.flags(copied.get(x, x))
86e8187b721a simplify flag handling
Matt Mackall <mpm@selenic.com>
parents: 6742
diff changeset
   524
        ff = self._repo.dirstate.flagfunc(cf)
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   525
        modified, added, removed, deleted, unknown = self._status[:5]
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   526
        for i, l in (("a", added), ("m", modified), ("u", unknown)):
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   527
            for f in l:
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   528
                man[f] = man.get(copied.get(f, f), nullid) + i
3823
676b75547d13 context: don't spuriously raise abort when a file goes missing.
Matt Mackall <mpm@selenic.com>
parents: 3715
diff changeset
   529
                try:
6743
86e8187b721a simplify flag handling
Matt Mackall <mpm@selenic.com>
parents: 6742
diff changeset
   530
                    man.set(f, ff(f))
3823
676b75547d13 context: don't spuriously raise abort when a file goes missing.
Matt Mackall <mpm@selenic.com>
parents: 3715
diff changeset
   531
                except OSError:
676b75547d13 context: don't spuriously raise abort when a file goes missing.
Matt Mackall <mpm@selenic.com>
parents: 3715
diff changeset
   532
                    pass
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   533
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   534
        for f in deleted + removed:
3325
50a18815e3f0 Revert changeset c67920d78248.
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents: 3313
diff changeset
   535
            if f in man:
50a18815e3f0 Revert changeset c67920d78248.
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents: 3313
diff changeset
   536
                del man[f]
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   537
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   538
        self._manifest = man
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   539
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   540
    def manifest(self): return self._manifest
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   541
6809
89ec85aa6cc3 context: trigger missing username warning only when necessary
Patrick Mezard <pmezard@gmail.com>
parents: 6772
diff changeset
   542
    def user(self): return self._user or self._repo.ui.username()
6709
f84f507c53d3 context: let workingctx.date(), .user() and description() be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6708
diff changeset
   543
    def date(self): return self._date
f84f507c53d3 context: let workingctx.date(), .user() and description() be overriden
Patrick Mezard <pmezard@gmail.com>
parents: 6708
diff changeset
   544
    def description(self): return self._text
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   545
    def files(self):
6762
f67d1468ac50 util: add sort helper
Matt Mackall <mpm@selenic.com>
parents: 6760
diff changeset
   546
        return util.sort(self._status[0] + self._status[1] + self._status[2])
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   547
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   548
    def modified(self): return self._status[0]
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   549
    def added(self): return self._status[1]
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   550
    def removed(self): return self._status[2]
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   551
    def deleted(self): return self._status[3]
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   552
    def unknown(self): return self._status[4]
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   553
    def clean(self): return self._status[5]
6708
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   554
    def branch(self): return self._extra['branch']
7566f00a3979 localrepo: let commit() get extra data from workingctx
Patrick Mezard <pmezard@gmail.com>
parents: 6707
diff changeset
   555
    def extra(self): return self._extra
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   556
4663
6b2e8cb39583 context: add tags() method
Matt Mackall <mpm@selenic.com>
parents: 4640
diff changeset
   557
    def tags(self):
6b2e8cb39583 context: add tags() method
Matt Mackall <mpm@selenic.com>
parents: 4640
diff changeset
   558
        t = []
6b2e8cb39583 context: add tags() method
Matt Mackall <mpm@selenic.com>
parents: 4640
diff changeset
   559
        [t.extend(p.tags()) for p in self.parents()]
6b2e8cb39583 context: add tags() method
Matt Mackall <mpm@selenic.com>
parents: 4640
diff changeset
   560
        return t
6b2e8cb39583 context: add tags() method
Matt Mackall <mpm@selenic.com>
parents: 4640
diff changeset
   561
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   562
    def children(self):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   563
        return []
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   564
6743
86e8187b721a simplify flag handling
Matt Mackall <mpm@selenic.com>
parents: 6742
diff changeset
   565
    def flags(self, path):
5389
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   566
        if '_manifest' in self.__dict__:
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   567
            try:
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   568
                return self._manifest.flags(path)
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   569
            except KeyError:
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   570
                return ''
5760
0145f9afb0e7 Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5558
diff changeset
   571
5389
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   572
        pnode = self._parents[0].changeset()[0]
5407
d7e7902bb190 Fix workingctx exec/link bit of copies on non-supporting systems
Patrick Mezard <pmezard@gmail.com>
parents: 5399
diff changeset
   573
        orig = self._repo.dirstate.copies().get(path, path)
d7e7902bb190 Fix workingctx exec/link bit of copies on non-supporting systems
Patrick Mezard <pmezard@gmail.com>
parents: 5399
diff changeset
   574
        node, flag = self._repo.manifest.find(pnode, orig)
5389
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   575
        try:
6743
86e8187b721a simplify flag handling
Matt Mackall <mpm@selenic.com>
parents: 6742
diff changeset
   576
            ff = self._repo.dirstate.flagfunc(lambda x: flag or '')
86e8187b721a simplify flag handling
Matt Mackall <mpm@selenic.com>
parents: 6742
diff changeset
   577
            return ff(path)
5389
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   578
        except OSError:
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   579
            pass
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   580
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   581
        if not node or path in self.deleted() or path in self.removed():
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   582
            return ''
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   583
        return flag
26c060922085 context: add fileflags() to avoid rebuilding manifests
Patrick Mezard <pmezard@gmail.com>
parents: 4909
diff changeset
   584
3966
b4eaa68dea1b context: create a filectxt with filelog reuse
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3965
diff changeset
   585
    def filectx(self, path, filelog=None):
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   586
        """get a file context from the working directory"""
3966
b4eaa68dea1b context: create a filectxt with filelog reuse
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3965
diff changeset
   587
        return workingfilectx(self._repo, path, workingctx=self,
b4eaa68dea1b context: create a filectxt with filelog reuse
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3965
diff changeset
   588
                              filelog=filelog)
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   589
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   590
    def ancestor(self, c2):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   591
        """return the ancestor context of self and c2"""
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   592
        return self._parents[0].ancestor(c2) # punt on two parents for now
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   593
6764
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   594
    def walk(self, match):
6829
fec1da46006e dirstate.walk: build a dict rather than yield
Matt Mackall <mpm@selenic.com>
parents: 6827
diff changeset
   595
        return util.sort(self._repo.dirstate.walk(match, True, False).keys())
6764
8db64464d136 context: add walk method
Matt Mackall <mpm@selenic.com>
parents: 6763
diff changeset
   596
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   597
class workingfilectx(filectx):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   598
    """A workingfilectx object makes access to data related to a particular
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   599
       file in the working directory convenient."""
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   600
    def __init__(self, repo, path, filelog=None, workingctx=None):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   601
        """changeid can be a changeset revision, node, or tag.
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   602
           fileid can be a file revision or node."""
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   603
        self._repo = repo
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   604
        self._path = path
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   605
        self._changeid = None
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   606
        self._filerev = self._filenode = None
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   607
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   608
        if filelog:
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   609
            self._filelog = filelog
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   610
        if workingctx:
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   611
            self._changectx = workingctx
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   612
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   613
    def __getattr__(self, name):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   614
        if name == '_changectx':
4414
b6146466b92a context: fix workingfilectx._changectx
Matt Mackall <mpm@selenic.com>
parents: 4179
diff changeset
   615
            self._changectx = workingctx(self._repo)
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   616
            return self._changectx
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   617
        elif name == '_repopath':
3298
45f0c49f0449 fix workingfilectx parents and ancestor functions
Matt Mackall <mpm@selenic.com>
parents: 3242
diff changeset
   618
            self._repopath = (self._repo.dirstate.copied(self._path)
45f0c49f0449 fix workingfilectx parents and ancestor functions
Matt Mackall <mpm@selenic.com>
parents: 3242
diff changeset
   619
                              or self._path)
45f0c49f0449 fix workingfilectx parents and ancestor functions
Matt Mackall <mpm@selenic.com>
parents: 3242
diff changeset
   620
            return self._repopath
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   621
        elif name == '_filelog':
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   622
            self._filelog = self._repo.file(self._repopath)
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   623
            return self._filelog
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   624
        else:
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   625
            raise AttributeError, name
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   626
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   627
    def __nonzero__(self):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   628
        return True
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   629
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   630
    def __str__(self):
3313
6c68bc1e7873 context: change workingctx str() from . to <node>+
Matt Mackall <mpm@selenic.com>
parents: 3310
diff changeset
   631
        return "%s@%s" % (self.path(), self._changectx)
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   632
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   633
    def filectx(self, fileid):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   634
        '''opens an arbitrary revision of the file without
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   635
        opening a new filelog'''
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   636
        return filectx(self._repo, self._repopath, fileid=fileid,
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   637
                       filelog=self._filelog)
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   638
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   639
    def rev(self):
3336
e44eadc92ec4 context: check self.__dict__ instead of using hasattr
Brendan Cully <brendan@kublai.com>
parents: 3313
diff changeset
   640
        if '_changectx' in self.__dict__:
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   641
            return self._changectx.rev()
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   642
        return self._filelog.linkrev(self._filenode)
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   643
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   644
    def data(self): return self._repo.wread(self._path)
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   645
    def renamed(self):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   646
        rp = self._repopath
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   647
        if rp == self._path:
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   648
            return None
3965
2e5161335e65 context: fix a bug in workingfilectx.renamed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3964
diff changeset
   649
        return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   650
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   651
    def parents(self):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   652
        '''return parent filectxs, following copies if necessary'''
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   653
        p = self._path
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   654
        rp = self._repopath
3298
45f0c49f0449 fix workingfilectx parents and ancestor functions
Matt Mackall <mpm@selenic.com>
parents: 3242
diff changeset
   655
        pcl = self._changectx._parents
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   656
        fl = self._filelog
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   657
        pl = [(rp, pcl[0]._manifest.get(rp, nullid), fl)]
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   658
        if len(pcl) > 1:
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   659
            if rp != p:
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   660
                fl = None
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   661
            pl.append((p, pcl[1]._manifest.get(p, nullid), fl))
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   662
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   663
        return [filectx(self._repo, p, fileid=n, filelog=l)
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3578
diff changeset
   664
                for p,n,l in pl if n != nullid]
3217
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   665
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   666
    def children(self):
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   667
        return []
6d98149d70fe contexts: add working dir and working file contexts
Matt Mackall <mpm@selenic.com>
parents: 3216
diff changeset
   668
3302
192085505f6f filectx: add size method
Matt Mackall <mpm@selenic.com>
parents: 3298
diff changeset
   669
    def size(self): return os.stat(self._repo.wjoin(self._path)).st_size
3962
2b8825c94c5a add date attribute to workingfilectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3961
diff changeset
   670
    def date(self):
2b8825c94c5a add date attribute to workingfilectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3961
diff changeset
   671
        t, tz = self._changectx.date()
2b8825c94c5a add date attribute to workingfilectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3961
diff changeset
   672
        try:
4117
eb0967c6e77b Use only integer part of mtime in workingfilectx.date(), fixes test-context.py
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4110
diff changeset
   673
            return (int(os.lstat(self._repo.wjoin(self._path)).st_mtime), tz)
3962
2b8825c94c5a add date attribute to workingfilectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3961
diff changeset
   674
        except OSError, err:
2b8825c94c5a add date attribute to workingfilectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3961
diff changeset
   675
            if err.errno != errno.ENOENT: raise
2b8825c94c5a add date attribute to workingfilectx
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3961
diff changeset
   676
            return (t, tz)
3310
0e370798eebf context: add cmp for filectxs
Matt Mackall <mpm@selenic.com>
parents: 3302
diff changeset
   677
0e370798eebf context: add cmp for filectxs
Matt Mackall <mpm@selenic.com>
parents: 3302
diff changeset
   678
    def cmp(self, text): return self._repo.wread(self._path) == text
6715
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   679
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   680
class memctx(object):
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   681
    """A memctx is a subset of changectx supposed to be built on memory
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   682
    and passed to commit functions.
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   683
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   684
    NOTE: this interface and the related memfilectx are experimental and
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   685
    may change without notice.
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   686
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   687
    parents - a pair of parent nodeids.
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   688
    filectxfn - a callable taking (repo, memctx, path) arguments and
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   689
    returning a memctx object.
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   690
    date - any valid date string or (unixtime, offset), or None.
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   691
    user - username string, or None.
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   692
    extra - a dictionary of extra values, or None.
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   693
    """
6721
521c6c6f3b9b kill some trailing spaces
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6718
diff changeset
   694
    def __init__(self, repo, parents, text, files, filectxfn, user=None,
6715
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   695
                 date=None, extra=None):
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   696
        self._repo = repo
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   697
        self._rev = None
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   698
        self._node = None
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   699
        self._text = text
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   700
        self._date = date and util.parsedate(date) or util.makedate()
6809
89ec85aa6cc3 context: trigger missing username warning only when necessary
Patrick Mezard <pmezard@gmail.com>
parents: 6772
diff changeset
   701
        self._user = user
6715
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   702
        parents = [(p or nullid) for p in parents]
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   703
        p1, p2 = parents
6747
f6c00b17387c use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents: 6744
diff changeset
   704
        self._parents = [changectx(self._repo, p) for p in (p1, p2)]
6762
f67d1468ac50 util: add sort helper
Matt Mackall <mpm@selenic.com>
parents: 6760
diff changeset
   705
        files = util.sort(list(files))
6715
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   706
        self._status = [files, [], [], [], []]
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   707
        self._filectxfn = filectxfn
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   708
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   709
        self._extra = extra and extra.copy() or {}
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   710
        if 'branch' not in self._extra:
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   711
            self._extra['branch'] = 'default'
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   712
        elif self._extra.get('branch') == '':
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   713
            self._extra['branch'] = 'default'
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   714
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   715
    def __str__(self):
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   716
        return str(self._parents[0]) + "+"
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   717
6763
403682f1c678 context: add __int__ and hex methods
Matt Mackall <mpm@selenic.com>
parents: 6762
diff changeset
   718
    def __int__(self):
403682f1c678 context: add __int__ and hex methods
Matt Mackall <mpm@selenic.com>
parents: 6762
diff changeset
   719
        return self._rev
403682f1c678 context: add __int__ and hex methods
Matt Mackall <mpm@selenic.com>
parents: 6762
diff changeset
   720
6715
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   721
    def __nonzero__(self):
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   722
        return True
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   723
6809
89ec85aa6cc3 context: trigger missing username warning only when necessary
Patrick Mezard <pmezard@gmail.com>
parents: 6772
diff changeset
   724
    def user(self): return self._user or self._repo.ui.username()
6715
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   725
    def date(self): return self._date
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   726
    def description(self): return self._text
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   727
    def files(self): return self.modified()
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   728
    def modified(self): return self._status[0]
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   729
    def added(self): return self._status[1]
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   730
    def removed(self): return self._status[2]
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   731
    def deleted(self): return self._status[3]
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   732
    def unknown(self): return self._status[4]
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   733
    def clean(self): return self._status[5]
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   734
    def branch(self): return self._extra['branch']
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   735
    def extra(self): return self._extra
6743
86e8187b721a simplify flag handling
Matt Mackall <mpm@selenic.com>
parents: 6742
diff changeset
   736
    def flags(self, f): return self[f].flags()
6715
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   737
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   738
    def parents(self):
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   739
        """return contexts for each parent changeset"""
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   740
        return self._parents
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   741
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   742
    def filectx(self, path, filelog=None):
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   743
        """get a file context from the working directory"""
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   744
        return self._filectxfn(self._repo, self, path)
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   745
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   746
class memfilectx(object):
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   747
    """A memfilectx is a subset of filectx supposed to be built by client
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   748
    code and passed to commit functions.
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   749
    """
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   750
    def __init__(self, path, data, islink, isexec, copied):
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   751
        """copied is the source file path, or None."""
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   752
        self._path = path
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   753
        self._data = data
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   754
        self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   755
        self._copied = None
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   756
        if copied:
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   757
            self._copied = (copied, nullid)
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   758
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   759
    def __nonzero__(self): return True
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   760
    def __str__(self): return "%s@%s" % (self.path(), self._changectx)
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   761
    def path(self): return self._path
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   762
    def data(self): return self._data
6743
86e8187b721a simplify flag handling
Matt Mackall <mpm@selenic.com>
parents: 6742
diff changeset
   763
    def flags(self): return self._flags
6715
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   764
    def isexec(self): return 'x' in self._flags
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   765
    def islink(self): return 'l' in self._flags
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   766
    def renamed(self): return self._copied
a3c41abfa828 context: add memctx for memory commits
Patrick Mezard <pmezard@gmail.com>
parents: 6709
diff changeset
   767