Merge remote-tracking branch 'origin/master'

This commit is contained in:
Dmitry Trofimov
2013-07-15 16:25:54 +02:00
4 changed files with 64 additions and 28 deletions
@@ -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<HgFile> 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<HgFile> files) {
@@ -88,17 +91,23 @@ public class HgCommitCommand {
List<String> parameters = new LinkedList<String>();
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;
@@ -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<String> 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) {
@@ -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;
}
}
@@ -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();
}
}