Patchwork [01,of,10,shelve-ext] shelve: move patch extension to a string constant

login
register
mail settings
Submitter Kostia Balytskyi
Date Nov. 29, 2016, 3:22 p.m.
Message ID <f6f0ab3f7b0ea0e05cfd.1480432975@dev1902.lla1.facebook.com>
Download mbox | patch
Permalink /patch/17785/
State Accepted
Headers show

Comments

Kostia Balytskyi - Nov. 29, 2016, 3:22 p.m.
# HG changeset patch
# User Kostia Balytskyi <ikostia@fb.com>
# Date 1480432803 28800
#      Tue Nov 29 07:20:03 2016 -0800
# Node ID f6f0ab3f7b0ea0e05cfdcd7afd4994ea21988fd9
# Parent  64b55bffc1c059eb4c11ca195b561ca8a287f59e
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.
Yuya Nishihara - Dec. 2, 2016, 1:39 p.m.
On Tue, 29 Nov 2016 07:22:55 -0800, Kostia Balytskyi wrote:
> # HG changeset patch
> # User Kostia Balytskyi <ikostia@fb.com>
> # Date 1480432803 28800
> #      Tue Nov 29 07:20:03 2016 -0800
> # Node ID f6f0ab3f7b0ea0e05cfdcd7afd4994ea21988fd9
> # Parent  64b55bffc1c059eb4c11ca195b561ca8a287f59e
> shelve: move patch extension to a string constant

Queued this first patch, thanks.

Patch

diff --git a/hgext/shelve.py b/hgext/shelve.py
--- a/hgext/shelve.py
+++ b/hgext/shelve.py
@@ -63,6 +63,8 @@  testedwith = 'ships-with-hg-core'
 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 @@  class shelvedstate(object):
 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 @@  def cleanupoldbackups(repo):
         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 @@  def getshelvename(repo, parent, opts):
     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 @@  def _shelvecreatedcommit(repo, node, nam
     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 @@  def deletecmd(ui, repo, pats):
                     # 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 @@  def listshelves(repo):
     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 @@  def listcmd(ui, repo, pats, opts):
         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 @@  def singlepatchcmds(ui, repo, pats, opts
         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 @@  def _dounshelve(ui, repo, *shelved, **op
     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
diff --git a/tests/test-shelve.t b/tests/test-shelve.t
--- a/tests/test-shelve.t
+++ b/tests/test-shelve.t
@@ -195,12 +195,12 @@  apply it and make sure our state is as e
 (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'