perform commit command with hgRepository instance as argument instead of VF

* tests fixed
* unnecessary checks removed
This commit is contained in:
Nadya Zabrodina
2014-12-24 14:35:31 +03:00
parent aa0d78697b
commit 56f2123663
7 changed files with 37 additions and 28 deletions
@@ -78,7 +78,7 @@ public class HgTaskHandler extends DvcsTaskHandler<HgRepository> {
Project project = repository.getProject();
VirtualFile repositoryRoot = repository.getRoot();
try {
new HgCommitCommand(project, repositoryRoot, "Automated merge with " + branch).execute();
new HgCommitCommand(project, repository, "Automated merge with " + branch).execute();
new HgBookmarkCommand(project, repositoryRoot, branch).deleteBookmark();
}
catch (HgCommandException e) {
@@ -18,7 +18,6 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.messages.MessageBus;
@@ -30,9 +29,7 @@ import org.zmlx.hg4idea.HgVcsMessages;
import org.zmlx.hg4idea.execution.HgCommandException;
import org.zmlx.hg4idea.execution.HgCommandExecutor;
import org.zmlx.hg4idea.repo.HgRepository;
import org.zmlx.hg4idea.repo.HgRepositoryManager;
import org.zmlx.hg4idea.util.HgEncodingUtil;
import org.zmlx.hg4idea.util.HgUtil;
import java.io.File;
import java.io.IOException;
@@ -49,7 +46,7 @@ public class HgCommitCommand {
private static final String TEMP_FILE_NAME = ".hg4idea-commit.tmp";
private final Project myProject;
private final VirtualFile myRoot;
private final HgRepository myRepository;
private final String myMessage;
@NotNull private final Charset myCharset;
private final boolean myAmend;
@@ -58,21 +55,21 @@ public class HgCommitCommand {
private Set<HgFile> myFiles = Collections.emptySet();
@NotNull private List<String> mySubrepos = Collections.emptyList();
public HgCommitCommand(@NotNull Project project, @NotNull VirtualFile root, String message, boolean amend, boolean closeBranch) {
public HgCommitCommand(@NotNull Project project, @NotNull HgRepository repository, String message, boolean amend, boolean closeBranch) {
myProject = project;
myRoot = root;
myRepository = repository;
myMessage = message;
myCharset = HgEncodingUtil.getDefaultCharset(myProject);
myAmend = amend;
myCloseBranch = closeBranch;
}
public HgCommitCommand(@NotNull Project project, @NotNull VirtualFile root, String message, boolean amend) {
this(project, root, message, amend, false);
public HgCommitCommand(@NotNull Project project, @NotNull HgRepository repo, String message, boolean amend) {
this(project, repo, message, amend, false);
}
public HgCommitCommand(Project project, @NotNull VirtualFile root, String message) {
this(project, root, message, false);
public HgCommitCommand(Project project, @NotNull HgRepository repo, String message) {
this(project, repo, message, false);
}
public void setFiles(@NotNull Set<HgFile> files) {
@@ -110,10 +107,7 @@ public class HgCommitCommand {
commitChunkFiles(chunk, amendCommit, false, myCloseBranch && i == size - 1);
}
}
if (!myProject.isDisposed()) {
HgRepositoryManager manager = HgUtil.getRepositoryManager(myProject);
manager.updateRepository(myRoot);
}
myRepository.update();
final MessageBus messageBus = myProject.getMessageBus();
messageBus.syncPublisher(HgVcs.REMOTE_TOPIC).update(myProject, null);
messageBus.syncPublisher(HgVcs.BRANCH_TOPIC).update(myProject, null);
@@ -125,8 +119,6 @@ public class HgCommitCommand {
private void commitChunkFiles(@NotNull List<String> chunk, boolean amendCommit, boolean withSubrepos, boolean closeBranch)
throws VcsException {
HgRepository repository = HgUtil.getRepositoryForFile(myProject, myRoot);
assert repository != null;
List<String> parameters = new LinkedList<String>();
parameters.add("--logfile");
parameters.add(saveCommitMessage().getAbsolutePath());
@@ -139,7 +131,7 @@ public class HgCommitCommand {
parameters.add("--amend");
}
if (closeBranch) {
if (chunk.isEmpty() && repository.getState() != Repository.State.MERGING) {
if (chunk.isEmpty() && myRepository.getState() != Repository.State.MERGING) {
//if there are changed files but nothing selected -> need to exclude all; if merge commit then nothing excluded
parameters.add("-X");
parameters.add("\"**\"");
@@ -149,7 +141,7 @@ public class HgCommitCommand {
parameters.addAll(chunk);
HgCommandExecutor executor = new HgCommandExecutor(myProject);
executor.setCharset(myCharset);
ensureSuccess(executor.executeInCurrentThread(myRoot, "commit", parameters));
ensureSuccess(executor.executeInCurrentThread(myRepository.getRoot(), "commit", parameters));
}
private File saveCommitMessage() throws VcsException {
@@ -104,7 +104,7 @@ public class HgCheckinEnvironment implements CheckinEnvironment {
HgRepository repo = entry.getKey();
Set<HgFile> selectedFiles = entry.getValue();
HgCommitCommand command =
new HgCommitCommand(myProject, repo.getRoot(), preparedComment, myNextCommitAmend, myCloseBranch);
new HgCommitCommand(myProject, repo, preparedComment, myNextCommitAmend, myCloseBranch);
if (isMergeCommit(repo.getRoot())) {
//partial commits are not allowed during merges
@@ -12,6 +12,7 @@
// limitations under the License.
package org.zmlx.hg4idea.provider.update;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
@@ -43,6 +44,7 @@ public class HgRegularUpdater implements HgUpdater {
@NotNull private final Project project;
@NotNull private final VirtualFile repoRoot;
@NotNull private final HgUpdateConfigurationSettings updateConfiguration;
private static final Logger LOG = Logger.getInstance(HgRegularUpdater.class);
public HgRegularUpdater(@NotNull Project project, @NotNull VirtualFile repository, @NotNull HgUpdateConfigurationSettings configuration) {
this.project = project;
@@ -185,14 +187,21 @@ public class HgRegularUpdater implements HgUpdater {
private void commitOrWarnAboutConflicts(List<VcsException> exceptions, HgCommandResult mergeResult) throws VcsException {
if (mergeResult.getExitValue() == 0) { //operation successful and no conflicts
try {
new HgCommitCommand(project, repoRoot, "Automated merge").execute();
} catch (HgCommandException e) {
HgRepository hgRepository = HgUtil.getRepositoryForFile(project, repoRoot);
if (hgRepository == null) {
LOG.warn("Couldn't find repository info for " + repoRoot.getName());
return;
}
new HgCommitCommand(project, hgRepository, "Automated merge").execute();
}
catch (HgCommandException e) {
throw new VcsException(e);
}
} else {
reportWarning(exceptions, HgVcsMessages.message("hg4idea.update.warning.merge.conflicts", repoRoot.getPath()));
}
}
else {
reportWarning(exceptions, HgVcsMessages.message("hg4idea.update.warning.merge.conflicts", repoRoot.getPath()));
}
}
private HgCommandResult doMerge(ProgressIndicator indicator) throws VcsException {
indicator.setText2(HgVcsMessages.message("hg4idea.update.progress.merging"));
@@ -22,6 +22,8 @@ import org.zmlx.hg4idea.HgFileRevision;
import org.zmlx.hg4idea.command.HgCommitCommand;
import org.zmlx.hg4idea.command.HgLogCommand;
import org.zmlx.hg4idea.execution.HgCommandException;
import org.zmlx.hg4idea.repo.HgRepository;
import org.zmlx.hg4idea.repo.HgRepositoryImpl;
import java.util.List;
@@ -37,7 +39,8 @@ public class HgEncodingTest extends HgPlatformTest {
public void testCommitUtfMessage() throws HgCommandException, VcsException {
cd(myRepository);
echo("file.txt", "lalala");
HgCommitCommand commitCommand = new HgCommitCommand(myProject, myRepository, "сообщение");
HgRepository hgRepo = HgRepositoryImpl.getInstance(myRepository, myProject, myProject);
HgCommitCommand commitCommand = new HgCommitCommand(myProject, hgRepo, "сообщение");
commitCommand.execute();
}
@@ -47,7 +50,8 @@ public class HgEncodingTest extends HgPlatformTest {
String fileName = "file.txt";
echo(fileName, "lalala");
String comment = "öäüß";
HgCommitCommand commitCommand = new HgCommitCommand(myProject, myRepository, comment);
HgRepository hgRepo = HgRepositoryImpl.getInstance(myRepository, myProject, myProject);
HgCommitCommand commitCommand = new HgCommitCommand(myProject, hgRepo, comment);
commitCommand.execute();
HgLogCommand logCommand = new HgLogCommand(myProject);
myRepository.refresh(false, true);
@@ -107,6 +107,7 @@ public abstract class HgPlatformTest extends UsefulTestCase {
File hgrc = new File(new File(repositoryRoot.getPath(), ".hg"), "hgrc");
FileUtil.appendToFile(hgrc, FileUtil.loadFile(hgrcFile));
assertTrue(hgrc.exists());
repositoryRoot.refresh(false, true);
}
protected static void appendToHgrc(@NotNull VirtualFile repositoryRoot, @NotNull String text) throws IOException {
@@ -23,6 +23,8 @@ import org.zmlx.hg4idea.HgFileRevision;
import org.zmlx.hg4idea.command.HgCommitCommand;
import org.zmlx.hg4idea.command.HgLogCommand;
import org.zmlx.hg4idea.execution.HgCommandException;
import org.zmlx.hg4idea.repo.HgRepository;
import org.zmlx.hg4idea.repo.HgRepositoryImpl;
import java.util.List;
@@ -51,7 +53,8 @@ public class HgCommitTest extends HgPlatformTest {
logCommand.setLogFile(false);
HgFile hgFile = new HgFile(myRepository, VfsUtilCore.virtualToIoFile(myRepository));
List<HgFileRevision> revisions = logCommand.execute(hgFile, -1, false);
HgCommitCommand commit = new HgCommitCommand(myProject, myRepository, changedCommit, true);
HgRepository hgRepo = HgRepositoryImpl.getInstance(myRepository, myProject, myProject);
HgCommitCommand commit = new HgCommitCommand(myProject, hgRepo, changedCommit, true);
commit.execute();
List<HgFileRevision> revisionsAfterAmendCommit = logCommand.execute(hgFile, -1, false);
assertTrue(revisions.size() == revisionsAfterAmendCommit.size());