RFR: 8314543: gitattributes: make diffs easier to read

Erik Joelsson erikj at openjdk.org
Thu Aug 17 22:00:27 UTC 2023


On Thu, 17 Aug 2023 17:25:52 GMT, Andrei Rybak <duke at openjdk.org> wrote:

> Git supports special hunk headers for several languages in diff output, which make it easier to read diffs of files in that language, generated by Git (git-diff, git-show, `git log -p`, etc).  For details, see `git help gitattributes` or [the online documentation](https://git-scm.com/docs/gitattributes).
> 
> Add entries to the root .gitattributes file to support showing the hunk headers for Java, C, C++, Markdown, Shell script, HTML, and CSS.  This makes it easier to read diffs generated by Git.
> 
> As an example, for j.l.Integer, before, the hunk header just shows the class name, found using the generic hunk header regex:
> 
> 
> $ git show --format='' 71ca85f5a6741a2db55a529192564f94b269fbd9 -- src/java.base/share/classes/java/lang/Integer.java
> diff --git a/src/java.base/share/classes/java/lang/Integer.java b/src/java.base/share/classes/java/lang/Integer.java
> index 7d200e7edf5..9ec9d3941f8 100644
> --- a/src/java.base/share/classes/java/lang/Integer.java
> +++ b/src/java.base/share/classes/java/lang/Integer.java
> @@ -517,13 +517,9 @@ public final class Integer extends Number
>          }
> 
>          // We know there are at most two digits left at this point.
> -        q = i / 10;
> -        r = (q * 10) - i;
> -        buf[--charPos] = (byte)('0' + r);
> -
> -        // Whatever left is the remaining digit.
> -        if (q < 0) {
> -            buf[--charPos] = (byte)('0' - q);
> +        buf[--charPos] = DigitOnes[-i];
> +        if (i < -9) {
> +            buf[--charPos] = DigitTens[-i];
>          }
> 
>          if (negative) {
> 
> 
> After, the hunk shows the method edited by the commit, derived using the regex specific to Java:
> 
> 
> $ git show --format='' 71ca85f5a6741a2db55a529192564f94b269fbd9 -- src/java.base/share/classes/java/lang/Integer.java
> diff --git a/src/java.base/share/classes/java/lang/Integer.java b/src/java.base/share/classes/java/lang/Integer.java
> index 7d200e7edf5..9ec9d3941f8 100644
> --- a/src/java.base/share/classes/java/lang/Integer.java
> +++ b/src/java.base/share/classes/java/lang/Integer.java
> @@ -517,13 +517,9 @@ static int getChars(int i, int index, byte[] buf) {
>          }
> 
>          // We know there are at most two digits left at this point.
> -        q = i / 10;
> -        r = (q * 10) - i;
> -        buf[--charPos] = (byte)('0' + r);
> -
> -        // Whatever left is the remaining digit.
> -        if (q < 0) {
> -            buf[--charPos] = (byte)('0' - q);
> +        buf[--charPos] = DigitOnes[-i];
> +        if (i < -9) {
> +            buf[--charPos] = DigitTens[-i];
>          }
> 
>          ...

That's a very detailed summary. We generally don't write that much. We try to make sure any relevant information can be found in the bug instead.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/15334#issuecomment-1683032056


More information about the build-dev mailing list