From 573405bb8835e1766df7c65650558c4e155bc562 Mon Sep 17 00:00:00 2001 From: Tim Ruffing Date: Fri, 7 Nov 2025 15:52:06 +0100 Subject: [PATCH 1/2] Hard-code default config paths in help strings This fixes a bug with fish completions. Before this commit, `borgmatic --fish-completion` would generate a fish completion script with an expanded XDG_CONFIG_HOME (if this variable is set). This is problematic for package maintainers if the variable is set during the creation of a package. Moreover, it is problematic if the user has set this variable because the fish completion scripts checks its own consistency with `borg --fish-completion` during startup to detect version mismatches. If the user has a packaged fish completion file with "~/.config/$HOME" in it, this won't match the output of `borg --fish-completion` because the latter contains the expansion of $XDG_CONFIG_HOME. As a result, the script will incorrectly conclude that it is outdated. --- borgmatic/commands/arguments.py | 9 ++++----- borgmatic/config/collect.py | 15 ++++++--------- tests/unit/config/test_collect.py | 8 -------- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index 8dd35b59..c51adc3b 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -568,8 +568,7 @@ def make_parsers(schema, unparsed_arguments): # noqa: PLR0915 ignoring actions, and the combined parser is handy for displaying help that includes everything: global flags, a list of actions, etc. ''' - config_paths = collect.get_default_config_paths(expand_home=True) - unexpanded_config_paths = collect.get_default_config_paths(expand_home=False) + config_paths = collect.get_default_config_paths() # Using allow_abbrev=False here prevents the global parser from erroring about "ambiguous" # options like --encryption. Such options are intended for an action parser rather than the @@ -582,7 +581,7 @@ def make_parsers(schema, unparsed_arguments): # noqa: PLR0915 '--config', dest='config_paths', action='append', - help=f"Configuration filename or directory, can specify flag multiple times, defaults to: -c {' -c '.join(unexpanded_config_paths)}", + help='Configuration filename or directory, can specify flag multiple times, defaults to "-c \'/etc/borgmatic/config.yaml\' -c \'/etc/borgmatic.d\' -c \'$XDG_CONFIG_HOME/borgmatic/config.yaml\' -c \'$XDG_CONFIG_HOME/borgmatic.d\'", where $XDG_CONFIG_HOME defaults to "$HOME/.config"', ) global_group.add_argument( '-n', @@ -1215,7 +1214,7 @@ def make_parsers(schema, unparsed_arguments): # noqa: PLR0915 '--destination', dest='destination_path', default=config_paths[0], - help=f'Destination configuration file (or directory if using --split), default: {unexpanded_config_paths[0]}', + help='Destination configuration file (or directory if using --split), default: /etc/borgmatic/config.yaml', ) config_generate_group.add_argument( '--overwrite', @@ -2015,7 +2014,7 @@ def parse_arguments(schema, *unparsed_arguments): ) if not arguments['global'].config_paths: - arguments['global'].config_paths = collect.get_default_config_paths(expand_home=True) + arguments['global'].config_paths = collect.get_default_config_paths() for action_name in ('bootstrap', 'generate', 'validate'): if action_name in arguments and len(arguments) > HIGHLANDER_ACTION_ARGUMENTS_COUNT: diff --git a/borgmatic/config/collect.py b/borgmatic/config/collect.py index 2677f5b1..0edf1412 100644 --- a/borgmatic/config/collect.py +++ b/borgmatic/config/collect.py @@ -1,17 +1,14 @@ import os -def get_default_config_paths(expand_home=True): +def get_default_config_paths(): ''' - Based on the value of the XDG_CONFIG_HOME and HOME environment variables, return a list of - default configuration paths. This includes both system-wide configuration and configuration in - the current user's home directory. - - Don't expand the home directory ($HOME) if the expand home flag is False. + Return a list of default configuration paths. This includes both system-wide + configuration and configuration in the current user's home directory. ''' - user_config_directory = os.getenv('XDG_CONFIG_HOME') or os.path.join('$HOME', '.config') - if expand_home: - user_config_directory = os.path.expandvars(user_config_directory) + user_config_directory = os.path.expandvars( + os.getenv('XDG_CONFIG_HOME') or os.path.join('$HOME', '.config') + ) return [ '/etc/borgmatic/config.yaml', diff --git a/tests/unit/config/test_collect.py b/tests/unit/config/test_collect.py index 63881371..037af38b 100644 --- a/tests/unit/config/test_collect.py +++ b/tests/unit/config/test_collect.py @@ -21,14 +21,6 @@ def test_get_default_config_paths_prefers_xdg_config_home_for_user_config_path() assert '/home/user/.etc/borgmatic/config.yaml' in config_paths -def test_get_default_config_paths_does_not_expand_home_when_false(): - flexmock(module.os, environ={'HOME': '/home/user'}) - - config_paths = module.get_default_config_paths(expand_home=False) - - assert '$HOME/.config/borgmatic/config.yaml' in config_paths - - def test_collect_config_filenames_collects_yml_file_endings(): config_paths = ('config.yaml', '/etc/borgmatic.d') mock_path = flexmock(module.os.path) From ea45a37db88da33e7bab1b7d6626c24c453d0e9f Mon Sep 17 00:00:00 2001 From: Tim Ruffing Date: Tue, 18 Nov 2025 09:55:54 +0100 Subject: [PATCH 2/2] Omit "-c" when describing default value for -c --- borgmatic/commands/arguments.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index c51adc3b..40b540f9 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -581,7 +581,7 @@ def make_parsers(schema, unparsed_arguments): # noqa: PLR0915 '--config', dest='config_paths', action='append', - help='Configuration filename or directory, can specify flag multiple times, defaults to "-c \'/etc/borgmatic/config.yaml\' -c \'/etc/borgmatic.d\' -c \'$XDG_CONFIG_HOME/borgmatic/config.yaml\' -c \'$XDG_CONFIG_HOME/borgmatic.d\'", where $XDG_CONFIG_HOME defaults to "$HOME/.config"', + help='Configuration filename or directory, can specify flag multiple times, defaults to /etc/borgmatic/config.yaml, /etc/borgmatic.d, $XDG_CONFIG_HOME/borgmatic/config.yaml, and $XDG_CONFIG_HOME/borgmatic.d, where $XDG_CONFIG_HOME defaults to $HOME/.config', ) global_group.add_argument( '-n',