修改pre-commit解决git中的 trailing whitespace问题-windows平台
来源:百度文库 编辑:神马文学网 时间:2024/04/19 19:23:35
git版本:1.7.3.1.msysgit.0 今天安装了windows平台下的git,但是commit的时候提示了一堆的trailing whitespace问题,打开文件后发现是写程序时再语句结束的后面多敲了几个空格或在文件末端多了几个空行所致。但是那么多文件不可能手动一个个改吧。 后来再网上看到有一个论坛的帖子解决了这个问题,有两种方法:1、临时解决办法在commit的时候指定--no-verify选项,不让git去检查这类错误。同时git也不去检查其它错误了。git commit --no-verify 2、永久解决导致这个问题的原因是:在.git\hooks目录下的pre-commit文件中有如下的几条检查语句: if (s/^\+//) {
$lineno++;
chomp;
if (/\s$/) {
bad_line("trailing whitespace", $_); 你可以将最后一句注释掉来永久避免弹出这个错误,但是我推荐大家还是利用命令将whitespace自动消除掉。这个文件中的命令我并不熟悉,不过在网上找到了用来消除trailing whitespace的shell脚本
$lineno++;
chomp;
if (/\s$/) {
bad_line("trailing whitespace", $_); 你可以将最后一句注释掉来永久避免弹出这个错误,但是我推荐大家还是利用命令将whitespace自动消除掉。这个文件中的命令我并不熟悉,不过在网上找到了用来消除trailing whitespace的shell脚本
#!/bin/sh
if git-rev-parse --verify HEAD >/dev/null 2>&1 ; then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Find files with trailing whitespace
for FILE in `exec git diff-index --check --cached $against -- | sed '/^[+-]/d' | sed -r 's/:[0-9]+:.*//' | uniq` ; do
# Fix them!
sed -i 's/[[:space:]]*$//' "$FILE"
done
exit
现在只要你把这段代码放到你的pre-commit文件中的合适位置就可以了。该文件的语法比较晦涩,起初不知道该怎么放,折腾了几次总算放对了,我在我的pre-commit文件中是这么放的(红色部分): #!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by git-commit with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, make this file executable.# This is slightly modified from Andrew Morton's Perfect Patch.
# Lines you introduce should not have trailing whitespace.
# Also check for an indentation that has SP before a TAB.if git-rev-parse --verify HEAD 2>/dev/null
then
git-diff-index -p -M --cached HEAD --
else
# NEEDSWORK: we should produce a diff with an empty tree here
# if we want to do the same verification for the initial import.
:
fi |
# Find files with trailing whitespace
for FILE in `exec git diff-index --check --cached $against -- | sed '/^[+-]/d' | sed -r 's/:[0-9]+:.*//' | uniq` ; do
# Fix them!
sed -i 's/[[:space:]]*$//' "$FILE"
done
perl -e '
my $found_bad = 0;
my $filename;
my $reported_filename = "";
my $lineno;
sub bad_line {
my ($why, $line) = @_;
if (!$found_bad) {
print STDERR "*\n";
print STDERR "* You have some suspicious patch lines:\n";
print STDERR "*\n";
$found_bad = 1;
}
if ($reported_filename ne $filename) {
print STDERR "* In $filename\n";
$reported_filename = $filename;
}
print STDERR "* $why (line $lineno)\n";
print STDERR "$filename:$lineno:$line\n";
}
while (<>) {
if (m|^diff --git a/(.*) b/\1$|) {
$filename = $1;
next;
}
if (/^@@ -\S+ \+(\d+)/) {
$lineno = $1 - 1;
next;
}
if (/^ /) {
$lineno++;
next;
}
if (s/^\+//) {
$lineno++;
chomp;
if (/\s$/) {
bad_line("trailing whitespace", $_);
}
if (/^\s* \t/) {
bad_line("indent SP followed by a TAB", $_);
}
if (/^([<>])\1{6} |^={7}$/) {
bad_line("unresolved merge conflict", $_);
}
}
}
exit($found_bad);
' 对pre-commit感兴趣的可以去看看git的hook方面的东西。似乎类似于vc里面 编译前或编译后指定要做的工作
修改pre-commit解决git中的 trailing whitespace问题-windows平台
An Illustrated Guide to Git on Windows
解决生活中的小问题
解决电脑中的各种问题
轻松解决Windows虚拟内存不够的问题
轻松解决Windows虚拟内存不够的问题
轻松解决Windows虚拟内存不够的问题
解决Windows虚拟内存不足的问题
轻松解决Windows系统棘手问题
妙用Windows安全模式解决诸多问题
解决windows installer服务不能访问的问题 - Windows Vista ...
Windows Mobile for SmartPhone 中的特权问题
认识 Windows 设备管理器中的问题符号
解决PowerPoint课件中的菜单问题1
解决PowerPoint课件中的菜单问题
解决“Unable to initialize Windows Sockets interface”的问题
不用重装轻松解决Windows系统棘手问题
Windows映射模式及相关问题的解决
五招解决Windows XP启动后操作迟延问题
不用重装轻松解决Windows系统棘手问题
解决Virtual pc/windows CE emulator警告问题
解决Windows 程序界面闪烁问题的一些经验-yaodong
完美解决windows 7 RC 英文版中文乱码问题
解决Virtual pc/windows CE emulator警告问题