如何在提交时限制文件大小?

How to limit file size on commit?

提交时是否有限制文件大小的选项?

例如:文件大小超过 500K 会产生警告。超过 10M 的文件大小将停止提交。

我完全了解这个问题,从技术上讲,这个问题是重复的,但答案只提供了一个推送解决方案,这对我的要求来说为时已晚。


这个预提交钩子会做文件大小检查:

.git/hooks/pre-commit

#!/bin/sh

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=500000}



list_new_or_modified_files()

{

  git diff --staged --name-status|sed -e '/^D/ d; /^D/! s/.\\s\\+//'

}



unmunge()

{

  local result="${1#"}"

  result="${result%"}"

  env echo -e"$result"

}



check_file_size()

{

  n=0

  while read -r munged_filename

  do

    f="$(unmunge"$munged_filename")"

    h=$(git ls-files -s"$f"|cut -d' ' -f 2)

    s=$(git cat-file -s"$h")

    if ["$s" -gt $hard_limit ]

    then

      env echo -E 1>&2"ERROR: hard size limit ($hard_limit) exceeded: $munged_filename ($s)"

      n=$((n+1))

    elif ["$s" -gt $soft_limit ]

    then

      env echo -E 1>&2"WARNING: soft size limit ($soft_limit) exceeded: $munged_filename ($s)"

    fi

  done



  [ $n -eq 0 ]

}



list_new_or_modified_files | check_file_size$ git config hooks.filesizesoftlimit 100000

$ git config hooks.filesizehardlimit 4000000#!/bin/bash

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=1000000}



status=0



bytesToHuman() {

 b=${1:-0}; d=''; s=0; S=({,K,M,G,T,P,E,Z,Y}B)

 while ((b > 1000)); do

  d="$(printf".%01d" $((b % 1000 * 10 / 1000)))"

  b=$((b / 1000))

  let s++

 done

 echo"$b$d${S[$s]}"

}



# Iterate over the zero-delimited list of staged files.

while IFS= read -r -d '' file ; do

 hash=$(git ls-files -s"$file" | cut -d ' ' -f 2)

 size=$(git cat-file -s"$hash")



 if (( $size > $hard_limit )); then

  echo"Error: Cannot commit '$file' because it is $(bytesToHuman $size), which exceeds the hard size limit of $(bytesToHuman $hard_limit)."

  status=1

 elif (( $size > $soft_limit )); then

  echo"Warning: '$file' is $(bytesToHuman $size), which exceeds the soft size limit of $(bytesToHuman $soft_limit). Please double check that you intended to commit this file."

 fi

done < <(git diff -z --staged --name-only --diff-filter=d)

exit $statusError: Cannot commit 'foo' because it is 117.9MB, which exceeds the hard size limit of 10.0MB.#!/bin/bash

# File size limit is meant to be configured through 'hooks.filesizelimit' setting

filesizelimit=$(git config hooks.filesizelimit)



# If we haven't configured a file size limit, use default value of about 10M

if [ -z"$filesizelimit" ]; then

    filesizelimit=10000000

fi



# You specify a warning limit

filesizewarning=500000



# With this command, we can find information about the file coming in that has biggest size

# We also normalize the line for excess whitespace

biggest_checkin_normalized=$(git ls-tree --full-tree -r -l HEAD | sort -k 4 -n -r | head -1 | sed 's/^ *//;s/ *$//;s/\\s\\{1,\\}/ /g' )



# Based on that, we can find what we are interested about

filesize=`echo $biggest_checkin_normalized | cut -d ' ' -f4,4`



# Actual comparison

# To cancel a push, we exit with status code 1

# It is also a good idea to print out some info about the cause of rejection

if [ $filesize -gt $filesizelimit ]; then



    # To be more user-friendly, we also look up the name of the offending file

    filename=`echo $biggest_checkin_normalized | cut -d ' ' -f5,5`



    echo"Error: Too large push attempted.">&2

    echo >&2

    echo"File size limit is $filesizelimit, and you tried to push file named $filename of size $filesize.">&2

    echo"Contact configuration team if you really need to do this.">&2

    exit 1

elif [ $filesize -gt $filesizewarning ]; then

    echo"WARNING ! A file size is bigger that $filesizewarning"

fi

exit 0[ -d"$f" ] && continue

以上脚本必须保存为 .git/hooks/pre-commit 并启用执行权限 (chmod +x .git/hooks/pre-commit)。

默认的软(警告)和硬(错误)大小限制设置为 500,000 和 10,000,000 字节,但可以分别通过 hooks.filesizesoftlimithooks.filesizehardlimit 设置覆盖:

#!/bin/sh

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=500000}



list_new_or_modified_files()

{

  git diff --staged --name-status|sed -e '/^D/ d; /^D/! s/.\\s\\+//'

}



unmunge()

{

  local result="${1#"}"

  result="${result%"}"

  env echo -e"$result"

}



check_file_size()

{

  n=0

  while read -r munged_filename

  do

    f="$(unmunge"$munged_filename")"

    h=$(git ls-files -s"$f"|cut -d' ' -f 2)

    s=$(git cat-file -s"$h")

    if ["$s" -gt $hard_limit ]

    then

      env echo -E 1>&2"ERROR: hard size limit ($hard_limit) exceeded: $munged_filename ($s)"

      n=$((n+1))

    elif ["$s" -gt $soft_limit ]

    then

      env echo -E 1>&2"WARNING: soft size limit ($soft_limit) exceeded: $munged_filename ($s)"

    fi

  done



  [ $n -eq 0 ]

}



list_new_or_modified_files | check_file_size$ git config hooks.filesizesoftlimit 100000

$ git config hooks.filesizehardlimit 4000000#!/bin/bash

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=1000000}



status=0



bytesToHuman() {

 b=${1:-0}; d=''; s=0; S=({,K,M,G,T,P,E,Z,Y}B)

 while ((b > 1000)); do

  d="$(printf".%01d" $((b % 1000 * 10 / 1000)))"

  b=$((b / 1000))

  let s++

 done

 echo"$b$d${S[$s]}"

}



# Iterate over the zero-delimited list of staged files.

while IFS= read -r -d '' file ; do

 hash=$(git ls-files -s"$file" | cut -d ' ' -f 2)

 size=$(git cat-file -s"$hash")



 if (( $size > $hard_limit )); then

  echo"Error: Cannot commit '$file' because it is $(bytesToHuman $size), which exceeds the hard size limit of $(bytesToHuman $hard_limit)."

  status=1

 elif (( $size > $soft_limit )); then

  echo"Warning: '$file' is $(bytesToHuman $size), which exceeds the soft size limit of $(bytesToHuman $soft_limit). Please double check that you intended to commit this file."

 fi

done < <(git diff -z --staged --name-only --diff-filter=d)

exit $statusError: Cannot commit 'foo' because it is 117.9MB, which exceeds the hard size limit of 10.0MB.#!/bin/bash

# File size limit is meant to be configured through 'hooks.filesizelimit' setting

filesizelimit=$(git config hooks.filesizelimit)



# If we haven't configured a file size limit, use default value of about 10M

if [ -z"$filesizelimit" ]; then

    filesizelimit=10000000

fi



# You specify a warning limit

filesizewarning=500000



# With this command, we can find information about the file coming in that has biggest size

# We also normalize the line for excess whitespace

biggest_checkin_normalized=$(git ls-tree --full-tree -r -l HEAD | sort -k 4 -n -r | head -1 | sed 's/^ *//;s/ *$//;s/\\s\\{1,\\}/ /g' )



# Based on that, we can find what we are interested about

filesize=`echo $biggest_checkin_normalized | cut -d ' ' -f4,4`



# Actual comparison

# To cancel a push, we exit with status code 1

# It is also a good idea to print out some info about the cause of rejection

if [ $filesize -gt $filesizelimit ]; then



    # To be more user-friendly, we also look up the name of the offending file

    filename=`echo $biggest_checkin_normalized | cut -d ' ' -f5,5`



    echo"Error: Too large push attempted.">&2

    echo >&2

    echo"File size limit is $filesizelimit, and you tried to push file named $filename of size $filesize.">&2

    echo"Contact configuration team if you really need to do this.">&2

    exit 1

elif [ $filesize -gt $filesizewarning ]; then

    echo"WARNING ! A file size is bigger that $filesizewarning"

fi

exit 0[ -d"$f" ] && continue

@Leon\\ 脚本的更短的 bash 特定版本,它以人类可读的格式打印文件大小。 --diff-filter=d 选项需要更新的 git:

#!/bin/sh

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=500000}



list_new_or_modified_files()

{

  git diff --staged --name-status|sed -e '/^D/ d; /^D/! s/.\\s\\+//'

}



unmunge()

{

  local result="${1#"}"

  result="${result%"}"

  env echo -e"$result"

}



check_file_size()

{

  n=0

  while read -r munged_filename

  do

    f="$(unmunge"$munged_filename")"

    h=$(git ls-files -s"$f"|cut -d' ' -f 2)

    s=$(git cat-file -s"$h")

    if ["$s" -gt $hard_limit ]

    then

      env echo -E 1>&2"ERROR: hard size limit ($hard_limit) exceeded: $munged_filename ($s)"

      n=$((n+1))

    elif ["$s" -gt $soft_limit ]

    then

      env echo -E 1>&2"WARNING: soft size limit ($soft_limit) exceeded: $munged_filename ($s)"

    fi

  done



  [ $n -eq 0 ]

}



list_new_or_modified_files | check_file_size$ git config hooks.filesizesoftlimit 100000

$ git config hooks.filesizehardlimit 4000000#!/bin/bash

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=1000000}



status=0



bytesToHuman() {

 b=${1:-0}; d=''; s=0; S=({,K,M,G,T,P,E,Z,Y}B)

 while ((b > 1000)); do

  d="$(printf".%01d" $((b % 1000 * 10 / 1000)))"

  b=$((b / 1000))

  let s++

 done

 echo"$b$d${S[$s]}"

}



# Iterate over the zero-delimited list of staged files.

while IFS= read -r -d '' file ; do

 hash=$(git ls-files -s"$file" | cut -d ' ' -f 2)

 size=$(git cat-file -s"$hash")



 if (( $size > $hard_limit )); then

  echo"Error: Cannot commit '$file' because it is $(bytesToHuman $size), which exceeds the hard size limit of $(bytesToHuman $hard_limit)."

  status=1

 elif (( $size > $soft_limit )); then

  echo"Warning: '$file' is $(bytesToHuman $size), which exceeds the soft size limit of $(bytesToHuman $soft_limit). Please double check that you intended to commit this file."

 fi

done < <(git diff -z --staged --name-only --diff-filter=d)

exit $statusError: Cannot commit 'foo' because it is 117.9MB, which exceeds the hard size limit of 10.0MB.#!/bin/bash

# File size limit is meant to be configured through 'hooks.filesizelimit' setting

filesizelimit=$(git config hooks.filesizelimit)



# If we haven't configured a file size limit, use default value of about 10M

if [ -z"$filesizelimit" ]; then

    filesizelimit=10000000

fi



# You specify a warning limit

filesizewarning=500000



# With this command, we can find information about the file coming in that has biggest size

# We also normalize the line for excess whitespace

biggest_checkin_normalized=$(git ls-tree --full-tree -r -l HEAD | sort -k 4 -n -r | head -1 | sed 's/^ *//;s/ *$//;s/\\s\\{1,\\}/ /g' )



# Based on that, we can find what we are interested about

filesize=`echo $biggest_checkin_normalized | cut -d ' ' -f4,4`



# Actual comparison

# To cancel a push, we exit with status code 1

# It is also a good idea to print out some info about the cause of rejection

if [ $filesize -gt $filesizelimit ]; then



    # To be more user-friendly, we also look up the name of the offending file

    filename=`echo $biggest_checkin_normalized | cut -d ' ' -f5,5`



    echo"Error: Too large push attempted.">&2

    echo >&2

    echo"File size limit is $filesizelimit, and you tried to push file named $filename of size $filesize.">&2

    echo"Contact configuration team if you really need to do this.">&2

    exit 1

elif [ $filesize -gt $filesizewarning ]; then

    echo"WARNING ! A file size is bigger that $filesizewarning"

fi

exit 0[ -d"$f" ] && continue

与其他答案一样,这必须以执行权限保存为 .git/hooks/pre-commit.

示例输出:

#!/bin/sh

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=500000}



list_new_or_modified_files()

{

  git diff --staged --name-status|sed -e '/^D/ d; /^D/! s/.\\s\\+//'

}



unmunge()

{

  local result="${1#"}"

  result="${result%"}"

  env echo -e"$result"

}



check_file_size()

{

  n=0

  while read -r munged_filename

  do

    f="$(unmunge"$munged_filename")"

    h=$(git ls-files -s"$f"|cut -d' ' -f 2)

    s=$(git cat-file -s"$h")

    if ["$s" -gt $hard_limit ]

    then

      env echo -E 1>&2"ERROR: hard size limit ($hard_limit) exceeded: $munged_filename ($s)"

      n=$((n+1))

    elif ["$s" -gt $soft_limit ]

    then

      env echo -E 1>&2"WARNING: soft size limit ($soft_limit) exceeded: $munged_filename ($s)"

    fi

  done



  [ $n -eq 0 ]

}



list_new_or_modified_files | check_file_size$ git config hooks.filesizesoftlimit 100000

$ git config hooks.filesizehardlimit 4000000#!/bin/bash

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=1000000}



status=0



bytesToHuman() {

 b=${1:-0}; d=''; s=0; S=({,K,M,G,T,P,E,Z,Y}B)

 while ((b > 1000)); do

  d="$(printf".%01d" $((b % 1000 * 10 / 1000)))"

  b=$((b / 1000))

  let s++

 done

 echo"$b$d${S[$s]}"

}



# Iterate over the zero-delimited list of staged files.

while IFS= read -r -d '' file ; do

 hash=$(git ls-files -s"$file" | cut -d ' ' -f 2)

 size=$(git cat-file -s"$hash")



 if (( $size > $hard_limit )); then

  echo"Error: Cannot commit '$file' because it is $(bytesToHuman $size), which exceeds the hard size limit of $(bytesToHuman $hard_limit)."

  status=1

 elif (( $size > $soft_limit )); then

  echo"Warning: '$file' is $(bytesToHuman $size), which exceeds the soft size limit of $(bytesToHuman $soft_limit). Please double check that you intended to commit this file."

 fi

done < <(git diff -z --staged --name-only --diff-filter=d)

exit $statusError: Cannot commit 'foo' because it is 117.9MB, which exceeds the hard size limit of 10.0MB.#!/bin/bash

# File size limit is meant to be configured through 'hooks.filesizelimit' setting

filesizelimit=$(git config hooks.filesizelimit)



# If we haven't configured a file size limit, use default value of about 10M

if [ -z"$filesizelimit" ]; then

    filesizelimit=10000000

fi



# You specify a warning limit

filesizewarning=500000



# With this command, we can find information about the file coming in that has biggest size

# We also normalize the line for excess whitespace

biggest_checkin_normalized=$(git ls-tree --full-tree -r -l HEAD | sort -k 4 -n -r | head -1 | sed 's/^ *//;s/ *$//;s/\\s\\{1,\\}/ /g' )



# Based on that, we can find what we are interested about

filesize=`echo $biggest_checkin_normalized | cut -d ' ' -f4,4`



# Actual comparison

# To cancel a push, we exit with status code 1

# It is also a good idea to print out some info about the cause of rejection

if [ $filesize -gt $filesizelimit ]; then



    # To be more user-friendly, we also look up the name of the offending file

    filename=`echo $biggest_checkin_normalized | cut -d ' ' -f5,5`



    echo"Error: Too large push attempted.">&2

    echo >&2

    echo"File size limit is $filesizelimit, and you tried to push file named $filename of size $filesize.">&2

    echo"Contact configuration team if you really need to do this.">&2

    exit 1

elif [ $filesize -gt $filesizewarning ]; then

    echo"WARNING ! A file size is bigger that $filesizewarning"

fi

exit 0[ -d"$f" ] && continue

你需要实现你已经在 pre-commit 钩子中寻找的 eis 脚本。

从文档中,我们了解到预提交挂钩

takes no parameters, and is invoked before obtaining the proposed commit log message and making a commit. Exiting with a non-zero status from this script causes the git commit command to abort before creating a commit.

基本上,调用钩子来检查是否允许用户提交他的更改。

eis原来在其他帖子上做的脚本变成了

#!/bin/sh

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=500000}



list_new_or_modified_files()

{

  git diff --staged --name-status|sed -e '/^D/ d; /^D/! s/.\\s\\+//'

}



unmunge()

{

  local result="${1#"}"

  result="${result%"}"

  env echo -e"$result"

}



check_file_size()

{

  n=0

  while read -r munged_filename

  do

    f="$(unmunge"$munged_filename")"

    h=$(git ls-files -s"$f"|cut -d' ' -f 2)

    s=$(git cat-file -s"$h")

    if ["$s" -gt $hard_limit ]

    then

      env echo -E 1>&2"ERROR: hard size limit ($hard_limit) exceeded: $munged_filename ($s)"

      n=$((n+1))

    elif ["$s" -gt $soft_limit ]

    then

      env echo -E 1>&2"WARNING: soft size limit ($soft_limit) exceeded: $munged_filename ($s)"

    fi

  done



  [ $n -eq 0 ]

}



list_new_or_modified_files | check_file_size$ git config hooks.filesizesoftlimit 100000

$ git config hooks.filesizehardlimit 4000000#!/bin/bash

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=1000000}



status=0



bytesToHuman() {

 b=${1:-0}; d=''; s=0; S=({,K,M,G,T,P,E,Z,Y}B)

 while ((b > 1000)); do

  d="$(printf".%01d" $((b % 1000 * 10 / 1000)))"

  b=$((b / 1000))

  let s++

 done

 echo"$b$d${S[$s]}"

}



# Iterate over the zero-delimited list of staged files.

while IFS= read -r -d '' file ; do

 hash=$(git ls-files -s"$file" | cut -d ' ' -f 2)

 size=$(git cat-file -s"$hash")



 if (( $size > $hard_limit )); then

  echo"Error: Cannot commit '$file' because it is $(bytesToHuman $size), which exceeds the hard size limit of $(bytesToHuman $hard_limit)."

  status=1

 elif (( $size > $soft_limit )); then

  echo"Warning: '$file' is $(bytesToHuman $size), which exceeds the soft size limit of $(bytesToHuman $soft_limit). Please double check that you intended to commit this file."

 fi

done < <(git diff -z --staged --name-only --diff-filter=d)

exit $statusError: Cannot commit 'foo' because it is 117.9MB, which exceeds the hard size limit of 10.0MB.#!/bin/bash

# File size limit is meant to be configured through 'hooks.filesizelimit' setting

filesizelimit=$(git config hooks.filesizelimit)



# If we haven't configured a file size limit, use default value of about 10M

if [ -z"$filesizelimit" ]; then

    filesizelimit=10000000

fi



# You specify a warning limit

filesizewarning=500000



# With this command, we can find information about the file coming in that has biggest size

# We also normalize the line for excess whitespace

biggest_checkin_normalized=$(git ls-tree --full-tree -r -l HEAD | sort -k 4 -n -r | head -1 | sed 's/^ *//;s/ *$//;s/\\s\\{1,\\}/ /g' )



# Based on that, we can find what we are interested about

filesize=`echo $biggest_checkin_normalized | cut -d ' ' -f4,4`



# Actual comparison

# To cancel a push, we exit with status code 1

# It is also a good idea to print out some info about the cause of rejection

if [ $filesize -gt $filesizelimit ]; then



    # To be more user-friendly, we also look up the name of the offending file

    filename=`echo $biggest_checkin_normalized | cut -d ' ' -f5,5`



    echo"Error: Too large push attempted.">&2

    echo >&2

    echo"File size limit is $filesizelimit, and you tried to push file named $filename of size $filesize.">&2

    echo"Contact configuration team if you really need to do this.">&2

    exit 1

elif [ $filesize -gt $filesizewarning ]; then

    echo"WARNING ! A file size is bigger that $filesizewarning"

fi

exit 0[ -d"$f" ] && continue

只是想评论@Leon 提供的解决方案太棒了。如果一个空目录开始尝试被跟踪,我遇到了一个小障碍,它中止了。所以我不得不添加

#!/bin/sh

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=500000}



list_new_or_modified_files()

{

  git diff --staged --name-status|sed -e '/^D/ d; /^D/! s/.\\s\\+//'

}



unmunge()

{

  local result="${1#"}"

  result="${result%"}"

  env echo -e"$result"

}



check_file_size()

{

  n=0

  while read -r munged_filename

  do

    f="$(unmunge"$munged_filename")"

    h=$(git ls-files -s"$f"|cut -d' ' -f 2)

    s=$(git cat-file -s"$h")

    if ["$s" -gt $hard_limit ]

    then

      env echo -E 1>&2"ERROR: hard size limit ($hard_limit) exceeded: $munged_filename ($s)"

      n=$((n+1))

    elif ["$s" -gt $soft_limit ]

    then

      env echo -E 1>&2"WARNING: soft size limit ($soft_limit) exceeded: $munged_filename ($s)"

    fi

  done



  [ $n -eq 0 ]

}



list_new_or_modified_files | check_file_size$ git config hooks.filesizesoftlimit 100000

$ git config hooks.filesizehardlimit 4000000#!/bin/bash

hard_limit=$(git config hooks.filesizehardlimit)

soft_limit=$(git config hooks.filesizesoftlimit)

: ${hard_limit:=10000000}

: ${soft_limit:=1000000}



status=0



bytesToHuman() {

 b=${1:-0}; d=''; s=0; S=({,K,M,G,T,P,E,Z,Y}B)

 while ((b > 1000)); do

  d="$(printf".%01d" $((b % 1000 * 10 / 1000)))"

  b=$((b / 1000))

  let s++

 done

 echo"$b$d${S[$s]}"

}



# Iterate over the zero-delimited list of staged files.

while IFS= read -r -d '' file ; do

 hash=$(git ls-files -s"$file" | cut -d ' ' -f 2)

 size=$(git cat-file -s"$hash")



 if (( $size > $hard_limit )); then

  echo"Error: Cannot commit '$file' because it is $(bytesToHuman $size), which exceeds the hard size limit of $(bytesToHuman $hard_limit)."

  status=1

 elif (( $size > $soft_limit )); then

  echo"Warning: '$file' is $(bytesToHuman $size), which exceeds the soft size limit of $(bytesToHuman $soft_limit). Please double check that you intended to commit this file."

 fi

done < <(git diff -z --staged --name-only --diff-filter=d)

exit $statusError: Cannot commit 'foo' because it is 117.9MB, which exceeds the hard size limit of 10.0MB.#!/bin/bash

# File size limit is meant to be configured through 'hooks.filesizelimit' setting

filesizelimit=$(git config hooks.filesizelimit)



# If we haven't configured a file size limit, use default value of about 10M

if [ -z"$filesizelimit" ]; then

    filesizelimit=10000000

fi



# You specify a warning limit

filesizewarning=500000



# With this command, we can find information about the file coming in that has biggest size

# We also normalize the line for excess whitespace

biggest_checkin_normalized=$(git ls-tree --full-tree -r -l HEAD | sort -k 4 -n -r | head -1 | sed 's/^ *//;s/ *$//;s/\\s\\{1,\\}/ /g' )



# Based on that, we can find what we are interested about

filesize=`echo $biggest_checkin_normalized | cut -d ' ' -f4,4`



# Actual comparison

# To cancel a push, we exit with status code 1

# It is also a good idea to print out some info about the cause of rejection

if [ $filesize -gt $filesizelimit ]; then



    # To be more user-friendly, we also look up the name of the offending file

    filename=`echo $biggest_checkin_normalized | cut -d ' ' -f5,5`



    echo"Error: Too large push attempted.">&2

    echo >&2

    echo"File size limit is $filesizelimit, and you tried to push file named $filename of size $filesize.">&2

    echo"Contact configuration team if you really need to do this.">&2

    exit 1

elif [ $filesize -gt $filesizewarning ]; then

    echo"WARNING ! A file size is bigger that $filesizewarning"

fi

exit 0[ -d"$f" ] && continue

ls-files命令之前有避免错误。

我更愿意发表评论,因为这不是答案,但我没有声誉积分。

注意:我知道 git \\'ignores\\' 目录,但显然不是在预提交挂钩运行之前。


有一个通用的预提交钩子。您可以编写一个脚本来检查文件大小,然后接受或拒绝提交。然而,Git 让用户能够从命令行类型"git help hooks"中绕过检查以获取更多信息。这是关于预提交挂钩的相关信息。

预提交

这个钩子由 git commit 调用,可以通过 --no-verify 选项绕过。它不带参数,并在获取建议的提交日志消息并进行提交之前调用。从此脚本中以非零状态退出会导致 git 提交中止。


相关推荐

  • Spring部署设置openshift

    Springdeploymentsettingsopenshift我有一个问题让我抓狂了三天。我根据OpenShift帐户上的教程部署了spring-eap6-quickstart代码。我已配置调试选项,并且已将Eclipse工作区与OpehShift服务器同步-服务器上的一切工作正常,但在Eclipse中出现无法消除的错误。我有这个错误:cvc-complex-type.2.4.a:Invali…
    2025-04-161
  • 检查Java中正则表达式中模式的第n次出现

    CheckfornthoccurrenceofpatterninregularexpressioninJava本问题已经有最佳答案,请猛点这里访问。我想使用Java正则表达式检查输入字符串中特定模式的第n次出现。你能建议怎么做吗?这应该可以工作:MatchResultfindNthOccurance(intn,Patternp,CharSequencesrc){Matcherm=p.matcher…
    2025-04-161
  • 如何让 JTable 停留在已编辑的单元格上

    HowtohaveJTablestayingontheeditedcell如果有人编辑JTable的单元格内容并按Enter,则内容会被修改并且表格选择会移动到下一行。是否可以禁止JTable在单元格编辑后转到下一行?原因是我的程序使用ListSelectionListener在单元格选择上同步了其他一些小部件,并且我不想在编辑当前单元格后选择下一行。Enter的默认绑定是名为selectNext…
    2025-04-161
  • Weblogic 12c 部署

    Weblogic12cdeploy我正在尝试将我的应用程序从Tomcat迁移到Weblogic12.2.1.3.0。我能够毫无错误地部署应用程序,但我遇到了与持久性提供程序相关的运行时错误。这是堆栈跟踪:javax.validation.ValidationException:CalltoTraversableResolver.isReachable()threwanexceptionatorg.…
    2025-04-161
  • Resteasy Content-Type 默认值

    ResteasyContent-Typedefaults我正在使用Resteasy编写一个可以返回JSON和XML的应用程序,但可以选择默认为XML。这是我的方法:@GET@Path("/content")@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})publicStringcontentListRequestXm…
    2025-04-161
  • 代码不会停止运行,在 Java 中

    thecodedoesn'tstoprunning,inJava我正在用Java解决项目Euler中的问题10,即"Thesumoftheprimesbelow10is2+3+5+7=17.Findthesumofalltheprimesbelowtwomillion."我的代码是packageprojecteuler_1;importjava.math.BigInteger;importjava…
    2025-04-161
  • Out of memory java heap space

    Outofmemoryjavaheapspace我正在尝试将大量文件从服务器发送到多个客户端。当我尝试发送大小为700mb的文件时,它显示了"OutOfMemoryjavaheapspace"错误。我正在使用Netbeans7.1.2版本。我还在属性中尝试了VMoption。但仍然发生同样的错误。我认为阅读整个文件存在一些问题。下面的代码最多可用于300mb。请给我一些建议。提前致谢publicc…
    2025-04-161
  • Log4j 记录到共享日志文件

    Log4jLoggingtoaSharedLogFile有没有办法将log4j日志记录事件写入也被其他应用程序写入的日志文件。其他应用程序可以是非Java应用程序。有什么缺点?锁定问题?格式化?Log4j有一个SocketAppender,它将向服务发送事件,您可以自己实现或使用与Log4j捆绑的简单实现。它还支持syslogd和Windows事件日志,这对于尝试将日志输出与来自非Java应用程序…
    2025-04-161