hgkw/keyword.py
changeset 206 8d16b70359da
parent 205 f2d2e36053f9
child 207 affc18a621f5
equal deleted inserted replaced
205:f2d2e36053f9 206:8d16b70359da
   245         if self.renamed(node):
   245         if self.renamed(node):
   246             t2 = super(kwfilelog, self).read(node)
   246             t2 = super(kwfilelog, self).read(node)
   247             return t2 != text
   247             return t2 != text
   248         return super(kwfilelog, self).cmp(node, text)
   248         return super(kwfilelog, self).cmp(node, text)
   249 
   249 
   250 def overwrite(ui, repo, files=None, expand=True):
   250 def _overwrite(ui, repo, files=None, expand=True):
   251     '''Expands/shrinks keywords in working directory.'''
   251     '''Expands/shrinks keywords in working directory.'''
   252     wlock = repo.wlock()
   252     wlock = None
   253     try:
   253     try:
       
   254         wlock = repo.wlock()
   254         bail_if_changed(repo)
   255         bail_if_changed(repo)
   255         ctx = repo.changectx()
   256         ctx = repo.changectx()
   256         if not ctx:
   257         if not ctx:
   257             raise hg.RepoError(_('no revision checked out'))
   258             raise hg.RepoError(_('no revision checked out'))
   258         kwfmatcher = keywordmatcher(ui, repo)
   259         kwfmatcher = keywordmatcher(ui, repo)
   268         if not files:
   269         if not files:
   269             ui.warn(_('files not configured for expansion or untracked\n'))
   270             ui.warn(_('files not configured for expansion or untracked\n'))
   270             return
   271             return
   271         kwt = kwtemplater(ui, repo, node=ctx.node(), expand=expand)
   272         kwt = kwtemplater(ui, repo, node=ctx.node(), expand=expand)
   272         kwt.overwrite(files, m, commit=False)
   273         kwt.overwrite(files, m, commit=False)
       
   274         wlock = None
   273     finally:
   275     finally:
   274         wlock.release()
   276         del wlock
   275 
   277 
   276 
   278 
   277 def shrink(ui, repo, *args):
   279 def shrink(ui, repo, *args):
   278     '''revert expanded keywords in working directory
   280     '''revert expanded keywords in working directory
   279 
   281 
   280     run before:
   282     run before:
   281                disabling keyword expansion
   283                disabling keyword expansion
   282                changing keyword expansion configuration
   284                changing keyword expansion configuration
   283     or if you experience problems with "hg import"
   285     or if you experience problems with "hg import"
   284     '''
   286     '''
   285     overwrite(ui, repo, files=args, expand=False)
   287     _overwrite(ui, repo, files=args, expand=False)
   286 
   288 
   287 def expand(ui, repo, *args):
   289 def expand(ui, repo, *args):
   288     '''expand keywords in working directory
   290     '''expand keywords in working directory
   289 
   291 
   290     run after (re)enabling keyword expansion
   292     run after (re)enabling keyword expansion
   291     '''
   293     '''
   292     overwrite(ui, repo, files=args)
   294     _overwrite(ui, repo, files=args)
   293 
   295 
   294 def demo(ui, repo, *args, **opts):
   296 def demo(ui, repo, *args, **opts):
   295     '''print [keywordmaps] configuration and an expansion example
   297     '''print [keywordmaps] configuration and an expansion example
   296 
   298 
   297     show current, custom, or default keyword template maps and their expansion
   299     show current, custom, or default keyword template maps and their expansion
   354                     'remove', 'rename', 'rollback']
   356                     'remove', 'rename', 'rollback']
   355 
   357 
   356     # for backwards compatibility
   358     # for backwards compatibility
   357     ui = repo.ui
   359     ui = repo.ui
   358 
   360 
   359     def getcmd():
   361     def _getcmd():
   360         # cmdutil.parse(ui, sys.argv[1:])[0] doesn't work for "hg diff -r"
   362         # cmdutil.parse(ui, sys.argv[1:])[0] doesn't work for "hg diff -r"
   361         args = fancyopts.fancyopts(sys.argv[1:], commands.globalopts, {})
   363         args = fancyopts.fancyopts(sys.argv[1:], commands.globalopts, {})
   362         if args:
   364         if args:
   363             cmd = args[0]
   365             cmd = args[0]
   364             aliases, i = findcmd(ui, cmd)
   366             aliases, i = findcmd(ui, cmd)
   365             return aliases[0]
   367             return aliases[0]
   366 
   368 
   367     if not repo.local() or getcmd() in nokwcommands:
   369     if not repo.local() or _getcmd() in nokwcommands:
   368         return
   370         return
   369 
   371 
   370     kwfmatcher = keywordmatcher(ui, repo)
   372     kwfmatcher = keywordmatcher(ui, repo)
   371     if kwfmatcher is None:
   373     if kwfmatcher is None:
   372         return
   374         return
   381             return filelog.filelog(self.sopener, f)
   383             return filelog.filelog(self.sopener, f)
   382 
   384 
   383         def commit(self, files=None, text='', user=None, date=None,
   385         def commit(self, files=None, text='', user=None, date=None,
   384                    match=util.always, force=False, lock=None, wlock=None,
   386                    match=util.always, force=False, lock=None, wlock=None,
   385                    force_editor=False, p1=None, p2=None, extra={}):
   387                    force_editor=False, p1=None, p2=None, extra={}):
   386             wrelease = False
       
   387             if not wlock:
       
   388                 wlock = self.wlock()
       
   389                 wrelease = True
       
   390             try:
   388             try:
       
   389                 if not wlock:
       
   390                     wlock = self.wlock()
   391                 removed = self.status(node1=p1, node2=p2, files=files,
   391                 removed = self.status(node1=p1, node2=p2, files=files,
   392                                       match=match, wlock=wlock)[2]
   392                                       match=match, wlock=wlock)[2]
   393 
   393 
   394                 node = super(kwrepo,
   394                 node = super(kwrepo,
   395                              self).commit(files=files, text=text, user=user,
   395                              self).commit(files=files, text=text, user=user,
   396                                           date=date, match=match, force=force,
   396                                           date=date, match=match, force=force,
   397                                           lock=lock, wlock=wlock,
   397                                           lock=lock, wlock=wlock,
   398                                           force_editor=force_editor,
   398                                           force_editor=force_editor,
   399                                           p1=p1, p2=p2, extra=extra)
   399                                           p1=p1, p2=p2, extra=extra)
   400                 if node is None:
   400                 if node is not None:
   401                     return node
   401                     cl = self.changelog.read(node)
   402 
   402                     candidates = [f for f in cl[3] if kwfmatcher(f)
   403                 cl = self.changelog.read(node)
   403                                   and f not in removed
   404                 candidates = [f for f in cl[3] if kwfmatcher(f)
   404                                   and not os.path.islink(self.wjoin(f))]
   405                               and f not in removed
   405                     if candidates:
   406                               and not os.path.islink(self.wjoin(f))]
   406                         m = self.manifest.read(cl[0])
   407                 if candidates:
   407                         kwt = kwtemplater(ui, self, node=node)
   408                     m = self.manifest.read(cl[0])
   408                         kwt.overwrite(candidates, m)
   409                     kwt = kwtemplater(ui, self, node=node)
   409                 wlock = None
   410                     kwt.overwrite(candidates, m)
       
   411                 return node
   410                 return node
   412             finally:
   411             finally:
   413                 if wrelease:
   412                 del wlock
   414                     wlock.release()
       
   415 
   413 
   416     repo.__class__ = kwrepo
   414     repo.__class__ = kwrepo
   417 
   415 
   418 
   416 
   419 cmdtable = {
   417 cmdtable = {