Formating old Elixir code automatically using Git Hooks and Docker

In the process of trying to fix a bug with one of our projects, it became apparent that the system had become depleted and reached its end of life on the newest Mac version. So was born the horror of Docker, Docker allows you to create a one-off virtual machine with just the OS, your dependencies and your code after the initial nightmare of its setup it makes everyone’s lives easier by making the whole project just work after 2 commands.

For the greater good

The Hook

The project has a requirement in DevOps for the project to be specially formatted luckily the Elixir team have a good implementation of this by using mix format but this differs from each Elixir Version and the requred verion no longer works on the Mac so the logical move?

Make a Git hook that will format it automatically. a Git hook is a cool feature of Git that allows you to run code on certain git actions. so the single file needed for this is attached below and will first check you have Docker installed then it will check that the container is built so it won’t take too long to format the commit it will then start the container up, run the format command then stop the container.

The user can then create a new commit message for the formatted code. All you have to do is put the code below into a file of the same name without .sh extension into the hook folder in the .git folder this script is pretty flexible and can be used on any Git that uses Docker and Elixir as long as you remember to change the docker container name in the code to the one you are using the one.

Lewis Taylor

💻 Junior Developer 📞 The Support Team 🌐 Fluent in English and Python

Tags
Git Docker