Skip to contentSkip to conversation

Add raw buffer fetch methods to FileHeader, HunkHeader

PATCH In review Shawn O. Pearce 2 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Add raw buffer fetch methods to FileHeader, HunkHeader

These permit application level code to read back the patch script, for example to slice it up and output parts into a UI on a per-file or per-hunk basis.

---

Two last-minute patches. While using this code in Gerrit 2 I realized I forgot to add a way to get the script back after its been parsed by the library. :-)

 .../src/org/spearce/jgit/patch/BinaryHunk.java     |   15 +++++++++++++++
 .../src/org/spearce/jgit/patch/FileHeader.java     |   15 +++++++++++++++
 .../src/org/spearce/jgit/patch/HunkHeader.java     |   15 +++++++++++++++
 3 files changed, 45 insertions(+), 0 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java +15 -0
index 92eab86..f43a1b9 100644
@@ -81,6 +81,21 @@ public FileHeader getFileHeader() {
 		return file;
 	}
 
+	/** @return the byte array holding this hunk's patch script. */
+	public byte[] getBuffer() {
+		return file.buf;
+	}
+
+	/** @return offset the start of this hunk in {@link #getBuffer()}. */
+	public int getStartOffset() {
+		return startOffset;
+	}
+
+	/** @return offset one past the end of the hunk in {@link #getBuffer()}. */
+	public int getEndOffset() {
+		return endOffset;
+	}
+
 	/** @return type of this binary hunk */
 	public Type getType() {
 		return type;
org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java +15 -0
index 79e4b0a..7c3a45a 100644
@@ -173,6 +173,21 @@ int getParentCount() {
 		return 1;
 	}
 
+	/** @return the byte array holding this file's patch script. */
+	public byte[] getBuffer() {
+		return buf;
+	}
+
+	/** @return offset the start of this file's script in {@link #getBuffer()}. */
+	public int getStartOffset() {
+		return startOffset;
+	}
+
+	/** @return offset one past the end of the file script. */
+	public int getEndOffset() {
+		return endOffset;
+	}
+
 	/**
 	 * Get the old name associated with this file.
 	 * <p>
org.spearce.jgit/src/org/spearce/jgit/patch/HunkHeader.java +15 -0
index fc149ac..12c670d 100644
@@ -123,6 +123,21 @@ public FileHeader getFileHeader() {
 		return file;
 	}
 
+	/** @return the byte array holding this hunk's patch script. */
+	public byte[] getBuffer() {
+		return file.buf;
+	}
+
+	/** @return offset the start of this hunk in {@link #getBuffer()}. */
+	public int getStartOffset() {
+		return startOffset;
+	}
+
+	/** @return offset one past the end of the hunk in {@link #getBuffer()}. */
+	public int getEndOffset() {
+		return endOffset;
+	}
+
 	/** @return information about the old image mentioned in this hunk. */
 	public OldImage getOldImage() {
 		return old;
Signature · 1 line
1.6.1.rc2.306.ge5d5e
#1 lore raw

PATCH 2/2 Add getPatchText functions to obtain the plain-text version of a patch

The conversion from byte[] to String is performed one line at a time, in case the patch is a character encoding conversion patch for the file. For simplicity we currently assume UTF-8 still as the default encoding for any content, but eventually we should support using the .gitattributes encoding property when performing this conversion.

---
 .../src/org/spearce/jgit/patch/BinaryHunk.java     |    8 ++
 .../src/org/spearce/jgit/patch/FileHeader.java     |    6 ++
 .../src/org/spearce/jgit/patch/HunkHeader.java     |    7 ++
 .../src/org/spearce/jgit/patch/PatchUtil.java      |   79 ++++++++++++++++++++
 4 files changed, 100 insertions(+), 0 deletions(-)
 create mode 100644 org.spearce.jgit/src/org/spearce/jgit/patch/PatchUtil.java
org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java +8 -0
index f43a1b9..f4e2ee3 100644
@@ -42,6 +42,8 @@
 import static org.spearce.jgit.util.RawParseUtils.nextLF;
 import static org.spearce.jgit.util.RawParseUtils.parseBase10;
 
+import org.spearce.jgit.lib.Constants;
+
 /** Part of a "GIT binary patch" to describe the pre-image or post-image */
 public class BinaryHunk {
 	private static final byte[] LITERAL = encodeASCII("literal ");
@@ -96,6 +98,12 @@ public int getEndOffset() {
 		return endOffset;
 	}
 
+	/** @return text of this patch file's script; best-effort decoded */
+	public String getHunkText() {
+		return PatchUtil.decode(Constants.CHARSET, getBuffer(),
+				getStartOffset(), getEndOffset());
+	}
+
 	/** @return type of this binary hunk */
 	public Type getType() {
 		return type;
org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java +6 -0
index 7c3a45a..0110f4a 100644
@@ -188,6 +188,12 @@ public int getEndOffset() {
 		return endOffset;
 	}
 
+	/** @return text of this patch file's script; best-effort decoded */
+	public String getScriptText() {
+		return PatchUtil.decode(Constants.CHARSET, getBuffer(),
+				getStartOffset(), getEndOffset());
+	}
+
 	/**
 	 * Get the old name associated with this file.
 	 * <p>
org.spearce.jgit/src/org/spearce/jgit/patch/HunkHeader.java +7 -0
index 12c670d..5a3b590 100644
@@ -42,6 +42,7 @@
 import static org.spearce.jgit.util.RawParseUtils.parseBase10;
 
 import org.spearce.jgit.lib.AbbreviatedObjectId;
+import org.spearce.jgit.lib.Constants;
 import org.spearce.jgit.util.MutableInteger;
 
 /** Hunk header describing the layout of a single block of lines */
@@ -138,6 +139,12 @@ public int getEndOffset() {
 		return endOffset;
 	}
 
+	/** @return text of this patch file's script; best-effort decoded */
+	public String getHunkText() {
+		return PatchUtil.decode(Constants.CHARSET, getBuffer(),
+				getStartOffset(), getEndOffset());
+	}
+
 	/** @return information about the old image mentioned in this hunk. */
 	public OldImage getOldImage() {
 		return old;
org.spearce.jgit/src/org/spearce/jgit/patch/PatchUtil.java new file +79 -0

79 lines not on this page · raw message

Signature · 1 line
1.6.1.rc2.306.ge5d5e
#2 lore raw

Re: [JGIT PATCH 2/2] Add getPatchText functions to obtain the plain-text version of a patch

lördag 13 december 2008 03:42:26 skrev Shawn O. Pearce:

Quoted 5 lines

The conversion from byte[] to String is performed one line at a time, in case the patch is a character encoding conversion patch for the file. For simplicity we currently assume UTF-8 still as the default encoding for any content, but eventually we should support using the .gitattributes encoding property when performing this conversion.

For usefulness we must be able to pass the encoding from outside, e.g. the encoding Eclipse uses, which often is not UTF-8-

-- robin

#3 lore raw

Re: [JGIT PATCH 2/2] Add getPatchText functions to obtain the plain-text version of a patch

lördag 13 december 2008 12:02:07 skrev Robin Rosenberg:

Quoted 9 lines

lördag 13 december 2008 03:42:26 skrev Shawn O. Pearce:

Quoted 5 lines

The conversion from byte[] to String is performed one line at a time, in case the patch is a character encoding conversion patch for the file. For simplicity we currently assume UTF-8 still as the default encoding for any content, but eventually we should support using the .gitattributes encoding property when performing this conversion.

For usefulness we must be able to pass the encoding from outside, e.g. the encoding Eclipse uses, which often is not UTF-8-

It's even worse. You should probably do the encoding guess on the whole patch, or per file and not per line so make success possible at all. Reading and writing as ISO-8859-1 will always work as that is just padding every byte with NUL on reading and dropping it on writing. I.e. if your convert to char at all...

-- robin

#4 lore raw

PATCH v2 2/2 Add getScriptText functions to obtain the plain-text version of a patch

The conversion from byte[] to String is performed one file at a time, in case the patch is a character encoding conversion patch for the file. For simplicity we currently assume UTF-8 still as the default encoding for any content, but eventually we should support using the .gitattributes encoding property when performing this conversion.

---

Quoted 8 lines · Robin Rosenberg
Quoted 2 lines

For usefulness we must be able to pass the encoding from outside, e.g. the encoding Eclipse uses, which often is not UTF-8-

It's even worse. You should probably do the encoding guess on the whole patch, or per file and not per line so make success possible at all. Reading and writing as ISO-8859-1 will always work as that is just padding every byte with NUL on reading and dropping it on writing. I.e. if your convert to char at all...

So this patch does the "whole file" thing. But there is a fast-path in getScriptText to try and bypass the multiple copies we have to make in order to shovel the entire file into the CharsetDecoder just to read the patch. It isn't common to see a character set conversion patch, so the fast case of decoding the whole patch text at once should happen most of the time.

 .../jgit/patch/testGetText_BothISO88591.patch      |   21 +++
 .../spearce/jgit/patch/testGetText_Convert.patch   |   21 +++
 .../spearce/jgit/patch/testGetText_DiffCc.patch    |   13 ++
 .../spearce/jgit/patch/testGetText_NoBinary.patch  |    4 +
 .../tst/org/spearce/jgit/patch/GetTextTest.java    |  142 ++++++++++++++++++++
 .../org/spearce/jgit/patch/CombinedFileHeader.java |   27 ++++
 .../org/spearce/jgit/patch/CombinedHunkHeader.java |  127 +++++++++++++++++
 .../src/org/spearce/jgit/patch/FileHeader.java     |  116 ++++++++++++++++
 .../src/org/spearce/jgit/patch/HunkHeader.java     |   86 ++++++++++++
 .../src/org/spearce/jgit/util/RawParseUtils.java   |   57 ++++++++-
 10 files changed, 611 insertions(+), 3 deletions(-)
 create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testGetText_BothISO88591.patch
 create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testGetText_Convert.patch
 create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testGetText_DiffCc.patch
 create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testGetText_NoBinary.patch
 create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/patch/GetTextTest.java
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testGetText_BothISO88591.patch new file +21 -0
new file mode 100644
index 0000000..8224fcc
@@ -0,0 +1,21 @@
+diff --git a/X b/X
+index 014ef30..8c80a36 100644
+--- a/X
++++ b/X
+@@ -1,7 +1,7 @@
+ a
+ b
+ c
+-�ngstr�m
++line 4 �ngstr�m
+ d
+ e
+ f
+@@ -13,6 +13,6 @@ k
+ l
+ m
+ n
+-�ngstr�m
++�ngstr�m; line 16
+ o
+ p
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testGetText_Convert.patch new file +21 -0
new file mode 100644
index 0000000..a43fef5
@@ -0,0 +1,21 @@
+diff --git a/X b/X
+index 014ef30..209db0d 100644
+--- a/X
++++ b/X
+@@ -1,7 +1,7 @@
+ a
+ b
+ c
+-�ngstr�m
++Ångström
+ d
+ e
+ f
+@@ -13,6 +13,6 @@ k
+ l
+ m
+ n
+-�ngstr�m
++Ångström
+ o
+ p
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testGetText_DiffCc.patch new file +13 -0
new file mode 100644
index 0000000..3f74a52
@@ -0,0 +1,13 @@
+diff --cc X
+index bdfc9f4,209db0d..474bd69
+--- a/X
++++ b/X
+@@@ -1,7 -1,7 +1,7 @@@
+  a
+--b
+  c
+ +test �ngstr�m
++ Ångström
+  d
+  e
+  f
org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/patch/testGetText_NoBinary.patch new file +4 -0
new file mode 100644
index 0000000..e4968dc
@@ -0,0 +1,4 @@
+diff --git a/org.spearce.egit.ui/icons/toolbar/fetchd.png b/org.spearce.egit.ui/icons/toolbar/fetchd.png
+new file mode 100644
+index 0000000..4433c54
+Binary files /dev/null and b/org.spearce.egit.ui/icons/toolbar/fetchd.png differ
org.spearce.jgit.test/tst/org/spearce/jgit/patch/GetTextTest.java new file +142 -0

142 lines not on this page · raw message

org.spearce.jgit/src/org/spearce/jgit/patch/CombinedFileHeader.java +27 -0
index 3ccc418..a27e0f8 100644
@@ -41,7 +41,9 @@
 import static org.spearce.jgit.util.RawParseUtils.match;
 import static org.spearce.jgit.util.RawParseUtils.nextLF;
 
+import java.nio.charset.Charset;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import org.spearce.jgit.lib.AbbreviatedObjectId;
@@ -111,6 +113,31 @@ public AbbreviatedObjectId getOldId(final int nthParent) {
 		return oldIds[nthParent];
 	}
 
+	@Override
+	public String getScriptText(final Charset ocs, final Charset ncs) {
+		final Charset[] cs = new Charset[getParentCount() + 1];
+		Arrays.fill(cs, ocs);
+		cs[getParentCount()] = ncs;
+		return getScriptText(cs);
+	}
+
+	/**
+	 * Convert the patch script for this file into a string.
+	 * 
+	 * @param charsetGuess
+	 *            optional array to suggest the character set to use when
+	 *            decoding each file's line. If supplied the array must have a
+	 *            length of <code>{@link #getParentCount()} + 1</code>
+	 *            representing the old revision character sets and the new
+	 *            revision character set.
+	 * @return the patch script, as a Unicode string.
+	 */
+	@Override
+	public String getScriptText(final Charset[] charsetGuess) {
+		return super.getScriptText(charsetGuess);
+	}
+
+	@Override
 	int parseGitHeaders(int ptr, final int end) {
 		while (ptr < end) {
 			final int eol = nextLF(buf, ptr);
org.spearce.jgit/src/org/spearce/jgit/patch/CombinedHunkHeader.java +127 -0

137 lines not on this page · raw message

org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java +116 -0

138 lines not on this page · raw message

org.spearce.jgit/src/org/spearce/jgit/patch/HunkHeader.java +86 -0

96 lines not on this page · raw message

org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java +54 -3

69 lines not on this page · raw message

Signature · 4 lines
1.6.1.rc3.302.gb14d9

-- 
Shawn.