我使用这个“教程”来设置 DSP 环境: http://toroid.org/ ams/git-website-howto (是的,我没有 T)。
我的工作流程非常简单:
- 本地开发 (D)
- 提交一些内容
- 提交更多内容
- 推送到暂存(和 Github)(S)
- 在暂存上测试新代码
- 推送到生产(P)
我的代码包含由我的代码缩小的 CSS 文件,然后保存到 1 个文件:all.css
。在本地,我已关闭该选项,因此每次更改 CSS 时都不必手动删除 all.css
。在登台和生产中,它们应该尽快缓存(因此从单独的 CSS 文件创建 all.css
)。
问题是每次我推送时,我都必须删除 all.css
(和 all.js
- 同样的故事)才能看到更改(并正确测试)。
在本教程中,我创建了一个 post-receive
挂钩,用于将更改签入某个文件夹(Apache 读取代码的位置)。
我当前的 post-receive
钩子:
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
我想重用 $GIT_WORK_TREE
来删除 $GIT_WORK_TREE
中的两个文件(即 www/all. css
和 www/all.js
),但我不能...下一行没有 var $GIT_WORK_TREE 。
所以我把它改成这样,但我不喜欢这样,特别是如果我想在将来用它做更多事情:
#!/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
不会像这样重用。
我尝试过但不起作用的事情:
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:文件不存在等(/www/all.css)($GIT_WORK_TREE
为空)
GIT_WORK_TREE=/var/www/www.example.org
git checkout -f
致命:此操作必须在工作树中运行
GIT_WORK_TREE=/var/www/www.example.org
cd $GIT_WORK_TREE
git checkout -f
致命:不在 git 存储库(或任何......)中
我想我的问题与 Bash 的工作方式和 GIT 的工作方式一样重要 =)
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:
- Develop locally (D)
- Commit a few things
- Commit more things
- Push to Staging (and Github) (S)
- Test new code on Staging
- 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 =)
发布评论
评论(3)
您目前正在尝试解决一些问题。也许最好依次解释一下您尝试过的每个示例的问题:
这里的问题是,如果您直接在命令之前定义变量,则环境变量仅在您正在运行的命令的环境中设置 - 它是未在当前 shell 中设置。您可以轻松地测试这一点:
您的下一次尝试是:
在这种情况下,为了在脚本中调用的其他命令的环境中设置变量,您需要导出它,例如使用
export GIT_WORK_TREE= /var/www/www.example.org
而不是您的第一行。您的最后一次尝试是:
这与您之前的尝试有相同的问题(即您需要导出
GIT_WORK_TREE
),但它还有一个更微妙的问题。正如我在这篇博文中所述,post-receive 脚本环境中,>GIT_DIR 设置为
.
(即当前目录)。因此,如果您将目录更改为工作树的顶部,GIT_DIR
将指向该目录,而不是.git
目录!一些好的经验规则是 (a) 您应该只将GIT_DIR
和GIT_WORK_TREE
设置在一起,以及 (b) 如果您确实设置了它们,则应该同时设置它们到绝对路径。所以,我认为以下钩子适用于您的情况: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:
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:
Your next attempt was:
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:
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 thepost-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 setGIT_DIR
andGIT_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:为什么不使用变量作为路径?
那么你不需要导出。
这对我来说非常适合。
why not use a variable for the path?
then you don't need to export.
This works perfect for me.
应该有效并且是最安全的(您并不总是需要导出环境变量,但通常它不会造成伤害:))
should work and is safest (you don't always need to export your environment variables, but usually it doesn't harm :))