2011-04-02 85 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