hgkw/keyword.py
changeset 809 028b6584decb
parent 808 cd9ec33f5bca
child 811 31205612c018
equal deleted inserted replaced
808:cd9ec33f5bca 809:028b6584decb
    90 
    90 
    91 commands.optionalrepo += ' kwdemo'
    91 commands.optionalrepo += ' kwdemo'
    92 
    92 
    93 # hg commands that do not act on keywords
    93 # hg commands that do not act on keywords
    94 nokwcommands = ('add addremove annotate bundle copy export grep incoming init'
    94 nokwcommands = ('add addremove annotate bundle copy export grep incoming init'
    95                 ' log outgoing push rename rollback tip verify'
    95                 ' log outgoing push rename tip verify convert email glog')
    96                 ' convert email glog')
       
    97 
    96 
    98 # hg commands that trigger expansion only when writing to working dir,
    97 # hg commands that trigger expansion only when writing to working dir,
    99 # not when reading filelog, and unexpand when reading from working dir
    98 # not when reading filelog, and unexpand when reading from working dir
   100 restricted = 'merge record qrecord resolve transplant'
    99 restricted = 'merge record qrecord resolve transplant'
   101 
   100 
   190         '''Returns true if path matches [keyword] pattern
   189         '''Returns true if path matches [keyword] pattern
   191         and is not a symbolic link.
   190         and is not a symbolic link.
   192         Caveat: localrepository._link fails on Windows.'''
   191         Caveat: localrepository._link fails on Windows.'''
   193         return self.match(path) and not 'l' in flagfunc(path)
   192         return self.match(path) and not 'l' in flagfunc(path)
   194 
   193 
   195     def overwrite(self, ctx, candidates, iswctx, expand):
   194     def overwrite(self, ctx, candidates, iswctx, expand, cfiles):
   196         '''Overwrites selected files expanding/shrinking keywords.'''
   195         '''Overwrites selected files expanding/shrinking keywords.'''
   197         if self.record:
   196         if cfiles is not None:
   198             candidates = [f for f in ctx.files() if f in ctx]
   197             candidates = [f for f in candidates if f in cfiles]
   199         candidates = [f for f in candidates if self.iskwfile(f, ctx.flags)]
   198         candidates = [f for f in candidates if self.iskwfile(f, ctx.flags)]
   200         if candidates:
   199         if candidates:
   201             restrict = self.restrict
   200             restrict = self.restrict
   202             self.restrict = True        # do not expand when reading
   201             self.restrict = True        # do not expand when reading
       
   202             rollback = kwtools['hgcmd'] == 'rollback'
   203             mf = ctx.manifest()
   203             mf = ctx.manifest()
   204             msg = (expand and _('overwriting %s expanding keywords\n')
   204             msg = (expand and _('overwriting %s expanding keywords\n')
   205                    or _('overwriting %s shrinking keywords\n'))
   205                    or _('overwriting %s shrinking keywords\n'))
   206             for f in candidates:
   206             for f in candidates:
   207                 if not self.record:
   207                 if not self.record and not rollback:
   208                     data = self.repo.file(f).read(mf[f])
   208                     data = self.repo.file(f).read(mf[f])
   209                 else:
   209                 else:
   210                     data = self.repo.wread(f)
   210                     data = self.repo.wread(f)
   211                 if util.binary(data):
   211                 if util.binary(data):
   212                     continue
   212                     continue
   218                 else:
   218                 else:
   219                     found = self.re_kw.search(data)
   219                     found = self.re_kw.search(data)
   220                 if found:
   220                 if found:
   221                     self.ui.note(msg % f)
   221                     self.ui.note(msg % f)
   222                     self.repo.wwrite(f, data, mf.flags(f))
   222                     self.repo.wwrite(f, data, mf.flags(f))
   223                     if iswctx:
   223                     if iswctx and not rollback:
   224                         self.repo.dirstate.normal(f)
   224                         self.repo.dirstate.normal(f)
   225                     elif self.record:
   225                     elif self.record:
   226                         self.repo.dirstate.normallookup(f)
   226                         self.repo.dirstate.normallookup(f)
   227             self.restrict = restrict
   227             self.restrict = restrict
   228 
   228 
   297     try:
   297     try:
   298         status = _status(ui, repo, kwt, *pats, **opts)
   298         status = _status(ui, repo, kwt, *pats, **opts)
   299         modified, added, removed, deleted, unknown, ignored, clean = status
   299         modified, added, removed, deleted, unknown, ignored, clean = status
   300         if modified or added or removed or deleted:
   300         if modified or added or removed or deleted:
   301             raise util.Abort(_('outstanding uncommitted changes'))
   301             raise util.Abort(_('outstanding uncommitted changes'))
   302         kwt.overwrite(wctx, clean, True, expand)
   302         kwt.overwrite(wctx, clean, True, expand, None)
   303     finally:
   303     finally:
   304         wlock.release()
   304         wlock.release()
   305 
   305 
   306 def demo(ui, repo, *args, **opts):
   306 def demo(ui, repo, *args, **opts):
   307     '''print [keywordmaps] configuration and an expansion example
   307     '''print [keywordmaps] configuration and an expansion example
   501         def kwcommitctx(self, ctx, error=False):
   501         def kwcommitctx(self, ctx, error=False):
   502             n = super(kwrepo, self).commitctx(ctx, error)
   502             n = super(kwrepo, self).commitctx(ctx, error)
   503             # no lock needed, only called from repo.commit() which already locks
   503             # no lock needed, only called from repo.commit() which already locks
   504             if not kwt.record:
   504             if not kwt.record:
   505                 kwt.overwrite(self[n], sorted(ctx.added() + ctx.modified()),
   505                 kwt.overwrite(self[n], sorted(ctx.added() + ctx.modified()),
   506                               False, True)
   506                               False, True, None)
   507             return n
   507             return n
       
   508 
       
   509         def rollback(self, dryrun=False):
       
   510             wlock = repo.wlock()
       
   511             try:
       
   512                 if not dryrun:
       
   513                     cfiles = self['.'].files()
       
   514                 ret = super(kwrepo, self).rollback(dryrun)
       
   515                 if not dryrun:
       
   516                     ctx = self['.']
       
   517                     modified, added = super(kwrepo, self).status()[:2]
       
   518                     kwt.overwrite(ctx, added, True, False, cfiles)
       
   519                     kwt.overwrite(ctx, modified, True, True, cfiles)
       
   520                 return ret
       
   521             finally:
       
   522                 wlock.release()
   508 
   523 
   509     # monkeypatches
   524     # monkeypatches
   510     def kwpatchfile_init(orig, self, ui, fname, opener,
   525     def kwpatchfile_init(orig, self, ui, fname, opener,
   511                          missing=False, eolmode=None):
   526                          missing=False, eolmode=None):
   512         '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
   527         '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
   534             # therefore compare nodes before and after
   549             # therefore compare nodes before and after
   535             ctx = repo['.']
   550             ctx = repo['.']
   536             ret = orig(ui, repo, commitfunc, *pats, **opts)
   551             ret = orig(ui, repo, commitfunc, *pats, **opts)
   537             recordctx = repo['.']
   552             recordctx = repo['.']
   538             if ctx != recordctx:
   553             if ctx != recordctx:
   539                 kwt.overwrite(recordctx, None, False, True)
   554                 kwt.overwrite(recordctx, recordctx.files(),
       
   555                               False, True, recordctx)
   540             return ret
   556             return ret
   541         finally:
   557         finally:
   542             wlock.release()
   558             wlock.release()
   543 
   559 
   544     repo.__class__ = kwrepo
   560     repo.__class__ = kwrepo