从预提交挂钩中排除某些文件类型

时间:2014-11-24 14:28:44

标签: git bash pre-commit-hook

我想要一个pre commit git hook来检查(如果可能的话,自动移动)尾随空格。

Make git automatically remove trailing whitespace before committing中,我找到了link to a github page,其中实现了此类挂钩。这工作正常,但作为@VonC 在该页面上提及

  

由于该钩子获取每个文件的文件名,我建议   请注意某些类型的文件:您不想删除   在.md(markdown)文件中尾随空格! - VonC

并进一步下载

  

我宁愿让钩子能够检测.md文件而不是删除   空格,而不是要求最终用户添加--no-verify   git commit上的选项。 - VonC

据我所知,没有提到解决方案。

由于我在项目中使用带有故意尾随空格的.md文件,这对我来说是一个问题。
解决方案可能是微不足道的,但我没有编写脚本的语言经验(目前也没有兴趣学习它)。

这是脚本(github的副本):

#!/bin/bash
#

# A git hook script to find and fix trailing whitespace
# in your commits. Bypass it with the --no-verify option
# to git-commit
#
# usage: make a soft link to this file, e.g., ln -s ~/config/pre-commit.git.sh ~/some_project/.git/hooks/pre-commit

# detect platform
platform="win"
uname_result=`uname`
if [ "$uname_result" = "Linux" ]; then
  platform="linux"
elif [ "$uname_result" = "Darwin" ]; then
  platform="mac"
fi

# change IFS to ignore filename's space in |for|
IFS="
"
# autoremove trailing whitespace
for line in `git diff --check --cached | sed '/^[+-]/d'` ; do
  # get file name
  if [ "$platform" = "mac" ]; then
    file="`echo $line | sed -E 's/:[0-9]+: .*//'`"
  else
    file="`echo $line | sed -r 's/:[0-9]+: .*//'`"
  fi  
  # display tips
  echo -e "auto remove trailing whitespace in \033[31m$file\033[0m!"
  # since $file in working directory isn't always equal to $file in index, so we backup it
  mv -f "$file" "${file}.save"
  # discard changes in working directory
  git checkout -- "$file"
  # remove trailing whitespace
  if [ "$platform" = "win" ]; then
    # in windows, `sed -i` adds ready-only attribute to $file(I don't kown why), so we use temp file instead
    sed 's/[[:space:]]*$//' "$file" > "${file}.bak"
    mv -f "${file}.bak" "$file"
  elif [ "$platform" == "mac" ]; then
    sed -i "" 's/[[:space:]]*$//' "$file"
  else
    sed -i 's/[[:space:]]*$//' "$file"
  fi  
  git add "$file"
  # restore the $file
  sed 's/[[:space:]]*$//' "${file}.save" > "$file"
  rm "${file}.save"
done

if [ "x`git status -s | grep '^[A|D|M]'`" = "x" ]; then
  # empty commit
  echo
  echo -e "\033[31mNO CHANGES ADDED, ABORT COMMIT!\033[0m"
  exit 1
fi

# Now we can commit
exit

如何修改它以便(例如).md文件被排除在支票中?此外,如果可以排除多种文件类型,那就太棒了。

1 个答案:

答案 0 :(得分:2)

试试这个:

#!/bin/bash
#

# A git hook script to find and fix trailing whitespace
# in your commits. Bypass it with the --no-verify option
# to git-commit
#
# usage: make a soft link to this file, e.g., ln -s ~/config/pre-commit.git.sh ~/some_project/.git/hooks/pre-commit

LIST="md txt c cpp"

lookup() {
    IFS=" "
    for i in $LIST
    do
    if [ "$i" = "$1" ]
    then
        return 1
        break
    fi
    done
    return 0
}

# detect platform
platform="win"
uname_result=`uname`
if [ "$uname_result" = "Linux" ]; then
    platform="linux"
elif [ "$uname_result" = "Darwin" ]; then
    platform="mac"
fi

# change IFS to ignore filename's space in |for|
IFS="
"
# autoremove trailing whitespace
for line in `git diff --check --cached | sed '/^[+-]/d'` ; do
    # get file name
    if [ "$platform" = "mac" ]; then
    file="`echo $line | sed -E 's/:[0-9]+: .*//'`"
    else
    file="`echo $line | sed -r 's/:[0-9]+: .*//'`"
    fi

    lookup $(echo "$file" | awk -F . '{print $NF}')
    if [ $? -eq 1 ]
    then
    echo Omitting "$file"
    continue
    fi

    # display tips
    echo -e "auto remove trailing whitespace in \033[31m$file\033[0m!"
    # since $file in working directory isn't always equal to $file in index, so we backup it
    mv -f "$file" "${file}.save"
    # discard changes in working directory
    git checkout -- "$file"
    # remove trailing whitespace
    if [ "$platform" = "win" ]; then
    # in windows, `sed -i` adds ready-only attribute to $file(I don't kown why), so we use temp file instead
    sed 's/[[:space:]]*$//' "$file" > "${file}.bak"
    mv -f "${file}.bak" "$file"
    elif [ "$platform" == "mac" ]; then
    sed -i "" 's/[[:space:]]*$//' "$file"
    else
    sed -i 's/[[:space:]]*$//' "$file"
    fi
    git add "$file"
    # restore the $file
    sed 's/[[:space:]]*$//' "${file}.save" > "$file"
    rm "${file}.save"
done

if [ "x`git status -s | grep '^[A|D|M]'`" = "x" ]; then
    # empty commit
    echo
    echo -e "\033[31mNO CHANGES ADDED, ABORT COMMIT!\033[0m"
    exit 1
fi

# Now we can commit
exit

应排除的文件扩展名列表位于脚本开头的LIST中:

LIST="md txt c cpp"