config storage structure changed; test added; most recently used config (default path) stored separately; util methods added to HgPlatformTest

This commit is contained in:
Nadya Zabrodina
2013-07-05 18:46:30 +04:00
parent 83dde310cd
commit 1d362fd015
6 changed files with 80 additions and 22 deletions
@@ -34,7 +34,7 @@ public class HgShowConfigCommand {
}
@NotNull
public Map<String, String> execute(@Nullable VirtualFile repo) {
public Map<String, Map<String, String>> execute(@Nullable VirtualFile repo) {
if (repo == null) {
return Collections.emptyMap();
}
@@ -47,13 +47,28 @@ public class HgShowConfigCommand {
return Collections.emptyMap();
}
Map<String, String> options = new HashMap<String, String>();
Map<String, Map<String, String>> configMap = new HashMap<String, Map<String, String>>();
for (String line : result.getOutputLines()) {
List<String> option = StringUtil.split(line, "=", true, false);
if (option.size() == 2) {
options.put(option.get(0).trim(), option.get(1).trim());
String sectionAndName = option.get(0).trim();
String value = option.get(1).trim();
int dotIndex = sectionAndName.indexOf('.');
if (dotIndex > 0) {
String sectionName = sectionAndName.substring(0, dotIndex);
String optionName = sectionAndName.substring(dotIndex + 1, sectionAndName.length());
if (configMap.containsKey(sectionName)) {
configMap.get(sectionName).put(optionName, value);
}
else {
HashMap<String, String> sectionMap = new HashMap<String, String>();
sectionMap.put(optionName, value);
configMap.put(sectionName, sectionMap);
}
}
}
}
return options;
return configMap;
}
}
@@ -115,7 +115,7 @@ public class HgHistoryProvider implements VcsHistoryProvider {
//workaround: --follow options doesn't work with largefiles extension;
//see http://selenic.com/pipermail/mercurial-devel/2013-May/051209.html
logCommand
.setFollowCopies(!filePath.isDirectory() && HgUtil.getRepositoryNamedConfig(project, vcsRoot, "extensions.largefiles") == null);
.setFollowCopies(!filePath.isDirectory() && HgUtil.getRepositoryNamedConfig(project, vcsRoot, "extensions", "largefiles") == null);
logCommand.setIncludeRemoved(true);
try {
return logCommand.execute(new HgFile(vcsRoot, filePath), limit, false);
@@ -19,7 +19,8 @@ public class HgConfig implements HgUpdater {
@NotNull private VirtualFile myRepo;
@NotNull private Project myProject;
@NotNull private Map<String, String> myConfigMap = Collections.emptyMap();
@NotNull private Map<String, Map<String, String>> myConfigMap = Collections.emptyMap();
@Nullable private String myDefaultPath; // cache most recent config
public HgConfig(@NotNull Project project, @NotNull VirtualFile repo) {
@@ -36,27 +37,26 @@ public class HgConfig implements HgUpdater {
// but default values for extension and repository root are not included in hgrc, so perform showconfig is better
// in windows configuration Mercurial.ini file may be used instead of hgrc
myConfigMap = new HgShowConfigCommand(myProject).execute(myRepo);
myDefaultPath = getNamedConfig("paths", "default");
}
@Nullable
public String getDefaultPath() {
return myConfigMap.get("paths.default");
return myDefaultPath;
}
@Nullable
public String getDefaultPushPath() {
String path = myConfigMap.get("paths.default-push");
if (path == null) {
path = myConfigMap.get("paths.default");
}
return path;
String path = getNamedConfig("paths", "default-push");
return path != null ? path : myDefaultPath;
}
@Nullable
public String getNamedConfig(@Nullable String configName) {
if (StringUtil.isEmptyOrSpaces(configName)) {
public String getNamedConfig(@NotNull String sectionName, @Nullable String configName) {
if (StringUtil.isEmptyOrSpaces(sectionName) || StringUtil.isEmptyOrSpaces(configName)) {
return null;
}
return myConfigMap.get(configName);
Map<String, String> sectionValues = myConfigMap.get(sectionName);
return sectionValues != null ? sectionValues.get(configName) : null;
}
}
@@ -569,8 +569,11 @@ public abstract class HgUtil {
}
@Nullable
public static String getRepositoryNamedConfig(@NotNull Project project, @NotNull VirtualFile root, @Nullable String configName) {
public static String getRepositoryNamedConfig(@NotNull Project project,
@NotNull VirtualFile root,
@NotNull String section,
@Nullable String configName) {
HgRepository hgRepository = getRepositoryManager(project).getRepositoryForRoot(root);
return hgRepository != null ? hgRepository.getRepositoryConfig().getNamedConfig(configName) : null;
return hgRepository != null ? hgRepository.getRepositoryConfig().getNamedConfig(section, configName) : null;
}
}
@@ -24,7 +24,10 @@ import com.intellij.testFramework.UsefulTestCase;
import com.intellij.testFramework.fixtures.IdeaProjectTestFixture;
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.zmlx.hg4idea.HgVcs;
import org.zmlx.hg4idea.repo.HgRepository;
import org.zmlx.hg4idea.util.HgUtil;
import java.io.File;
import java.io.IOException;
@@ -105,6 +108,26 @@ public abstract class HgPlatformTest extends UsefulTestCase {
assertTrue(hgrc.exists());
}
protected static void appendToHgrc(@NotNull VirtualFile repositoryRoot, @NotNull String text) {
cd(".hg");
File hgrc = new File(new File(repositoryRoot.getPath(), ".hg"), "hgrc");
try {
FileUtil.appendToFile(hgrc, text);
}
catch (IOException e) {
e.printStackTrace();
fail("Can not update hgrc file.");
}
assertTrue(hgrc.exists());
}
protected static void updateRepoConfig(@NotNull Project project, @Nullable VirtualFile repo) {
HgRepository hgRepository = HgUtil.getRepositoryManager(project).getRepositoryForRoot(repo);
assertNotNull(hgRepository);
hgRepository.getRepositoryConfig().update(project, null);
}
protected void createRepository(VirtualFile root) {
initRepo(root.getPath());
}
@@ -2,7 +2,6 @@ package hg4idea.test.config;
import com.intellij.openapi.util.io.FileUtil;
import hg4idea.test.HgPlatformTest;
import org.zmlx.hg4idea.repo.HgRepository;
import org.zmlx.hg4idea.util.HgUtil;
import java.io.File;
@@ -29,6 +28,26 @@ public class HgConfigTest extends HgPlatformTest {
FileUtil.toSystemIndependentName(defaultPath));
}
public void testPushPathInClonedRepo() {
cd(myChildRepo);
String pushPath = "somePath";
appendToHgrc(myChildRepo, "\n[paths]\n" +
"default-push=" + pushPath);
updateRepoConfig(myProject, myChildRepo);
final String defaultPushPath = HgUtil.getRepositoryDefaultPushPath(myProject, myChildRepo);
assertNotNull(defaultPushPath);
assertEquals(FileUtil.toSystemIndependentName(myChildRepo.getCanonicalPath() + "/" + pushPath),
FileUtil.toSystemIndependentName(defaultPushPath));
}
public void testPushPathWithoutAppropriateConfig() {
cd(myChildRepo);
final String defaultPushPath = HgUtil.getRepositoryDefaultPushPath(myProject, myChildRepo);
assertNotNull(defaultPushPath);
assertEquals(myRepository.getCanonicalPath(),
FileUtil.toSystemIndependentName(defaultPushPath));
}
public void testLargeExtensionInClonedRepo() {
cd(myChildRepo);
File hgrc = new File(new File(myChildRepo.getPath(), ".hg"), "hgrc");
@@ -41,9 +60,7 @@ public class HgConfigTest extends HgPlatformTest {
e.printStackTrace();
fail("Can not update hgrc file.");
}
HgRepository hgRepository = HgUtil.getRepositoryManager(myProject).getRepositoryForRoot(myChildRepo);
assertNotNull(hgRepository);
hgRepository.getRepositoryConfig().update(myProject, null);
assertNotNull(HgUtil.getRepositoryNamedConfig(myProject, myChildRepo, "extensions.largefiles"));
updateRepoConfig(myProject, myChildRepo);
assertNotNull(HgUtil.getRepositoryNamedConfig(myProject, myChildRepo, "extensions", "largefiles"));
}
}