Basic VersionUtil structure created

*hgVersionCommand changed for version parsing
*isValid moved to new VersionUtil class
*isAmendSupported added to VersionUtil
This commit is contained in:
Nadya Zabrodina
2013-07-23 15:42:20 +04:00
parent 5f5161cebb
commit 8d87df5fd0
6 changed files with 65 additions and 18 deletions
@@ -16,7 +16,7 @@ import com.intellij.execution.ExecutableValidator;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.zmlx.hg4idea.command.HgVersionCommand;
import org.zmlx.hg4idea.util.HgVersionUtil;
public class HgExecutableValidator extends ExecutableValidator {
@@ -42,7 +42,7 @@ public class HgExecutableValidator extends ExecutableValidator {
@Override
public boolean isExecutableValid(@NotNull String executable) {
return new HgVersionCommand().isValid(executable, myVcs.getGlobalSettings().isRunViaBash());
return HgVersionUtil.isValid(executable, myVcs.getGlobalSettings().isRunViaBash());
}
}
@@ -56,6 +56,7 @@ import org.zmlx.hg4idea.status.ui.HgHideableWidget;
import org.zmlx.hg4idea.status.ui.HgIncomingOutgoingWidget;
import org.zmlx.hg4idea.status.ui.HgStatusWidget;
import org.zmlx.hg4idea.util.HgUtil;
import org.zmlx.hg4idea.util.HgVersionUtil;
import java.io.File;
import java.util.Collections;
@@ -122,6 +123,7 @@ public class HgVcs extends AbstractVcs<CommittedChangeList> {
commitedChangesProvider = new HgCachingCommitedChangesProvider(project, this);
myMergeProvider = new HgMergeProvider(myProject);
myCommitAndPushExecutor = new HgCommitAndPushExecutor(checkinEnvironment);
HgVersionUtil.updateHgVersion(globalSettings.getHgExecutable(), globalSettings.isRunViaBash());
}
public String getDisplayName() {
@@ -12,27 +12,50 @@
// limitations under the License.
package org.zmlx.hg4idea.command;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.Nullable;
import org.zmlx.hg4idea.execution.HgCommandResult;
import org.zmlx.hg4idea.execution.ShellCommand;
import org.zmlx.hg4idea.execution.ShellCommandException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HgVersionCommand {
public boolean isValid(String executable, boolean isRunViaBash) {
private static final Logger LOGGER = Logger.getInstance(HgVersionCommand.class);
private static final Pattern HG_VERSION_PATTERN = Pattern.compile(".+\\(\\s*version\\s+([0-9]+\\.[0-9]*)\\+?([0-9]*)\\s*\\)\\s*");
public Double getVersion(String executable, boolean isRunViaBash) {
String hgExecutable = executable == null ? null : executable.trim();
ShellCommand shellCommand = new ShellCommand(isRunViaBash);
List<String> cmdArgs = new ArrayList<String>();
cmdArgs.add(hgExecutable);
cmdArgs.add("version");
cmdArgs.add("-q");
try {
return !shellCommand
.execute(Arrays.asList(hgExecutable, "version"), null, Charset.defaultCharset())
.getOutputLines()
.isEmpty();
} catch (ShellCommandException e) {
return false;
} catch (Exception e) {
return false;
HgCommandResult versionResult = shellCommand
.execute(cmdArgs, null, Charset.defaultCharset());
return parseVersion(versionResult);
}
catch (InterruptedException e) {
LOGGER.error(e);
}
catch (ShellCommandException e) {
LOGGER.error(e);
}
return null;
}
@Nullable
private static Double parseVersion(HgCommandResult versionResult) {
Matcher matcher = HG_VERSION_PATTERN.matcher(versionResult.getRawOutput());
if (matcher.matches()) {
return Double.valueOf(matcher.group(1).concat(matcher.group(2)));
}
return null;
}
}
@@ -18,7 +18,7 @@ import com.intellij.openapi.util.SystemInfo;
import org.jetbrains.annotations.NotNull;
import org.zmlx.hg4idea.HgProjectSettings;
import org.zmlx.hg4idea.HgVcsMessages;
import org.zmlx.hg4idea.command.HgVersionCommand;
import org.zmlx.hg4idea.util.HgVersionUtil;
import javax.swing.*;
@@ -65,8 +65,7 @@ public class HgConfigurationProjectPanel {
public void validate() throws ConfigurationException {
String hgExecutable;
hgExecutable = getCurrentPath();
HgVersionCommand command = new HgVersionCommand();
if (!command.isValid(hgExecutable, myRunHgAsBashCheckBox.isSelected())) {
if (!HgVersionUtil.isValid(hgExecutable, myRunHgAsBashCheckBox.isSelected())) {
throw new ConfigurationException(
HgVcsMessages.message("hg4idea.configuration.executable.error", hgExecutable)
);
@@ -6,7 +6,7 @@ import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.vfs.VirtualFile;
import org.zmlx.hg4idea.HgProjectSettings;
import org.zmlx.hg4idea.HgVcsMessages;
import org.zmlx.hg4idea.command.HgVersionCommand;
import org.zmlx.hg4idea.util.HgVersionUtil;
import java.awt.event.ActionListener;
import java.util.HashSet;
@@ -23,9 +23,8 @@ class HgSetExecutablePathPanel extends TextFieldWithBrowseButton {
HgSetExecutablePathPanel(final HgProjectSettings projectSettings) {
FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, false, false, false) {
public void validateSelectedFiles(VirtualFile[] files) throws Exception {
HgVersionCommand command = new HgVersionCommand();
String path = files[0].getPath();
if (!command.isValid(path, projectSettings.isRunViaBash())) {
if (!HgVersionUtil.isValid(path, projectSettings.isRunViaBash())) {
throw new ConfigurationException(HgVcsMessages.message("hg4idea.configuration.executable.error", path));
}
for (ActionListener okListener : myOkListeners) {
@@ -0,0 +1,24 @@
package org.zmlx.hg4idea.util;
import org.zmlx.hg4idea.command.HgVersionCommand;
/**
* @author Nadya Zabrodina
*/
public class HgVersionUtil {
private static Double myHgVersion = null;
public static void updateHgVersion(String executable, boolean isRunViaBash) {
myHgVersion = new HgVersionCommand().getVersion(executable, isRunViaBash);
}
public static boolean isAmendSupported() {
return myHgVersion!=null && myHgVersion >= 2.2; // amend commit supported only for 2.2 or later
}
public static boolean isValid(String executable, boolean isRunViaBash) {
updateHgVersion(executable, isRunViaBash);
return myHgVersion != null;
}
}