mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 18:11:48 +04:00
Merge pull request #3 from avlos/minimalize-upstream-changes
Minimize upstream changes.
This commit is contained in:
commit
5660da7d32
6 changed files with 153 additions and 2 deletions
0
pre_commit/avlos/__init__.py
Normal file
0
pre_commit/avlos/__init__.py
Normal file
6
pre_commit/avlos/constants.py
Normal file
6
pre_commit/avlos/constants.py
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
HOME_DIRECTORY = str(Path.home())
|
||||||
|
CONFIG_FILE = '.pre-commit-config.yaml'
|
||||||
|
DEFAULT_CONFIG_FILE = HOME_DIRECTORY + '/' + CONFIG_FILE
|
||||||
|
GITHUB_DOTFILES_REPOSITORY = 'https://github.com/avlos/dotfiles/'
|
||||||
104
pre_commit/avlos/install_uninstall.py
Normal file
104
pre_commit/avlos/install_uninstall.py
Normal file
|
|
@ -0,0 +1,104 @@
|
||||||
|
import itertools
|
||||||
|
import logging
|
||||||
|
import os.path
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Sequence
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
from pre_commit import git
|
||||||
|
from pre_commit import output
|
||||||
|
from pre_commit.clientlib import load_config
|
||||||
|
from pre_commit.repository import all_hooks
|
||||||
|
from pre_commit.repository import install_hook_envs
|
||||||
|
from pre_commit.store import Store
|
||||||
|
from pre_commit.util import make_executable
|
||||||
|
from pre_commit.util import resource_text
|
||||||
|
from pre_commit.commands.install_uninstall import _install_hook_script
|
||||||
|
from pre_commit.commands.install_uninstall import _hook_paths
|
||||||
|
|
||||||
|
# import avlos github with dotfiles #
|
||||||
|
from pathlib import Path
|
||||||
|
from git.repo.base import Repo
|
||||||
|
from pre_commit.avlos.constants import GITHUB_DOTFILES_REPOSITORY
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
##################################
|
||||||
|
### Avlos installation scripts ###
|
||||||
|
##################################
|
||||||
|
def setup_avlos() -> int:
|
||||||
|
"""
|
||||||
|
Installs the remote repository with dotfiles
|
||||||
|
and copies them over to home directory
|
||||||
|
"""
|
||||||
|
### install github repository ###
|
||||||
|
home_directory = str(Path.home())
|
||||||
|
installation_directory = home_directory + '/.dotfiles'
|
||||||
|
logger.info(
|
||||||
|
"Cloning git repository {} to {}".format(GITHUB_DOTFILES_REPOSITORY, installation_directory)
|
||||||
|
)
|
||||||
|
# check if folder exists, remove #
|
||||||
|
if os.path.isdir(installation_directory):
|
||||||
|
shutil.rmtree(installation_directory)
|
||||||
|
Repo.clone_from(GITHUB_DOTFILES_REPOSITORY, installation_directory)
|
||||||
|
|
||||||
|
### copy dotfiles to home directory ###
|
||||||
|
dotfiles = os.listdir(installation_directory)
|
||||||
|
dotfiles = [d for d in dotfiles if d.startswith('.') and d not in ['.git', '.gitignore']]
|
||||||
|
|
||||||
|
# check for existing dotfiles #
|
||||||
|
existing_dotfiles = list(set(os.listdir(home_directory)) & set(dotfiles))
|
||||||
|
if len(existing_dotfiles) > 0:
|
||||||
|
logger.warning(
|
||||||
|
"The configuration files {} already exist.".format(", ".join(existing_dotfiles))
|
||||||
|
)
|
||||||
|
answer = input("Overwrite? (Y/N): ")
|
||||||
|
if not answer.lower().startswith('y'):
|
||||||
|
dotfiles = list(set(dotfiles) - set(existing_dotfiles))
|
||||||
|
|
||||||
|
# copy to home directory
|
||||||
|
for dotfile in dotfiles:
|
||||||
|
shutil.copyfile(installation_directory + '/' + dotfile, home_directory + '/' + dotfile)
|
||||||
|
|
||||||
|
logger.info("Pre-commit configuration installed.")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def install_avlos(
|
||||||
|
store: Store,
|
||||||
|
hook_types: Sequence[str],
|
||||||
|
overwrite: bool = False,
|
||||||
|
hooks: bool = False,
|
||||||
|
skip_on_missing_config: bool = False,
|
||||||
|
git_dir: Optional[str] = None,
|
||||||
|
) -> int:
|
||||||
|
"""
|
||||||
|
Installs the remote repository with dotfiles
|
||||||
|
and copies them over to home directory
|
||||||
|
"""
|
||||||
|
### install github repository ###
|
||||||
|
logger.info("Installing hooks at git repository")
|
||||||
|
home_directory = str(Path.home())
|
||||||
|
|
||||||
|
installation_directory = home_directory + '/.dotfiles'
|
||||||
|
if os.path.isdir(installation_directory):
|
||||||
|
logger.warning("The configuration repository ~/.dotfiles already exists. If you want to update it run `pre-commit avlos-setup`")
|
||||||
|
else:
|
||||||
|
setup_avlos()
|
||||||
|
|
||||||
|
config_file = home_directory + '/.pre-commit-config.yaml'
|
||||||
|
|
||||||
|
for hook_type in hook_types:
|
||||||
|
_install_hook_script(
|
||||||
|
config_file,
|
||||||
|
hook_type,
|
||||||
|
overwrite=overwrite,
|
||||||
|
skip_on_missing_config=skip_on_missing_config,
|
||||||
|
git_dir=git_dir,
|
||||||
|
)
|
||||||
|
|
||||||
|
if hooks:
|
||||||
|
install_hooks(config_file, store)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
import sys
|
import sys
|
||||||
|
from pre_commit.avlos.constants import DEFAULT_CONFIG_FILE
|
||||||
|
|
||||||
if sys.version_info < (3, 8): # pragma: no cover (<PY38)
|
if sys.version_info < (3, 8): # pragma: no cover (<PY38)
|
||||||
import importlib_metadata
|
import importlib_metadata
|
||||||
else: # pragma: no cover (PY38+)
|
else: # pragma: no cover (PY38+)
|
||||||
import importlib.metadata as importlib_metadata
|
import importlib.metadata as importlib_metadata
|
||||||
|
|
||||||
CONFIG_FILE = '.pre-commit-config.yaml'
|
CONFIG_FILE = DEFAULT_CONFIG_FILE
|
||||||
MANIFEST_FILE = '.pre-commit-hooks.yaml'
|
MANIFEST_FILE = '.pre-commit-hooks.yaml'
|
||||||
|
|
||||||
# Bump when installation changes in a backwards / forwards incompatible way
|
# Bump when installation changes in a backwards / forwards incompatible way
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,8 @@ from pre_commit.error_handler import error_handler
|
||||||
from pre_commit.logging_handler import logging_handler
|
from pre_commit.logging_handler import logging_handler
|
||||||
from pre_commit.store import Store
|
from pre_commit.store import Store
|
||||||
|
|
||||||
|
from pre_commit.avlos.install_uninstall import setup_avlos
|
||||||
|
from pre_commit.avlos.install_uninstall import install_avlos
|
||||||
|
|
||||||
logger = logging.getLogger('pre_commit')
|
logger = logging.getLogger('pre_commit')
|
||||||
|
|
||||||
|
|
@ -36,7 +38,7 @@ logger = logging.getLogger('pre_commit')
|
||||||
os.environ.pop('__PYVENV_LAUNCHER__', None)
|
os.environ.pop('__PYVENV_LAUNCHER__', None)
|
||||||
|
|
||||||
|
|
||||||
COMMANDS_NO_GIT = {'clean', 'gc', 'init-templatedir', 'sample-config'}
|
COMMANDS_NO_GIT = {'clean', 'gc', 'init-templatedir', 'sample-config', 'avlos-setup'}
|
||||||
|
|
||||||
|
|
||||||
def _add_config_option(parser: argparse.ArgumentParser) -> None:
|
def _add_config_option(parser: argparse.ArgumentParser) -> None:
|
||||||
|
|
@ -239,6 +241,34 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
)
|
)
|
||||||
_add_hook_type_option(init_templatedir_parser)
|
_add_hook_type_option(init_templatedir_parser)
|
||||||
|
|
||||||
|
######################################
|
||||||
|
# add custom setup command for avlos #
|
||||||
|
######################################
|
||||||
|
setup_avlos_parser = subparsers.add_parser(
|
||||||
|
'avlos-setup', help='Sets up Avlos pre-commit configuration.',
|
||||||
|
)
|
||||||
|
add_color_option(setup_avlos_parser)
|
||||||
|
_add_config_option(setup_avlos_parser)
|
||||||
|
|
||||||
|
install_avlos_parser = subparsers.add_parser(
|
||||||
|
'avlos-install', help='Install Avlos pre-commit configuration to git repository.',
|
||||||
|
)
|
||||||
|
add_color_option(install_avlos_parser)
|
||||||
|
_add_config_option(install_avlos_parser)
|
||||||
|
install_avlos_parser.add_argument(
|
||||||
|
'-f', '--overwrite', action='store_true',
|
||||||
|
help='Overwrite existing hooks / remove migration mode.',
|
||||||
|
)
|
||||||
|
install_avlos_parser.add_argument(
|
||||||
|
'--install-hooks', action='store_true',
|
||||||
|
help=(
|
||||||
|
'Whether to install hook environments for all environments '
|
||||||
|
'in the config file.'
|
||||||
|
),
|
||||||
|
)
|
||||||
|
_add_hook_type_option(install_avlos_parser)
|
||||||
|
######################################
|
||||||
|
|
||||||
install_parser = subparsers.add_parser(
|
install_parser = subparsers.add_parser(
|
||||||
'install', help='Install the pre-commit script.',
|
'install', help='Install the pre-commit script.',
|
||||||
)
|
)
|
||||||
|
|
@ -371,6 +401,15 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
hooks=args.install_hooks,
|
hooks=args.install_hooks,
|
||||||
skip_on_missing_config=args.allow_missing_config,
|
skip_on_missing_config=args.allow_missing_config,
|
||||||
)
|
)
|
||||||
|
elif args.command == 'avlos-install':
|
||||||
|
return install_avlos(
|
||||||
|
store,
|
||||||
|
hook_types=args.hook_types,
|
||||||
|
overwrite=args.overwrite,
|
||||||
|
hooks=args.install_hooks
|
||||||
|
)
|
||||||
|
elif args.command == 'avlos-setup':
|
||||||
|
return setup_avlos()
|
||||||
elif args.command == 'init-templatedir':
|
elif args.command == 'init-templatedir':
|
||||||
return init_templatedir(
|
return init_templatedir(
|
||||||
args.config, store, args.directory,
|
args.config, store, args.directory,
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ install_requires =
|
||||||
virtualenv>=20.0.8
|
virtualenv>=20.0.8
|
||||||
importlib-metadata;python_version<"3.8"
|
importlib-metadata;python_version<"3.8"
|
||||||
importlib-resources;python_version<"3.7"
|
importlib-resources;python_version<"3.7"
|
||||||
|
GitPython>=3.1.14
|
||||||
python_requires = >=3.6.1
|
python_requires = >=3.6.1
|
||||||
|
|
||||||
[options.packages.find]
|
[options.packages.find]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue