threads / discuss / 48359

java diffs show no method context

Subject: java diffs show no method context

## tl;dr

4 messages between Apr 25, 2018 and Apr 26, 2018.

replies: 3people: 3as markdown or json

Ulrich Windl· Apr 25, 2018, 12:53 UTC · lore
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· Apr 25, 2018, 15:05 UTC · re: Ulrich Windl · lore

Re: java diffs show no method context

Le 25/04/2018 à 14:53, Ulrich Windl a écrit :
Show 5 quoted lines
> 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· Apr 26, 2018, 06:30 UTC · re: Alban Gruin · lore

Antw: Re: java diffs show no method context

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>:

Show 6 quoted lines
> 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 
Show 19 quoted lines
> 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· Apr 26, 2018, 07:43 UTC · re: Ulrich Windl · lore

Re: Antw: Re: java diffs show no method context

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.

← back to recent threads