[Concept,3/6] patman: Allow overriding the database directory with PATMAN_DB_DIR

Message ID 20260506153006.529909-4-sjg@u-boot.org
State New
Headers
Series patman: Concurrent DB access and per-series review worktrees |

Commit Message

Simon Glass May 6, 2026, 3:29 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

The database location is hardcoded to .patman.db in the top of the
current git repo, so running 'patman review' (or any other patman
command) in a second worktree or unrelated repo creates and uses a
fresh database there. Combined with the new WAL support, it would be
nice to keep a single 'main' database and reach it from anywhere.

Provide a PATMAN_DB_DIR environment variable that, when set, names the
directory in which patman looks for .patman.db, mirroring the default
layout. The override applies only to the database; git operations
still run against the current repo, so for example::

    export PATMAN_DB_DIR=~/u-boot
    cd ~/some-other-tree
    patman review -s my-series

uses the main database while applying patches in the other tree. The
value is passed through os.path.expanduser() so '~' works.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 tools/patman/cser_helper.py |  9 ++++++++-
 tools/patman/patman.rst     | 11 +++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
  

Patch

diff --git a/tools/patman/cser_helper.py b/tools/patman/cser_helper.py
index 0ce59e8ecc0..0274e28f42b 100644
--- a/tools/patman/cser_helper.py
+++ b/tools/patman/cser_helper.py
@@ -106,7 +106,14 @@  class CseriesHelper:
             if not self.topdir:
                 raise ValueError('No git repo detected in current directory')
         self.gitdir = os.path.join(self.topdir, '.git')
-        fname = f'{self.topdir}/.patman.db'
+
+        # PATMAN_DB_DIR lets the user keep a single 'main' database while
+        # running patman in other repos. Only the DB path is overridden;
+        # git operations still run against the current repo. Use 'or' (not
+        # os.environ.get(key, default)) so an empty PATMAN_DB_DIR='' falls
+        # back to topdir rather than producing '/.patman.db'
+        db_dir = os.environ.get('PATMAN_DB_DIR') or self.topdir
+        fname = os.path.join(os.path.expanduser(db_dir), '.patman.db')
 
         # For the first instance, start it up with the expected schema
         self.db, is_new = Database.get_instance(fname)
diff --git a/tools/patman/patman.rst b/tools/patman/patman.rst
index ef8e2e2025c..98a51bcfc6f 100644
--- a/tools/patman/patman.rst
+++ b/tools/patman/patman.rst
@@ -1465,6 +1465,17 @@  Patman stores series tracking, review and workflow state in a SQLite
 database (``.patman.db``) in the top-level git directory. The schema is
 versioned and auto-migrated on startup (currently at v10).
 
+Set the ``PATMAN_DB_DIR`` environment variable to use a database elsewhere
+(``~`` is expanded). The variable names a *directory* in which patman looks
+for ``.patman.db``, mirroring the default layout. This is useful if you keep
+a single 'main' patman database in one repo but want to run, for example,
+``patman review`` in another worktree or repo: the DB path is overridden but
+git operations still run against the current repo. For instance::
+
+    export PATMAN_DB_DIR=~/u-boot
+    cd ~/some-other-tree
+    patman review -s my-series
+
 The connection is opened in WAL mode (``journal_mode=WAL``,
 ``synchronous=NORMAL``) with a 30-second busy timeout, which lets multiple
 patman invocations share the database safely: concurrent readers do not block