blackbox: just try writing to repo.vfs and update lastlogger on success
authorYuya Nishihara <yuya@tcha.org>
Sat, 17 Nov 2018 22:10:27 +0900
changeset 40797 ea2688c84e4b
parent 40796 03bca908d9fb
child 40798 644adf9c20fb
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('.').
hgext/blackbox.py
tests/test-blackbox.t
--- a/hgext/blackbox.py	Tue Nov 20 22:31:12 2018 +0900
+++ b/hgext/blackbox.py	Sat Nov 17 22:10:27 2018 +0900
@@ -121,7 +121,7 @@
                            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 @@
         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 @@
         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)
--- a/tests/test-blackbox.t	Tue Nov 20 22:31:12 2018 +0900
+++ b/tests/test-blackbox.t	Sat Nov 17 22:10:27 2018 +0900
@@ -327,6 +327,29 @@
 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