为什么我的git不能从行尾预先提交钩子来修剪空白? [英] Why isn't my git pre-commit hook trimming white space from the end of lines?

查看:100
本文介绍了为什么我的git不能从行尾预先提交钩子来修剪空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac Mojave上.我已经在〜/.git-templates/hooks/pre-commit创建了一个文件,我想从我提交的文件的行尾删除空白.我希望在所有我的项目中都能做到这一点.

I'm on Mac Mojave. I have created a file at ~/.git-templates/hooks/pre-commit , which I wanted to remove white space from the end of lines in files I'm committing. I would like this to happen globally, across all of my projects.

# A git hook script to find and fix trailing whitespace in your commits. Bypass
# it with the --no-verify option to git-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="
"

# remove trailing whitespace in modified lines
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]+: .*//'`"
    line_number="`echo $line | sed -E 's/.*:([0-9]+).*/\1/'`"
  else
    file="`echo $line | sed -r 's/:[0-9]+: .*//'`"
    line_number="`echo $line | sed -r 's/.*:([0-9]+).*/\1/'`"
  fi

  # since $file in working directory isn't always equal to $file in index,
  # we backup it; thereby we can add our whitespace fixes without accidently
  # adding unstaged changes
  backup_file="${file}.working_directory_backup"
  cat "$file" > "$backup_file"
  git checkout -- "$file" # discard unstaged changes in working directory

  # remove trailing whitespace in $file (modified lines only)
  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 "${line_number}s/[[:space:]]*$//" "$file" > "${file}.bak"
    mv -f "${file}.bak" "$file"
  elif [[ "$platform" == "mac" ]]; then
    sed -i "" "${line_number}s/[[:space:]]*$//" "$file"
  else
    sed -i "${line_number}s/[[:space:]]*$//" "$file"
  fi
  git add "$file" # to index, so our whitespace changes will be committed

  # restore unstaged changes in $file from its working directory backup, fixing
  # whitespace that we fixed above
  sed "${line_number}s/[[:space:]]*$//" "$backup_file" > "$file"
  rm "$backup_file"

  [[ "$platform" == "mac" ]] || e_option="-e" # mac does not understand -e
  echo $e_option "Removed trailing whitespace in \033[31m$file\033[0m:$line_number"
done

echo

# credits:
# https://github.com/philz/snippets/blob/master/pre-commit-remove-trailing-whitespace.sh
# https://github.com/imoldman/config/blob/master/pre-commit.git.sh

# If there still are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

# Now we can commit
exit

所以问题是它没有修剪行尾的空白.提交后打开文件时,仍然看到空白.所以我的问题是如何解决这个问题?我是在错误的位置上挂了钩子还是在文件中还需要做其他事情?

So the problem is that it is not trimming the white space at the end of lines. When I open my file after doing a commit, I still see the white space. So my quesiton is how do I fix this? Did I put the hook at the wrong location or is there something else I need to be doing in my file?

推荐答案

首先,确保该钩子位于全局钩子路径引用的文件夹中(

First, make sure the hook is in a folder referenced by a global hook path (available since Git 2.9)

git config --global core.hooksPath /path/to/my/centralized/hooks

然后,检查pre-commit挂钩是否可执行,并实际运行:在开始处至少添加一个回显,以验证其在提交时的执行.

Then, check the pre-commit hook is executable, and actually runs: Add at least one echo at the start, to validate its execution on a commit.

这篇关于为什么我的git不能从行尾预先提交钩子来修剪空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆