git pre-commit hook,将文件添加到索引中 [英] git pre-commit hook, add file into index

查看:193
本文介绍了git pre-commit hook,将文件添加到索引中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个简单的pre-commit钩子来检查文件是否被修改,如果是这样,压缩它并将其添加到当前索引中,像这样

 #!/ bin / sh 

#是否修改了文件?
mf ='git status | grep jquery.detectBrowser.js'

#是暂存区中的非压缩文件吗?
if [$ mf!=]
然后
#提醒用户
echoJavascript文件已修改,YUI Compressor将立即开始。
$ b $转到犀牛
cd $ HOME / apps / rhino / yuicompressor-2.4.7 / build

#压缩我的文件
java -jar yuicompressor-2.4.7.jar〜/ www / jquery.detectBrowser.js / jquery.detectBrowser.js -o〜/ www / jquery.detectBrowser.js / jquery.detectBrowser.min.js

#回到初始目录
cd -

#将新文件添加到索引
git add〜/ www / jquery.detectBrowser.js / jquery.detectBrowser.min.js
fi

我有两个问题,1我的条件是失败的,每次我必须有一个错字或类似的东西,但我无法弄清楚它是什么?这是我回来的错误:

  [:23:git:unexpected operator 

我的第二个问题是,即使我删除了该条件,该文件从未实际添加到提交中,但它已被修改,但从未添加过。



谢谢,
Leo

解决方案

'不要引用 $ mf 。将其更改为$ mf。虽然也许有更好的方法比输入一个人类可读的命令更好......例如,你可以看看 git status --porcelain 。甚至是 git diff --cached< path> ,然后检查退出代码,例如:

  if! git diff --quiet --cached< path>;然后
#文件被修改;做东西
fi

我认为Amber可能误导了你:你 / em>使用 - cached ,因为如果更改没有被执行,那么就这个提交而言,没有改变,所以我假设你没有想做别的事。



当然,我不知道你的项目,但我不确定你为什么要这样做 - 通常你不希望检查机器生成的内容,只需轻松地从检入内容进行重建即可。



至于最后一个问题,文件被修改但没有添加到提交,我不能用玩具的例子来重现它。我做了一个预先提交的钩子:

 #!/ bin / bash 
touch z
git add z

并进行了提交,并且z按照预期创建,添加和提交。 / p>

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

解决方案

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.

这篇关于git pre-commit hook,将文件添加到索引中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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