Skip to content

Instantly share code, notes, and snippets.

@colinrymer
Forked from christhekeele/pre-commit
Created May 17, 2013 03:09
Show Gist options
  • Save colinrymer/5596666 to your computer and use it in GitHub Desktop.
Save colinrymer/5596666 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# A pre-commit hook script to ensure that no local gem dependencies (gem 'asdf', path: '~/local')
# exist in your Gemfile before commiting.`
# Allows gems to be loaded from source within the current project, but not from outside.
puts 'Checking for local dependencies in your Gemfile.'
ROOT_PATH = File.expand_path('../../..', __FILE__)
begin
require 'rubygems'
gemfile = ENV['BUNDLE_GEMFILE'] || File.join(ROOT_PATH, 'Gemfile')
require 'bundler/setup' if File.exists?(gemfile)
rescue => e
puts 'There was an issue loading Bundler in the pre-commit git hook that ensures your Gemfile is not dependent on locally sourced gems.'
puts 'Either the script is screwy, or your environment is misconfigured for Bundler.'
raise e
else
local_gems = Gem.loaded_specs.values.select do |g|
local = g.source.instance_of? Bundler::Source::Path
nested = Dir["#{ROOT_PATH}/**/*.gemspec"].any? do |filename|
filename.include?(g.name)
end
local && !nested
end
unless local_gems.empty?
raise SyntaxError,
"You have local dependencies in your Gemfile:" \
" #{local_gems.map{|g| g.name + ' is loading from ' + g.source.path.to_s}.join(', ')}." \
" Remove the :path parameter from this/these gems in your Gemfile before commiting."
else
puts 'No local Gemfile dependencies detected. You\'re good to commit!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment