hgext/shelve.py
changeset 19904 5b327880a660
parent 19887 dd7c294365f0
child 19908 07ee5c8867ca
equal deleted inserted replaced
19903:ca875b271ac3 19904:5b327880a660
    19 You can have more than one shelved change outstanding at a time; each
    19 You can have more than one shelved change outstanding at a time; each
    20 shelved change has a distinct name. For details, see the help for "hg
    20 shelved change has a distinct name. For details, see the help for "hg
    21 shelve".
    21 shelve".
    22 """
    22 """
    23 
    23 
    24 try:
       
    25     import cPickle as pickle
       
    26     pickle.dump # import now
       
    27 except ImportError:
       
    28     import pickle
       
    29 from mercurial.i18n import _
    24 from mercurial.i18n import _
    30 from mercurial.node import nullid
    25 from mercurial.node import nullid, bin, hex
    31 from mercurial import changegroup, cmdutil, scmutil, phases
    26 from mercurial import changegroup, cmdutil, scmutil, phases
    32 from mercurial import error, hg, mdiff, merge, patch, repair, util
    27 from mercurial import error, hg, mdiff, merge, patch, repair, util
    33 from mercurial import templatefilters
    28 from mercurial import templatefilters
    34 from mercurial import lock as lockmod
    29 from mercurial import lock as lockmod
    35 import errno
    30 import errno
    86     _filename = 'shelvedstate'
    81     _filename = 'shelvedstate'
    87 
    82 
    88     @classmethod
    83     @classmethod
    89     def load(cls, repo):
    84     def load(cls, repo):
    90         fp = repo.opener(cls._filename)
    85         fp = repo.opener(cls._filename)
    91         (version, name, parents, stripnodes) = pickle.load(fp)
    86         try:
    92 
    87             version = int(fp.readline().strip())
    93         if version != cls._version:
    88 
    94             raise util.Abort(_('this version of shelve is incompatible '
    89             if version != cls._version:
    95                                'with the version used in this repo'))
    90                 raise util.Abort(_('this version of shelve is incompatible '
       
    91                                    'with the version used in this repo'))
       
    92             name = fp.readline().strip()
       
    93             parents = [bin(h) for h in fp.readline().split()]
       
    94             stripnodes = [bin(h) for h in fp.readline().split()]
       
    95         finally:
       
    96             fp.close()
    96 
    97 
    97         obj = cls()
    98         obj = cls()
    98         obj.name = name
    99         obj.name = name
    99         obj.parents = parents
   100         obj.parents = parents
   100         obj.stripnodes = stripnodes
   101         obj.stripnodes = stripnodes
   102         return obj
   103         return obj
   103 
   104 
   104     @classmethod
   105     @classmethod
   105     def save(cls, repo, name, stripnodes):
   106     def save(cls, repo, name, stripnodes):
   106         fp = repo.opener(cls._filename, 'wb')
   107         fp = repo.opener(cls._filename, 'wb')
   107         pickle.dump((cls._version, name,
   108         fp.write('%i\n' % cls._version)
   108                      repo.dirstate.parents(),
   109         fp.write('%s\n' % name)
   109                      stripnodes), fp)
   110         fp.write('%s\n' % ' '.join([hex(p) for p in repo.dirstate.parents()]))
       
   111         fp.write('%s\n' % ' '.join([hex(n) for n in stripnodes]))
   110         fp.close()
   112         fp.close()
   111 
   113 
   112     @staticmethod
   114     @staticmethod
   113     def clear(repo):
   115     def clear(repo):
   114         util.unlinkpath(repo.join('shelvedstate'), ignoremissing=True)
   116         util.unlinkpath(repo.join('shelvedstate'), ignoremissing=True)