From b35d61311c7a7fd3c88ddfb71f53d606fb992ddb Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Thu, 11 Jun 2026 11:55:19 +0200 Subject: [PATCH] App: Support --topdir-format "relative" The global --topdir-format option governs how a project's root directory is represented in paths output by various queries. "absolute" means as absolute path, "unaltered" means verbatim as specified via --topdir, make:xyz means replaced by the string $(xyz), for later expansion in a makefile variable. This commit adds another variant: "relative" yields the shortest possible output format of the output path in question relative to --topdir, with "shortest possible" in this context meaning canonicalized and leading "./" stripped. Signed-off-by: Jan Lindemann --- src/python/jw/pkg/App.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/python/jw/pkg/App.py b/src/python/jw/pkg/App.py index c7775ae5..a018fddb 100644 --- a/src/python/jw/pkg/App.py +++ b/src/python/jw/pkg/App.py @@ -83,6 +83,8 @@ class App(Base): match fmt: case 'unaltered': return path + case 'relative': + return os.path.relpath(path) case None | 'absolute': return os.path.abspath(path) case _: @@ -135,6 +137,8 @@ class App(Base): return os.path.abspath(pd) if self.__topdir_fmt == 'unaltered': return pd + if self.__topdir_fmt == 'relative': + return os.path.relpath(pd, self.__pretty_topdir) if name == self.__top_name: return self.__pretty_topdir raise NotImplementedError( @@ -151,7 +155,10 @@ class App(Base): if os.path.isdir(path): ret = format_pd(name, pd, pretty) if sd and sd[0] != '/': - ret += '/' + if ret == '.': + ret = '' + else: + ret += '/' ret += sd return ret for ret in search_absdirs: @@ -345,7 +352,7 @@ class App(Base): default = 'absolute', help = ( 'Output references to topdir as one of "make:", ' - '"unaltered", "absolute". Absolute topdir by default' + '"unaltered", "relative", "absolute". Absolute topdir by default' ), ) parser.add_argument(