Repository navigation
fix(setup-hooks): work in a linked git worktree (#137) #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
royosherove
wants to merge
2
commits into
main
Choose a base branch
from
crew/drew/issue-137
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+139
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| #!/usr/bin/env bash | ||
| # tests/test-setup-hooks.sh — Test for scripts/setup-hooks.sh. | ||
| # | ||
| # In a linked worktree (git worktree add), .git is a file, not a directory, | ||
| # and git reads hooks from the common git directory. This test makes a | ||
| # temporary repository with a linked worktree and makes sure that: | ||
| # - setup-hooks.sh exits 0 in the main checkout and in the linked worktree | ||
| # - the pre-commit hook is in the hooks directory that git uses | ||
| # - setup-hooks.sh exits 1 outside a git repository | ||
| # - setup-hooks.sh exits 1 when git finds only an ancestor repository, and | ||
| # installs no hook into that repository | ||
| # - setup-hooks.sh exits 0 when core.hooksPath points to scripts/ | ||
| # | ||
| # No AWS required. Run standalone or via CI (discovered by pack-tests.yml). | ||
| set -euo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" | ||
|
|
||
| RED='\033[0;31m'; GREEN='\033[0;32m'; BOLD='\033[1m'; NC='\033[0m' | ||
| PASS=0; FAIL=0 | ||
|
|
||
| pass() { printf "${GREEN} \xE2\x9C\x93${NC} %s\n" "$1"; PASS=$((PASS + 1)); } | ||
| fail() { printf "${RED} \xE2\x9C\x97${NC} %s\n" "$1"; FAIL=$((FAIL + 1)); } | ||
|
|
||
| printf "${BOLD}scripts/setup-hooks.sh${NC}\n" | ||
|
|
||
| WORK="$(mktemp -d)" | ||
| trap 'rm -rf "$WORK"' EXIT | ||
|
|
||
| # Isolate from the user's git configuration. | ||
| export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 | ||
| G=(git -c user.name=test -c user.email=test@example.invalid -c init.defaultBranch=main) | ||
|
|
||
| MAIN="$WORK/main" | ||
| mkdir -p "$MAIN/scripts" | ||
| cp "$REPO_ROOT/scripts/setup-hooks.sh" "$REPO_ROOT/scripts/pre-commit" "$MAIN/scripts/" | ||
| "${G[@]}" -C "$MAIN" init -q | ||
| "${G[@]}" -C "$MAIN" add scripts | ||
| "${G[@]}" -C "$MAIN" commit -q -m init | ||
| "${G[@]}" -C "$MAIN" worktree add -q "$WORK/linked" -b linked | ||
|
|
||
| # Main checkout. | ||
| if bash "$MAIN/scripts/setup-hooks.sh" >/dev/null 2>&1; then | ||
| pass "exits 0 in the main checkout" | ||
| else | ||
| fail "exits non-zero in the main checkout" | ||
| fi | ||
| if [[ -x "$MAIN/.git/hooks/pre-commit" ]]; then | ||
| pass "pre-commit hook installed in .git/hooks" | ||
| else | ||
| fail "pre-commit hook missing from .git/hooks" | ||
| fi | ||
|
|
||
| # Linked worktree: remove the hook first, so the check below proves this run. | ||
| rm -f "$MAIN/.git/hooks/pre-commit" | ||
| if bash "$WORK/linked/scripts/setup-hooks.sh" >/dev/null 2>&1; then | ||
| pass "exits 0 in a linked worktree" | ||
| else | ||
| fail "exits non-zero in a linked worktree" | ||
| fi | ||
| hooks_dir="$(git -C "$WORK/linked" rev-parse --git-path hooks)" | ||
| [[ "$hooks_dir" = /* ]] || hooks_dir="$WORK/linked/$hooks_dir" | ||
| if [[ -x "$hooks_dir/pre-commit" ]]; then | ||
| pass "pre-commit hook installed in the hooks directory git uses" | ||
| else | ||
| fail "pre-commit hook missing from $hooks_dir" | ||
| fi | ||
| if [[ ! -e "$WORK/linked/.git/hooks" ]]; then | ||
| pass "no hooks directory created under the worktree .git file" | ||
| else | ||
| fail "hooks directory created under the worktree .git file" | ||
| fi | ||
|
|
||
| # Not a git repository. | ||
| NOREPO="$WORK/norepo" | ||
| mkdir -p "$NOREPO/scripts" | ||
| cp "$REPO_ROOT/scripts/setup-hooks.sh" "$NOREPO/scripts/" | ||
| rc=0 | ||
| GIT_CEILING_DIRECTORIES="$WORK" bash "$NOREPO/scripts/setup-hooks.sh" >/dev/null 2>&1 || rc=$? | ||
| if [[ $rc -eq 1 ]]; then | ||
| pass "exits 1 outside a git repository" | ||
| else | ||
| fail "exits $rc outside a git repository (expected 1)" | ||
| fi | ||
|
|
||
| # Copy inside an unrelated repository: git finds only the ancestor. | ||
| OUTER="$WORK/outer" | ||
| "${G[@]}" init -q "$OUTER" | ||
| mkdir -p "$OUTER/vendor/lowkey/scripts" | ||
| cp "$REPO_ROOT/scripts/setup-hooks.sh" "$REPO_ROOT/scripts/pre-commit" "$OUTER/vendor/lowkey/scripts/" | ||
| rc=0 | ||
| bash "$OUTER/vendor/lowkey/scripts/setup-hooks.sh" >/dev/null 2>&1 || rc=$? | ||
| if [[ $rc -eq 1 ]]; then | ||
| pass "exits 1 when git finds only an ancestor repository" | ||
| else | ||
| fail "exits $rc when git finds only an ancestor repository (expected 1)" | ||
| fi | ||
| if [[ ! -e "$OUTER/.git/hooks/pre-commit" ]]; then | ||
| pass "no hook installed into the ancestor repository" | ||
| else | ||
| fail "hook installed into the ancestor repository" | ||
| fi | ||
|
|
||
| # core.hooksPath points to the directory that holds the source hook. | ||
| "${G[@]}" -C "$MAIN" config core.hooksPath scripts | ||
| if bash "$MAIN/scripts/setup-hooks.sh" >/dev/null 2>&1; then | ||
| pass "exits 0 when core.hooksPath is scripts" | ||
| else | ||
| fail "exits non-zero when core.hooksPath is scripts" | ||
| fi | ||
| if [[ -x "$MAIN/scripts/pre-commit" ]]; then | ||
| pass "pre-commit hook in scripts/ is executable" | ||
| else | ||
| fail "pre-commit hook in scripts/ is not executable" | ||
| fi | ||
| "${G[@]}" -C "$MAIN" config --unset core.hooksPath | ||
|
|
||
| printf "\n%d passed, %d failed\n" "$PASS" "$FAIL" | ||
| [[ $FAIL -eq 0 ]] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
core.hooksPathis configured asscripts—including through a global configuration—this resolvesHOOKS_DIRto the directory already containing the source hook. The latercp scripts/pre-commit scripts/pre-commitexits with “are the same file” underset -e, so setup fails for a valid hooks-path configuration that this change intends to support. Detect an identical source and destination and only ensure the existing hook is executable.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in bcda06e. When the source hook and the destination are the same file (
-ef), the script does not copy and only runschmod +x. A new test case setscore.hooksPath=scripts; it fails on 63b1808 and passes on bcda06e.