diff options
Diffstat (limited to 'hgext/keyword.py')
-rw-r--r-- | hgext/keyword.py | 141 |
1 files changed, 52 insertions, 89 deletions
diff --git a/hgext/keyword.py b/hgext/keyword.py index 54bab17..90654b9 100644 --- a/hgext/keyword.py +++ b/hgext/keyword.py @@ -1,6 +1,6 @@ # keyword.py - $Keyword$ expansion for Mercurial # -# Copyright 2007-2012 Christian Ebert <blacktrash@gmx.net> +# Copyright 2007-2010 Christian Ebert <blacktrash@gmx.net> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. @@ -92,7 +92,6 @@ commands.optionalrepo += ' kwdemo' cmdtable = {} command = cmdutil.command(cmdtable) -testedwith = 'internal' # hg commands that do not act on keywords nokwcommands = ('add addremove annotate bundle export grep incoming init log' @@ -188,7 +187,7 @@ class kwtemplater(object): self.repo = repo self.match = match.match(repo.root, '', [], inc, exc) self.restrict = kwtools['hgcmd'] in restricted.split() - self.postcommit = False + self.record = False kwmaps = self.ui.configitems('keywordmaps') if kwmaps: # override default templates @@ -238,26 +237,22 @@ class kwtemplater(object): def iskwfile(self, cand, ctx): '''Returns subset of candidates which are configured for keyword - expansion but are not symbolic links.''' - return [f for f in cand if self.match(f) and 'l' not in ctx.flags(f)] + expansion are not symbolic links.''' + return [f for f in cand if self.match(f) and not 'l' in ctx.flags(f)] def overwrite(self, ctx, candidates, lookup, expand, rekw=False): '''Overwrites selected files expanding/shrinking keywords.''' - if self.restrict or lookup or self.postcommit: # exclude kw_copy + if self.restrict or lookup or self.record: # exclude kw_copy candidates = self.iskwfile(candidates, ctx) if not candidates: return kwcmd = self.restrict and lookup # kwexpand/kwshrink if self.restrict or expand and lookup: mf = ctx.manifest() - if self.restrict or rekw: - re_kw = self.rekw - else: - re_kw = self.rekwexp - if expand: - msg = _('overwriting %s expanding keywords\n') - else: - msg = _('overwriting %s shrinking keywords\n') + lctx = ctx + re_kw = (self.restrict or rekw) and self.rekw or self.rekwexp + msg = (expand and _('overwriting %s expanding keywords\n') + or _('overwriting %s shrinking keywords\n')) for f in candidates: if self.restrict: data = self.repo.file(f).read(mf[f]) @@ -267,20 +262,21 @@ class kwtemplater(object): continue if expand: if lookup: - ctx = self.linkctx(f, mf[f]) - data, found = self.substitute(data, f, ctx, re_kw.subn) + lctx = self.linkctx(f, mf[f]) + data, found = self.substitute(data, f, lctx, re_kw.subn) elif self.restrict: found = re_kw.search(data) else: data, found = _shrinktext(data, re_kw.subn) if found: self.ui.note(msg % f) - fp = self.repo.wopener(f, "wb", atomictemp=True) - fp.write(data) - fp.close() + fpath = self.repo.wjoin(f) + mode = os.lstat(fpath).st_mode + self.repo.wwrite(f, data, ctx.flags(f)) + os.chmod(fpath, mode) if kwcmd: self.repo.dirstate.normal(f) - elif self.postcommit: + elif self.record: self.repo.dirstate.normallookup(f) def shrink(self, fname, text): @@ -300,9 +296,7 @@ class kwtemplater(object): def wread(self, fname, data): '''If in restricted mode returns data read from wdir with keyword substitutions removed.''' - if self.restrict: - return self.shrink(fname, data) - return data + return self.restrict and self.shrink(fname, data) or data class kwfilelog(filelog.filelog): ''' @@ -331,11 +325,11 @@ class kwfilelog(filelog.filelog): text = self.kwt.shrink(self.path, text) return super(kwfilelog, self).cmp(node, text) -def _status(ui, repo, wctx, kwt, *pats, **opts): +def _status(ui, repo, kwt, *pats, **opts): '''Bails out if [keyword] configuration is not active. Returns status of working directory.''' if kwt: - return repo.status(match=scmutil.match(wctx, pats, opts), clean=True, + return repo.status(match=scmutil.match(repo[None], pats, opts), clean=True, unknown=opts.get('unknown') or opts.get('all')) if ui.configitems('keyword'): raise util.Abort(_('[keyword] patterns cannot match')) @@ -349,7 +343,7 @@ def _kwfwrite(ui, repo, expand, *pats, **opts): kwt = kwtools['templater'] wlock = repo.wlock() try: - status = _status(ui, repo, wctx, kwt, *pats, **opts) + status = _status(ui, repo, kwt, *pats, **opts) modified, added, removed, deleted, unknown, ignored, clean = status if modified or added or removed or deleted: raise util.Abort(_('outstanding uncommitted changes')) @@ -421,10 +415,7 @@ def demo(ui, repo, *args, **opts): ui.setconfig('keywordmaps', k, v) else: ui.status(_('\n\tconfiguration using current keyword template maps\n')) - if uikwmaps: - kwmaps = dict(uikwmaps) - else: - kwmaps = _defaultkwmaps(ui) + kwmaps = dict(uikwmaps) or _defaultkwmaps(ui) uisetup(ui) reposetup(ui, repo) @@ -442,7 +433,7 @@ def demo(ui, repo, *args, **opts): if name.split('.', 1)[0].find('commit') > -1: repo.ui.setconfig('hooks', name, '') msg = _('hg keyword configuration and expansion example') - ui.note("hg ci -m '%s'\n" % msg) # check-code-ignore + ui.note("hg ci -m '%s'\n" % msg) repo.commit(text=msg) ui.status(_('\n\tkeywords expanded\n')) ui.write(repo.wread(fn)) @@ -487,13 +478,13 @@ def files(ui, repo, *pats, **opts): i = ignored (not tracked) ''' kwt = kwtools['templater'] - wctx = repo[None] - status = _status(ui, repo, wctx, kwt, *pats, **opts) + status = _status(ui, repo, kwt, *pats, **opts) cwd = pats and repo.getcwd() or '' modified, added, removed, deleted, unknown, ignored, clean = status files = [] if not opts.get('unknown') or opts.get('all'): files = sorted(modified + added + clean) + wctx = repo[None] kwfiles = kwt.iskwfile(files, wctx) kwdeleted = kwt.iskwfile(deleted, wctx) kwunknown = kwt.iskwfile(unknown, wctx) @@ -505,18 +496,11 @@ def files(ui, repo, *pats, **opts): showfiles += ([f for f in files if f not in kwfiles], [f for f in unknown if f not in kwunknown]) kwlabels = 'enabled deleted enabledunknown ignored ignoredunknown'.split() - kwstates = zip(kwlabels, 'K!kIi', showfiles) - fm = ui.formatter('kwfiles', opts) - fmt = '%.0s%s\n' - if opts.get('all') or ui.verbose: - fmt = '%s %s\n' - for kwstate, char, filenames in kwstates: - label = 'kwfiles.' + kwstate + kwstates = zip('K!kIi', showfiles, kwlabels) + for char, filenames, kwstate in kwstates: + fmt = (opts.get('all') or ui.verbose) and '%s %%s\n' % char or '%s\n' for f in filenames: - fm.startitem() - fm.write('kwstatus path', fmt, char, - repo.pathto(f, cwd), label=label) - fm.end() + ui.write(fmt % repo.pathto(f, cwd), label='kwfiles.' + kwstate) @command('kwshrink', commands.walkopts, _('hg kwshrink [OPTION]... [FILE]...')) def shrink(ui, repo, *pats, **opts): @@ -590,7 +574,7 @@ def reposetup(ui, repo): def kwcommitctx(self, ctx, error=False): n = super(kwrepo, self).commitctx(ctx, error) # no lock needed, only called from repo.commit() which already locks - if not kwt.postcommit: + if not kwt.record: restrict = kwt.restrict kwt.restrict = True kwt.overwrite(self[n], sorted(ctx.added() + ctx.modified()), @@ -598,12 +582,12 @@ def reposetup(ui, repo): kwt.restrict = restrict return n - def rollback(self, dryrun=False, force=False): + def rollback(self, dryrun=False): wlock = self.wlock() try: if not dryrun: changed = self['.'].files() - ret = super(kwrepo, self).rollback(dryrun, force) + ret = super(kwrepo, self).rollback(dryrun) if not dryrun: ctx = self['.'] modified, added = _preselect(self[None].status(), changed) @@ -632,21 +616,6 @@ def reposetup(ui, repo): kwt.match = util.never return orig(web, req, tmpl) - def kw_amend(orig, ui, repo, commitfunc, old, extra, pats, opts): - '''Wraps cmdutil.amend expanding keywords after amend.''' - wlock = repo.wlock() - try: - kwt.postcommit = True - newid = orig(ui, repo, commitfunc, old, extra, pats, opts) - if newid != old.node(): - ctx = repo[newid] - kwt.restrict = True - kwt.overwrite(ctx, ctx.files(), False, True) - kwt.restrict = False - return newid - finally: - wlock.release() - def kw_copy(orig, ui, repo, pats, opts, rename=False): '''Wraps cmdutil.copy so that copy/rename destinations do not contain expanded keywords. @@ -657,29 +626,25 @@ def reposetup(ui, repo): For the latter we have to follow the symlink to find out whether its target is configured for expansion and we therefore must unexpand the keywords in the destination.''' - wlock = repo.wlock() - try: - orig(ui, repo, pats, opts, rename) - if opts.get('dry_run'): - return - wctx = repo[None] - cwd = repo.getcwd() - - def haskwsource(dest): - '''Returns true if dest is a regular file and configured for - expansion or a symlink which points to a file configured for - expansion. ''' - source = repo.dirstate.copied(dest) - if 'l' in wctx.flags(source): - source = scmutil.canonpath(repo.root, cwd, - os.path.realpath(source)) - return kwt.match(source) - - candidates = [f for f in repo.dirstate.copies() if - 'l' not in wctx.flags(f) and haskwsource(f)] - kwt.overwrite(wctx, candidates, False, False) - finally: - wlock.release() + orig(ui, repo, pats, opts, rename) + if opts.get('dry_run'): + return + wctx = repo[None] + cwd = repo.getcwd() + + def haskwsource(dest): + '''Returns true if dest is a regular file and configured for + expansion or a symlink which points to a file configured for + expansion. ''' + source = repo.dirstate.copied(dest) + if 'l' in wctx.flags(source): + source = scmutil.canonpath(repo.root, cwd, + os.path.realpath(source)) + return kwt.match(source) + + candidates = [f for f in repo.dirstate.copies() if + not 'l' in wctx.flags(f) and haskwsource(f)] + kwt.overwrite(wctx, candidates, False, False) def kw_dorecord(orig, ui, repo, commitfunc, *pats, **opts): '''Wraps record.dorecord expanding keywords after recording.''' @@ -687,7 +652,7 @@ def reposetup(ui, repo): try: # record returns 0 even when nothing has changed # therefore compare nodes before and after - kwt.postcommit = True + kwt.record = True ctx = repo['.'] wstatus = repo[None].status() ret = orig(ui, repo, commitfunc, *pats, **opts) @@ -707,8 +672,7 @@ def reposetup(ui, repo): # not make sense if (fctx._filerev is None and (self._repo._encodefilterpats or - kwt.match(fctx.path()) and 'l' not in fctx.flags() or - self.size() - 4 == fctx.size()) or + kwt.match(fctx.path()) and not 'l' in fctx.flags()) or self.size() == fctx.size()): return self._filelog.cmp(self._filenode, fctx.data()) return True @@ -716,7 +680,6 @@ def reposetup(ui, repo): extensions.wrapfunction(context.filectx, 'cmp', kwfilectx_cmp) extensions.wrapfunction(patch.patchfile, '__init__', kwpatchfile_init) extensions.wrapfunction(patch, 'diff', kw_diff) - extensions.wrapfunction(cmdutil, 'amend', kw_amend) extensions.wrapfunction(cmdutil, 'copy', kw_copy) for c in 'annotate changeset rev filediff diff'.split(): extensions.wrapfunction(webcommands, c, kwweb_skip) |