[PATCH 2 of 8 shelve-ext] shelve: move shelve name generation to a separate function

Kostia Balytskyi ikostia at fb.com
Wed Nov 9 09:50:51 EST 2016


On 11/9/16, 10:01 AM, "Durham Goode" <durham at fb.com> wrote:    
    On 11/8/16 1:51 PM, Kostia Balytskyi wrote:
    > # HG changeset patch
    > # User Kostia Balytskyi <ikostia at fb.com>
    > # Date 1478527237 28800
    > #      Mon Nov 07 06:00:37 2016 -0800
    > # Node ID 334f462a1c345e5fa453ea641281ef9713789718
    > # Parent  b2d851fac63c8e12605948c7d182f86974b5096c
    > shelve: move shelve name generation to a separate function
    >
    > diff --git a/hgext/shelve.py b/hgext/shelve.py
    > --- a/hgext/shelve.py
    > +++ b/hgext/shelve.py
    > @@ -243,6 +243,36 @@ def createcmd(ui, repo, pats, opts):
    >           cmdutil.checkunfinished(repo)
    >           return _docreatecmd(ui, repo, pats, opts)
    >   
    > +def getshelvename(repo, parent, opts):
    > +    """Decide on the name this shelve is going to have"""
    > +    def gennames():
    > +        yield label
    > +        for i in xrange(1, 100):
    > +            yield '%s-%02d' % (label, i)
    > +    name = opts.get('name')
    > +    label = repo._activebookmark or parent.branch() or 'default'
    > +    # slashes aren't allowed in filenames, therefore we rename it
    > +    label = label.replace('/', '_')
    > +
    > +    if name:
    > +        if shelvedfile(repo, name, 'hg').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():
    > +                name = n
    > +                break
    > +        else:
    > +            raise error.Abort(_("too many shelved changes named '%s'") % label)
    > +
    > +    # ensure we are not creating a subdirectory or a hidden file
    > +    if '/' in name or '\\' in name:
    > +        raise error.Abort(_('shelved change names may not contain slashes'))
    > +    if name.startswith('.'):
    > +        raise error.Abort(_("shelved change names may not start with '.'"))
    > +    return name
    > +
    >   def _docreatecmd(ui, repo, pats, opts):
    >       def mutableancestors(ctx):
    >           """return all mutable ancestors for ctx (included)
    > @@ -270,15 +300,6 @@ def _docreatecmd(ui, repo, pats, opts):
    >   
    >       # we never need the user, so we use a generic user for all shelve operations
    >       user = 'shelve at localhost'
    > -    label = repo._activebookmark or parent.branch() or 'default'
    > -
    > -    # slashes aren't allowed in filenames, therefore we rename it
    > -    label = label.replace('/', '_')
    > -
    > -    def gennames():
    > -        yield label
    > -        for i in xrange(1, 100):
    > -            yield '%s-%02d' % (label, i)
    >   
    >       if parent.node() != nodemod.nullid:
    >           desc = "changes to: %s" % parent.description().split('\n', 1)[0]
    > @@ -287,8 +308,7 @@ def _docreatecmd(ui, repo, pats, opts):
    >   
    >       if not opts.get('message'):
    >           opts['message'] = desc
    > -
    > -    name = opts.get('name')
    > +    name = getshelvename(repo, parent, opts)
    This moves the name generation from inside the lock to outside the 
    lock.  In theory, this could mean two hg processes pick the same name 
    and overwrite each other.  I'd just move this line to be inside the lock.
Good catch, I will move it back inside the lock.
    >   
    >       lock = tr = None
    >       try:
    > @@ -298,24 +318,6 @@ def _docreatecmd(ui, repo, pats, opts):
    >           # pull races. ensure we don't print the abort message to stderr.
    >           tr = repo.transaction('commit', report=lambda x: None)
    >   
    > -        if name:
    > -            if shelvedfile(repo, name, 'hg').exists():
    > -                raise error.Abort(_("a shelved change named '%s' already exists"
    > -                                   ) % name)
    > -        else:
    > -            for n in gennames():
    > -                if not shelvedfile(repo, n, 'hg').exists():
    > -                    name = n
    > -                    break
    > -            else:
    > -                raise error.Abort(_("too many shelved changes named '%s'") %
    > -                                 label)
    > -
    > -        # ensure we are not creating a subdirectory or a hidden file
    > -        if '/' in name or '\\' in name:
    > -            raise error.Abort(_('shelved change names may not contain slashes'))
    > -        if name.startswith('.'):
    > -            raise error.Abort(_("shelved change names may not start with '.'"))
    >           interactive = opts.get('interactive', False)
    >           includeunknown = (opts.get('unknown', False) and
    >                             not opts.get('addremove', False))
    > _______________________________________________
    > Mercurial-devel mailing list
    > Mercurial-devel at mercurial-scm.org
    > https://urldefense.proofpoint.com/v2/url?u=https-3A__www.mercurial-2Dscm.org_mailman_listinfo_mercurial-2Ddevel&d=DQIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=nuarHzhP1wi1T9iURRCj1A&m=oPrwX8d9ZXkEo2oQLgppGSa4Vxar6e5Rhq_0VzRtvHk&s=wo0ECwAdjmaXURiCMVLIrPMtX8gFAxj6o6JFdo1Fass&e=
    
    



More information about the Mercurial-devel mailing list