重用GIT_WORK_TREE在后收到钩RM几个文件 [英] Reuse GIT_WORK_TREE in post-receive hook to rm a few files

查看:118
本文介绍了重用GIT_WORK_TREE在后收到钩RM几个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个教程建立一个DSP环境: http://toroid.org/ams/混帐网站-HOWTO (是的,我没有一个T)。

我的工作流程非常简单:


  1. 在当地发展( D

  2. 提交的几件事情

  3. 提交更多的事情

  4. 一键分期(和Github上)(取值

  5. 测试新的$ C $舞台上ç

  6. 一键生产( P

我的code包含由我的code精缩,然后保存到1个文件CSS文件: all.css 。在当地,我已经变成了选项关闭,所以我不必手动删除 all.css 所有每次我改变我的CSS的时间。在这两个临时和生产,他们应该尽快缓存越好(因此从单独的CSS文件创建 all.css )。

问题是每次我推的时候,我必须删除 all.css (和 all.js的 - - 完全相同的故事),看到的变化(和正确测试)

这是我做了一个后接受挂钩,检查出来的变化到一个特定的文件夹教程(如Apache的读取code)。

我现在的后接受挂钩:

 #!/ bin / sh的
GIT_WORK_TREE = /无功/网络/ www.example.org git的结帐-f

我想重用 $ GIT_WORK_TREE 来在 $ GIT_WORK_TREE 删除这两个文件(即 WWW / all.css WWW / all.js的),但我不能......有下一行没有变种$ GIT_WORK_TREE

所以我改成了这一点,但我不喜欢这样,尤其是当我想要做的更多的是在未来的:

 #!/ bin / sh的
GIT_WORK_TREE = /无功/网络/ www.example.org git的结帐-f
RM /var/www/www.example.org/www/all.css
RM /var/www/www.example.org/www/all.js

$ GIT_WORK_TREE 不重用这样的。

事情我已经试过了不起作用:

  GIT_WORK_TREE = /无功/网络/ www.example.org git的结帐-f
RM $ GIT_WORK_TREE / WWW / all.css
RM $ GIT_WORK_TREE / WWW / all.js的


  

RM:文件不存在等(/www/all.css)( $ GIT_WORK_TREE 为空)


  GIT_WORK_TREE = /无功/网络/ www.example.org
git的结帐-f


  

致命:此操作必须在工作树中运行


  GIT_WORK_TREE = /无功/网络/ www.example.org
CD $ GIT_WORK_TREE
git的结帐-f


  

致命的:不是在一个Git仓库(或任何.....)


我想我的问题是尽可能多的使用bash是如何工作的,因为它与GIT是如何工作=)


解决方案

有与你此刻想的几个问题。也许是最好的解释有什么错,你又试过每一个例子:

  GIT_WORK_TREE = /无功/网络/ www.example.org git的结帐-f
RM $ GIT_WORK_TREE / WWW / all.css
RM $ GIT_WORK_TREE / WWW / all.js的

这里的问题是,如果你直接在命令之前定义一个变量,环境变量只在您正在运行的命令的环境中设置 - 它不是在当前shell设置。您可以轻松地测试:

  $ FOO =酒吧SH -c'回声$ FOO
酒吧
$回声$ FOO$

你的下一个尝试是:

  GIT_WORK_TREE = /无功/网络/ www.example.org
git的结帐-f

在这种情况下,为了使变量在你的脚本中调用其他命令的环境中进行设置,您需要使用出口GIT_WORK_TREE =在/ var / WWW导出,例如/ www.example.org ,而不是你的第一道防线。

您最后一次尝试是:

  GIT_WORK_TREE = /无功/网络/ www.example.org
CD $ GIT_WORK_TREE
git的结帐-f

这有同样的问题作为您previous尝试(即你需要导出 GIT_WORK_TREE ),但它也有一个更微妙的问题。正如我描述这个博客帖子 GIT_DIR 后接受脚本环境设置为(即当前目录) 。所以,如果你改变目录到工作树的顶部, GIT_DIR 将指向该目录中,而不是的.git 目录!一对夫妇的拇指良好的规则是:(a)您应该只设置 GIT_DIR GIT_WORK_TREE 在一起,和(b )如果你设置他们,你应该将它们都设置为绝对路径。所以,我认为下面的钩子将工作你的情况:

 #!/ bin / sh的
出口GIT_WORK_TREE = /无功/网络/ www.example.org
出口GIT_DIR = /无功/网络/ www.example.org / git的
CD $ GIT_WORK_TREE
git的结帐-f
RM WWW / all.css
RM WWW / all.js的

I've used this 'tutorial' to set up a DSP environment: http://toroid.org/ams/git-website-howto (Yes, I don't have a T).

My workflow is very easy:

  1. Develop locally (D)
  2. Commit a few things
  3. Commit more things
  4. Push to Staging (and Github) (S)
  5. Test new code on Staging
  6. Push to Production (P)

My code contains CSS files that are minified by my code and then saved to 1 file: all.css. Locally, I've turned that option off, so I don't have to manually remove all.css all the time every time I change my CSS. On both Staging and Production, they should cache as soon as possible (so create all.css from the separate CSS files).

The problem is every time I push, I have to remove all.css (and all.js -- same exact story) to see the changes (and properly test).

From the tutorial I made a post-receive hook that checks out the changes into a certain folder (where Apache reads the code).

My current post-receive hook:

#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f

I want to reuse $GIT_WORK_TREE to remove two files within $GIT_WORK_TREE (being www/all.css and www/all.js), but I can't... There's no var $GIT_WORK_TREE on the next line.

So I changed it to this, but I don't like that, especially if I want to do more with it in the future:

#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
rm /var/www/www.example.org/www/all.css
rm /var/www/www.example.org/www/all.js

$GIT_WORK_TREE is NOT reused like this.

Things I've tried that don't work:

GIT_WORK_TREE=/var/www/www.example.org git checkout -f
rm $GIT_WORK_TREE/www/all.css
rm $GIT_WORK_TREE/www/all.js

rm: file doesn't exist etc (/www/all.css) ($GIT_WORK_TREE is empty)

GIT_WORK_TREE=/var/www/www.example.org
git checkout -f

fatal: this operation must be run in a work tree

GIT_WORK_TREE=/var/www/www.example.org
cd $GIT_WORK_TREE
git checkout -f

fatal: Not in a git repository (or any .....)

I guess my problem is as much with how Bash works as it is with how GIT works =)

解决方案

There are a few problems with that you're trying at the moment. Perhaps it's best to explain what's wrong with each example you've tried in turn:

GIT_WORK_TREE=/var/www/www.example.org git checkout -f
rm $GIT_WORK_TREE/www/all.css
rm $GIT_WORK_TREE/www/all.js

The problem here is that if you define a variable directly before the command, the environment variable is only set in the environment of the command you're running - it's not set in the current shell. You can easily test this:

$ FOO=bar sh -c 'echo $FOO'            
bar
$ echo $FOO

$ 

Your next attempt was:

GIT_WORK_TREE=/var/www/www.example.org
git checkout -f

In this case, in order for the variable to be set in the environment of other commands you invoke in the script, you need to export it, for example with export GIT_WORK_TREE=/var/www/www.example.org instead of your first line.

Your last attempt was:

GIT_WORK_TREE=/var/www/www.example.org
cd $GIT_WORK_TREE
git checkout -f

This has the same problem as your previous attempt (i.e. that you need to export GIT_WORK_TREE) but it also has a more subtle problem. As I describe in this blog post, GIT_DIR is set to . (i.e. the current directory) in the post-receive script's environment. So, if you change directory to the top of the working tree, GIT_DIR will point to that directory, and not the .git directory! A couple of good rules of thumb are (a) that you should only set GIT_DIR and GIT_WORK_TREE together, and (b) if you do set them, you should set them both to absolute paths. So, I think the following hook will work in your case:

#!/bin/sh
export GIT_WORK_TREE=/var/www/www.example.org
export GIT_DIR=/var/www/www.example.org/.git
cd $GIT_WORK_TREE
git checkout -f
rm www/all.css
rm www/all.js

这篇关于重用GIT_WORK_TREE在后收到钩RM几个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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