`gemfile:` option causes post-checkout to frequently crash a rebase when working with a team that frequently updates the Gemfile.lock
I don't think this is really a bug, but maybe missing in documentation? Let me know what you think!
The short explanation is that, when you use the gemfile option on overcommit.yml and you pull some changes including an updated Gemfile.lock, the post-checkout git-hook may fail unless you run bundle install. This leaves the user on a detached HEAD state when trying to rebase their branch with the more up-to-date branch (as the running rebase will trigger the post-checkout hook too).
This has been causing much friction in my current team, as we're currently using overcommit's master branch because of this patch (may I also take the opportunity to ask for a patch version bump? 😄). We have some internal gems that are constantly updated and therefore Gemfile.lock is updated a couple dozen times every week.
Long version with in-depth reproducible example:
- Given an empty directory with:
# Gemfile
source 'https://rubygems.org'
gem 'rake', '= 13.0.5'
gem 'overcommit', github: 'sds/overcommit', branch: 'master'
# .overcommit.yml
gemfile: Gemfile
- And I installed the gems, committed that, installed overcommit hooks, and created a new branch from that initial commit:
$ bundle install
$ git init && git add . && git commit -m "Initial commit"
(main) $ bundle exec overcommit --install
(main) $ git branch overcommit-test
- And I change some gem version on the Gemfile, update Gemfile.lock and uninstall the gem
- obs.: this is really just simulating that someone else from your project updated a gem on the main branch and you just pulled it
(main) $ sed -i '' 's/13.0.5/13.0.6/g' Gemfile
(main) $ bundle install # to update Gemfile.lock
(main) $ git add . && git commit -m "Second commit"
(main) $ gem uninstall rake:13.0.6
- When I checkout to
overcommit-testand try to rebase it with the main branch, I get an error and the rebase fail halfway through (notice at the end the git is atdetached HEADstate - indicating a failure halfway the rebase):
(main) $ git checkout overcommit-test
(overcommit-test) $ git rebase main
Note: switching to '3ec8664c0d96c0b0b374c7422816173c6ce22171'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 3ec8664 Second commit
Problem loading 'Gemfile': Could not find rake-13.0.6 in any of the sources
Try running:
bundle install --gemfile=Gemfile
error: could not detach HEAD
(3ec8664) $
Note: as a temporary workaround, I found you can define a Gemfile just for overcommit, i.e.:
# .overcommit_gems.rb
source 'https://rubygems.org'
gem 'overcommit', github: 'sds/overcommit', branch: 'master'
# .overcommit.yml
gemfile: .overcommit_gems.rb
Since the respective Gemfile.lock should rarely change, this issue should rarely show up again.