# java diffs show no method context

4 messages from 2018-04-25 to 2018-04-26. Participants: Ulrich Windl, Alban Gruin, Ævar Arnfjörð Bjarmason.
Thread: https://gitlist.dev/t/48359

## Ulrich Windl, 2018-04-25 12:53

Subject: java diffs show no method context
Message-ID: <5AE07A3A020000A10002B5C5@gwsmtp1.uni-regensburg.de>
URL: https://gitlist.dev/e/5AE07A3A020000A10002B5C5%40gwsmtp1.uni-regensburg.de

```
Hi!

This is for git 2.13.6, and it may be an FAQ or frequent feature request. Anyway:
I'm new to Java, and writing my first project using Git, I found that "git diff" only reports the class in the diff context, but not the method (as seen for C, for example).
I'd wish to have the method where the diff is located. Here is an example chunk:


@@ -100,6 +119,8 @@ public class PdfParser {
        PdfObject obj, value;
        PdfObjectName name;
        for ( int pos = startIndex; pos < endIndex; pos += 2 ) {
+           if ( (obj = objects.get(pos)).type != PdfObject.Type.Name )
+               exception("Name expected");
            name = (PdfObjectName) obj;
            if ( pos + 1 == endIndex )
                exception("missing value");

The corrsponding definition of the method is like this:

    /**
     * Populate Dictionary with parsed objects
     * @param dict Dictionary to fill
     * @param startIndex Position of first item to add
     * @param endIndex Position of first item not to add
     */
    private void populateDictionary(PdfObjectDictionary dict, int startIndex, in
t endIndex)
    {
...

Regards,
Ulrich





```

## Alban Gruin, 2018-04-25 15:05

Subject: Re: java diffs show no method context
Message-ID: <aae8d748-101d-1eb2-f8e8-9da2e53e2388@gmail.com>
URL: https://gitlist.dev/e/aae8d748-101d-1eb2-f8e8-9da2e53e2388%40gmail.com
In-Reply-To: <5AE07A3A020000A10002B5C5@gwsmtp1.uni-regensburg.de>

```
Le 25/04/2018 à 14:53, Ulrich Windl a écrit :
> Hi!
> 
> This is for git 2.13.6, and it may be an FAQ or frequent feature request. Anyway:
> I'm new to Java, and writing my first project using Git, I found that "git diff" only reports the class in the diff context, but not the method (as seen for C, for example).
> I'd wish to have the method where the diff is located.

Hi,

to achieve this behaviour, you have to create a file named
".gitattributes" at the root of your project, containing this line:

*.java diff=java

.gitattributes allows you to configure other things, as described in the
documentation[1].

I hope it helps.

[1] https://www.git-scm.com/docs/gitattributes

Cheers,
Alban

```

## Ulrich Windl, 2018-04-26 06:30

Subject: Antw: Re: java diffs show no method context
Message-ID: <5AE17203020000A10002B5EF@gwsmtp1.uni-regensburg.de>
URL: https://gitlist.dev/e/5AE17203020000A10002B5EF%40gwsmtp1.uni-regensburg.de
In-Reply-To: <aae8d748-101d-1eb2-f8e8-9da2e53e2388@gmail.com>

```
Hi!

Thanks for that. It sounds plausible, but I wonder why it works automagically
for C, but not for Java (Politcal reasons put aside): Using ".c" for C is about
as common as using ".java" for Java ;-)

Regards,
Ulrich

>>> Alban Gruin <alban.gruin@gmail.com> schrieb am 25.04.2018 um 17:05 in
Nachricht
<aae8d748-101d-1eb2-f8e8-9da2e53e2388@gmail.com>:
> Le 25/04/2018 à 14:53, Ulrich Windl a écrit :
>> Hi!
>> 
>> This is for git 2.13.6, and it may be an FAQ or frequent feature request. 
> Anyway:
>> I'm new to Java, and writing my first project using Git, I found that "git

> diff" only reports the class in the diff context, but not the method (as
seen 
> for C, for example).
>> I'd wish to have the method where the diff is located.
> 
> Hi,
> 
> to achieve this behaviour, you have to create a file named
> ".gitattributes" at the root of your project, containing this line:
> 
> *.java diff=java
> 
> .gitattributes allows you to configure other things, as described in the
> documentation[1].
> 
> I hope it helps.
> 
> [1] https://www.git-scm.com/docs/gitattributes 
> 
> Cheers,
> Alban




```

## Ævar Arnfjörð Bjarmason, 2018-04-26 07:43

Subject: Re: Antw: Re: java diffs show no method context
Message-ID: <87y3hacszl.fsf@evledraar.gmail.com>
URL: https://gitlist.dev/e/87y3hacszl.fsf%40evledraar.gmail.com
In-Reply-To: <5AE17203020000A10002B5EF@gwsmtp1.uni-regensburg.de>

```

On Thu, Apr 26 2018, Ulrich Windl wrote:

> Thanks for that. It sounds plausible, but I wonder why it works automagically
> for C, but not for Java (Politcal reasons put aside): Using ".c" for C is about
> as common as using ".java" for Java ;-)

It has a bit to do with it being in C, but not in the way you think. By
default Git doesn't enable the "cpp" driver either for *.c, but it just
so happens to do the right thing more of the time because the default
heuristic is basically to search for a nearby line that doesn't start
with whitespace for context.

This doesn't work for Java because your methods tend to be indented
since they're part of the class you're working on.

```
