5

我编写了一个非常简单的“部署”脚本,post-update在我的裸 git repo 中作为我的钩子运行。

变量如下

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 repos(非裸),并且我已将我的裸 repo 作为远程添加到它们中的每一个(命名core),以便git pull core stagegit pull core live将从各自branchcorerepo 中提取更新的文件。

脚本如下:

#!/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

我已经尝试从这个 bash 脚本中调整文件的“更新”,例如,它使用unset GIT_DIR来运行下一个 git 命令git pull core stage。是在服务器上的不同文件夹中core添加remote了我的存储库。bare

但是,在执行上面的脚本时,出现以下错误:

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 脚本相同的事情?

非常感谢,

詹尼斯

4

2 回答 2

6

这看起来像

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

可以完成这项工作。

推测原因(推测因为我不熟悉 ruby​​):您在与运行unset命令的 shell 不同的 shell中运行命令git pull(并且正如samold在他的回答中指出的那样,当前工作目录也会出现同样的问题)。

这表明可能有一些 ruby​​ API 可以操纵环境 ruby​​ 传递给它使用反引号运算符启动的 shell,并更改当前工作目录。

于 2011-04-02T23:33:15.910 回答
5

尝试用这个替换你的行:

ENV['GIT_DIR']=nil

我不确定你的:

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

即使GIT_DIR已正确取消设置,部分也将起作用;每个反引号都会启动一个新的 shell,与旧的 shell 无关,并且子 shell 不能更改其父进程的当前工作目录。

试试这个:

ENV["GIT_DIR"]=nil
`cd ~/stage.mydomain.com ; git pull core stage`
于 2011-04-02T23:35:23.043 回答