git pre-commit hook,将文件添加到索引中

发布于 2024-12-20 20:47:10 字数 1050 浏览 4 评论 0原文

我正在尝试编写一个简单的预提交挂钩来检查文件是否被修改,如果是,则压缩它并将其添加到当前索引中,像这样

#!/bin/sh                                                                                                                                                    

# was the file modified?
mf='git status | grep jquery.detectBrowser.js'

# is the non-compressed file in the staging area?
if [ $mf != "" ]
then
  # alert the user
  echo "Javascript file modified, YUI Compressor will begin now."

  # go to rhino
  cd $HOME/apps/rhino/yuicompressor-2.4.7/build

  # compress my file
  java -jar yuicompressor-2.4.7.jar ~/www/jquery.detectBrowser.js/jquery.detectBrowser.js -o ~/www/jquery.detectBrowser.js/jquery.detectBrowser.min.js

  # comeback to the initial directory
  cd -

  # add the new file into the index
  git add ~/www/jquery.detectBrowser.js/jquery.detectBrowser.min.js
fi

我有2个问题,1我的条件失败,每次,我一定有错字或类似的东西,但我不明白它是什么?这是我得到的错误:

[: 23: git: unexpected operator

我的第二个问题是,即使我删除条件,文件也从未真正添加到提交中,它被修改了,但从未添加。

谢谢, 狮子座

I'm trying to write a simple pre-commit hook to check if a file was modified, if so, compress it and add it into the current index, something like this

#!/bin/sh                                                                                                                                                    

# was the file modified?
mf='git status | grep jquery.detectBrowser.js'

# is the non-compressed file in the staging area?
if [ $mf != "" ]
then
  # alert the user
  echo "Javascript file modified, YUI Compressor will begin now."

  # go to rhino
  cd $HOME/apps/rhino/yuicompressor-2.4.7/build

  # compress my file
  java -jar yuicompressor-2.4.7.jar ~/www/jquery.detectBrowser.js/jquery.detectBrowser.js -o ~/www/jquery.detectBrowser.js/jquery.detectBrowser.min.js

  # comeback to the initial directory
  cd -

  # add the new file into the index
  git add ~/www/jquery.detectBrowser.js/jquery.detectBrowser.min.js
fi

I have 2 issues, 1 my condition is failing, every time, I must have a typo or something like that, but I can't figure out what it is? This is the error I get back:

[: 23: git: unexpected operator

And my second problem is that even if I remove the condition the file is never actually ADDED into the commit, it's modified, but never ADDED.

Thanks,
Leo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深者入戏 2024-12-27 20:47:10

您的错误是因为您没有引用 $mf。将其更改为“$mf”。尽管也许有比 grep 人类可读命令的输出更好的方法......例如,您可以查看 git status --porcelain 。或者甚至是 git diff --cached,只需检查退出代码,例如:

if ! git diff --quiet --cached <path>; then
     # the file was modified; do stuff
fi

我认为 Amber 可能误导了您:您应该使用 - -cached,因为如果更改没有暂存,那么就本次提交而言,没有任何更改,所以我假设您不想做任何其他事情。

当然,我不知道你的项目,但我不确定你为什么要做这样的事情 - 通常你不想签入机器生成的内容,只是让你可以轻松地从现有内容重建签入。

至于您的最后一个问题,文件被修改但未添加到提交中,我无法用玩具示例重现它。我将其作为预提交挂钩:

#!/bin/bash
touch z
git add z

并进行了提交,并且 z 按预期创建、添加和提交。

Your error is because you're not quoting $mf. Change it to "$mf". Though there are perhaps better ways than grepping the output of a human-readable command... you could have a look at git status --porcelain for example. Or even git diff --cached <path>, and just examine the exit code, e.g.:

if ! git diff --quiet --cached <path>; then
     # the file was modified; do stuff
fi

I think Amber may have misled you: you should use --cached, because if the changes are not staged, then as far as this commit is concerned, there are no changes, so I assume you don't want to do anything else.

And of course, I don't know your project, but I'm not sure why you're doing something like this - usually you don't want to check in machine-generated content, just make it easy to rebuild from what is checked in.

As for your last problem, the file being modified but not added to the commit, I can't reproduce it with a toy example. I made this as a pre-commit hook:

#!/bin/bash
touch z
git add z

and made a commit, and z was as expected created, added, and committed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文