Patchwork [2,of,2,V4] debuglocks: allow setting a lock

login
register
mail settings
Submitter Paul Morelle
Date Dec. 13, 2017, 12:58 a.m.
Message ID <8bcccbbeafba2bc80ed9.1513126686@taranis.localdomain>
Download mbox | patch
Permalink /patch/26256/
State Accepted
Headers show

Comments

Paul Morelle - Dec. 13, 2017, 12:58 a.m.
# HG changeset patch
# User Paul Morelle <paul.morelle@octobus.net>
# Date 1510497286 -3600
#      Sun Nov 12 15:34:46 2017 +0100
# Node ID 8bcccbbeafba2bc80ed9e427945e11a4728802e8
# Parent  9beb49c91570014c034c3eaad9ce0a7a37e4c931
# EXP-Topic debugsetlocks
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 8bcccbbeafba
debuglocks: allow setting a lock
Yuya Nishihara - Dec. 13, 2017, 12:26 p.m.
On Wed, 13 Dec 2017 01:58:06 +0100, Paul Morelle wrote:
> # HG changeset patch
> # User Paul Morelle <paul.morelle@octobus.net>
> # Date 1510497286 -3600
> #      Sun Nov 12 15:34:46 2017 +0100
> # Node ID 8bcccbbeafba2bc80ed9e427945e11a4728802e8
> # Parent  9beb49c91570014c034c3eaad9ce0a7a37e4c931
> # EXP-Topic debugsetlocks
> # Available At https://bitbucket.org/octobus/mercurial-devel/
> #              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 8bcccbbeafba
> debuglocks: allow setting a lock

> +_confirmlockremovalmsg = _("ready to release the lock (Y)? $$ &Yes")

Used only once. Inlined.

> -  > repo = hg.repository(uimod.ui.load(), path='.')
> -  > `[ -n "${options["wlock"]}" ] && echo "with repo.wlock(False):" || echo "if True:"`
> -  >     `[ -n "${options["lock"]}" ] && echo "with repo.lock(False):" || echo "if True:"`
> -  >         while not os.path.exists('.hg/unlock'):
> -  >             time.sleep(0.1)
> -  > os.unlink('.hg/unlock')
> -  > EOF
> +  >     {
> +  >         waitlock .hg/unlock
> +  >         rm -f .hg/unlock
> +  >         echo y
> +  >     } | hg debuglocks "$@"
>    > }

I've changed it to send output to /dev/null for test stability.

Patch

diff -r 9beb49c91570 -r 8bcccbbeafba mercurial/debugcommands.py
--- a/mercurial/debugcommands.py	Sun Nov 12 15:34:19 2017 +0100
+++ b/mercurial/debugcommands.py	Sun Nov 12 15:34:46 2017 +0100
@@ -77,6 +77,8 @@ 
 
 command = registrar.command()
 
+_confirmlockremovalmsg = _("ready to release the lock (Y)? $$ &Yes")
+
 @command('debugancestor', [], _('[INDEX] REV1 REV2'), optionalrepo=True)
 def debugancestor(ui, repo, *args):
     """find the ancestor revision of two revisions in a given index"""
@@ -1275,7 +1277,10 @@ 
 @command('debuglocks',
          [('L', 'force-lock', None, _('free the store lock (DANGEROUS)')),
           ('W', 'force-wlock', None,
-           _('free the working state lock (DANGEROUS)'))],
+           _('free the working state lock (DANGEROUS)')),
+          ('s', 'set-lock', None, _('set the store lock until stopped')),
+          ('S', 'set-wlock', None,
+           _('set the working state lock until stopped'))],
          _('[OPTION]...'))
 def debuglocks(ui, repo, **opts):
     """show or modify state of locks
@@ -1294,6 +1299,10 @@ 
     instance, on a shared filesystem). Removing locks may also be
     blocked by filesystem permissions.
 
+    Setting a lock will prevent other commands from changing the data.
+    The command will wait until an interruption (SIGINT, SIGTERM, ...) occurs.
+    The set locks are removed when the command exits.
+
     Returns 0 if no locks are held.
 
     """
@@ -1305,6 +1314,24 @@ 
     if opts.get(r'force_lock') or opts.get(r'force_wlock'):
         return 0
 
+    locks = []
+    try:
+        if opts.get(r'set_wlock'):
+            try:
+                locks.append(repo.wlock(False))
+            except error.LockHeld:
+                raise error.Abort(_('wlock is already held'))
+        if opts.get(r'set_lock'):
+            try:
+                locks.append(repo.lock(False))
+            except error.LockHeld:
+                raise error.Abort(_('lock is already held'))
+        if len(locks):
+            ui.promptchoice(_confirmlockremovalmsg)
+            return 0
+    finally:
+        release(*locks)
+
     now = time.time()
     held = 0
 
diff -r 9beb49c91570 -r 8bcccbbeafba tests/test-completion.t
--- a/tests/test-completion.t	Sun Nov 12 15:34:19 2017 +0100
+++ b/tests/test-completion.t	Sun Nov 12 15:34:46 2017 +0100
@@ -274,7 +274,7 @@ 
   debuginstall: template
   debugknown: 
   debuglabelcomplete: 
-  debuglocks: force-lock, force-wlock
+  debuglocks: force-lock, force-wlock, set-lock, set-wlock
   debugmergestate: 
   debugnamecomplete: 
   debugobsolete: flags, record-parents, rev, exclusive, index, delete, date, user, template
diff -r 9beb49c91570 -r 8bcccbbeafba tests/test-debugcommands.t
--- a/tests/test-debugcommands.t	Sun Nov 12 15:34:19 2017 +0100
+++ b/tests/test-debugcommands.t	Sun Nov 12 15:34:46 2017 +0100
@@ -1,4 +1,6 @@ 
   $ cat << EOF >> $HGRCPATH
+  > [ui]
+  > interactive=yes
   > [format]
   > usegeneraldelta=yes
   > EOF
@@ -157,7 +159,7 @@ 
 amount of time, displays error message and returns 1
   $ waitlock() {
   >     start=`date +%s`
-  >     timeout=1
+  >     timeout=5
   >     while [ \( ! -f $1 \) -a \( ! -L $1 \) ]; do
   >         now=`date +%s`
   >         if [ "`expr $now - $start`" -gt $timeout ]; then
@@ -167,26 +169,16 @@ 
   >         sleep 0.1
   >     done
   > }
-dolock [wlock] [lock] will set the locks until interrupted
   $ dolock() {
-  >     declare -A options
-  >     options=([${1:-nolock}]=1 [${2:-nowlock}]=1)
-  >     python <<EOF
-  > from mercurial import hg, ui as uimod
-  > import os
-  > import time
-  > 
-  > repo = hg.repository(uimod.ui.load(), path='.')
-  > `[ -n "${options["wlock"]}" ] && echo "with repo.wlock(False):" || echo "if True:"`
-  >     `[ -n "${options["lock"]}" ] && echo "with repo.lock(False):" || echo "if True:"`
-  >         while not os.path.exists('.hg/unlock'):
-  >             time.sleep(0.1)
-  > os.unlink('.hg/unlock')
-  > EOF
+  >     {
+  >         waitlock .hg/unlock
+  >         rm -f .hg/unlock
+  >         echo y
+  >     } | hg debuglocks "$@"
   > }
-
-  $ dolock lock &
+  $ dolock -s &
   $ waitlock .hg/store/lock
+  ready to release the lock (Y)?  (no-eol)
 
   $ hg debuglocks
   lock:  user *, process * (*s) (glob)
@@ -194,11 +186,15 @@ 
   [1]
   $ touch .hg/unlock
   $ wait
+   y
+  $ [ -f .hg/store/lock ] || echo "There is no lock"
+  There is no lock
 
 * Test setting the wlock
 
-  $ dolock wlock &
+  $ dolock -S &
   $ waitlock .hg/wlock
+  ready to release the lock (Y)?  (no-eol)
 
   $ hg debuglocks
   lock:  free
@@ -206,18 +202,34 @@ 
   [1]
   $ touch .hg/unlock
   $ wait
+   y
+  $ [ -f .hg/wlock ] || echo "There is no wlock"
+  There is no wlock
 
 * Test setting both locks
 
-  $ dolock wlock lock &
+  $ dolock -Ss &
   $ waitlock .hg/wlock && waitlock .hg/store/lock
+  ready to release the lock (Y)?  (no-eol)
 
   $ hg debuglocks
   lock:  user *, process * (*s) (glob)
   wlock: user *, process * (*s) (glob)
   [2]
+
+* Test failing to set a lock
+
+  $ hg debuglocks -s
+  abort: lock is already held
+  [255]
+
+  $ hg debuglocks -S
+  abort: wlock is already held
+  [255]
+
   $ touch .hg/unlock
   $ wait
+   y
 
   $ hg debuglocks
   lock:  free
@@ -225,8 +237,9 @@ 
 
 * Test forcing the lock
 
-  $ dolock lock &
+  $ dolock -s &
   $ waitlock .hg/store/lock
+  ready to release the lock (Y)?  (no-eol)
 
   $ hg debuglocks
   lock:  user *, process * (*s) (glob)
@@ -241,11 +254,13 @@ 
 
   $ touch .hg/unlock
   $ wait
+   y
 
 * Test forcing the wlock
 
-  $ dolock wlock &
+  $ dolock -S &
   $ waitlock .hg/wlock
+  ready to release the lock (Y)?  (no-eol)
 
   $ hg debuglocks
   lock:  free
@@ -260,6 +275,7 @@ 
 
   $ touch .hg/unlock
   $ wait
+   y
 
 Test WdirUnsupported exception