GIT& Ruby:我如何从ruby脚本中取消设置GIT_DIR变量? [英] GIT & Ruby: How can I unset the GIT_DIR variable from inside a ruby script?

查看:166
本文介绍了GIT& Ruby:我如何从ruby脚本中取消设置GIT_DIR变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的裸git仓库里写了一个非常简单的'deploy'脚本作为我的更新后的钩子。



变量如下:

  live domain =〜/ mydomain.com 
staging domain =〜 /stage.mydomain.com
git repo location =〜/ git.mydomain.com / thisrepo.git(裸)
$ b $ core =〜/ git.mydomain.com / thisrepo.git
core ==添加到每个live&舞台乐曲

live & stage 已初始化git repos(非裸),并且我已将裸露的回购作为每个回收(分别命名为 core ),这样 git pull core stage git pull core live 会从相应的分支核心回购。



该脚本为如下所示:

 #!/ usr / bin / env ruby​​ 


ARGV.each do | branch |

#如果它匹配舞台,那么'更新'分段文件
if branch ==refs / heads / stage

puts
表示看起来升级分支已更新。
puts现在运行一个树结账...
puts
`cd〜/ stage.mydomain.com`
`unset GIT_DIR`#<= break!
`git pull core stage`
puts
puts在分支分支上完成树拉。
puts

#如果是实时网站更新,更新这些文件
elsif branch ==refs / heads / live

puts
puts看起来活着的分支已经更新了。
puts现在运行一个树结算...
puts
`cd〜/ mydomain.com`
`unset GIT_DIR`#<= break!
`git pull core live`
puts
puts在分支上完成树结算。
puts

结束

结束

我尝试了修改这个bash脚本,它使用 unset GIT_DIR 来运行下一个git命令 git pull core stage 例如。 核心是我的 bare 回购中添加的远程不同的文件夹在服务器上。



然而当执行上面的脚本时,我得到以下错误:

  remote:hooks / post-update:35:找不到命令:unset GIT_DIR 
remote:fatal:/ usr / lib / git-core / git-pull无法使用工作的树。

有没有办法像取消设置GIT_DIR 在我的ruby脚本中的bash脚本中?

非常感谢,

Jannis

解决方案

这看起来像

  cd〜/ stage.mydomain.com&&取消设置GIT_DIR&& git pull core stage` 

可以完成这项工作。



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

这表明可能有一些ruby API操纵ruby传递到它使用反引号操作符启动的shell的环境,并且也改变当前的工作目录。

I've written a very simple 'deploy' script running as my post-update hook inside my bare git repo.

The variables are as follows

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

both live & stage have initialized git repos (non-bare) and I have added my bare repo as a remote to each of them (named core) so that git pull core stage or git pull core live would pull the updated files from the respective branch of the core repo.

The script is as follows:

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

I have tried adapting the 'updating' of files from this bash script here which uses the unset GIT_DIR to run the next git command git pull core stage for example. core is the added remote of my bare repo in a different folder on the server.

However when executing the script above I'm getting the following errors:

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.        

Is there any way to do the same thing as unset GIT_DIR in a bash script within my ruby script?

Many thanks,

Jannis

解决方案

This looks like

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

could do the job.

Speculating on why (speculating as I am not familiar with ruby): you were running the unset command in a different shell from the one which runs the git pull (and as samold points out in his answer, the same issue happens with the current working directory).

Which suggests that there might be some ruby API which manipulates the environment ruby passes to the shell it launches with the backtick operator, and also to change the current working directory.

这篇关于GIT&amp; Ruby:我如何从ruby脚本中取消设置GIT_DIR变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆