Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing headings in git diff

Tags:

git

git-diff

diff

When viewing a diff using git diff, each change starts with line like:

@@ -28,41 +20,10 @@ namespace ConsoleApplication1

For C, the final part is quite descriptive – it shows for example the function in which the change occurred. But for C#, it only ever shows the namespace, which isn't very useful. I think that's because it shows last line that isn't indented. Is there a way how to configure this?

like image 236
svick Avatar asked Aug 04 '10 20:08

svick


1 Answers

I have found it, the line is called hunk header and the documentation says how to customize it:

Defining a custom hunk-header

Each group of changes (called a "hunk") in the textual diff output is prefixed with a line of the form:

@@ -k,l +n,m @@ TEXT

This is called a hunk header. The "TEXT" portion is by default a line that begins with an alphabet, an underscore or a dollar sign; this matches what GNU diff -p output uses. This default selection however is not suited for some contents, and you can use a customized pattern to make a selection.

First, in .gitattributes, you would assign the diff attribute for paths.

*.tex   diff=tex

Then, you would define a "diff.tex.xfuncname" configuration to specify a regular expression that matches a line that you would want to appear as the hunk header "TEXT". Add a section to your $GIT_DIR/config file (or $HOME/.gitconfig file) like this:

[diff "tex"]
        xfuncname = "^(\\\\(sub)*section\\{.*)$"

Note. A single level of backslashes are eaten by the configuration file parser, so you would need to double the backslashes; the pattern above picks a line that begins with a backslash, and zero or more occurrences of sub followed by section followed by open brace, to the end of line.

like image 50
svick Avatar answered Oct 10 '22 07:10

svick