Patchwork [2,of,5] blackbox: just try writing to repo.vfs and update lastlogger on success

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 30, 2018, 1:41 p.m.
Message ID <c299fe88974e640b8319.1543585285@mimosa>
Download mbox | patch
Permalink /patch/36879/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 30, 2018, 1:41 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1542460227 -32400
#      Sat Nov 17 22:10:27 2018 +0900
# Node ID c299fe88974e640b83192dcedabb9c4e599d7ed3
# Parent  d4060b39fec28b51ba98818eaa23e067da906402
blackbox: just try writing to repo.vfs and update lastlogger on success

This is simpler and more robust. Before, an empty ".hg" directory would be
created if it's removed after checking vfs.isdir('.').

Patch

diff --git a/hgext/blackbox.py b/hgext/blackbox.py
--- a/hgext/blackbox.py
+++ b/hgext/blackbox.py
@@ -121,7 +121,7 @@  def _openlogfile(ui, vfs):
                            newpath='%s.%d' % (path, i))
                 rotate(oldpath=path,
                        newpath=maxfiles > 0 and path + '.1')
-    return vfs(name, 'a')
+    return vfs(name, 'a', makeparentdirs=False)
 
 class proxylogger(object):
     """Forward log events to another logger to be set later"""
@@ -143,22 +143,10 @@  class blackboxlogger(object):
         self._repo = repo
         self._trackedevents = set(ui.configlist('blackbox', 'track'))
 
-    @property
-    def _bbvfs(self):
-        vfs = self._repo.vfs
-        if not vfs.isdir('.'):
-            vfs = None
-        return vfs
-
     def tracked(self, event):
         return b'*' in self._trackedevents or event in self._trackedevents
 
     def log(self, ui, event, msg, opts):
-        if self._bbvfs:
-            _lastlogger.logger = self
-        else:
-            return
-
         default = ui.configdate('devel', 'default-date')
         date = dateutil.datestr(default, ui.config('blackbox', 'date-format'))
         user = procutil.getuser()
@@ -178,13 +166,15 @@  class blackboxlogger(object):
         try:
             fmt = '%s %s @%s%s (%s)%s> %s'
             args = (date, user, rev, changed, pid, src, msg)
-            with _openlogfile(ui, self._bbvfs) as fp:
+            with _openlogfile(ui, self._repo.vfs) as fp:
                 fp.write(fmt % args)
         except (IOError, OSError) as err:
             # deactivate this to avoid failed logging again
             self._trackedevents.clear()
             ui.debug('warning: cannot write to blackbox.log: %s\n' %
                      encoding.strtolocal(err.strerror))
+            return
+        _lastlogger.logger = self
 
 def uipopulate(ui):
     ui.setlogger(b'blackbox', _lastlogger)
diff --git a/tests/test-blackbox.t b/tests/test-blackbox.t
--- a/tests/test-blackbox.t
+++ b/tests/test-blackbox.t
@@ -327,6 +327,29 @@  Test log recursion from dirty status che
 cleanup
   $ cd ..
 
+Test missing log directory, which shouldn't be created automatically
+
+  $ cat <<'EOF' > closeremove.py
+  > def reposetup(ui, repo):
+  >     class rmrepo(repo.__class__):
+  >         def close(self):
+  >             super(rmrepo, self).close()
+  >             self.ui.debug(b'removing %s\n' % self.vfs.base)
+  >             self.vfs.rmtree()
+  >     repo.__class__ = rmrepo
+  > EOF
+
+  $ hg init gone
+  $ cd gone
+  $ cat <<'EOF' > .hg/hgrc
+  > [extensions]
+  > closeremove = ../closeremove.py
+  > EOF
+  $ hg log --debug
+  removing $TESTTMP/gone/.hg
+  warning: cannot write to blackbox.log: $ENOENT$
+  $ cd ..
+
 #if chg
 
 when using chg, blackbox.log should get rotated correctly