git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 2/2] Add getPatchText functions to obtain the plain-text version of a patch
Date: Fri, 12 Dec 2008 18:42:26 -0800	[thread overview]
Message-ID: <1229136146-15359-2-git-send-email-spearce@spearce.org> (raw)
In-Reply-To: <1229136146-15359-1-git-send-email-spearce@spearce.org>

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.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../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

diff --git a/org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java b/org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java
index f43a1b9..f4e2ee3 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java
@@ -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;
diff --git a/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java b/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java
index 7c3a45a..0110f4a 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java
@@ -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>
diff --git a/org.spearce.jgit/src/org/spearce/jgit/patch/HunkHeader.java b/org.spearce.jgit/src/org/spearce/jgit/patch/HunkHeader.java
index 12c670d..5a3b590 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/patch/HunkHeader.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/patch/HunkHeader.java
@@ -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;
diff --git a/org.spearce.jgit/src/org/spearce/jgit/patch/PatchUtil.java b/org.spearce.jgit/src/org/spearce/jgit/patch/PatchUtil.java
new file mode 100644
index 0000000..89136c0
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/patch/PatchUtil.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2008, Google Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.spearce.jgit.patch;
+
+import java.nio.charset.Charset;
+
+import org.spearce.jgit.util.RawParseUtils;
+
+/** Patch related utility functions. */
+public class PatchUtil {
+	/**
+	 * Decode a region of a buffer one line at a time.
+	 * <p>
+	 * Unlike {@link RawParseUtils#decode(Charset, byte[], int, int)} this
+	 * method reads the input one line at a time and decodes each line
+	 * individually. This permits a decoding of a file converting from
+	 * ISO-8859-1 to UTF-8 encoding (for example), as each line in the patch
+	 * script will be in one encoding or the other.
+	 * 
+	 * @param cs
+	 *            preferred character set to use when decoding the buffer.
+	 * @param buf
+	 *            buffer to pull the raw bytes from.
+	 * @param ptr
+	 *            first position to read.
+	 * @param end
+	 *            one position past the last position to read.
+	 * @return a string representation of the region, decoded per-line.
+	 */
+	public static String decode(final Charset cs, final byte[] buf, int ptr,
+			final int end) {
+		final StringBuilder r = new StringBuilder(end - ptr);
+		while (ptr < end) {
+			final int eol = Math.min(end, RawParseUtils.nextLF(buf, ptr));
+			r.append(RawParseUtils.decode(cs, buf, ptr, eol));
+			ptr = eol;
+		}
+		return r.toString();
+	}
+
+	private PatchUtil() {
+		// No instances
+	}
+}
-- 
1.6.1.rc2.306.ge5d5e

  reply	other threads:[~2008-12-13  2:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-13  2:42 [JGIT PATCH 1/2] Add raw buffer fetch methods to FileHeader, HunkHeader Shawn O. Pearce
2008-12-13  2:42 ` Shawn O. Pearce [this message]
2008-12-13 11:02   ` [JGIT PATCH 2/2] Add getPatchText functions to obtain the plain-text version of a patch Robin Rosenberg
2008-12-13 21:26     ` Robin Rosenberg
2008-12-17 20:13       ` [JGIT PATCH 2/2 v2] Add getScriptText " Shawn O. Pearce

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1229136146-15359-2-git-send-email-spearce@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=robin.rosenberg@dewire.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).