[PATCH] censor: make various path forms available like other Mercurial commands

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Thu Jul 16 10:25:20 CDT 2015


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1437060136 -32400
#      Fri Jul 17 00:22:16 2015 +0900
# Node ID 6700f07c605b212f34392a3eebad5994c677b272
# Parent  35fa7c77c754aa4d156c42abfdb61ca178468872
censor: make various path forms available like other Mercurial commands

Before this patch, censored file should be exactly "a path relative to
repository root" regardless of current working directory, because "hg
censor" passes "path" to "repo.file()" directly without any
preparations.

To make various path forms available like other Mercurial commands,
this patch gets a target file path in the way of "hg parents FILE".

Getting "wctx" is relocated to reuse "wctx" for efficiency.

diff --git a/hgext/censor.py b/hgext/censor.py
--- a/hgext/censor.py
+++ b/hgext/censor.py
@@ -47,6 +47,12 @@
     if not rev:
         raise util.Abort(_('must specify revision to censor'))
 
+    wctx = repo[None]
+
+    m = scmutil.match(wctx, (path,))
+    if m.anypats() or len(m.files()) != 1:
+        raise util.Abort(_('can only specify an explicit filename'))
+    path = m.files()[0]
     flog = repo.file(path)
     if not len(flog):
         raise util.Abort(_('cannot censor file with no history'))
@@ -70,7 +76,6 @@
         raise util.Abort(_('cannot censor file in heads (%s)') % headlist,
             hint=_('clean/delete and commit first'))
 
-    wctx = repo[None]
     wp = wctx.parents()
     if ctx.node() in [p.node() for p in wp]:
         raise util.Abort(_('cannot censor working directory'),
diff --git a/tests/test-censor.t b/tests/test-censor.t
--- a/tests/test-censor.t
+++ b/tests/test-censor.t
@@ -72,7 +72,10 @@
 
 Censor revision with 2 offenses
 
-  $ hg censor -r $C2 -t "remove password" target
+(this also tests file pattern matching: path relative to cwd case)
+
+  $ mkdir -p foo/bar/baz
+  $ hg --cwd foo/bar/baz censor -r $C2 -t "remove password" ../../../target
   $ hg cat -r $H1 target
   Tainted file is now sanitized
   $ hg cat -r $H2 target
@@ -89,7 +92,9 @@
 
 Censor revision with 1 offense
 
-  $ hg censor -r $C1 target
+(this also tests file pattern matching: with 'path:' scheme)
+
+  $ hg --cwd foo/bar/baz censor -r $C1 path:target
   $ hg cat -r $H1 target
   Tainted file is now sanitized
   $ hg cat -r $H2 target


More information about the Mercurial-devel mailing list