Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db51d30

Browse files
committed
adjust relative --commit-msg-filename if in subdir
1 parent 78a2d86 commit db51d30

2 files changed

Lines changed: 41 additions & 22 deletions

File tree

pre_commit/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
155155
args.config = os.path.abspath(args.config)
156156
if args.command in {'run', 'try-repo'}:
157157
args.files = [os.path.abspath(filename) for filename in args.files]
158+
if args.commit_msg_filename is not None:
159+
args.commit_msg_filename = os.path.abspath(
160+
args.commit_msg_filename,
161+
)
158162
if args.command == 'try-repo' and os.path.exists(args.repo):
159163
args.repo = os.path.abspath(args.repo)
160164

@@ -164,6 +168,10 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
164168
args.config = os.path.relpath(args.config)
165169
if args.command in {'run', 'try-repo'}:
166170
args.files = [os.path.relpath(filename) for filename in args.files]
171+
if args.commit_msg_filename is not None:
172+
args.commit_msg_filename = os.path.relpath(
173+
args.commit_msg_filename,
174+
)
167175
if args.command == 'try-repo' and os.path.exists(args.repo):
168176
args.repo = os.path.relpath(args.repo)
169177

tests/main_test.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
def _args(**kwargs):
1818
kwargs.setdefault('command', 'help')
1919
kwargs.setdefault('config', C.CONFIG_FILE)
20+
if kwargs['command'] in {'run', 'try-repo'}:
21+
kwargs.setdefault('commit_msg_filename', None)
2022
return argparse.Namespace(**kwargs)
2123

2224

@@ -35,13 +37,24 @@ def test_adjust_args_and_chdir_noop(in_git_dir):
3537

3638
def test_adjust_args_and_chdir_relative_things(in_git_dir):
3739
in_git_dir.join('foo/cfg.yaml').ensure()
38-
in_git_dir.join('foo').chdir()
39-
40-
args = _args(command='run', files=['f1', 'f2'], config='cfg.yaml')
41-
main._adjust_args_and_chdir(args)
42-
assert os.getcwd() == in_git_dir
43-
assert args.config == os.path.join('foo', 'cfg.yaml')
44-
assert args.files == [os.path.join('foo', 'f1'), os.path.join('foo', 'f2')]
40+
with in_git_dir.join('foo').as_cwd():
41+
args = _args(command='run', files=['f1', 'f2'], config='cfg.yaml')
42+
main._adjust_args_and_chdir(args)
43+
assert os.getcwd() == in_git_dir
44+
assert args.config == os.path.join('foo', 'cfg.yaml')
45+
assert args.files == [
46+
os.path.join('foo', 'f1'),
47+
os.path.join('foo', 'f2'),
48+
]
49+
50+
51+
def test_adjust_args_and_chdir_relative_commit_msg(in_git_dir):
52+
in_git_dir.join('foo/cfg.yaml').ensure()
53+
with in_git_dir.join('foo').as_cwd():
54+
args = _args(command='run', files=[], commit_msg_filename='t.txt')
55+
main._adjust_args_and_chdir(args)
56+
assert os.getcwd() == in_git_dir
57+
assert args.commit_msg_filename == os.path.join('foo', 't.txt')
4558

4659

4760
@pytest.mark.skipif(os.name != 'nt', reason='windows feature')
@@ -56,24 +69,22 @@ def test_install_on_subst(in_git_dir, store): # pragma: posix no cover
5669

5770

5871
def test_adjust_args_and_chdir_non_relative_config(in_git_dir):
59-
in_git_dir.join('foo').ensure_dir().chdir()
60-
61-
args = _args()
62-
main._adjust_args_and_chdir(args)
63-
assert os.getcwd() == in_git_dir
64-
assert args.config == C.CONFIG_FILE
72+
with in_git_dir.join('foo').ensure_dir().as_cwd():
73+
args = _args()
74+
main._adjust_args_and_chdir(args)
75+
assert os.getcwd() == in_git_dir
76+
assert args.config == C.CONFIG_FILE
6577

6678

6779
def test_adjust_args_try_repo_repo_relative(in_git_dir):
68-
in_git_dir.join('foo').ensure_dir().chdir()
69-
70-
args = _args(command='try-repo', repo='../foo', files=[])
71-
assert args.repo is not None
72-
assert os.path.exists(args.repo)
73-
main._adjust_args_and_chdir(args)
74-
assert os.getcwd() == in_git_dir
75-
assert os.path.exists(args.repo)
76-
assert args.repo == 'foo'
80+
with in_git_dir.join('foo').ensure_dir().as_cwd():
81+
args = _args(command='try-repo', repo='../foo', files=[])
82+
assert args.repo is not None
83+
assert os.path.exists(args.repo)
84+
main._adjust_args_and_chdir(args)
85+
assert os.getcwd() == in_git_dir
86+
assert os.path.exists(args.repo)
87+
assert args.repo == 'foo'
7788

7889

7990
FNS = (

0 commit comments

Comments
 (0)