确保所有提交的文本文件都只有LF换行符

时间:2019-07-16 05:23:15

标签: git normalization gitattributes end-of-line

.gitattributes文件中带有* text=auto后跟git add --renormalize .的文件似乎是从存储库中清除CRLF的好方法。

但仍然(根据https://git-scm.com/docs/gitattributes

  

使用CRLF提交文件后,不会进行任何转换。

是否有一种方法可以在存储库中指定提交时所有CRLF都将在文本文件中转换为LF?

1 个答案:

答案 0 :(得分:1)

是的,您可以做到。 您可以使用此类钩子来防止提交多个文件,或出于其他目的(例如在您的情况下)“转换”它们并验证EOF。

pre-receive hook

#!/bin/sh

# Redirect output to screen.
exec 1>&2

# Get the list of files in the commit and "update" the file EOF as you wish
# Loop over the files and do your stuff......
for file in $( git diff-tree -r --name-only ); do
        ..... <do your stuff here > 
    done        

# Some personal message
red='\033[0;31m';
green='\033[0;32m';
yellow='\033[0;33m';
default='\033[0;m';

echo "${red}"
echo "                                         "
echo "                   |ZZzzz                "
echo "                   |                     "
echo "                   |                     "
echo "      |ZZzzz      /^\            |ZZzzz  "
echo "      |          |~~~|           |       "
echo "      |        |-     -|        / \      "
echo "     /^\       |[]+    |       |^^^|     "
echo "  |^^^^^^^|    |    +[]|       |   |     "
echo "  |    +[]|/\/\/\/\^/\/\/\/\/|^^^^^^^|   "
echo "  |+[]+   |~~~~~~~~~~~~~~~~~~|    +[]|   "
echo "  |       |  []   /^\   []   |+[]+   |   "
echo "  |   +[]+|  []  || ||  []   |   +[]+|   "
echo "  |[]+    |      || ||       |[]+    |   "
echo "  |_______|------------------|_______|   "
echo "                                         "
echo "                                         "
echo "  ${green}All text are fixed             " 
echo "                                         "
echo "${default}"


# set the exit code to 0 or 1 based upon your needs
# 0 = good to push
exit 0;

注意:

Github不支持以这种方式使用钩子。
他们有自己的WebHooks

在这种情况下,您也可以在客户端使用钩子。
可以将相同的代码放在客户端上的pre-commit挂钩中。