17

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?

4
  • did you manage to get a good configuration? I tried *.cs diff=csharp, but I still get namespaces. diff=java works better though..
    – elmarco
    Commented Jun 28, 2012 at 18:50
  • Yeah, it does work for me, do you have a recent enough version of git? diff=csharp should work since 1.7.3.
    – svick
    Commented Jun 28, 2012 at 20:22
  • 1.7.10 here, from debian unstable, I'll try to dig later, for now java is ok :)
    – elmarco
    Commented Jun 29, 2012 at 13:20
  • Related - stackoverflow.com/questions/28111035/…
    – sashoalm
    Commented Oct 24, 2016 at 8:44

2 Answers 2

22

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.

4

See gitattributes manpage, the "Generating diff text" section, and e.g. diff.csharp.xfuncname configuration (search for xfuncname).

Not the answer you're looking for? Browse other questions tagged or ask your own question.