comparison mercurial/debugcommands.py @ 30958:23c801570449

debugcommands: move 'debuglocks' in the new module
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
date Wed, 01 Feb 2017 17:42:49 +0100
parents a9aa67ba3f96
children 9e39d196cdf5
comparison
equal deleted inserted replaced
30957:a9aa67ba3f96 30958:23c801570449
5 # This software may be used and distributed according to the terms of the 5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 from __future__ import absolute_import 8 from __future__ import absolute_import
9 9
10 import errno
10 import operator 11 import operator
11 import os 12 import os
12 import random 13 import random
14 import socket
13 import sys 15 import sys
14 import tempfile 16 import tempfile
17 import time
15 18
16 from .i18n import _ 19 from .i18n import _
17 from .node import ( 20 from .node import (
18 bin, 21 bin,
19 hex, 22 hex,
1041 @command('debuglabelcomplete', [], _('LABEL...')) 1044 @command('debuglabelcomplete', [], _('LABEL...'))
1042 def debuglabelcomplete(ui, repo, *args): 1045 def debuglabelcomplete(ui, repo, *args):
1043 '''backwards compatibility with old bash completion scripts (DEPRECATED)''' 1046 '''backwards compatibility with old bash completion scripts (DEPRECATED)'''
1044 commands.debugnamecomplete(ui, repo, *args) 1047 commands.debugnamecomplete(ui, repo, *args)
1045 1048
1049 @command('debuglocks',
1050 [('L', 'force-lock', None, _('free the store lock (DANGEROUS)')),
1051 ('W', 'force-wlock', None,
1052 _('free the working state lock (DANGEROUS)'))],
1053 _('[OPTION]...'))
1054 def debuglocks(ui, repo, **opts):
1055 """show or modify state of locks
1056
1057 By default, this command will show which locks are held. This
1058 includes the user and process holding the lock, the amount of time
1059 the lock has been held, and the machine name where the process is
1060 running if it's not local.
1061
1062 Locks protect the integrity of Mercurial's data, so should be
1063 treated with care. System crashes or other interruptions may cause
1064 locks to not be properly released, though Mercurial will usually
1065 detect and remove such stale locks automatically.
1066
1067 However, detecting stale locks may not always be possible (for
1068 instance, on a shared filesystem). Removing locks may also be
1069 blocked by filesystem permissions.
1070
1071 Returns 0 if no locks are held.
1072
1073 """
1074
1075 if opts.get('force_lock'):
1076 repo.svfs.unlink('lock')
1077 if opts.get('force_wlock'):
1078 repo.vfs.unlink('wlock')
1079 if opts.get('force_lock') or opts.get('force_lock'):
1080 return 0
1081
1082 now = time.time()
1083 held = 0
1084
1085 def report(vfs, name, method):
1086 # this causes stale locks to get reaped for more accurate reporting
1087 try:
1088 l = method(False)
1089 except error.LockHeld:
1090 l = None
1091
1092 if l:
1093 l.release()
1094 else:
1095 try:
1096 stat = vfs.lstat(name)
1097 age = now - stat.st_mtime
1098 user = util.username(stat.st_uid)
1099 locker = vfs.readlock(name)
1100 if ":" in locker:
1101 host, pid = locker.split(':')
1102 if host == socket.gethostname():
1103 locker = 'user %s, process %s' % (user, pid)
1104 else:
1105 locker = 'user %s, process %s, host %s' \
1106 % (user, pid, host)
1107 ui.write(("%-6s %s (%ds)\n") % (name + ":", locker, age))
1108 return 1
1109 except OSError as e:
1110 if e.errno != errno.ENOENT:
1111 raise
1112
1113 ui.write(("%-6s free\n") % (name + ":"))
1114 return 0
1115
1116 held += report(repo.svfs, "lock", repo.lock)
1117 held += report(repo.vfs, "wlock", repo.wlock)
1118
1119 return held
1120
1046 @command('debugmergestate', [], '') 1121 @command('debugmergestate', [], '')
1047 def debugmergestate(ui, repo, *args): 1122 def debugmergestate(ui, repo, *args):
1048 """print merge state 1123 """print merge state
1049 1124
1050 Use --verbose to print out information about whether v1 or v2 merge state 1125 Use --verbose to print out information about whether v1 or v2 merge state