summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java31
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java5
2 files changed, 34 insertions, 2 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java
index f30253675c..7ae491b38b 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java
@@ -49,6 +49,7 @@
package org.eclipse.jgit.storage.file;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -70,8 +71,13 @@ import org.eclipse.jgit.util.RawParseUtils;
*/
public class FileBasedConfig extends StoredConfig {
private final File configFile;
+
private final FS fs;
+
+ private boolean utf8Bom;
+
private volatile FileSnapshot snapshot;
+
private volatile ObjectId hash;
/**
@@ -141,7 +147,16 @@ public class FileBasedConfig extends StoredConfig {
else
snapshot = newSnapshot;
} else {
- fromText(RawParseUtils.decode(in));
+ final String decoded;
+ if (in.length >= 3 && in[0] == (byte) 0xEF
+ && in[1] == (byte) 0xBB && in[2] == (byte) 0xBF) {
+ decoded = RawParseUtils.decode(RawParseUtils.UTF8_CHARSET,
+ in, 3, in.length);
+ utf8Bom = true;
+ } else {
+ decoded = RawParseUtils.decode(in);
+ }
+ fromText(decoded);
snapshot = newSnapshot;
hash = newHash;
}
@@ -170,7 +185,19 @@ public class FileBasedConfig extends StoredConfig {
* the file could not be written.
*/
public void save() throws IOException {
- final byte[] out = Constants.encode(toText());
+ final byte[] out;
+ final String text = toText();
+ if (utf8Bom) {
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ bos.write(0xEF);
+ bos.write(0xBB);
+ bos.write(0xBF);
+ bos.write(text.getBytes(RawParseUtils.UTF8_CHARSET));
+ out = bos.toByteArray();
+ } else {
+ out = Constants.encode(text);
+ }
+
final LockFile lf = new LockFile(getFile(), fs);
if (!lf.lock())
throw new LockFailedException(getFile());
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
index 91184bad61..89fbeed890 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
@@ -65,6 +65,11 @@ import org.eclipse.jgit.lib.PersonIdent;
/** Handy utility functions to parse raw object contents. */
public final class RawParseUtils {
+ /**
+ * UTF-8 charset constant.
+ */
+ public static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
+
private static final byte[] digits10;
private static final byte[] digits16;