comparison hgext/shelve.py @ 30554:1775975dd439

shelve: move patch extension to a string constant We are using 'name + ".patch"' pattern throughout the shelve code to identify the existence of a shelve with a particular name. In two cases however we use 'name + ".hg"' instead. This commit makes 'patch' be used in all places and "emphasizes" it by moving 'patch' to live in a constant. Also, this allows to extract file name without extension like this: f[:-(1 + len(patchextension))] instead of: f[:-6] which is good IMO. This is a first patch from this initial "obsshelve" series. This series does not include tests, although locally I have all of test-shelve.t ported to test obs-shelve as well. I will send tests later as a separate series.
author Kostia Balytskyi <ikostia@fb.com>
date Tue, 29 Nov 2016 07:20:03 -0800
parents 64b55bffc1c0
children 64a75655b988
comparison
equal deleted inserted replaced
30553:98033f9c0109 30554:1775975dd439
61 testedwith = 'ships-with-hg-core' 61 testedwith = 'ships-with-hg-core'
62 62
63 backupdir = 'shelve-backup' 63 backupdir = 'shelve-backup'
64 shelvedir = 'shelved' 64 shelvedir = 'shelved'
65 shelvefileextensions = ['hg', 'patch'] 65 shelvefileextensions = ['hg', 'patch']
66 # universal extension is present in all types of shelves
67 patchextension = 'patch'
66 68
67 # we never need the user, so we use a 69 # we never need the user, so we use a
68 # generic user for all shelve operations 70 # generic user for all shelve operations
69 shelveuser = 'shelve@localhost' 71 shelveuser = 'shelve@localhost'
70 72
218 util.unlinkpath(repo.join(cls._filename), ignoremissing=True) 220 util.unlinkpath(repo.join(cls._filename), ignoremissing=True)
219 221
220 def cleanupoldbackups(repo): 222 def cleanupoldbackups(repo):
221 vfs = scmutil.vfs(repo.join(backupdir)) 223 vfs = scmutil.vfs(repo.join(backupdir))
222 maxbackups = repo.ui.configint('shelve', 'maxbackups', 10) 224 maxbackups = repo.ui.configint('shelve', 'maxbackups', 10)
223 hgfiles = [f for f in vfs.listdir() if f.endswith('.hg')] 225 hgfiles = [f for f in vfs.listdir()
226 if f.endswith('.' + patchextension)]
224 hgfiles = sorted([(vfs.stat(f).st_mtime, f) for f in hgfiles]) 227 hgfiles = sorted([(vfs.stat(f).st_mtime, f) for f in hgfiles])
225 if 0 < maxbackups and maxbackups < len(hgfiles): 228 if 0 < maxbackups and maxbackups < len(hgfiles):
226 bordermtime = hgfiles[-maxbackups][0] 229 bordermtime = hgfiles[-maxbackups][0]
227 else: 230 else:
228 bordermtime = None 231 bordermtime = None
229 for mtime, f in hgfiles[:len(hgfiles) - maxbackups]: 232 for mtime, f in hgfiles[:len(hgfiles) - maxbackups]:
230 if mtime == bordermtime: 233 if mtime == bordermtime:
231 # keep it, because timestamp can't decide exact order of backups 234 # keep it, because timestamp can't decide exact order of backups
232 continue 235 continue
233 base = f[:-3] 236 base = f[:-(1 + len(patchextension))]
234 for ext in shelvefileextensions: 237 for ext in shelvefileextensions:
235 try: 238 try:
236 vfs.unlink(base + '.' + ext) 239 vfs.unlink(base + '.' + ext)
237 except OSError as err: 240 except OSError as err:
238 if err.errno != errno.ENOENT: 241 if err.errno != errno.ENOENT:
262 label = repo._activebookmark or parent.branch() or 'default' 265 label = repo._activebookmark or parent.branch() or 'default'
263 # slashes aren't allowed in filenames, therefore we rename it 266 # slashes aren't allowed in filenames, therefore we rename it
264 label = label.replace('/', '_') 267 label = label.replace('/', '_')
265 268
266 if name: 269 if name:
267 if shelvedfile(repo, name, 'hg').exists(): 270 if shelvedfile(repo, name, patchextension).exists():
268 e = _("a shelved change named '%s' already exists") % name 271 e = _("a shelved change named '%s' already exists") % name
269 raise error.Abort(e) 272 raise error.Abort(e)
270 else: 273 else:
271 for n in gennames(): 274 for n in gennames():
272 if not shelvedfile(repo, n, 'hg').exists(): 275 if not shelvedfile(repo, n, patchextension).exists():
273 name = n 276 name = n
274 break 277 break
275 else: 278 else:
276 raise error.Abort(_("too many shelved changes named '%s'") % label) 279 raise error.Abort(_("too many shelved changes named '%s'") % label)
277 280
335 338
336 def _shelvecreatedcommit(repo, node, name): 339 def _shelvecreatedcommit(repo, node, name):
337 bases = list(mutableancestors(repo[node])) 340 bases = list(mutableancestors(repo[node]))
338 shelvedfile(repo, name, 'hg').writebundle(bases, node) 341 shelvedfile(repo, name, 'hg').writebundle(bases, node)
339 cmdutil.export(repo, [node], 342 cmdutil.export(repo, [node],
340 fp=shelvedfile(repo, name, 'patch').opener('wb'), 343 fp=shelvedfile(repo, name, patchextension).opener('wb'),
341 opts=mdiff.diffopts(git=True)) 344 opts=mdiff.diffopts(git=True))
342 345
343 def _includeunknownfiles(repo, pats, opts, extra): 346 def _includeunknownfiles(repo, pats, opts, extra):
344 s = repo.status(match=scmutil.match(repo[None], pats, opts), 347 s = repo.status(match=scmutil.match(repo[None], pats, opts),
345 unknown=True) 348 unknown=True)
442 # patch file is necessary, as it should 445 # patch file is necessary, as it should
443 # be present for any kind of shelve, 446 # be present for any kind of shelve,
444 # but the .hg file is optional as in future we 447 # but the .hg file is optional as in future we
445 # will add obsolete shelve with does not create a 448 # will add obsolete shelve with does not create a
446 # bundle 449 # bundle
447 if shfile.exists() or suffix == 'patch': 450 if shfile.exists() or suffix == patchextension:
448 shfile.movetobackup() 451 shfile.movetobackup()
449 cleanupoldbackups(repo) 452 cleanupoldbackups(repo)
450 except OSError as err: 453 except OSError as err:
451 if err.errno != errno.ENOENT: 454 if err.errno != errno.ENOENT:
452 raise 455 raise
461 raise 464 raise
462 return [] 465 return []
463 info = [] 466 info = []
464 for (name, _type) in names: 467 for (name, _type) in names:
465 pfx, sfx = name.rsplit('.', 1) 468 pfx, sfx = name.rsplit('.', 1)
466 if not pfx or sfx != 'patch': 469 if not pfx or sfx != patchextension:
467 continue 470 continue
468 st = shelvedfile(repo, name).stat() 471 st = shelvedfile(repo, name).stat()
469 info.append((st.st_mtime, shelvedfile(repo, pfx).filename())) 472 info.append((st.st_mtime, shelvedfile(repo, pfx).filename()))
470 return sorted(info, reverse=True) 473 return sorted(info, reverse=True)
471 474
489 used = 16 492 used = 16
490 age = '(%s)' % templatefilters.age(util.makedate(mtime), abbrev=True) 493 age = '(%s)' % templatefilters.age(util.makedate(mtime), abbrev=True)
491 ui.write(age, label='shelve.age') 494 ui.write(age, label='shelve.age')
492 ui.write(' ' * (12 - len(age))) 495 ui.write(' ' * (12 - len(age)))
493 used += 12 496 used += 12
494 with open(name + '.patch', 'rb') as fp: 497 with open(name + '.' + patchextension, 'rb') as fp:
495 while True: 498 while True:
496 line = fp.readline() 499 line = fp.readline()
497 if not line: 500 if not line:
498 break 501 break
499 if not line.startswith('#'): 502 if not line.startswith('#'):
517 """subcommand that displays a single shelf""" 520 """subcommand that displays a single shelf"""
518 if len(pats) != 1: 521 if len(pats) != 1:
519 raise error.Abort(_("--%s expects a single shelf") % subcommand) 522 raise error.Abort(_("--%s expects a single shelf") % subcommand)
520 shelfname = pats[0] 523 shelfname = pats[0]
521 524
522 if not shelvedfile(repo, shelfname, 'patch').exists(): 525 if not shelvedfile(repo, shelfname, patchextension).exists():
523 raise error.Abort(_("cannot find shelf %s") % shelfname) 526 raise error.Abort(_("cannot find shelf %s") % shelfname)
524 527
525 listcmd(ui, repo, pats, opts) 528 listcmd(ui, repo, pats, opts)
526 529
527 def checkparents(repo, state): 530 def checkparents(repo, state):
821 basename = util.split(shelved[0][1])[1] 824 basename = util.split(shelved[0][1])[1]
822 ui.status(_("unshelving change '%s'\n") % basename) 825 ui.status(_("unshelving change '%s'\n") % basename)
823 else: 826 else:
824 basename = shelved[0] 827 basename = shelved[0]
825 828
826 if not shelvedfile(repo, basename, 'patch').exists(): 829 if not shelvedfile(repo, basename, patchextension).exists():
827 raise error.Abort(_("shelved change '%s' not found") % basename) 830 raise error.Abort(_("shelved change '%s' not found") % basename)
828 831
829 oldquiet = ui.quiet 832 oldquiet = ui.quiet
830 lock = tr = None 833 lock = tr = None
831 forcemerge = ui.backupconfig('ui', 'forcemerge') 834 forcemerge = ui.backupconfig('ui', 'forcemerge')