Skip to content

Instantly share code, notes, and snippets.

@textgoeshere
Last active December 19, 2015 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save textgoeshere/5956134 to your computer and use it in GitHub Desktop.
Save textgoeshere/5956134 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
###############################################################
# Prevents a commit when Gemfile has references to local gems #
###############################################################
if
test "$(git diff-index --cached HEAD -G'\Wpath(:|\s?=\>)' -- Gemfile)"
then
echo "Error: you have references to local gems in your Gemfile. Commit aborted."
exit 1
fi
@textgoeshere
Copy link
Author

@Ianfeather
Copy link

I actually did the same thing this morning after your email yesterday ;)

Won't the problem with the above gist be that it's going to test unstaged gemfiles?

I went with this approach:

#!/bin/sh
#

if git rev-parse --verify HEAD >/dev/null 2>&1
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

if 
    test "$(git diff-index --cached -S/Users/Ian/ $against)"
then
    echo "Error: Attempt to add a local gemfile"
    exit 1
fi

@textgoeshere
Copy link
Author

that is a damn good catch - thanks

@textgoeshere
Copy link
Author

updated

@textgoeshere
Copy link
Author

  • Drop this into .git/hooks/ of each project in which you'd like to use it
  • make it executable e.g. chmod +x .git/hooks/pre-commit
  • You can also follow these instructions to deploy this hook to all new projects http://stackoverflow.com/a/8842663/9474

@remybach
Copy link

remybach commented Jul 9, 2013

Does the regex need to start with \W? It doesn't work for me unless I remove it because I have a space between the , and path in my Gemfile.

@fifitrixabelle
Copy link

I had this as mine already (stolen from some blog post some where on the internet):

$ #!/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)
$ NESTED_GEMSPECS = Dir["#{ROOT_PATH}/*/.gemspec"]
$ GEMFILE = ENV['BUNDLE_GEMFILE'] || File.join(ROOT_PATH, 'Gemfile')

$ def local?(gemspec)
$ gemspec.source.instance_of?(Bundler::Source::Path)
$ end
$ def nested?(gemspec)
$ NESTED_GEMSPECS.any? { |filename| filename.include?(gemspec.name) }
$ end

$ if open(GEMFILE) { |gemfile| gemfile.grep(/path/) }
$ require 'rubygems'
$ require 'bundler/setup'
$ local_gems = Gem.loaded_specs.values.select do |gemspec|
$ local?(gemspec) and not nested?(gemspec)
$ end
$ unless local_gems.empty? then 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 committing." end
$ end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment