Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/shellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ jobs:

- name: Run ShellCheck on all shell scripts
run: |
shellcheck pre-commit commit-msg pre-push post-checkout post-commit post-merge install.sh uninstall.sh
shellcheck pre-commit commit-msg pre-push post-checkout post-commit post-merge install.sh uninstall.sh test/install_test.sh

- name: Run install script regression tests
run: bash test/install_test.sh
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ See what's currently configured without changing anything:
cd ~/.git-hooks && ./install.sh --check
```

`--check` can be combined with other flags like `--template` to inspect what would be used without changing your git config.

### Uninstall

```bash
Expand Down
37 changes: 35 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,41 @@
set -euo pipefail

hook_dir="$(cd "$(dirname "$0")" && pwd)"
use_template=false
check_only=false

if [ "${1:-}" = "--check" ]; then
usage() {
cat <<EOF
Usage: ./install.sh [--template] [--check] [--help]

Options:
--template Use init.templateDir instead of core.hooksPath
--check Show current hooks configuration without changing anything
--help Show this help text
EOF
}

for arg in "$@"; do
case "$arg" in
--template)
use_template=true
;;
--check)
check_only=true
;;
--help|-h)
usage
exit 0
;;
*)
echo "Unknown option: $arg" >&2
usage >&2
exit 1
;;
esac
done

if [ "$check_only" = true ]; then
hooks_path=$(git config --global --get core.hooksPath 2>/dev/null || true)
template_dir=$(git config --global --get init.templateDir 2>/dev/null || true)
if [ -n "$hooks_path" ]; then
Expand All @@ -33,7 +66,7 @@ if [ "${1:-}" = "--check" ]; then
exit 0
fi

if [ "${1:-}" = "--template" ]; then
if [ "$use_template" = true ]; then
git config --global init.templateDir "$hook_dir"
echo "Installed: init.templateDir set to $hook_dir"
echo "New repos created with git init/clone will copy these hooks."
Expand Down
67 changes: 67 additions & 0 deletions test/install_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash

set -euo pipefail

repo_root="$(cd "$(dirname "$0")/.." && pwd)"
install_script="$repo_root/install.sh"
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT

export HOME="$tmpdir/home"
mkdir -p "$HOME"

global_config="$HOME/.gitconfig"

run() {
local expected_status="$1"
shift
set +e
output=$(GIT_CONFIG_GLOBAL="$global_config" "$@" 2>&1)
status=$?
set -e
if [ "$status" -ne "$expected_status" ]; then
echo "expected exit $expected_status, got $status" >&2
echo "$output" >&2
exit 1
fi
printf '%s' "$output"
}

assert_contains() {
local haystack="$1"
local needle="$2"
if ! printf '%s' "$haystack" | grep -Fq "$needle"; then
echo "expected output to contain: $needle" >&2
echo "$haystack" >&2
exit 1
fi
}

assert_unset() {
local key="$1"
if GIT_CONFIG_GLOBAL="$global_config" git config --global --get "$key" >/dev/null 2>&1; then
echo "expected $key to be unset" >&2
exit 1
fi
}

output=$(run 0 bash "$install_script" --check)
assert_contains "$output" "No global hooks configuration found."

output=$(run 0 bash "$install_script" --template --check)
assert_contains "$output" "No global hooks configuration found."
assert_unset core.hooksPath
assert_unset init.templateDir

output=$(run 0 bash "$install_script" --template)
assert_contains "$output" "Installed: init.templateDir set to $repo_root"
template_dir=$(GIT_CONFIG_GLOBAL="$global_config" git config --global --get init.templateDir)
[ "$template_dir" = "$repo_root" ]

output=$(run 1 bash "$install_script" --bogus)
assert_contains "$output" "Unknown option: --bogus"

output=$(run 0 bash "$install_script" --help)
assert_contains "$output" "Usage: ./install.sh"

echo "install.sh regression tests passed"
Loading