censor: make censor acquire locks before processing
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Wed, 09 Dec 2015 08:28:53 +0900
changeset 27290 525d9b3f0a31
parent 27289 ee33e677f0ac
child 27291 a18328aad48c
censor: make censor acquire locks before processing Before this patch, "hg censor" executes below: - without acquisition of wlock, examine whether the working directory refers the revision of the file to be censored or not - without acquisition of store lock (slock), replace existing filelog of file to be censored with censored one, Replacement consists of steps below, and it is assumed that the destination filelog at (1) isn't changed before renaming at (3). 1. read existing filelog in 2. write filelog entries (both censored and not) into temporary file 3. rename from temporary file to existing filelog to be censored It may cause unintentional result, if another command runs parallelly (see also issue4368). This patch makes "hg censor" acquire wlock and slock before processing.
hgext/censor.py
--- a/hgext/censor.py	Wed Dec 09 08:28:53 2015 +0900
+++ b/hgext/censor.py	Wed Dec 09 08:28:53 2015 +0900
@@ -28,6 +28,7 @@
 from mercurial.node import short
 from mercurial import cmdutil, error, filelog, revlog, scmutil, util
 from mercurial.i18n import _
+from mercurial import lock as lockmod
 
 cmdtable = {}
 command = cmdutil.command(cmdtable)
@@ -42,6 +43,15 @@
      ('t', 'tombstone', '', _('replacement tombstone data'), _('TEXT'))],
     _('-r REV [-t TEXT] [FILE]'))
 def censor(ui, repo, path, rev='', tombstone='', **opts):
+    wlock = lock = None
+    try:
+        wlock = repo.wlock()
+        lock = repo.lock()
+        return _docensor(ui, repo, path, rev, tombstone, **opts)
+    finally:
+        lockmod.release(lock, wlock)
+
+def _docensor(ui, repo, path, rev='', tombstone='', **opts):
     if not path:
         raise error.Abort(_('must specify file path to censor'))
     if not rev: