GIT和Ruby:如何在ruby脚本中取消设置GIT_DIR变量?

Jan*_*nis 5 ruby git variables shell unset

我写了一个非常简单的'部署'脚本作为我的post-update钩子在我的裸git仓库中运行.

变量如下

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
Run Code Online (Sandbox Code Playgroud)

livestage已经初始化的git回购(非裸)和我已经加入我的裸回购作为远程他们每个人(命名core),这样git pull core stagegit pull core live会从各自的拉动更新的文件branch中的core回购.

脚本如下:

#!/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
Run Code Online (Sandbox Code Playgroud)

我试过在这个bash脚本中调整'更新'文件,例如使用unset GIT_DIR运行下一个git命令git pull core stage.coreremote我的barerepo 添加到服务器上的另一个文件夹中.

但是,当执行上面的脚本时,我收到以下错误:

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.        
Run Code Online (Sandbox Code Playgroud)

有没有办法unset GIT_DIR在我的ruby脚本中执行与bash脚本相同的操作?

非常感谢,

Jannis

ndi*_*dim 6

这看起来像

`cd ~/stage.mydomain.com && unset GIT_DIR && git pull core stage`
Run Code Online (Sandbox Code Playgroud)

可以做这个工作.

推测为什么(推测我不熟悉ruby):你在运行unset命令的shell 中运行命令git pull(并且在他的回答中指出samold指出,当前工作目录也会出现同样的问题).

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


sar*_*old 5

尝试用这个替换你的行:

ENV['GIT_DIR']=nil
Run Code Online (Sandbox Code Playgroud)

我不确定你的:

`cd ~/stage.mydomain.com`
`unset GIT_DIR` # <= breaks!
`git pull core stage`
Run Code Online (Sandbox Code Playgroud)

部分即使在GIT_DIR未正确设置的情况下也能正常工作; 每个反引号都会启动一个与旧shell无关的新shell,子shell无法更改其父进程的当前工作目录.

试试这个:

ENV["GIT_DIR"]=nil
`cd ~/stage.mydomain.com ; git pull core stage`
Run Code Online (Sandbox Code Playgroud)