changeset 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 98033f9c0109
children 6a672c3b7860
files hgext/shelve.py tests/test-shelve.t
diffstat 2 files changed, 19 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/shelve.py	Thu Dec 01 15:55:45 2016 -0600
+++ b/hgext/shelve.py	Tue Nov 29 07:20:03 2016 -0800
@@ -63,6 +63,8 @@
 backupdir = 'shelve-backup'
 shelvedir = 'shelved'
 shelvefileextensions = ['hg', 'patch']
+# universal extension is present in all types of shelves
+patchextension = 'patch'
 
 # we never need the user, so we use a
 # generic user for all shelve operations
@@ -220,7 +222,8 @@
 def cleanupoldbackups(repo):
     vfs = scmutil.vfs(repo.join(backupdir))
     maxbackups = repo.ui.configint('shelve', 'maxbackups', 10)
-    hgfiles = [f for f in vfs.listdir() if f.endswith('.hg')]
+    hgfiles = [f for f in vfs.listdir()
+               if f.endswith('.' + patchextension)]
     hgfiles = sorted([(vfs.stat(f).st_mtime, f) for f in hgfiles])
     if 0 < maxbackups and maxbackups < len(hgfiles):
         bordermtime = hgfiles[-maxbackups][0]
@@ -230,7 +233,7 @@
         if mtime == bordermtime:
             # keep it, because timestamp can't decide exact order of backups
             continue
-        base = f[:-3]
+        base = f[:-(1 + len(patchextension))]
         for ext in shelvefileextensions:
             try:
                 vfs.unlink(base + '.' + ext)
@@ -264,12 +267,12 @@
     label = label.replace('/', '_')
 
     if name:
-        if shelvedfile(repo, name, 'hg').exists():
+        if shelvedfile(repo, name, patchextension).exists():
             e = _("a shelved change named '%s' already exists") % name
             raise error.Abort(e)
     else:
         for n in gennames():
-            if not shelvedfile(repo, n, 'hg').exists():
+            if not shelvedfile(repo, n, patchextension).exists():
                 name = n
                 break
         else:
@@ -337,7 +340,7 @@
     bases = list(mutableancestors(repo[node]))
     shelvedfile(repo, name, 'hg').writebundle(bases, node)
     cmdutil.export(repo, [node],
-                   fp=shelvedfile(repo, name, 'patch').opener('wb'),
+                   fp=shelvedfile(repo, name, patchextension).opener('wb'),
                    opts=mdiff.diffopts(git=True))
 
 def _includeunknownfiles(repo, pats, opts, extra):
@@ -444,7 +447,7 @@
                     # but the .hg file is optional as in future we
                     # will add obsolete shelve with does not create a
                     # bundle
-                    if shfile.exists() or suffix == 'patch':
+                    if shfile.exists() or suffix == patchextension:
                         shfile.movetobackup()
             cleanupoldbackups(repo)
         except OSError as err:
@@ -463,7 +466,7 @@
     info = []
     for (name, _type) in names:
         pfx, sfx = name.rsplit('.', 1)
-        if not pfx or sfx != 'patch':
+        if not pfx or sfx != patchextension:
             continue
         st = shelvedfile(repo, name).stat()
         info.append((st.st_mtime, shelvedfile(repo, pfx).filename()))
@@ -491,7 +494,7 @@
         ui.write(age, label='shelve.age')
         ui.write(' ' * (12 - len(age)))
         used += 12
-        with open(name + '.patch', 'rb') as fp:
+        with open(name + '.' + patchextension, 'rb') as fp:
             while True:
                 line = fp.readline()
                 if not line:
@@ -519,7 +522,7 @@
         raise error.Abort(_("--%s expects a single shelf") % subcommand)
     shelfname = pats[0]
 
-    if not shelvedfile(repo, shelfname, 'patch').exists():
+    if not shelvedfile(repo, shelfname, patchextension).exists():
         raise error.Abort(_("cannot find shelf %s") % shelfname)
 
     listcmd(ui, repo, pats, opts)
@@ -823,7 +826,7 @@
     else:
         basename = shelved[0]
 
-    if not shelvedfile(repo, basename, 'patch').exists():
+    if not shelvedfile(repo, basename, patchextension).exists():
         raise error.Abort(_("shelved change '%s' not found") % basename)
 
     oldquiet = ui.quiet
--- a/tests/test-shelve.t	Thu Dec 01 15:55:45 2016 -0600
+++ b/tests/test-shelve.t	Tue Nov 29 07:20:03 2016 -0800
@@ -195,12 +195,12 @@
 (this also tests that same timestamp prevents backups from being
 removed, even though there are more than 'maxbackups' backups)
 
-  $ f -t .hg/shelve-backup/default.hg
-  .hg/shelve-backup/default.hg: file
-  $ touch -t 200001010000 .hg/shelve-backup/default.hg
-  $ f -t .hg/shelve-backup/default-1.hg
-  .hg/shelve-backup/default-1.hg: file
-  $ touch -t 200001010000 .hg/shelve-backup/default-1.hg
+  $ f -t .hg/shelve-backup/default.patch
+  .hg/shelve-backup/default.patch: file
+  $ touch -t 200001010000 .hg/shelve-backup/default.patch
+  $ f -t .hg/shelve-backup/default-1.patch
+  .hg/shelve-backup/default-1.patch: file
+  $ touch -t 200001010000 .hg/shelve-backup/default-1.patch
 
   $ hg unshelve
   unshelving change 'default-01'