From 982515cf10dfbdfb9b40cb3582dfc54e2855a79d Mon Sep 17 00:00:00 2001 From: Constantine Plotnikov Date: Thu, 8 Oct 2009 20:12:08 +0400 Subject: [PATCH] Make the default encoding when reading commits UTF-8 When reading commits the system default encoding was used if no encoding was specified in the commit. The patch modifies the test to add a check that commit message was encoded correctly (the test fails on old implementation if system encoding is not UTF-8) and fixes Commit.decode() method to use UTF-8 if encoding is not specified in the commit object. Change-Id: I27101da3c2eb6edd0c4a9e4c0523e48b286e3cd5 Signed-off-by: Constantine Plotnikov Signed-off-by: Shawn O. Pearce --- .../tst/org/eclipse/jgit/lib/T0003_Basic.java | 3 +++ .../src/org/eclipse/jgit/lib/Commit.java | 18 +++++++----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0003_Basic.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0003_Basic.java index 081e8e4c9..2cf557fb0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0003_Basic.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0003_Basic.java @@ -344,6 +344,9 @@ public class T0003_Basic extends RepositoryTestCase { commit.setMessage("\u00dcbergeeks"); ObjectId cid = new ObjectWriter(db).writeCommit(commit); assertEquals("4680908112778718f37e686cbebcc912730b3154", cid.name()); + Commit loadedCommit = db.mapCommit(cid); + assertNotSame(loadedCommit, commit); + assertEquals(commit.getMessage(), loadedCommit.getMessage()); } public void test024_createCommitNonAscii() throws IOException { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Commit.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Commit.java index cdfab7cc3..c43256312 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Commit.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Commit.java @@ -305,17 +305,13 @@ public class Commit implements Treeish { br.read(readBuf); int msgstart = readBuf.length != 0 ? ( readBuf[0] == '\n' ? 1 : 0 ) : 0; - if (encoding != null) { - // TODO: this isn't reliable so we need to guess the encoding from the actual content - author = new PersonIdent(new String(rawAuthor.getBytes(),encoding.name())); - committer = new PersonIdent(new String(rawCommitter.getBytes(),encoding.name())); - message = new String(readBuf,msgstart, readBuf.length-msgstart, encoding.name()); - } else { - // TODO: use config setting / platform / ascii / iso-latin - author = new PersonIdent(new String(rawAuthor.getBytes())); - committer = new PersonIdent(new String(rawCommitter.getBytes())); - message = new String(readBuf, msgstart, readBuf.length-msgstart); - } + // If encoding is not specified, the default for commit is UTF-8 + if (encoding == null) encoding = Constants.CHARSET; + + // TODO: this isn't reliable so we need to guess the encoding from the actual content + author = new PersonIdent(new String(rawAuthor.getBytes(),encoding.name())); + committer = new PersonIdent(new String(rawCommitter.getBytes(),encoding.name())); + message = new String(readBuf,msgstart, readBuf.length-msgstart, encoding.name()); } catch (IOException e) { e.printStackTrace(); } finally {