Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example .gitattributes file that will configure custom hunk header patterns for some common languages and file formats. See https://tekin.co.uk/2020/10/better-git-diff-output-for-ruby-python-elixir-and-more for more details.
# Stick this in your home directory and point your Global Git config at it by running:
#
# $ git config --global core.attributesfile ~/.gitattributes
#
# See https://tekin.co.uk/2020/10/better-git-diff-output-for-ruby-python-elixir-and-more for more details
*.c diff=cpp
*.h diff=cpp
*.c++ diff=cpp
*.h++ diff=cpp
*.cpp diff=cpp
*.hpp diff=cpp
*.cc diff=cpp
*.hh diff=cpp
*.m diff=objc
*.mm diff=objc
*.cs diff=csharp
*.css diff=css
*.html diff=html
*.xhtml diff=html
*.ex diff=elixir
*.exs diff=elixir
*.go diff=golang
*.php diff=php
*.pl diff=perl
*.py diff=python
*.md diff=markdown
*.rb diff=ruby
*.rake diff=ruby
*.rs diff=rust
*.lisp diff=lisp
*.el diff=lisp
# Additional manually-configured matchers you may want to add to your .gitconfig
[diff "rspec"]
xfuncname = "^[ \t]*((RSpec|describe|context|it|before|after|around|feature|scenario|background)[ \t].*)$"
[diff "swift"]
xfuncname = ^[ \t]*((class|func)[ \t].*)$
@rickclare

This comment has been minimized.

Copy link

@rickclare rickclare commented Oct 19, 2020

Hi Tekin, I'm loving your Better Git diff output article. I'm definitely going to add your suggestions to my local dev environment!

I've been working a lot with Elixir lately, so I have a suggestion to add to your Gist (i.e. Elixir has 2 possible file formats)

   *.xhtml diff=html
+  *.ex    diff=elixir
   *.exs   diff=elixir
@dlinch

This comment has been minimized.

Copy link

@dlinch dlinch commented Oct 19, 2020

I noticed JavaScript is missing as well, I did some digging and it seems the following would be valid:

*.js    diff=javascript
*.ts    diff=javascript

However when I added it to my project and tested it didn't seem to make a big difference, and always scoped to the class definition rather than a function definition with or without the git attributes config. Is JavaScript unsupported at this time?

@DiegoSalazar

This comment has been minimized.

Copy link

@DiegoSalazar DiegoSalazar commented Oct 19, 2020

@dlinch there doesn't appear to be javascript support builtin. I only see java here: https://github.com/git/git/blob/master/userdiff.c#L78

@tekin

This comment has been minimized.

Copy link
Owner Author

@tekin tekin commented Oct 19, 2020

Cheers @rickclare, updated now.

@tekin

This comment has been minimized.

Copy link
Owner Author

@tekin tekin commented Oct 19, 2020

@dlinch yeah, as @DiegoSalazar points out, there is no pre-defined pattern for JavaScript built into Git. It is possible to define your own custom regular expression pattern though. See the docs on defining a custom hunk-header.

@withzombies

This comment has been minimized.

Copy link

@withzombies withzombies commented Oct 19, 2020

*.m diff=objc
*.mm diff=objc
@Rafi993

This comment has been minimized.

Copy link

@Rafi993 Rafi993 commented Oct 20, 2020

This is awesome. Any thoughts on how you do this for an erb file ? should it be

*.erb diff=html

or

*.erb diff=erb
@citizen428

This comment has been minimized.

Copy link

@citizen428 citizen428 commented Oct 20, 2020

@Rafi993 There aren't any defined regex for ERb in the git source, so it's either HTML or nothing.

@Rafi993

This comment has been minimized.

Copy link

@Rafi993 Rafi993 commented Oct 20, 2020

Thank you @citizen428

Edit:
I think you mentioned some other user by accident :)

@triwats

This comment has been minimized.

Copy link

@triwats triwats commented Oct 20, 2020

This is great, thanks a lot! Super useful if you're using a less rich editor such as Vim and working mainly out of a terminal.

@jimryan

This comment has been minimized.

Copy link

@jimryan jimryan commented Oct 22, 2020

Thank you so much for this! I work with rspec and capybara a lot and found using the built-in ruby diff for spec files broke the hunk header entirely for them, so I wrote my own regex for specs based on the native ruby regex:

# .gitconfig

[diff "rspec"]
        xfuncname = "^[ \t]*((RSpec|describe|context|it|before|after|around|feature|scenario|background)[ \t].*)$"

You can then use "rspec" as a diff driver:

# .gitattributes

*.rb       diff=ruby
*_spec.rb  diff=rspec
@tekin

This comment has been minimized.

Copy link
Owner Author

@tekin tekin commented Oct 22, 2020

@jimryan that’s super cool, thanks for sharing!

@zubin

This comment has been minimized.

Copy link

@zubin zubin commented Oct 22, 2020

@jimryan Might want to add around too. Thanks!

@jimryan

This comment has been minimized.

Copy link

@jimryan jimryan commented Oct 22, 2020

@zubin Good point! I've updated the snippet above. Thanks!

@ulwlu

This comment has been minimized.

Copy link

@ulwlu ulwlu commented Oct 30, 2020

Thank you for a great tip.
I found this gist as well and comment here for additional info.

https://gist.github.com/ruediger/5647207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.