diff --git a/scripts/githooks/pre-commit.sh b/scripts/githooks/pre-commit.sh index b9d19ff0..abcb2ab7 100644 --- a/scripts/githooks/pre-commit.sh +++ b/scripts/githooks/pre-commit.sh @@ -1,6 +1,8 @@ -chmod +x scripts/githooks/pre-commit.sh +#!/usr/bin/env bash + #!/usr/bin/env bash chmod +x scripts/githooks/pre-commit.sh +#!/usr/bin/env bash # Copyright © 2023 OpenIMSDK. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,7 +17,7 @@ chmod +x scripts/githooks/pre-commit.sh # See the License for the specific language governing permissions and # limitations under the License. # -chmod +x scripts/githooks/pre-commit.sh + # ============================================================================== # This is a pre-commit hook that ensures attempts to commit files that are @@ -27,7 +29,7 @@ chmod +x scripts/githooks/pre-commit.sh # ============================================================================== # -LC_ALL=C +export LC_ALL=C local_branch="$(git rev-parse --abbrev-ref HEAD)" valid_branch_regex="^(main|master|develop|release(-[a-zA-Z0-9._-]+)?)$|(feature|feat|openim|hotfix|test|bug|bot|refactor|revert|ci|cicd|style|)\/[a-z0-9._-]+$|^HEAD$" @@ -75,7 +77,7 @@ HEREDOC } # Move to the repo root so git files paths make sense -repo_root=$( git rev-parse --show-toplevel ) +repo_root=$(git rev-parse --show-toplevel 2>/dev/null) cd $repo_root against=HEAD @@ -86,14 +88,14 @@ IFS=' shouldFail=false for file in $( git diff-index --cached --name-only $against ); do - file_size=$(([ ! -f $file ] && echo 0) || (ls -la $file | awk '{ print $5 }')) + file_size=$(([ ! -f $file ] && echo 0) || (ls -la "$file" | awk '{ print $5 }')) if [ "$file_size" -gt "$limit" ] && { grep -q ".github/release-drafter.yml" $file ;}; then printError "File $file is $(( $file_size / 10**6 )) MB, which is larger than our configured limit of $limitInMB MB" shouldFail=true fi done -if $shouldFail +if [ "$shouldFail" = true ] then printMessage "If you really need to commit this file, you can override the size limit by setting the GIT_FILE_SIZE_LIMIT environment variable, e.g. GIT_FILE_SIZE_LIMIT=42000000 for 42MB. Or, commit with the --no-verify switch to skip the check entirely." printError "Commit aborted" @@ -102,10 +104,8 @@ fi if [[ ! $local_branch =~ $valid_branch_regex ]] then - printError "The branch name format is invalid. Branch names in this project must adhere to the following format: $valid_branch_regex. Valid branch names should adhere to the following format: {valid format regex}. -Ensure that your branch follows the valid format (e.g., feat/name or bug/name) and try again. - -For more information, refer to: https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694" + printError "The branch name format is invalid. Branch names in this project must adhere to the following format: $valid_branch_regex. Valid branch names should adhere to the following format: {feature|feat|openim|hotfix|test|bug|bot|refactor|revert|ci|cicd|style|}/name.\nEnsure that your branch follows the valid format (e.g., feat/name or bug/name) and try again.\n\nFor more information, refer to: https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694" + exit 1 printError "For more information, refer to: https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694" exit 1 fi \ No newline at end of file