diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCommitCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCommitCommand.java index bbf4ce0d5a58..e6b23d899f35 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCommitCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCommitCommand.java @@ -32,6 +32,7 @@ import org.zmlx.hg4idea.util.HgEncodingUtil; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -46,6 +47,7 @@ public class HgCommitCommand { private final Project myProject; private final VirtualFile myRoot; private final String myMessage; + @NotNull private final Charset myCharset; private Set myFiles = Collections.emptySet(); @@ -53,6 +55,7 @@ public class HgCommitCommand { myProject = project; myRoot = root; myMessage = message; + myCharset = HgEncodingUtil.getDefaultCharset(myProject); } public void setFiles(@NotNull Set files) { @@ -88,17 +91,23 @@ public class HgCommitCommand { List parameters = new LinkedList(); parameters.add("--logfile"); parameters.add(saveCommitMessage().getAbsolutePath()); + if (System.getenv("HGENCODING") == null) { + parameters.add("--encoding"); + parameters.add(myCharset.name()); + } parameters.addAll(chunk); - ensureSuccess(new HgCommandExecutor(myProject).executeInCurrentThread(myRoot, "commit", parameters)); + HgCommandExecutor executor = new HgCommandExecutor(myProject); + executor.setCharset(myCharset); + ensureSuccess(executor.executeInCurrentThread(myRoot, "commit", parameters)); } private File saveCommitMessage() throws VcsException { File systemDir = new File(PathManager.getSystemPath()); File tempFile = new File(systemDir, TEMP_FILE_NAME); - try { - FileUtil.writeToFile(tempFile, myMessage.getBytes(HgEncodingUtil.getDefaultCharset())); - } catch (IOException e) { + FileUtil.writeToFile(tempFile, myMessage.getBytes(myCharset)); + } + catch (IOException e) { throw new VcsException("Couldn't prepare commit message", e); } return tempFile; diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java b/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java index fe7cd45e2cbc..36ba135b7929 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java @@ -62,7 +62,7 @@ public final class HgCommandExecutor { private final HgVcs myVcs; private final String myDestination; - private Charset myCharset = HgEncodingUtil.getDefaultCharset(); + private Charset myCharset; private boolean myIsSilent = false; private boolean myShowOutput = false; private List myOptions = DEFAULT_OPTIONS; @@ -81,6 +81,7 @@ public final class HgCommandExecutor { myVcs = HgVcs.getInstance(project); myDestination = destination; myState = state; + myCharset = HgEncodingUtil.getDefaultCharset(myProject); } public void setCharset(Charset charset) { diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgEncodingUtil.java b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgEncodingUtil.java index cf25b716c942..ff8402112e2e 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgEncodingUtil.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgEncodingUtil.java @@ -1,7 +1,9 @@ package org.zmlx.hg4idea.util; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.util.SystemInfo; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.encoding.EncodingProjectManager; import org.jetbrains.annotations.NotNull; import java.nio.charset.Charset; @@ -13,37 +15,24 @@ public class HgEncodingUtil { private static final Logger LOG = Logger.getInstance(HgEncodingUtil.class); - private static final String WINDOWS_DEFAULT_CHARSET = "cp1251"; - private static final String UNIX_DEFAULT_CHARSET = "UTF-8"; - - private static final Charset DEFAULT_CHARSET = updateDefaultEncoding(); - - public static Charset updateDefaultEncoding() { - String name = SystemInfo.isWindows ? WINDOWS_DEFAULT_CHARSET : UNIX_DEFAULT_CHARSET; + @NotNull + public static Charset getDefaultCharset(@NotNull Project project) { + String name = ""; try { String enc = System.getenv("HGENCODING"); if (enc != null && enc.length() > 0 && Charset.isSupported(enc)) { name = enc; } - return Charset.forName(name); + else { + name = EncodingProjectManager.getInstance(project).getDefaultCharsetName(); + } + if (!StringUtil.isEmptyOrSpaces(name)) { + return Charset.forName(name); + } } catch (Exception e) { LOG.info("Couldn't find encoding " + name, e); } return Charset.defaultCharset(); } - - private HgEncodingUtil() { - } - - /** - * Returns the default charset for Mercurial. - * It is value of{@code HGENCODING} or cp1251 for Windows, and UTF-8 for Unix-like systems. - * - * @return user encoding/cp1251 for windows/UTF-8 for Unix-like systems. - */ - @NotNull - public static Charset getDefaultCharset() { - return DEFAULT_CHARSET; - } } diff --git a/plugins/hg4idea/testSrc/hg4idea/test/HgEncodingTest.java b/plugins/hg4idea/testSrc/hg4idea/test/HgEncodingTest.java new file mode 100644 index 000000000000..1f5484c4090d --- /dev/null +++ b/plugins/hg4idea/testSrc/hg4idea/test/HgEncodingTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package hg4idea.test; + +import com.intellij.openapi.vcs.VcsException; +import org.zmlx.hg4idea.command.HgCommitCommand; +import org.zmlx.hg4idea.execution.HgCommandException; + +import static com.intellij.dvcs.test.Executor.cd; +import static com.intellij.dvcs.test.Executor.echo; + +/** + * @author Nadya Zabrodina + */ +public class HgEncodingTest extends HgPlatformTest { + + //test for default EncodingProject settings + public void testCommitUtfMessage() throws HgCommandException, VcsException { + cd(myRepository); + echo("file.txt", "lalala"); + HgCommitCommand commitCommand = new HgCommitCommand(myProject, myRepository, "сообщение"); + commitCommand.execute(); + } +}