Skip to content

Instantly share code, notes, and snippets.

@hieu-v
Created May 22, 2024 00:53
Show Gist options
  • Save hieu-v/6360eebb61dac4289a62121cb2f67538 to your computer and use it in GitHub Desktop.
Save hieu-v/6360eebb61dac4289a62121cb2f67538 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Get the name of the current branch
branch_name=$(git symbolic-ref --short -q HEAD)
# Read the patterns to be managed from .common_files
patterns=$(cat .common_files)
# Get the list of changed files
changed_files=$(git diff --cached --name-only)
# Check if the branch name starts with "common"
if [[ $branch_name == common/* ]]; then
# Check if there are changes to files not matching the patterns in .common_files
for file in $changed_files; do
match_found=false
for pattern in $patterns; do
if [[ $file =~ $pattern ]]; then
match_found=true
break
fi
done
if ! $match_found; then
echo "Error: You can't commit changes to $file on a common branch."
exit 1
fi
done
else
# Check if there are changes to files matching the patterns in .common_files
for file in $changed_files; do
for pattern in $patterns; do
if [[ $file =~ $pattern ]]; then
echo "Error: You can't commit changes to $file on a non-common branch."
exit 1
fi
done
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment