吉特与Ruby:如何从 ruby​​ 脚本内部取消设置 GIT_DIR 变量?

发布于 2024-10-29 18:18:02 字数 2089 浏览 1 评论 0原文

我编写了一个非常简单的“部署”脚本,作为我的 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

live & stage 已初始化 git 存储库(非裸),并且我已将裸存储库作为远程添加到每个存储库(名为 core),以便 git pull core stage 或 git 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

我尝试调整 此处的 bash 脚本,它使用 unset GIT_DIR 来运行下一个 git 命令git pull core stagecore 是我的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.        

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

非常感谢,

詹尼斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无悔心 2024-11-05 18:18:02

这看起来

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

可以完成这项工作。

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

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

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.

鲜血染红嫁衣 2024-11-05 18:18:02

尝试用以下内容替换您的行:

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`

Try replacing your line with this:

ENV['GIT_DIR']=nil

I'm not sure your:

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

sections will work even when the GIT_DIR has been properly unset; each backticks starts a new shell, unrelated to the old shell, and child shells cannot change the current working directory of their parent processes.

Try this:

ENV["GIT_DIR"]=nil
`cd ~/stage.mydomain.com ; git pull core stage`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文