You've probably noticed that Git puts additional context data in the hunk headers: the lines that start with @@ and specify the line numbers where the hunk fits. For example, in the Rails Git repository, git show 13e7849 reveals the following two lines:
@@ -39,12 +39,7 @@ module ActionController #:nodoc: @@ -67,6 +62,12 @@ module ActionController #:nodoc:
The algorithm Git uses to choose these lines is simple: search backwards for the first line with something other than whitespace in the first column, and use that. This works great for C, but as you can see above, it's not quite as informative when it comes to Ruby. Lucky for us, it can be customized with a two step process.
First, let's specify a regex for an "interesting" context line. This will create an entry in ~/.gitconfig. If you omit the --global, .git/config from the current repository is used instead. This is generally less useful but would allow different patterns for different repositories.
$ git config --global diff.ruby.funcname '^ *\(\(class\|module\|def\) .*\)'
Next, we need an attributes file to specify when this pattern applies. We have two options here:
- .gitattributes, which can be versioned
- .git/info/attributes, which is local to your repository
Regardless of which you choose, the file's contents are the same:
*.rb diff=ruby
With the configuration in place, let's take another look at the context lines from before:
@@ -39,12 +39,7 @@ def self.included(base) #:nodoc: @@ -67,6 +62,12 @@ def process_with_components(...) #:nodoc:
As you might have guessed, the regexp given matches the most recent
class, module, or def line in
the preceding code.
For more information, see gitattributes(5).

