From 1bf65b37c6ee4c6d9e38d129b8c19d6fb447589e Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Sat, 2 Oct 2021 18:59:48 +0200 Subject: [PATCH] Use os.path.realpath() for path canonicalization os.getcwd() returns the real path of the current working directory. In a directory which has been symlinked this does not necessarily match the same prefix of the current directory. When os.getcwd() is cd-ed to relativize path names a few lines below this results in paths which are relative to symlinked tree, not to the real tree. This makes path rule matches fail. Use os.path.realpath() when making paths absolute, so that relative paths refer to the same tree as os.getcwd() --- pre_commit/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pre_commit/main.py b/pre_commit/main.py index 2b50c91b..5103bf9b 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -159,11 +159,11 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None: def _adjust_args_and_chdir(args: argparse.Namespace) -> None: # `--config` was specified relative to the non-root working directory if os.path.exists(args.config): - args.config = os.path.abspath(args.config) + args.config = os.path.realpath(args.config) if args.command in {'run', 'try-repo'}: - args.files = [os.path.abspath(filename) for filename in args.files] + args.files = [os.path.realpath(filename) for filename in args.files] if args.command == 'try-repo' and os.path.exists(args.repo): - args.repo = os.path.abspath(args.repo) + args.repo = os.path.realpath(args.repo) toplevel = git.get_root() os.chdir(toplevel)