Contact Us
#Tech | 2 min read | Updated: 2/22/2024

Custom Git commands in Ruby

Updated: 2/22/2024
, Lead Ruby on Rails Developer
#Tech
2 min read

Automation is a great thing. “If you do anything more than once you should write a script”. Of course, they’re always a place for quick and dirty hacks for “right here and right now”.

They solve a particular problem and give you the ability to focus on more important things. But if any routines start to eat too much of your time… It might be that very moment to automate the process.
It’s a balance anyway.

My team is working on a CityFALCON project, a rapidly-evolving fintech startup. It is an ecosystem of several Rails applications: the main website and a couple of services. At a certain stage we had to create another version of the same Rails web application but with the slightly different structure of controllers, routes and views.

While we were figuring out which way of new application structure would be the best to have these two versions coexisting, we decided to get quick and dirty and just branch the new app of our master.

Due to reasons beyond our control, we could not incorporate the code from the gem into the application at that stage.

So we came to the following setup: application had ‘master’ and ‘v2’ branches linked via Gemfile to gem’s ‘master’ and ‘v2’ branches respectively. And then the problem came out.

We had to be able to switch between these versions quickly. Each such switch required to perform 2 git checkouts in corresponding dirs after ensuring that both of the working copies are clean or stashing any changes before the switch. It was pretty easy to perform this manually but soon it became a procedure to be done several times a day.

It was that very moment for the automation to come to the stage. The most natural solution to use was custom Git commands – a very neat feature to extend your Git workflow in any way you like. While it was interesting to dig into Bash scripting and feel a light breeze of Perl madness :), we realized soon that lack of experience in this area was a blocker for us.

And we were amazed by how it didn’t come to our minds first. Let’s (re)write everything in Ruby! We named our custom command git checkout. This is how it looks at the moment of writing:

The util.rb file contains code shared between different custom commands (we implemented a couple of additional custom commands along with this one). Here’s an excerpt from it:

Our commands make use of Dotenv. For instance, the path to the gem dir (to be able to cd there) is stored in the .env file. It allows us to add any configuration for these commands and it will be self-contained within the project.

So, we spent not so much time to create a tool which greatly simplified our development process. Our time and efforts were saved, we’ve gained a new engineering experience. An important factor of success was choosing the right tools for the job.

What is your story for the automation put into the right place?

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

Share:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
Recommended articles
#Tech 4 min

Web application frameworks are like cars. One may be good for racing. Another may assist you with massive tasks like a monster truck. And some are dependable everyday cars that are perfect for day-to-day needs….

16 min

At Sloboda Studio, we have worked with Ruby on Rails projects for 11 years now.

As technology constantly evolves, new web development tools and frameworks emerge in the market. Even though this provides lots of options for devs and startup owners to choose from, that variety is a double-edged sword….

Scale your team with us

Drive your business with our dedicated developers

    Alex, VP of Client Engagement
    alex@sloboda-studio.com