Permalink
Browse files

Merge pull request #1213 from tigris/bundler

---

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...
2 parents d9dd6ed + 127cf07 commit 45c4529daf2a2e807638fb167f2b6424590eb121 @indirect indirect committed Sep 18, 2011
Showing with 17 additions and 22 deletions.
  1. +17 −22 lib/bundler/source.rb
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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

0 comments on commit 45c4529

Please sign in to comment.