Skip to content

Instantly share code, notes, and snippets.

@luehm
Last active February 22, 2024 16:57
Show Gist options
  • Save luehm/5c58c27119cdcee3bd51b71d3a647a2a to your computer and use it in GitHub Desktop.
Save luehm/5c58c27119cdcee3bd51b71d3a647a2a to your computer and use it in GitHub Desktop.
branch-specific ignore hook
#!/bin/sh
# Compatible with git worktrees
# Assumes that root of "main" worktree has .gitignore files with the following format:
## .gitignore.$branchName
#
# Will stip out branch prefixes (e.g., bug/DEV-123 becomes DEV-123)
# If no branch-ignore is found, nothing happens
old_ref=$1
new_ref=$2
branch_switched=$3
if [[ $branch_switched != '1' ]]
then
exit 0
fi
echo "---- POST CHECKOUT ----"
current_branch=$(git rev-parse --abbrev-ref HEAD | cut -d '/' -f 2)
hook_dir=$(dirname $0)
git_dir="$(git rev-parse --git-dir)"
root_dir="$(git rev-parse --git-common-dir)/.."
info_dir="$git_dir/info"
exclude_target='.gitignore'
echo "Checking for branch-specific gitignore $root_dir/$exclude_target.$current_branch"
if [[ -f "$root_dir/$exclude_target.$current_branch" ]]
then
echo "Found a branch-specific gitignore!"
echo "Prepare to use .gitignore.$current_branch as exclude file"
exclude_target=.gitignore.$current_branch
if [ ! -d "$info_dir" ];
then
echo "Making info directory: $info_dir"
mkdir "$info_dir"
fi
cd "$info_dir"
rm exclude
echo "Copy .gitignore.$current_branch file in place of exclude"
cp "$root_dir/$exclude_target" exclude
fi
echo "--- POST CHECKOUT END ---"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment