How to: Use SourceTree, with git hook via lefthook, running bundle exec with asdf managing Ruby versions

pikachuexe

PikachuEXE

Posted on October 21, 2021

How to: Use SourceTree, with git hook via lefthook, running bundle exec with asdf managing Ruby versions

That's a long title huh?

Anyway I got a combination of software I use with an issue that I cannot find the answer on the Internet.

Background

At the time of writing, I use

  • SourceTree 3.2.1
  • Git (latest, version doesn't matter)
  • lefthook 0.7.7 (as git hook manager)
  • asdf with ruby plugin

Lefthook Setup

I have hook scripts inside folder ./.githooks under project root (all version controlled)

git config core.hooksPath .githooks
brew install lefthook
Enter fullscreen mode Exit fullscreen mode

Workaround for source tree for path issue

sudo launchctl config user path `echo $PATH`
Enter fullscreen mode Exit fullscreen mode

Ref:

Issue

I have recently switched from RVM to asdf.
After a restart of my computer I realize that one of my git hooks running bundle exec rubocop no longer works.
The workaround above works for RVM.

Investigation

  • lefthook binary is found (with workaround above)
  • bundle is using the binary from MacOS built-in Ruby

Solution

In my hook script pre-commit
Add the following before the code running lefthook

# ASDF compatibility
if test -f /usr/local/opt/asdf/libexec/asdf.sh ; then
  . /usr/local/opt/asdf/libexec/asdf.sh
fi
if test -f /opt/homebrew/opt/asdf/libexec/asdf.sh ; then
  . /opt/homebrew/opt/asdf/libexec/asdf.sh
fi

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
pikachuexe
PikachuEXE

Posted on October 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related