从git pre-commit钩子运行时,`git diff`不起作用 [英] `git diff` does not work when run from a git pre-commit hook

查看:110
本文介绍了从git pre-commit钩子运行时,`git diff`不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个git pre-commit钩子,可以在提交之前对所有已修改的文件进行一些样式检查.

I have a git pre-commit hook that does some style checking on any modified files before committing.

实现是无关紧要的,但是它通过调用git diff开始.这是我在(repo)/.git/hooks/pre-commit中所拥有的.

The implementation is irrelevant, but it starts by calling git diff. Here's what i have in (repo)/.git/hooks/pre-commit.

#!/bin/sh


echo "=== Running script..."
git diff
echo "=== Done running script..."

# Other stuf
# ....

# Always exit with 1 so pre-commit hook always fails.
# Useful for testing
exit 1

当我实际尝试提交某些内容时,pre-commit钩子会正确触发,但是git diff命令不会输出任何内容(肯定有修改过的文件)

When I actually try committing something, the pre-commit hook correctly fires, but the git diff command doesn't output anything (there are definitely modified files)

> git commit --all -m "foo"
=== Running script...
=== Done running script...

但是,如果我直接/手动运行pre-commit钩子脚本,它确实有效

However if I run the pre-commit hook script directly/manually, it does work

> ./.git/hooks/pre-commit
=== Running script...
(... outputs git diff ...)
=== Done running script...

git调用钩子与手动调用钩子有什么不同?无论哪种方式(我的用户名),它都以同一用户身份运行

What's different about git calling the hook versus me manually calling it? It runs as the same user either way (my username)

我还尝试了

I've also tried suggestions from this thread, but unset GIT_DIR, --git-dir=, and work-tree= didn't fix anything.

谢谢!

推荐答案

您需要使用git diff --cached,因为更改已经进行.

You need to use git diff --cached because the changes are already staged.

这篇关于从git pre-commit钩子运行时,`git diff`不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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