首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从预提交钩子中排除某些文件类型

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

Stack Overflow用户
提问于 2014-11-24 14:28:43
回答 2查看 4.5K关注 0票数 2

我希望有一个预提交git钩子来检查(如果可能的话,自动删除)尾随空格。

使git在提交之前自动删除尾随空格。中,我找到了一个链接到github页面,其中实现了这样一个钩子。这很好,但正如@VonC在该页面中提到的那样

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

再往下走

我更愿意让钩子能够检测.md文件,而不是删除空白空间,而不是要求最终用户在git上添加一个-不验证选项。- VonC

据我所见,没有提到解决这一问题的办法。

由于我在我的项目中使用.md文件时有意识地尾随空格,这对我来说是个问题。

解决方案可能是微不足道的,但我对脚本所用的语言没有经验(目前也对学习它不感兴趣)。

这是脚本(github的副本):

代码语言:javascript
复制
#!/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文件?另外,如果有可能排除多个文件类型,那就太好了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-24 15:28:38

试试这个:

代码语言:javascript
复制
#!/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中:

代码语言:javascript
复制
LIST="md txt c cpp"
票数 1
EN

Stack Overflow用户

发布于 2022-04-29 12:30:48

您可以使用预承诺。最简单的配置是

代码语言:javascript
复制
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.2.0
    hooks:
      - id: trailing-whitespace
        args: [--markdown-linebreak-ext=md]

检查另一个钩子,例如一个检测合并冲突的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27107118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档