hgext/shelve.py
changeset 26587 56b2bcea2529
parent 26577 8f2ff40fe9c9
child 26602 c062a9c0293c
equal deleted inserted replaced
26586:d51c658d3f04 26587:56b2bcea2529
    88         try:
    88         try:
    89             return self.vfs(self.fname, mode)
    89             return self.vfs(self.fname, mode)
    90         except IOError as err:
    90         except IOError as err:
    91             if err.errno != errno.ENOENT:
    91             if err.errno != errno.ENOENT:
    92                 raise
    92                 raise
    93             raise util.Abort(_("shelved change '%s' not found") % self.name)
    93             raise error.Abort(_("shelved change '%s' not found") % self.name)
    94 
    94 
    95     def applybundle(self):
    95     def applybundle(self):
    96         fp = self.opener()
    96         fp = self.opener()
    97         try:
    97         try:
    98             gen = exchange.readbundle(self.repo.ui, fp, self.fname, self.vfs)
    98             gen = exchange.readbundle(self.repo.ui, fp, self.fname, self.vfs)
   133         fp = repo.vfs(cls._filename)
   133         fp = repo.vfs(cls._filename)
   134         try:
   134         try:
   135             version = int(fp.readline().strip())
   135             version = int(fp.readline().strip())
   136 
   136 
   137             if version != cls._version:
   137             if version != cls._version:
   138                 raise util.Abort(_('this version of shelve is incompatible '
   138                 raise error.Abort(_('this version of shelve is incompatible '
   139                                    'with the version used in this repo'))
   139                                    'with the version used in this repo'))
   140             name = fp.readline().strip()
   140             name = fp.readline().strip()
   141             wctx = fp.readline().strip()
   141             wctx = fp.readline().strip()
   142             pendingctx = fp.readline().strip()
   142             pendingctx = fp.readline().strip()
   143             parents = [bin(h) for h in fp.readline().split()]
   143             parents = [bin(h) for h in fp.readline().split()]
   237                         visit.append(parent)
   237                         visit.append(parent)
   238 
   238 
   239     wctx = repo[None]
   239     wctx = repo[None]
   240     parents = wctx.parents()
   240     parents = wctx.parents()
   241     if len(parents) > 1:
   241     if len(parents) > 1:
   242         raise util.Abort(_('cannot shelve while merging'))
   242         raise error.Abort(_('cannot shelve while merging'))
   243     parent = parents[0]
   243     parent = parents[0]
   244 
   244 
   245     # we never need the user, so we use a generic user for all shelve operations
   245     # we never need the user, so we use a generic user for all shelve operations
   246     user = 'shelve@localhost'
   246     user = 'shelve@localhost'
   247     label = repo._activebookmark or parent.branch() or 'default'
   247     label = repo._activebookmark or parent.branch() or 'default'
   288         # pull races. ensure we don't print the abort message to stderr.
   288         # pull races. ensure we don't print the abort message to stderr.
   289         tr = repo.transaction('commit', report=lambda x: None)
   289         tr = repo.transaction('commit', report=lambda x: None)
   290 
   290 
   291         if name:
   291         if name:
   292             if shelvedfile(repo, name, 'hg').exists():
   292             if shelvedfile(repo, name, 'hg').exists():
   293                 raise util.Abort(_("a shelved change named '%s' already exists")
   293                 raise error.Abort(_("a shelved change named '%s' already exists"
   294                                  % name)
   294                                    ) % name)
   295         else:
   295         else:
   296             for n in gennames():
   296             for n in gennames():
   297                 if not shelvedfile(repo, n, 'hg').exists():
   297                 if not shelvedfile(repo, n, 'hg').exists():
   298                     name = n
   298                     name = n
   299                     break
   299                     break
   300             else:
   300             else:
   301                 raise util.Abort(_("too many shelved changes named '%s'") %
   301                 raise error.Abort(_("too many shelved changes named '%s'") %
   302                                  label)
   302                                  label)
   303 
   303 
   304         # ensure we are not creating a subdirectory or a hidden file
   304         # ensure we are not creating a subdirectory or a hidden file
   305         if '/' in name or '\\' in name:
   305         if '/' in name or '\\' in name:
   306             raise util.Abort(_('shelved change names may not contain slashes'))
   306             raise error.Abort(_('shelved change names may not contain slashes'))
   307         if name.startswith('.'):
   307         if name.startswith('.'):
   308             raise util.Abort(_("shelved change names may not start with '.'"))
   308             raise error.Abort(_("shelved change names may not start with '.'"))
   309         interactive = opts.get('interactive', False)
   309         interactive = opts.get('interactive', False)
   310 
   310 
   311         def interactivecommitfunc(ui, repo, *pats, **opts):
   311         def interactivecommitfunc(ui, repo, *pats, **opts):
   312             match = scmutil.match(repo['.'], pats, {})
   312             match = scmutil.match(repo['.'], pats, {})
   313             message = opts['message']
   313             message = opts['message']
   357         lockmod.release(wlock)
   357         lockmod.release(wlock)
   358 
   358 
   359 def deletecmd(ui, repo, pats):
   359 def deletecmd(ui, repo, pats):
   360     """subcommand that deletes a specific shelve"""
   360     """subcommand that deletes a specific shelve"""
   361     if not pats:
   361     if not pats:
   362         raise util.Abort(_('no shelved changes specified!'))
   362         raise error.Abort(_('no shelved changes specified!'))
   363     wlock = repo.wlock()
   363     wlock = repo.wlock()
   364     try:
   364     try:
   365         for name in pats:
   365         for name in pats:
   366             for suffix in 'hg patch'.split():
   366             for suffix in 'hg patch'.split():
   367                 shelvedfile(repo, name, suffix).movetobackup()
   367                 shelvedfile(repo, name, suffix).movetobackup()
   368         cleanupoldbackups(repo)
   368         cleanupoldbackups(repo)
   369     except OSError as err:
   369     except OSError as err:
   370         if err.errno != errno.ENOENT:
   370         if err.errno != errno.ENOENT:
   371             raise
   371             raise
   372         raise util.Abort(_("shelved change '%s' not found") % name)
   372         raise error.Abort(_("shelved change '%s' not found") % name)
   373     finally:
   373     finally:
   374         lockmod.release(wlock)
   374         lockmod.release(wlock)
   375 
   375 
   376 def listshelves(repo):
   376 def listshelves(repo):
   377     """return all shelves in repo as list of (time, filename)"""
   377     """return all shelves in repo as list of (time, filename)"""
   439             fp.close()
   439             fp.close()
   440 
   440 
   441 def singlepatchcmds(ui, repo, pats, opts, subcommand):
   441 def singlepatchcmds(ui, repo, pats, opts, subcommand):
   442     """subcommand that displays a single shelf"""
   442     """subcommand that displays a single shelf"""
   443     if len(pats) != 1:
   443     if len(pats) != 1:
   444         raise util.Abort(_("--%s expects a single shelf") % subcommand)
   444         raise error.Abort(_("--%s expects a single shelf") % subcommand)
   445     shelfname = pats[0]
   445     shelfname = pats[0]
   446 
   446 
   447     if not shelvedfile(repo, shelfname, 'patch').exists():
   447     if not shelvedfile(repo, shelfname, 'patch').exists():
   448         raise util.Abort(_("cannot find shelf %s") % shelfname)
   448         raise error.Abort(_("cannot find shelf %s") % shelfname)
   449 
   449 
   450     listcmd(ui, repo, pats, opts)
   450     listcmd(ui, repo, pats, opts)
   451 
   451 
   452 def checkparents(repo, state):
   452 def checkparents(repo, state):
   453     """check parent while resuming an unshelve"""
   453     """check parent while resuming an unshelve"""
   454     if state.parents != repo.dirstate.parents():
   454     if state.parents != repo.dirstate.parents():
   455         raise util.Abort(_('working directory parents do not match unshelve '
   455         raise error.Abort(_('working directory parents do not match unshelve '
   456                            'state'))
   456                            'state'))
   457 
   457 
   458 def pathtofiles(repo, files):
   458 def pathtofiles(repo, files):
   459     cwd = repo.getcwd()
   459     cwd = repo.getcwd()
   460     return [repo.pathto(f, cwd) for f in files]
   460     return [repo.pathto(f, cwd) for f in files]
   525     lock = None
   525     lock = None
   526     try:
   526     try:
   527         checkparents(repo, state)
   527         checkparents(repo, state)
   528         ms = merge.mergestate(repo)
   528         ms = merge.mergestate(repo)
   529         if [f for f in ms if ms[f] == 'u']:
   529         if [f for f in ms if ms[f] == 'u']:
   530             raise util.Abort(
   530             raise error.Abort(
   531                 _("unresolved conflicts, can't continue"),
   531                 _("unresolved conflicts, can't continue"),
   532                 hint=_("see 'hg resolve', then 'hg unshelve --continue'"))
   532                 hint=_("see 'hg resolve', then 'hg unshelve --continue'"))
   533 
   533 
   534         lock = repo.lock()
   534         lock = repo.lock()
   535 
   535 
   608     if not abortf and not continuef:
   608     if not abortf and not continuef:
   609         cmdutil.checkunfinished(repo)
   609         cmdutil.checkunfinished(repo)
   610 
   610 
   611     if abortf or continuef:
   611     if abortf or continuef:
   612         if abortf and continuef:
   612         if abortf and continuef:
   613             raise util.Abort(_('cannot use both abort and continue'))
   613             raise error.Abort(_('cannot use both abort and continue'))
   614         if shelved:
   614         if shelved:
   615             raise util.Abort(_('cannot combine abort/continue with '
   615             raise error.Abort(_('cannot combine abort/continue with '
   616                                'naming a shelved change'))
   616                                'naming a shelved change'))
   617 
   617 
   618         try:
   618         try:
   619             state = shelvedstate.load(repo)
   619             state = shelvedstate.load(repo)
   620         except IOError as err:
   620         except IOError as err:
   621             if err.errno != errno.ENOENT:
   621             if err.errno != errno.ENOENT:
   622                 raise
   622                 raise
   623             raise util.Abort(_('no unshelve operation underway'))
   623             raise error.Abort(_('no unshelve operation underway'))
   624 
   624 
   625         if abortf:
   625         if abortf:
   626             return unshelveabort(ui, repo, state, opts)
   626             return unshelveabort(ui, repo, state, opts)
   627         elif continuef:
   627         elif continuef:
   628             return unshelvecontinue(ui, repo, state, opts)
   628             return unshelvecontinue(ui, repo, state, opts)
   629     elif len(shelved) > 1:
   629     elif len(shelved) > 1:
   630         raise util.Abort(_('can only unshelve one change at a time'))
   630         raise error.Abort(_('can only unshelve one change at a time'))
   631     elif not shelved:
   631     elif not shelved:
   632         shelved = listshelves(repo)
   632         shelved = listshelves(repo)
   633         if not shelved:
   633         if not shelved:
   634             raise util.Abort(_('no shelved changes to apply!'))
   634             raise error.Abort(_('no shelved changes to apply!'))
   635         basename = util.split(shelved[0][1])[1]
   635         basename = util.split(shelved[0][1])[1]
   636         ui.status(_("unshelving change '%s'\n") % basename)
   636         ui.status(_("unshelving change '%s'\n") % basename)
   637     else:
   637     else:
   638         basename = shelved[0]
   638         basename = shelved[0]
   639 
   639 
   640     if not shelvedfile(repo, basename, 'patch').exists():
   640     if not shelvedfile(repo, basename, 'patch').exists():
   641         raise util.Abort(_("shelved change '%s' not found") % basename)
   641         raise error.Abort(_("shelved change '%s' not found") % basename)
   642 
   642 
   643     oldquiet = ui.quiet
   643     oldquiet = ui.quiet
   644     wlock = lock = tr = None
   644     wlock = lock = tr = None
   645     try:
   645     try:
   646         wlock = repo.wlock()
   646         wlock = repo.wlock()
   806     ]
   806     ]
   807     def checkopt(opt):
   807     def checkopt(opt):
   808         if opts[opt]:
   808         if opts[opt]:
   809             for i, allowable in allowables:
   809             for i, allowable in allowables:
   810                 if opts[i] and opt not in allowable:
   810                 if opts[i] and opt not in allowable:
   811                     raise util.Abort(_("options '--%s' and '--%s' may not be "
   811                     raise error.Abort(_("options '--%s' and '--%s' may not be "
   812                                        "used together") % (opt, i))
   812                                        "used together") % (opt, i))
   813             return True
   813             return True
   814     if checkopt('cleanup'):
   814     if checkopt('cleanup'):
   815         if pats:
   815         if pats:
   816             raise util.Abort(_("cannot specify names when using '--cleanup'"))
   816             raise error.Abort(_("cannot specify names when using '--cleanup'"))
   817         return cleanupcmd(ui, repo)
   817         return cleanupcmd(ui, repo)
   818     elif checkopt('delete'):
   818     elif checkopt('delete'):
   819         return deletecmd(ui, repo, pats)
   819         return deletecmd(ui, repo, pats)
   820     elif checkopt('list'):
   820     elif checkopt('list'):
   821         return listcmd(ui, repo, pats, opts)
   821         return listcmd(ui, repo, pats, opts)