Posted over 1 year ago. Visible to the public. Repeats. Linked content.

Rails developers: Have better context in Git diffs

Git diffs show the surrounding contexts for diff hunks. It does so by applying regular expressions to find the beginning of a context. When it comes to Ruby, however, it will not find method heads and travel up to the class definition:

Copy
@@ -24,7 +24,7 @@ class TicketPdf # <=== Actually expected here: the method definition ApplicationController.render( "tickets/index.html.haml", layout: "tickets", - assigns: { tickets: tickets } + assigns: { tickets: tickets, event_name: event_name } ) end end

When Git is told how to recognize a Ruby file, it will correctly display the expected context:

Copy
@@ -24,7 +24,7 @@ def tickets_as_html # <=== Now correct ApplicationController.render( "tickets/index.html.haml", layout: "tickets", - assigns: { tickets: tickets } + assigns: { tickets: tickets, event_name: event_name } ) end end

How to configure

Copy
# in ~/.gitattributes: *.rb diff=ruby *.rake diff=ruby *_spec.rb diff=rspec *.feature diff=cucumber

Then tell Git where it can find its global attributes file with git config --global core.attributesfile ~/.gitattributes. (Note that you could configure this on a per-project base as well.)

Ruby support is built in, but RSpec and Cucumber need a little more help:

Copy
# in ~/.gitconfig [diff "rspec"] xfuncname = "^[ \t]*((RSpec|describe|context|it|before|after|around|feature|scenario)[ \t].*)$" [diff "cucumber"] xfuncname = "^[ \t]*((Feature|Background|Scenario|Scenario Outline|Examples): .*)$"

Configuring more formats is a simple as extending ~/.gitattributes, and if Git has no built-in support, adding an entry to ~/.gitconfig like above.

Your development team has a full backlog of feature requests, chores and refactoring coupled with deadlines? We are familiar with that. With our "DevOps as a Service" offering, we support developer teams with infrastructure and operations expertise.

Owner of this card:

Avatar
Dominik Schöler
Last edit:
over 1 year ago
by Tobias Kraze
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more