2011-04-02 89 views
5

我写运行作为我的我裸露的git仓库内post-update钩一个非常简单的“部署”脚本。GIT&Ruby:如何从ruby脚本中取消设置GIT_DIR变量?

的变量如下

live domain   = ~/mydomain.com 
staging domain  = ~/stage.mydomain.com 
git repo location = ~/git.mydomain.com/thisrepo.git (bare) 

core    = ~/git.mydomain.com/thisrepo.git 
core    == added remote into each live & stage gits 

两个live & stage已初始化的git回购(非裸)和我已经加入我的裸回购作为远程他们每个人(命名为core),以便git pull core stagegit pull core live将从core回购的各branch拉更新的文件。

的脚本如下:

#!/usr/bin/env ruby 

# Loop over each passed in argument 
ARGV.each do |branch| 

    # If it matches the stage then 'update' the staging files 
    if branch == "refs/heads/stage" 

    puts "" 
    puts "Looks like the staging branch was updated." 
    puts "Running a tree checkout now…" 
    puts "" 
    `cd ~/stage.mydomain.com` 
    `unset GIT_DIR` # <= breaks! 
    `git pull core stage` 
    puts "" 
    puts "Tree pull completed on staging branch." 
    puts "" 

    # If it's a live site update, update those files 
    elsif branch == "refs/heads/live" 

    puts "" 
    puts "Looks like the live branch was updated." 
    puts "Running a tree checkout now…" 
    puts "" 
    `cd ~/mydomain.com` 
    `unset GIT_DIR` # <= breaks! 
    `git pull core live` 
    puts "" 
    puts "Tree checkout completed on live branch." 
    puts "" 

    end 

end 

我试图适应从this bash script here文件的“更新”,它使用unset GIT_DIR运行下一个混帐命令git pull core stage例如。 core在服务器上的其他文件夹中添加的我bare回购remote。在执行上面的脚本,我发现了以下错误

然而,当:

remote: hooks/post-update:35: command not found: unset GIT_DIR   
remote: fatal: /usr/lib/git-core/git-pull cannot be used without a working tree.   

有没有办法做同样的事情,unset GIT_DIR在我的Ruby脚本中的bash脚本?

非常感谢,

Jannis

回答

6

这看起来像

`cd ~/stage.mydomain.com && unset GIT_DIR && git pull core stage` 

可以做的工作。

猜测为什么(猜测,因为我不熟悉的红宝石):你从一个不同的外壳,其运行git pull(和samold指出了他的答案运行unset命令,同样的问题与当前工作目录发生)。

这表明可能有一些红宝石的API,它操纵红宝石传递到外壳环境将其与反引号操作符启动,并改变当前的工作目录。

+0

这就像魔术!非常感谢你。现在就完成并运行!关闭以了解如何通过将特殊格式的注释传递到提交说明中来实现同样的效果:)我想知道是否可以使用'[update:live]'或'[update:stage]'来触发这个'pull '行动... – Jannis 2011-04-03 00:41:13

+0

刚刚救了我一大堆挫折 - 谢谢!没有意识到这些变数有这样的意义! – 2013-05-28 18:31:57

4

尝试用这个替换您行:

ENV['GIT_DIR']=nil 

我不知道你:

`cd ~/stage.mydomain.com` 
`unset GIT_DIR` # <= breaks! 
`git pull core stage` 

段将工作即使GIT_DIR一直未设置正确;每个反引号会启动一个与旧shell无关的新shell,并且子shell不能更改其父进程的当前工作目录。

试试这个:

​​
+0

感谢您的提示!这同样和__ndim __的答案一样好,但是因为我的最终脚本使用了单个字符串,所以我选择接受他的答案而不是这个答案。不管多谢,再次感谢! – Jannis 2011-04-03 00:42:35

+1

@Jannis,取决于你的目标:@ ndim的答案是更好的,如果你想只为'_one_调用'取消设置'GIT_DIR'。如果你想为整个脚本取消设置'GIT_DIR',Mine会更好。他们做不同的事情:)所以选择适合的任务更好。 – sarnold 2011-04-03 00:46:39

+0

哦,..现在你提到它,这确实是完全合理的!谢谢你让我知道这件事,在这种情况下,我会保留它作为'未设置..',但为了将来的脚本冒险,这肯定会非常方便。再次感谢。 – Jannis 2011-04-03 01:00:52