Skip to content

Commit

Permalink
Add in precommit hook install.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexhuszagh committed Sep 8, 2024
1 parent d5e5a70 commit 0f524e6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"*.svg.in": "xml",
"stdexcept": "cpp"
},
"cmake.ignoreCMakeListsMissing": true
"cmake.ignoreCMakeListsMissing": true,
"python.REPL.enableREPLSmartSend": false
}
77 changes: 76 additions & 1 deletion vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,26 @@
'TODO.md',
]

HOOK_SCRIPT = '''
#!/usr/bin/env bash
#
# A custom Git hook.
set -eux pipefail
hooks_home="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
git_home="$(dirname "${hooks_home}")"
project_home="$(dirname "${git_home}")"
'''


def parse_args(argv=None):
'''Parse the command-line options.'''

parser = argparse.ArgumentParser(description='Git configuration changes.')
parser.add_argument('-v', '--version', action='version', version=f'%(prog)s {__version__}')

dist = parser.add_mutually_exclusive_group()
dist.add_argument(
'--track-dist',
Expand All @@ -245,6 +259,7 @@ def parse_args(argv=None):
help='do not track changes to distribution files',
action='store_true',
)

gitignore = parser.add_mutually_exclusive_group()
gitignore.add_argument(
'--track-gitignore',
Expand All @@ -257,6 +272,18 @@ def parse_args(argv=None):
action='store_true',
)

hooks = parser.add_mutually_exclusive_group()
hooks.add_argument(
'--install-hooks',
help='install our git hooks',
action='store_true',
)
hooks.add_argument(
'--uninstall-hooks',
help='uninstall our git hooks',
action='store_true',
)

return parser.parse_args(argv)


Expand Down Expand Up @@ -310,6 +337,48 @@ def write_gitignore(entries):
file.write(f'{custom}\n{PYTHON_GITIGNORE}\n{CPP_GITIGNORE}\n')


def pip_install(packages):
'''Install our PIP dependencies.'''
subprocess.check_call(
[sys.executable, '-m', 'pip', 'install'] + packages + ['--user'],
stdin=subprocess.DEVNULL,
shell=False,
)


def chmod(mode, *args):
'''Modify our permissions for one or more files.'''
subprocess.check_call(
['chmod', mode, *args],
stdin=subprocess.DEVNULL,
shell=False,
)


def install_hooks():
'''Install our Git hooks.'''

pip_install(['pylint', 'pyright', 'flake8', 'isort', 'black'])
if os.name == 'nt':
pip_install(['winrt-Windows.UI.ViewManagement', 'winrt-Windows.UI'])
scripts = ['lint', 'fmt', 'configure']
precommit = HOOK_SCRIPT + '\n'.join([f'scripts/{i}.sh' for i in scripts])
path = f'{home}/.git/hooks/pre-commit'
with open(path, 'w', encoding='utf8') as file:
file.write(precommit)
chmod('+x', path)


def uninstall_hooks():
'''Uninstall our Git hooks.'''

path = f'{home}/.git/hooks/pre-commit'
try:
os.unlink(path)
except FileNotFoundError:
pass


def main(argv=None):
'''Configuration entry point'''

Expand All @@ -328,13 +397,19 @@ def main(argv=None):
if git is None:
raise FileNotFoundError(errno.ENOENT, "No such file or directory: 'git'")

# if we're installing our hooks, do it quickly.
if args.install_hooks:
install_hooks()
elif args.uninstall_hooks:
uninstall_hooks()

# Determine if we need to assume unchanged gitignore,
# and then update our track dist. This is since normally
# we assume tracking/untracking dist should ignore
# our gitignore file.
if args.track_gitignore:
no_assume_unchanged(git, '.gitignore')
else:
elif args.no_track_gitignore:
assume_unchanged(git, '.gitignore')

# Determine if we need to update our gitignore.
Expand Down

0 comments on commit 0f524e6

Please sign in to comment.