hgext/blackbox.py
author Jun Wu <quark@fb.com>
Tue, 26 Sep 2017 12:48:15 -0700
changeset 34319 d64c2c050b54
parent 34307 e6723c939344
child 34517 49b72b6f6d66
permissions -rw-r--r--
progress: demonstrate non-linear progress has a bad ETA experience Previously, the ETA and speed assumes the progress is linear. Often, due to network or other issues, it could be fast for the most time, and suddenly slow down: [====================================================> ] \___________________________________________/\______/ very fast suddenly much slower This patch adds a test demonstrating the ETA could be way off in those cases. Differential Revision: https://phab.mercurial-scm.org/D819
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18676
1506eb487ddd blackbox: fix copyright
Bryan O'Sullivan <bryano@fb.com>
parents: 18675
diff changeset
     1
# blackbox.py - log repository events to a file for post-mortem debugging
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     2
#
18676
1506eb487ddd blackbox: fix copyright
Bryan O'Sullivan <bryano@fb.com>
parents: 18675
diff changeset
     3
# Copyright 2010 Nicolas Dumazet
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     4
# Copyright 2013 Facebook, Inc.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     5
#
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     6
# This software may be used and distributed according to the terms of the
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     7
# GNU General Public License version 2 or any later version.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     8
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     9
"""log repository events to a blackbox for debugging
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    10
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    11
Logs event information to .hg/blackbox.log to help debug and diagnose problems.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    12
The events that get logged can be configured via the blackbox.track config key.
28246
b862e793ec10 blackbox: log dirty state
timeless <timeless@mozdev.org>
parents: 28245
diff changeset
    13
19162
27013ace80eb blackbox: fix literal block syntax
Takumi IINO <trot.thunder@gmail.com>
parents: 19066
diff changeset
    14
Examples::
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    15
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    16
  [blackbox]
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    17
  track = *
28303
ce24de063aa5 blackbox: rewrite dirty documentation noting it is expensive
timeless <timeless@mozdev.org>
parents: 28248
diff changeset
    18
  # dirty is *EXPENSIVE* (slow);
ce24de063aa5 blackbox: rewrite dirty documentation noting it is expensive
timeless <timeless@mozdev.org>
parents: 28248
diff changeset
    19
  # each log entry indicates `+` if the repository is dirty, like :hg:`id`.
28246
b862e793ec10 blackbox: log dirty state
timeless <timeless@mozdev.org>
parents: 28245
diff changeset
    20
  dirty = True
28305
f5ae291dfedf blackbox: optionally log event source
timeless <timeless@mozdev.org>
parents: 28304
diff changeset
    21
  # record the source of log messages
f5ae291dfedf blackbox: optionally log event source
timeless <timeless@mozdev.org>
parents: 28304
diff changeset
    22
  logsource = True
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    23
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    24
  [blackbox]
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    25
  track = command, commandfinish, commandexception, exthook, pythonhook
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    26
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    27
  [blackbox]
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    28
  track = incoming
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    29
19066
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    30
  [blackbox]
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    31
  # limit the size of a log file
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    32
  maxsize = 1.5 MB
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    33
  # rotate up to N log files when the current one gets too big
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    34
  maxfiles = 3
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    35
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    36
"""
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    37
28090
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    38
from __future__ import absolute_import
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    39
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    40
import errno
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    41
import re
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    42
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    43
from mercurial.i18n import _
28245
caa2a0c6fbb7 blackbox: log working directory version
timeless <timeless@mozdev.org>
parents: 28244
diff changeset
    44
from mercurial.node import hex
caa2a0c6fbb7 blackbox: log working directory version
timeless <timeless@mozdev.org>
parents: 28244
diff changeset
    45
28090
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    46
from mercurial import (
32376
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32204
diff changeset
    47
    registrar,
28248
851c41a21869 blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents: 28247
diff changeset
    48
    ui as uimod,
28090
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    49
    util,
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    50
)
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    51
29852
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 28552
diff changeset
    52
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 23877
diff changeset
    53
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 23877
diff changeset
    54
# be specifying the version(s) of Mercurial they are tested with, or
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 23877
diff changeset
    55
# leave the attribute unspecified.
29852
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 28552
diff changeset
    56
testedwith = 'ships-with-hg-core'
33141
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32450
diff changeset
    57
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32450
diff changeset
    58
cmdtable = {}
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32450
diff changeset
    59
command = registrar.command(cmdtable)
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32450
diff changeset
    60
33142
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33141
diff changeset
    61
configtable = {}
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33141
diff changeset
    62
configitem = registrar.configitem(configtable)
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33141
diff changeset
    63
33191
1df74b71396d configitems: register the 'blackbox.dirty' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33143
diff changeset
    64
configitem('blackbox', 'dirty',
1df74b71396d configitems: register the 'blackbox.dirty' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33143
diff changeset
    65
    default=False,
1df74b71396d configitems: register the 'blackbox.dirty' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33143
diff changeset
    66
)
33142
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33141
diff changeset
    67
configitem('blackbox', 'maxsize',
33143
59c135bb31bc blackbox: use a human readable version of the default
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33142
diff changeset
    68
    default='1 MB',
33142
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33141
diff changeset
    69
)
33192
0ef40bb20264 configitems: register the 'blackbox.logsource' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33191
diff changeset
    70
configitem('blackbox', 'logsource',
0ef40bb20264 configitems: register the 'blackbox.logsource' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33191
diff changeset
    71
    default=False,
0ef40bb20264 configitems: register the 'blackbox.logsource' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33191
diff changeset
    72
)
33142
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33141
diff changeset
    73
28247
d2c0527af364 blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents: 28246
diff changeset
    74
lastui = None
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    75
34307
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    76
def _openlogfile(ui, vfs):
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    77
    def rotate(oldpath, newpath):
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    78
        try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    79
            vfs.unlink(newpath)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    80
        except OSError as err:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    81
            if err.errno != errno.ENOENT:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    82
                ui.debug("warning: cannot remove '%s': %s\n" %
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    83
                         (newpath, err.strerror))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    84
        try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    85
            if newpath:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    86
                vfs.rename(oldpath, newpath)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    87
        except OSError as err:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    88
            if err.errno != errno.ENOENT:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    89
                ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    90
                         (newpath, oldpath, err.strerror))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    91
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    92
    maxsize = ui.configbytes('blackbox', 'maxsize')
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    93
    name = 'blackbox.log'
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    94
    if maxsize > 0:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    95
        try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    96
            st = vfs.stat(name)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    97
        except OSError:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    98
            pass
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
    99
        else:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   100
            if st.st_size >= maxsize:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   101
                path = vfs.join(name)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   102
                maxfiles = ui.configint('blackbox', 'maxfiles', 7)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   103
                for i in xrange(maxfiles - 1, 1, -1):
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   104
                    rotate(oldpath='%s.%d' % (path, i - 1),
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   105
                           newpath='%s.%d' % (path, i))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   106
                rotate(oldpath=path,
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   107
                       newpath=maxfiles > 0 and path + '.1')
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   108
    return vfs(name, 'a')
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   109
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   110
def wrapui(ui):
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   111
    class blackboxui(ui.__class__):
34125
029b33adbd17 blackbox: remove _bbvfs state
Jun Wu <quark@fb.com>
parents: 34124
diff changeset
   112
        @property
029b33adbd17 blackbox: remove _bbvfs state
Jun Wu <quark@fb.com>
parents: 34124
diff changeset
   113
        def _bbvfs(self):
34306
b1d4ac068961 blackbox: do not prevent 'chg init' from working
Jun Wu <quark@fb.com>
parents: 34283
diff changeset
   114
            vfs = None
34125
029b33adbd17 blackbox: remove _bbvfs state
Jun Wu <quark@fb.com>
parents: 34124
diff changeset
   115
            repo = getattr(self, '_bbrepo', None)
029b33adbd17 blackbox: remove _bbvfs state
Jun Wu <quark@fb.com>
parents: 34124
diff changeset
   116
            if repo:
34306
b1d4ac068961 blackbox: do not prevent 'chg init' from working
Jun Wu <quark@fb.com>
parents: 34283
diff changeset
   117
                vfs = repo.vfs
b1d4ac068961 blackbox: do not prevent 'chg init' from working
Jun Wu <quark@fb.com>
parents: 34283
diff changeset
   118
                if not vfs.isdir('.'):
b1d4ac068961 blackbox: do not prevent 'chg init' from working
Jun Wu <quark@fb.com>
parents: 34283
diff changeset
   119
                    vfs = None
b1d4ac068961 blackbox: do not prevent 'chg init' from working
Jun Wu <quark@fb.com>
parents: 34283
diff changeset
   120
            return vfs
34125
029b33adbd17 blackbox: remove _bbvfs state
Jun Wu <quark@fb.com>
parents: 34124
diff changeset
   121
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   122
        @util.propertycache
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   123
        def track(self):
19052
63a783d1ac85 blackbox: fix a case of name capture
Bryan O'Sullivan <bryano@fb.com>
parents: 18831
diff changeset
   124
            return self.configlist('blackbox', 'track', ['*'])
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   125
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   126
        def log(self, event, *msg, **opts):
28247
d2c0527af364 blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents: 28246
diff changeset
   127
            global lastui
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   128
            super(blackboxui, self).log(event, *msg, **opts)
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   129
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   130
            if not '*' in self.track and not event in self.track:
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   131
                return
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   132
34124
cf04db16f583 blackbox: do not cache file objects
Jun Wu <quark@fb.com>
parents: 34123
diff changeset
   133
            if self._bbvfs:
28247
d2c0527af364 blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents: 28246
diff changeset
   134
                ui = self
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   135
            else:
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   136
                # certain ui instances exist outside the context of
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   137
                # a repo, so just default to the last blackbox that
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   138
                # was seen.
28247
d2c0527af364 blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents: 28246
diff changeset
   139
                ui = lastui
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   140
34124
cf04db16f583 blackbox: do not cache file objects
Jun Wu <quark@fb.com>
parents: 34123
diff changeset
   141
            if not ui:
28407
63da8bd0c65e blackbox: guard against recursion from dirty check
timeless <timeless@mozdev.org>
parents: 28305
diff changeset
   142
                return
34306
b1d4ac068961 blackbox: do not prevent 'chg init' from working
Jun Wu <quark@fb.com>
parents: 34283
diff changeset
   143
            vfs = ui._bbvfs
b1d4ac068961 blackbox: do not prevent 'chg init' from working
Jun Wu <quark@fb.com>
parents: 34283
diff changeset
   144
            if not vfs:
b1d4ac068961 blackbox: do not prevent 'chg init' from working
Jun Wu <quark@fb.com>
parents: 34283
diff changeset
   145
                return
b1d4ac068961 blackbox: do not prevent 'chg init' from working
Jun Wu <quark@fb.com>
parents: 34283
diff changeset
   146
34281
a37e18b5f055 blackbox: simplify ui states
Jun Wu <quark@fb.com>
parents: 34125
diff changeset
   147
            repo = getattr(ui, '_bbrepo', None)
a37e18b5f055 blackbox: simplify ui states
Jun Wu <quark@fb.com>
parents: 34125
diff changeset
   148
            if not lastui or repo:
28407
63da8bd0c65e blackbox: guard against recursion from dirty check
timeless <timeless@mozdev.org>
parents: 28305
diff changeset
   149
                lastui = ui
34281
a37e18b5f055 blackbox: simplify ui states
Jun Wu <quark@fb.com>
parents: 34125
diff changeset
   150
            if getattr(ui, '_bbinlog', False):
34124
cf04db16f583 blackbox: do not cache file objects
Jun Wu <quark@fb.com>
parents: 34123
diff changeset
   151
                # recursion and failure guard
28407
63da8bd0c65e blackbox: guard against recursion from dirty check
timeless <timeless@mozdev.org>
parents: 28305
diff changeset
   152
                return
34282
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   153
            ui._bbinlog = True
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   154
            default = self.configdate('devel', 'default-date')
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   155
            date = util.datestr(default, '%Y/%m/%d %H:%M:%S')
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   156
            user = util.getuser()
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   157
            pid = '%d' % util.getpid()
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   158
            formattedmsg = msg[0] % msg[1:]
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   159
            rev = '(unknown)'
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   160
            changed = ''
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   161
            if repo:
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   162
                ctx = repo[None]
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   163
                parents = ctx.parents()
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   164
                rev = ('+'.join([hex(p.node()) for p in parents]))
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   165
                if (ui.configbool('blackbox', 'dirty') and
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   166
                    ctx.dirty(missing=True, merge=False, branch=False)):
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   167
                    changed = '+'
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   168
            if ui.configbool('blackbox', 'logsource'):
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   169
                src = ' [%s]' % event
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   170
            else:
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   171
                src = ''
28407
63da8bd0c65e blackbox: guard against recursion from dirty check
timeless <timeless@mozdev.org>
parents: 28305
diff changeset
   172
            try:
34282
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   173
                fmt = '%s %s @%s%s (%s)%s> %s'
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   174
                args = (date, user, rev, changed, pid, src, formattedmsg)
34307
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34306
diff changeset
   175
                with _openlogfile(ui, vfs) as fp:
34282
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   176
                    fp.write(fmt % args)
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   177
            except (IOError, OSError) as err:
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   178
                self.debug('warning: cannot write to blackbox.log: %s\n' %
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   179
                           err.strerror)
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   180
                # do not restore _bbinlog intentionally to avoid failed
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   181
                # logging again
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   182
            else:
86a5df995880 blackbox: unindent a try block
Jun Wu <quark@fb.com>
parents: 34281
diff changeset
   183
                ui._bbinlog = False
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   184
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   185
        def setrepo(self, repo):
28248
851c41a21869 blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents: 28247
diff changeset
   186
            self._bbrepo = repo
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   187
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   188
    ui.__class__ = blackboxui
28248
851c41a21869 blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents: 28247
diff changeset
   189
    uimod.ui = blackboxui
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   190
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   191
def uisetup(ui):
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   192
    wrapui(ui)
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   193
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   194
def reposetup(ui, repo):
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   195
    # During 'hg pull' a httppeer repo is created to represent the remote repo.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   196
    # It doesn't have a .hg directory to put a blackbox in, so we don't do
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   197
    # the blackbox setup for it.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   198
    if not repo.local():
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   199
        return
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   200
19230
6d6f148cada9 blackbox: fix blackbox causing exceptions in tests
Durham Goode <durham@fb.com>
parents: 19162
diff changeset
   201
    if util.safehasattr(ui, 'setrepo'):
6d6f148cada9 blackbox: fix blackbox causing exceptions in tests
Durham Goode <durham@fb.com>
parents: 19162
diff changeset
   202
        ui.setrepo(repo)
34283
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34282
diff changeset
   203
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34282
diff changeset
   204
        # Set lastui even if ui.log is not called. This gives blackbox a
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34282
diff changeset
   205
        # fallback place to log.
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34282
diff changeset
   206
        global lastui
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34282
diff changeset
   207
        if lastui is None:
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34282
diff changeset
   208
            lastui = ui
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34282
diff changeset
   209
33436
9bb4decd43b0 repovfs: add a ward to check if locks are properly taken
Boris Feld <boris.feld@octobus.net>
parents: 33361
diff changeset
   210
    repo._wlockfreeprefix.add('blackbox.log')
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   211
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   212
@command('^blackbox',
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   213
    [('l', 'limit', 10, _('the number of events to show')),
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   214
    ],
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   215
    _('hg blackbox [OPTION]...'))
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   216
def blackbox(ui, repo, *revs, **opts):
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   217
    '''view the recent repository events
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   218
    '''
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   219
28026
a6db1163befa blackbox: refactor use of vfs as _bbvfs
timeless <timeless@mozdev.org>
parents: 28025
diff changeset
   220
    if not repo.vfs.exists('blackbox.log'):
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   221
        return
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   222
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   223
    limit = opts.get('limit')
28244
c17d7b1c40be blackbox: rename fp variable
timeless <timeless@mozdev.org>
parents: 28243
diff changeset
   224
    fp = repo.vfs('blackbox.log', 'r')
c17d7b1c40be blackbox: rename fp variable
timeless <timeless@mozdev.org>
parents: 28243
diff changeset
   225
    lines = fp.read().split('\n')
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   226
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   227
    count = 0
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   228
    output = []
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   229
    for line in reversed(lines):
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   230
        if count >= limit:
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   231
            break
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   232
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   233
        # count the commands by matching lines like: 2013/01/23 19:13:36 root>
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   234
        if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line):
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   235
            count += 1
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   236
        output.append(line)
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   237
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   238
    ui.status('\n'.join(reversed(output)))