diff --git a/README.md b/README.md index f930c35..243df56 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,151 @@ # abcd add haha ... git-auto-commit Action -# add env +# add env #!/usr/bin/env bash + + +set -o pipefail + +readonly DEFAULT_FILE_MAXSIZE_MB="30" +readonly CONFIG_NAME="hooks.maxfilesize" +readonly NULLSHA="0000000000000000000000000000000000000000" +readonly EXIT_SUCCESS=0 +readonly EXIT_FAILURE=1 +readonly DEFAULT_REPO_MAXSIZE_MB="1024" +readonly CHECK_FLAG_ON=1 + +env + +echo "CHECK_FLAG_ON=====$CHECK_FLAG_ON" +echo "PUSH_SIZE_CHECK_FLAG=====$PUSH_SIZE_CHECK_FLAG" + +status="$EXIT_SUCCESS" + +# skip this hook entirely if shell check is not open +check_flag=1 #${PUSH_SIZE_CHECK_FLAG} +if [[ $check_flag != $CHECK_FLAG_ON ]]; then +exit $EXIT_SUCCESS +fi + +export REPO_MAX_SIZE=50 + +echo "REPO_MAX_FILE_SIZE===${REPO_MAX_FILE_SIZE}" +echo "REPO_MAX_SIZE===${REPO_MAX_SIZE}" +echo "REPO_CURRENT_SIZE===${REPO_CURRENT_SIZE}" + + + + +####################################### +# check the file max size limit +####################################### + +# get maximum filesize (from repository-specific config) +maxsize_mb="${REPO_MAX_FILE_SIZE}" + +if [[ "$?" != $EXIT_SUCCESS ]]; then +echo "failed to get ${CONFIG_NAME} from config" +exit "$EXIT_FAILURE" +fi + +push_size="0" +# read lines from stdin (format: " \n") +while read oldref newref refname; do +# skip branch deletions +if [[ "$newref" == "$NULLSHA" ]]; then + continue +fi + +# find large objects +# check all objects from $oldref (possible $NULLSHA) to $newref, but +# skip all objects that have already been accepted (i.e. are referenced by +# another branch or tag). + +new_branch_flag=0 +if [[ "$oldref" == "$NULLSHA" ]]; then + target="$newref" + new_branch_flag=1 + echo "You are creating a new remote branch,openI will check all files in commit history to find oversize files" +else + target="${oldref}..${newref}" +fi +echo "target==${target}" + +maxsize=`expr $maxsize_mb \* 1048576` + +# find objects in this push_size +# print like: +# 08da8e2ab9ae4095bf94dd71ac913132b880b463 commit 214 +# 43e993b768ede5740e8c65de2ed6edec25053ea1 tree 185 +# 4476971d76569039df7569af1b8d03c288f6b193 blob 20167318 b0417e6593a1.zip +files="$(git rev-list --objects "$target" | \ + git cat-file $'--batch-check=%(objectname) %(objecttype) %(objectsize) %(rest)' | \ + awk -F ' ' -v maxbytes="$maxsize" 'BEGIN {totalIn=0} {if( $3 > maxbytes && $2 == "blob") { totalIn+=$3; print $4} else { totalIn+=$3}} END { printf ("totalIn=\t%s",totalIn)}' )" + +if [[ "$?" != $EXIT_SUCCESS ]]; then + echo "failed to check for large files in ref ${refname}" + continue +fi + +IFS=$'\n' +# rewrite IFS to seperate line in $files +for file in $files; do + # if don't unset IFS,temp_array=(${file}) will get error answer + + if [[ ${file} == totalIn=* ]]; then + IFS=$'\t' + temp_array=(${file}) + push_size=${temp_array[1]} + continue + fi + unset IFS + if [[ "$status" == $EXIT_SUCCESS ]]; then + echo -e "Error: Your push was rejected because it contains files larger than $(numfmt --to=iec "$maxsize_mb") Mb" + echo "help document -- https://openi.pcl.ac.cn/zeizei/OpenI_Learning/src/branch/master/docs/git/repository_capacity_help.md" + echo "oversize files:" + # status="$EXIT_FAILURE" + fi + echo -e "\033[31m- ${file}\033[0m " +done + +echo "push_size=======${push_size}" + +if [[ "$status" != $EXIT_SUCCESS ]]; then + exit "$status" +fi + +done + +####################################### +# check the repo max size limit +####################################### +if [[ $push_size -eq "0" ]]; then + exit $EXIT_SUCCESS +fi + +# if create new branch or tag,use count-objects -v to get pack size +if [[ $new_branch_flag -eq 1 ]]; then + size_kb=`git count-objects -v | grep 'size-pack' | sed 's/.*\(size-pack:\).//'` + size_pack_kb=`git count-objects -v | grep 'size:' | sed 's/.*\(size:\).//'` + total_kb=`expr $size_kb + $size_pack_kb` + let push_size=$total_kb*1024 +fi + +sizelimit_mb="${REPO_MAX_SIZE}" +let sizelimit_b=$sizelimit_mb*1024*1024 + +# repo size at here means the size of repo directory in server +reposize_b=${REPO_CURRENT_SIZE} + +total=`expr $push_size + $reposize_b` + +if [ $total -gt $sizelimit_b ]; then + echo "Error: Your push was rejected because the repository size is large than $sizelimit_mb Mb" + echo "see the help document--https://openi.pcl.ac.cn/zeizei/OpenI_Learning/src/branch/master/docs/git/repository_capacity_help.md" + exit $EXIT_FAILURE +fi + + +exit $EXIT_SUCCESS +v > The GitHub Action for committing files for the 80% use case.