Skip to content

Instantly share code, notes, and snippets.

@islomar
Created December 18, 2024 14:57
Show Gist options
  • Save islomar/e6ec1d43faad5e689b7c98b054ffe2a8 to your computer and use it in GitHub Desktop.
Save islomar/e6ec1d43faad5e689b7c98b054ffe2a8 to your computer and use it in GitHub Desktop.
Example of pre-commit and pre-push files for a monorepo with lots of different modules (low-cost solution)
#!/bin/bash
FOLDERS_TO_INSPECT=("folder1" "folder2" "folder3")
STAGED_FILES="$(git diff --cached --name-only)"
function main {
for folder_to_inspect in "${FOLDERS_TO_INSPECT[@]}" ; do
echo $folder_to_inspect
run_actions_if_needed $folder_to_inspect
done
}
run_actions_if_needed() {
if echo "$STAGED_FILES" | grep -q "$1"; then
echo -e "\nThere are files staged under '$1'. Let's run their pre-commit actions!!\n"
cd $1
make pre-commit
IS_PRE_COMMIT_SUCCESSFUL="$(echo $?)"
cd -
if [[ "$IS_PRE_COMMIT_SUCCESSFUL" != "0" ]]; then
exit "$IS_PRE_COMMIT_SUCCESSFUL"
fi
else
echo "No files staged under '$1'."
fi
echo -e "\n"
}
main
#!/bin/bash
FOLDERS_TO_INSPECT=("folder1" "folder2" "folder3")
CHANGED_FILES_IN_COMMITS="$(git diff --name-only origin/main..HEAD)"
function main {
for folder_to_inspect in "${FOLDERS_TO_INSPECT[@]}" ; do
echo $folder_to_inspect
run_actions_if_needed $folder_to_inspect
done
}
run_actions_if_needed() {
if echo "$CHANGED_FILES_IN_COMMITS" | grep -q "$1"; then
echo "There are files committed under '$1'. Let's run the pre-push actions!!"
cd $1
make pre-push
IS_PRE_PUSH_SUCCESSFUL="$(echo $?)"
cd -
if [[ "$IS_PRE_PUSH_SUCCESSFUL" != "0" ]]; then
exit "$IS_PRE_PUSH_SUCCESSFUL"
fi
else
echo "No files committed under '$1'."
fi
echo -e "\n"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment