Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1213 from tigris/bundler
Browse files Browse the repository at this point in the history
---

Use the --git-dir and --work-tree options for existing git repos rather than chdir to them. This is due to some problem with cron not properly chdiring.

We run a "bundle install" in our git/hook/post-merge, and "git pull" out of cron ever hour. So if you push code to github, cron will pull it out on the hour and then post-merge will run the bundle install. All non-interactive. Unforunately this barfs on gems that use git as the source. From debugging it seems that when bundler does a chdir and then invokes git, the chain is something like cron -> invoked git -> invoked bundler -> chdird -> invoked git. For some reason the last git doesnt see the chdir and tries to run its git commands from ".".

The only solution I see is to not chdir for git, but instead pass git the flags it needs to know which repo to act upon. Which is the solution I have implemented here, and it fixes the problem while still working normally as well (outside of cron).
  • Loading branch information
indirect committed Sep 18, 2011
2 parents d9dd6ed + 127cf07 commit 45c4529
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions lib/bundler/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -615,16 +615,16 @@ def uri_hash
Digest::SHA1.hexdigest(input)
end

# Escape the URI for git commands
def uri_escaped
# Escape an argument for shell commands.
def escape(arg)
if Bundler::WINDOWS
# Windows quoting requires double quotes only, with double quotes
# inside the string escaped by being doubled.
'"' + uri.gsub('"') {|s| '""'} + '"'
'"' + arg.to_s.gsub('"') {|s| '""'} + '"'
else
# Bash requires single quoted strings, with the single quotes escaped
# by ending the string, escaping the quote, and restarting the string.
"'" + uri.gsub("'") {|s| "'\\''"} + "'"
"'" + arg.to_s.gsub("'") {|s| "'\\''"} + "'"
end
end

Expand All @@ -644,13 +644,12 @@ def cache
if cached?
return if has_revision_cached?
Bundler.ui.info "Updating #{uri}"
in_cache do
git %|fetch --force --quiet --tags #{uri_escaped} "refs/heads/*:refs/heads/*"|
end
cache unless cached?
git %|--git-dir #{escape cache_path} fetch --force --quiet --tags #{escape uri} "refs/heads/*:refs/heads/*"|
else
Bundler.ui.info "Fetching #{uri}"
FileUtils.mkdir_p(cache_path.dirname)
git %|clone #{uri_escaped} "#{cache_path}" --bare --no-hardlinks|
git %|clone #{escape uri} "#{cache_path}" --bare --no-hardlinks|
end
end

Expand All @@ -661,20 +660,20 @@ def checkout
git %|clone --no-checkout "#{cache_path}" "#{path}"|
File.chmod((0777 & ~File.umask), path)
end
Dir.chdir(path) do
git %|fetch --force --quiet --tags "#{cache_path}"|
git "reset --hard #{revision}"
dir_opts = %|--git-dir #{escape path}/.git --work-tree #{escape path}|
git %|#{dir_opts} fetch --force --quiet --tags "#{cache_path}"|
git "#{dir_opts} reset --hard #{revision}"

if @submodules
git "submodule init"
git "submodule update"
end
if @submodules
git "#{dir_opts} submodule init"
git "#{dir_opts} submodule update"
end
end

def has_revision_cached?
return unless @revision
in_cache { git %|cat-file -e #{@revision}| }
cache unless cached?
git %|--git-dir #{escape cache_path} cat-file -e #{@revision}|
true
rescue GitError
false
Expand All @@ -687,7 +686,8 @@ def allow_git_ops?
def revision
@revision ||= begin
if allow_git_ops?
in_cache { git("rev-parse #{ref}").strip }
cache unless cached?
git("--git-dir #{escape cache_path} rev-parse #{ref}").strip
else
raise GitError, "The git source #{uri} is not yet checked out. Please run `bundle install` before trying to start your application"
end
Expand All @@ -697,11 +697,6 @@ def revision
def cached?
cache_path.exist?
end

def in_cache(&blk)
cache unless cached?
Dir.chdir(cache_path, &blk)
end
end

end
Expand Down

0 comments on commit 45c4529

Please sign in to comment.