mercurial/shelve.py
changeset 49426 24ffd13893cc
parent 49425 35c9f0bc2648
child 49427 c4417029e6c2
equal deleted inserted replaced
49425:35c9f0bc2648 49426:24ffd13893cc
    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 import collections
    24 import collections
       
    25 import io
    25 import itertools
    26 import itertools
    26 import stat
    27 import stat
    27 
    28 
    28 from .i18n import _
    29 from .i18n import _
    29 from .node import (
    30 from .node import (
   181             fp.close()
   182             fp.close()
   182 
   183 
   183     def open_patch(self, mode=b'rb'):
   184     def open_patch(self, mode=b'rb'):
   184         return self.vfs(self.name + b'.patch', mode)
   185         return self.vfs(self.name + b'.patch', mode)
   185 
   186 
       
   187     def patch_from_node(self, repo, node):
       
   188         repo = repo.unfiltered()
       
   189         match = _optimized_match(repo, node)
       
   190         fp = io.BytesIO()
       
   191         cmdutil.exportfile(
       
   192             repo,
       
   193             [node],
       
   194             fp,
       
   195             opts=mdiff.diffopts(git=True),
       
   196             match=match,
       
   197         )
       
   198         fp.seek(0)
       
   199         return fp
       
   200 
       
   201     def load_patch(self, repo):
       
   202         try:
       
   203             # prefer node-based shelf
       
   204             return self.patch_from_node(repo, self.readinfo()[b'node'])
       
   205         except (FileNotFoundError, error.RepoLookupError):
       
   206             return self.open_patch()
       
   207 
   186     def _backupfilename(self, backupvfs, filename):
   208     def _backupfilename(self, backupvfs, filename):
   187         def gennames(base):
   209         def gennames(base):
   188             yield base
   210             yield base
   189             base, ext = base.rsplit(b'.', 1)
   211             base, ext = base.rsplit(b'.', 1)
   190             for i in itertools.count(1):
   212             for i in itertools.count(1):
   672         date = dateutil.makedate(mtime)
   694         date = dateutil.makedate(mtime)
   673         age = b'(%s)' % templatefilters.age(date, abbrev=True)
   695         age = b'(%s)' % templatefilters.age(date, abbrev=True)
   674         ui.write(age, label=b'shelve.age')
   696         ui.write(age, label=b'shelve.age')
   675         ui.write(b' ' * (12 - len(age)))
   697         ui.write(b' ' * (12 - len(age)))
   676         used += 12
   698         used += 12
   677         with shelf_dir.get(name).open_patch() as fp:
   699         with shelf_dir.get(name).load_patch(repo) as fp:
   678             while True:
   700             while True:
   679                 line = fp.readline()
   701                 line = fp.readline()
   680                 if not line:
   702                 if not line:
   681                     break
   703                     break
   682                 if not line.startswith(b'#'):
   704                 if not line.startswith(b'#'):