mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
Merge pull request #2459 from pre-commit/relative-commit-msg-filename
adjust relative --commit-msg-filename if in subdir
This commit is contained in:
commit
c1e0836d6b
2 changed files with 40 additions and 21 deletions
|
|
@ -155,6 +155,10 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
|
||||||
args.config = os.path.abspath(args.config)
|
args.config = os.path.abspath(args.config)
|
||||||
if args.command in {'run', 'try-repo'}:
|
if args.command in {'run', 'try-repo'}:
|
||||||
args.files = [os.path.abspath(filename) for filename in args.files]
|
args.files = [os.path.abspath(filename) for filename in args.files]
|
||||||
|
if args.commit_msg_filename is not None:
|
||||||
|
args.commit_msg_filename = os.path.abspath(
|
||||||
|
args.commit_msg_filename,
|
||||||
|
)
|
||||||
if args.command == 'try-repo' and os.path.exists(args.repo):
|
if args.command == 'try-repo' and os.path.exists(args.repo):
|
||||||
args.repo = os.path.abspath(args.repo)
|
args.repo = os.path.abspath(args.repo)
|
||||||
|
|
||||||
|
|
@ -164,6 +168,10 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
|
||||||
args.config = os.path.relpath(args.config)
|
args.config = os.path.relpath(args.config)
|
||||||
if args.command in {'run', 'try-repo'}:
|
if args.command in {'run', 'try-repo'}:
|
||||||
args.files = [os.path.relpath(filename) for filename in args.files]
|
args.files = [os.path.relpath(filename) for filename in args.files]
|
||||||
|
if args.commit_msg_filename is not None:
|
||||||
|
args.commit_msg_filename = os.path.relpath(
|
||||||
|
args.commit_msg_filename,
|
||||||
|
)
|
||||||
if args.command == 'try-repo' and os.path.exists(args.repo):
|
if args.command == 'try-repo' and os.path.exists(args.repo):
|
||||||
args.repo = os.path.relpath(args.repo)
|
args.repo = os.path.relpath(args.repo)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ from testing.util import cwd
|
||||||
def _args(**kwargs):
|
def _args(**kwargs):
|
||||||
kwargs.setdefault('command', 'help')
|
kwargs.setdefault('command', 'help')
|
||||||
kwargs.setdefault('config', C.CONFIG_FILE)
|
kwargs.setdefault('config', C.CONFIG_FILE)
|
||||||
|
if kwargs['command'] in {'run', 'try-repo'}:
|
||||||
|
kwargs.setdefault('commit_msg_filename', None)
|
||||||
return argparse.Namespace(**kwargs)
|
return argparse.Namespace(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -35,13 +37,24 @@ def test_adjust_args_and_chdir_noop(in_git_dir):
|
||||||
|
|
||||||
def test_adjust_args_and_chdir_relative_things(in_git_dir):
|
def test_adjust_args_and_chdir_relative_things(in_git_dir):
|
||||||
in_git_dir.join('foo/cfg.yaml').ensure()
|
in_git_dir.join('foo/cfg.yaml').ensure()
|
||||||
in_git_dir.join('foo').chdir()
|
with in_git_dir.join('foo').as_cwd():
|
||||||
|
args = _args(command='run', files=['f1', 'f2'], config='cfg.yaml')
|
||||||
|
main._adjust_args_and_chdir(args)
|
||||||
|
assert os.getcwd() == in_git_dir
|
||||||
|
assert args.config == os.path.join('foo', 'cfg.yaml')
|
||||||
|
assert args.files == [
|
||||||
|
os.path.join('foo', 'f1'),
|
||||||
|
os.path.join('foo', 'f2'),
|
||||||
|
]
|
||||||
|
|
||||||
args = _args(command='run', files=['f1', 'f2'], config='cfg.yaml')
|
|
||||||
main._adjust_args_and_chdir(args)
|
def test_adjust_args_and_chdir_relative_commit_msg(in_git_dir):
|
||||||
assert os.getcwd() == in_git_dir
|
in_git_dir.join('foo/cfg.yaml').ensure()
|
||||||
assert args.config == os.path.join('foo', 'cfg.yaml')
|
with in_git_dir.join('foo').as_cwd():
|
||||||
assert args.files == [os.path.join('foo', 'f1'), os.path.join('foo', 'f2')]
|
args = _args(command='run', files=[], commit_msg_filename='t.txt')
|
||||||
|
main._adjust_args_and_chdir(args)
|
||||||
|
assert os.getcwd() == in_git_dir
|
||||||
|
assert args.commit_msg_filename == os.path.join('foo', 't.txt')
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(os.name != 'nt', reason='windows feature')
|
@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
|
||||||
|
|
||||||
|
|
||||||
def test_adjust_args_and_chdir_non_relative_config(in_git_dir):
|
def test_adjust_args_and_chdir_non_relative_config(in_git_dir):
|
||||||
in_git_dir.join('foo').ensure_dir().chdir()
|
with in_git_dir.join('foo').ensure_dir().as_cwd():
|
||||||
|
args = _args()
|
||||||
args = _args()
|
main._adjust_args_and_chdir(args)
|
||||||
main._adjust_args_and_chdir(args)
|
assert os.getcwd() == in_git_dir
|
||||||
assert os.getcwd() == in_git_dir
|
assert args.config == C.CONFIG_FILE
|
||||||
assert args.config == C.CONFIG_FILE
|
|
||||||
|
|
||||||
|
|
||||||
def test_adjust_args_try_repo_repo_relative(in_git_dir):
|
def test_adjust_args_try_repo_repo_relative(in_git_dir):
|
||||||
in_git_dir.join('foo').ensure_dir().chdir()
|
with in_git_dir.join('foo').ensure_dir().as_cwd():
|
||||||
|
args = _args(command='try-repo', repo='../foo', files=[])
|
||||||
args = _args(command='try-repo', repo='../foo', files=[])
|
assert args.repo is not None
|
||||||
assert args.repo is not None
|
assert os.path.exists(args.repo)
|
||||||
assert os.path.exists(args.repo)
|
main._adjust_args_and_chdir(args)
|
||||||
main._adjust_args_and_chdir(args)
|
assert os.getcwd() == in_git_dir
|
||||||
assert os.getcwd() == in_git_dir
|
assert os.path.exists(args.repo)
|
||||||
assert os.path.exists(args.repo)
|
assert args.repo == 'foo'
|
||||||
assert args.repo == 'foo'
|
|
||||||
|
|
||||||
|
|
||||||
FNS = (
|
FNS = (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue