Skip to content

Commit

Permalink
feat(scripts): Improve pre-push hook and use updatable hooks (#605)
Browse files Browse the repository at this point in the history
- scripts/pre-commit, scripts/pre-push will be added to actual hooks
  while scripts/pre-commit.sh, scripts/pre-push.sh will remain as
  updatable (versioned) implementation of them.
- Use a temporarily added git remote to resolve the "bad revision"
  error when using `--changed-since` against a branch in a fork repo.
- editorconfig: Make shell script indents consistent
  • Loading branch information
achimnol authored Jul 26, 2022
1 parent 72e5aa4 commit 76f933a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.sh]
indent_style = space
indent_size = 2

[*.{py,md}]
max_line_length = 100
indent_style = space
Expand Down
8 changes: 4 additions & 4 deletions scripts/install-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ install_git_hooks() {
:
else
echo "" >> .git/hooks/pre-commit
cat scripts/pre-commit.sh >> .git/hooks/pre-commit
cat scripts/pre-commit >> .git/hooks/pre-commit
fi
else
cp scripts/pre-commit.sh .git/hooks/pre-commit
cp scripts/pre-commit .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
fi
local magic_str="monorepo standard pre-push hook"
Expand All @@ -422,10 +422,10 @@ install_git_hooks() {
:
else
echo "" >> .git/hooks/pre-push
cat scripts/pre-push.sh >> .git/hooks/pre-push
cat scripts/pre-push >> .git/hooks/pre-push
fi
else
cp scripts/pre-push.sh .git/hooks/pre-push
cp scripts/pre-push .git/hooks/pre-push
chmod +x .git/hooks/pre-push
fi
}
Expand Down
3 changes: 3 additions & 0 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# backend.ai monorepo standard pre-push hook
BASE_PATH=$(cd "$(dirname "$0")"/../.. && pwd)
${BASE_PATH}/scripts/pre-commit.sh "$@"
4 changes: 2 additions & 2 deletions scripts/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# backend.ai monorepo standard pre-commit hook
BASE_PATH=$(cd "$(dirname "$0")"/../.. && pwd)
# implementation: backend.ai monorepo standard pre-commit hook
BASE_PATH=$(pwd)
if [ -f "$BASE_PATH/pants-local" ]; then
PANTS="$BASE_PATH/pants-local"
else
Expand Down
3 changes: 3 additions & 0 deletions scripts/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# backend.ai monorepo standard pre-push hook
BASE_PATH=$(cd "$(dirname "$0")"/../.. && pwd)
${BASE_PATH}/scripts/pre-push.sh "$@"
20 changes: 16 additions & 4 deletions scripts/pre-push.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# backend.ai monorepo standard pre-push hook
BASE_PATH=$(cd "$(dirname "$0")"/../.. && pwd)
# implementation: backend.ai monorepo standard pre-push hook
BASE_PATH=$(pwd)
if [ -f "$BASE_PATH/pants-local" ]; then
PANTS="$BASE_PATH/pants-local"
else
Expand All @@ -13,6 +13,18 @@ if [ -n "$(echo "$CURRENT_BRANCH" | sed -n '/[[:digit:]]\{1,\}\.[[:digit:]]\{1,\
else
BASE_BRANCH="main"
fi
echo "Performing lint and check on $1/${BASE_BRANCH}..HEAD@${CURRENT_COMMIT} ..."
if [ "$1" != "origin" ]; then
# extract the owner name of the target repo
ORIGIN="$(echo "$1" | grep -o '://[^/]\+/[^/]\+/' | grep -o '/[^/]\+/$' | tr -d '/')"
cleanup_remote() {
git remote remove "$ORIGIN"
}
trap cleanup_remote EXIT
git remote add "$ORIGIN" "$1"
git fetch -q --depth=1 --no-tags "$ORIGIN" "$BASE_BRANCH"
else
ORIGIN="origin"
fi
echo "Performing lint and check on ${ORIGIN}/${BASE_BRANCH}..HEAD@${CURRENT_COMMIT} ..."
"$PANTS" tailor --check update-build-files --check
"$PANTS" lint check --changed-since="$1/${BASE_BRANCH}"
"$PANTS" lint check --changed-since="${ORIGIN}/${BASE_BRANCH}"

0 comments on commit 76f933a

Please sign in to comment.