hgkw/keyword.py
changeset 209 430837dbe7f4
parent 208 5afdcec8a01f
child 210 304f9ac35869
equal deleted inserted replaced
208:5afdcec8a01f 209:430837dbe7f4
   182         Keywords are expanded if keyword templater is initialized,
   182         Keywords are expanded if keyword templater is initialized,
   183         otherwise their substitution is removed.'''
   183         otherwise their substitution is removed.'''
   184         expand = self.t is not None
   184         expand = self.t is not None
   185         action = ('shrinking', 'expanding')[expand]
   185         action = ('shrinking', 'expanding')[expand]
   186         notify = (self.ui.note, self.ui.debug)[commit]
   186         notify = (self.ui.note, self.ui.debug)[commit]
       
   187         # backwards compatibility: older versions have dirstate.update
       
   188         nodirstateupdate = not hasattr(self.repo.dirstate, 'update')
   187         files = []
   189         files = []
   188         for f in candidates:
   190         for f in candidates:
   189             fp = self.repo.file(f, kwexp=expand, kwcnt=True)
   191             fp = self.repo.file(f, kwexp=expand, kwcnt=True)
   190             data, kwfound = fp.read(man[f])
   192             data, kwfound = fp.read(man[f])
   191             if kwfound:
   193             if kwfound:
   193                 try:
   195                 try:
   194                     self.repo.wwrite(f, data, man.flags(f))
   196                     self.repo.wwrite(f, data, man.flags(f))
   195                 except AttributeError:
   197                 except AttributeError:
   196                     # older versions want file descriptor as 3. optional arg
   198                     # older versions want file descriptor as 3. optional arg
   197                     self.repo.wwrite(f, data)
   199                     self.repo.wwrite(f, data)
   198                 files.append(f)
   200                 if nodirstateupdate:
       
   201                     self.repo.dirstate.normal(f)
       
   202                 else:
       
   203                     files.append(f)
   199         if files:
   204         if files:
   200             self.repo.dirstate.update(files, 'n')
   205             self.repo.dirstate.update(files, 'n')
   201 
   206 
   202 class kwfilelog(filelog.filelog):
   207 class kwfilelog(filelog.filelog):
   203     '''
   208     '''
   246         return util.matcher(repo.root, inc=inc, exc=exc)[1]
   251         return util.matcher(repo.root, inc=inc, exc=exc)[1]
   247     return None
   252     return None
   248 
   253 
   249 def _overwrite(ui, repo, files, expand):
   254 def _overwrite(ui, repo, files, expand):
   250     '''Expands/shrinks keywords in working directory.'''
   255     '''Expands/shrinks keywords in working directory.'''
   251     wlock = None
   256     wlock = lock = None
   252     try:
   257     try:
   253         wlock = repo.wlock()
   258         wlock = repo.wlock()
       
   259         lock = repo.lock()
   254         bail_if_changed(repo)
   260         bail_if_changed(repo)
   255         ctx = repo.changectx()
   261         ctx = repo.changectx()
   256         if not ctx:
   262         if not ctx:
   257             raise hg.RepoError(_('no revision checked out'))
   263             raise hg.RepoError(_('no revision checked out'))
   258         kwfmatcher = _keywordmatcher(ui, repo)
   264         kwfmatcher = _keywordmatcher(ui, repo)
   262         m = ctx.manifest()
   268         m = ctx.manifest()
   263         if files:
   269         if files:
   264             files = [f for f in files if f in m.keys()]
   270             files = [f for f in files if f in m.keys()]
   265         else:
   271         else:
   266             files = m.keys()
   272             files = m.keys()
   267         files = [f for f in files if kwfmatcher(f) and not os.path.islink(f)]
   273         if hasattr(repo, '_link'):
       
   274             files = [f for f in files if kwfmatcher(f) and not repo._link(f)]
       
   275         else:
       
   276             files = [f for f in files if kwfmatcher(f)
       
   277                      and not os.path.islink(repo.wjoin(f))]
   268         if not files:
   278         if not files:
   269             ui.warn(_('files not configured for expansion or untracked\n'))
   279             ui.warn(_('files not configured for expansion or untracked\n'))
   270             return
   280             return
   271         commit = False
   281         commit = False
   272         kwt = kwtemplater(ui, repo, expand, node=ctx.node())
   282         kwt = kwtemplater(ui, repo, expand, node=ctx.node())
   273         kwt.overwrite(files, m, commit)
   283         kwt.overwrite(files, m, commit)
   274         wlock = None
       
   275     finally:
   284     finally:
   276         del wlock
   285         del wlock, lock
   277 
   286 
   278 
   287 
   279 def shrink(ui, repo, *args):
   288 def shrink(ui, repo, *args):
   280     '''revert expanded keywords in working directory
   289     '''revert expanded keywords in working directory
   281 
   290 
   390 
   399 
   391         def commit(self, files=None, text='', user=None, date=None,
   400         def commit(self, files=None, text='', user=None, date=None,
   392                    match=util.always, force=False, lock=None, wlock=None,
   401                    match=util.always, force=False, lock=None, wlock=None,
   393                    force_editor=False, p1=None, p2=None, extra={}):
   402                    force_editor=False, p1=None, p2=None, extra={}):
   394             try:
   403             try:
   395                 if not wlock:
   404                 kwlock = lock
   396                     wlock = self.wlock()
   405                 kwwlock = wlock
   397                 removed = self.status(node1=p1, node2=p2, files=files,
   406                 del wlock, lock
   398                                       match=match, wlock=wlock)[2]
   407             except NameError:
   399 
   408                 # (w)lock arguments removed in recent Hg versions
   400                 node = super(kwrepo,
   409                 kwwlock = kwlock = None
   401                              self).commit(files=files, text=text, user=user,
   410             try:
   402                                           date=date, match=match, force=force,
   411                 if not kwwlock:
   403                                           lock=lock, wlock=wlock,
   412                     kwwlock = self.wlock()
   404                                           force_editor=force_editor,
   413                 if not kwlock:
   405                                           p1=p1, p2=p2, extra=extra)
   414                     kwlock = self.lock()
       
   415                 try:
       
   416                     removed = self.status(node1=p1, node2=p2, files=files,
       
   417                                           match=match, wlock=kwwlock)[2]
       
   418                     node = super(kwrepo,
       
   419                                  self).commit(files=files, text=text,
       
   420                                               user=user, date=date,
       
   421                                               match=match, force=force,
       
   422                                               lock=kwlock, wlock=kwwlock,
       
   423                                               force_editor=force_editor,
       
   424                                               p1=p1, p2=p2, extra=extra)
       
   425                 except TypeError:
       
   426                     removed = self.status(node1=p1, node2=p2,
       
   427                                           files=files, match=match)[2]
       
   428                     node = super(kwrepo,
       
   429                                  self).commit(files=files, text=text,
       
   430                                               user=user, date=date,
       
   431                                               match=match, force=force,
       
   432                                               force_editor=force_editor,
       
   433                                               p1=p1, p2=p2, extra=extra)
   406                 if node is not None:
   434                 if node is not None:
   407                     cl = self.changelog.read(node)
   435                     cl = self.changelog.read(node)
   408                     candidates = [f for f in cl[3] if kwfmatcher(f)
   436                     if hasattr(self, '_link'):
   409                                   and f not in removed
   437                         candidates = [f for f in cl[3] if kwfmatcher(f)
   410                                   and not os.path.islink(self.wjoin(f))]
   438                                       and f not in removed
       
   439                                       and not self._link(f)]
       
   440                     else:
       
   441                         candidates = [f for f in cl[3] if kwfmatcher(f)
       
   442                                       and f not in removed
       
   443                                       and not os.path.islink(self.wjoin(f))]
   411                     if candidates:
   444                     if candidates:
   412                         m = self.manifest.read(cl[0])
   445                         mn = self.manifest.read(cl[0])
   413                         expand = commit = True
   446                         expand = commit = True
   414                         kwt = kwtemplater(ui, self, expand, node=node)
   447                         kwt = kwtemplater(ui, self, expand, node=node)
   415                         kwt.overwrite(candidates, m, commit)
   448                         kwt.overwrite(candidates, mn, commit)
   416                 wlock = None
       
   417                 return node
   449                 return node
   418             finally:
   450             finally:
   419                 del wlock
   451                 del kwwlock, kwlock
   420 
   452 
   421     repo.__class__ = kwrepo
   453     repo.__class__ = kwrepo
   422 
   454 
   423 
   455 
   424 cmdtable = {
   456 cmdtable = {