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 <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-06-11 11:55:19 +02:00
commit b35d61311c
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -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,6 +155,9 @@ class App(Base):
if os.path.isdir(path):
ret = format_pd(name, pd, pretty)
if sd and sd[0] != '/':
if ret == '.':
ret = ''
else:
ret += '/'
ret += sd
return ret
@ -345,7 +352,7 @@ class App(Base):
default = 'absolute',
help = (
'Output references to topdir as one of "make:<var-name>", '
'"unaltered", "absolute". Absolute topdir by default'
'"unaltered", "relative", "absolute". Absolute topdir by default'
),
)
parser.add_argument(