Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT & Ruby: How can I unset the GIT_DIR variable from inside a ruby script?

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

like image 802
Jannis Avatar asked Apr 02 '11 23:04

Jannis


2 Answers

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.

like image 88
ndim Avatar answered Sep 23 '22 09:09

ndim


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`
like image 39
sarnold Avatar answered Sep 22 '22 09:09

sarnold