From 365f5e9ef9be96d6d4181d3227e238d6e92f6125 Mon Sep 17 00:00:00 2001 From: Nadya Zabrodina Date: Fri, 21 Nov 2014 15:54:10 +0300 Subject: [PATCH] IDEA-125298 hg4idea: Ability to create bookmark upon "Open Task" * ability to create bookmark from common bookmarks or from common branches implemented; * separated tests added; * git4idea dependency removed from task-test; * HgUtil.executeOnPooledThreadIfNeeded changed for tests; * annotations added; * tasks-test dependencies added to appropriate vcs test modules --- .../intellij/dvcs/branch/DvcsTaskHandler.java | 2 +- plugins/git4idea/git4idea.iml | 4 +- .../git4idea/branch/GitTaskBranchesTest.java | 68 ++++++++++ plugins/hg4idea/hg4idea.iml | 4 +- plugins/hg4idea/src/META-INF/plugin.xml | 1 + .../src/org/zmlx/hg4idea/HgTaskHandler.java | 100 +++++++++++++++ .../zmlx/hg4idea/command/HgMergeCommand.java | 16 ++- .../src/org/zmlx/hg4idea/util/HgUtil.java | 2 +- .../testSrc/hg4idea/test/HgPlatformTest.java | 2 +- .../test/branch/HgTaskBranchesTest.java | 87 +++++++++++++ plugins/tasks/tasks-tests/tasks-tests.iml | 5 +- .../intellij/tasks/vcs/TaskBranchesTest.java | 120 +++++++++--------- 12 files changed, 338 insertions(+), 73 deletions(-) create mode 100644 plugins/git4idea/tests/git4idea/branch/GitTaskBranchesTest.java create mode 100644 plugins/hg4idea/src/org/zmlx/hg4idea/HgTaskHandler.java create mode 100644 plugins/hg4idea/testSrc/hg4idea/test/branch/HgTaskBranchesTest.java diff --git a/platform/dvcs-impl/src/com/intellij/dvcs/branch/DvcsTaskHandler.java b/platform/dvcs-impl/src/com/intellij/dvcs/branch/DvcsTaskHandler.java index 6d25b020aad2..0d128e319954 100644 --- a/platform/dvcs-impl/src/com/intellij/dvcs/branch/DvcsTaskHandler.java +++ b/platform/dvcs-impl/src/com/intellij/dvcs/branch/DvcsTaskHandler.java @@ -185,7 +185,7 @@ public abstract class DvcsTaskHandler extends VcsTaskHandl }); } - protected abstract void checkout(String taskName, List repos, @Nullable Runnable callInAwtLater); + protected abstract void checkout(@NotNull String taskName, @NotNull List repos, @Nullable Runnable callInAwtLater); protected abstract void checkoutAsNewBranch(@NotNull String name, @NotNull List repositories); diff --git a/plugins/git4idea/git4idea.iml b/plugins/git4idea/git4idea.iml index c109bd529797..60f7a1ac5b8c 100644 --- a/plugins/git4idea/git4idea.iml +++ b/plugins/git4idea/git4idea.iml @@ -59,6 +59,6 @@ + - - + \ No newline at end of file diff --git a/plugins/git4idea/tests/git4idea/branch/GitTaskBranchesTest.java b/plugins/git4idea/tests/git4idea/branch/GitTaskBranchesTest.java new file mode 100644 index 000000000000..171144f8beae --- /dev/null +++ b/plugins/git4idea/tests/git4idea/branch/GitTaskBranchesTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2000-2014 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 git4idea.branch; + +import com.intellij.dvcs.repo.Repository; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vcs.VcsException; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.tasks.vcs.TaskBranchesTest; +import git4idea.config.GitVcsSettings; +import git4idea.repo.GitRepository; +import git4idea.test.GitExecutor; +import git4idea.test.GitTestUtil; +import git4idea.util.GitFileUtils; +import org.jetbrains.annotations.NotNull; + +import java.io.File; + +public class GitTaskBranchesTest extends TaskBranchesTest { + + @Override + protected void setUp() throws Exception { + super.setUp(); + GitVcsSettings.getInstance(myProject).getAppSettings().setPathToGit(GitExecutor.PathHolder.GIT_EXECUTABLE); + } + + @NotNull + @Override + protected Repository initRepository(@NotNull String name) { + String tempDirectory = FileUtil.getTempDirectory(); + String root = tempDirectory + "/" + name; + assertTrue(new File(root).mkdirs()); + GitRepository repository = GitTestUtil.createRepository(getProject(), root); + GitBranchesCollection branches = repository.getBranches(); + assertEquals(1, branches.getLocalBranches().size()); + return repository; + } + + @NotNull + @Override + protected String getDefaultBranchName() { + return "master"; + } + + @Override + protected int getNumberOfBranches(@NotNull Repository repository) { + return repository instanceof GitRepository ? ((GitRepository)repository).getBranches().getLocalBranches().size() : 0; + } + + @Override + protected void addFiles(@NotNull Project project, @NotNull VirtualFile root, @NotNull VirtualFile file) throws VcsException { + GitFileUtils.addFiles(project, root, file); + } +} diff --git a/plugins/hg4idea/hg4idea.iml b/plugins/hg4idea/hg4idea.iml index d765458336ea..5d7958cc7021 100644 --- a/plugins/hg4idea/hg4idea.iml +++ b/plugins/hg4idea/hg4idea.iml @@ -21,6 +21,6 @@ + - - + \ No newline at end of file diff --git a/plugins/hg4idea/src/META-INF/plugin.xml b/plugins/hg4idea/src/META-INF/plugin.xml index 33ec190e1e15..9b9726dfa293 100644 --- a/plugins/hg4idea/src/META-INF/plugin.xml +++ b/plugins/hg4idea/src/META-INF/plugin.xml @@ -28,6 +28,7 @@ + diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/HgTaskHandler.java b/plugins/hg4idea/src/org/zmlx/hg4idea/HgTaskHandler.java new file mode 100644 index 000000000000..8aea83070925 --- /dev/null +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/HgTaskHandler.java @@ -0,0 +1,100 @@ +/* + * Copyright 2000-2014 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 org.zmlx.hg4idea; + +import com.intellij.dvcs.branch.DvcsTaskHandler; +import com.intellij.openapi.fileEditor.FileDocumentManager; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vcs.VcsException; +import com.intellij.openapi.vcs.VcsNotifier; +import com.intellij.openapi.vcs.update.UpdatedFiles; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.zmlx.hg4idea.branch.HgBranchUtil; +import org.zmlx.hg4idea.command.HgBookmarkCommand; +import org.zmlx.hg4idea.command.HgCommitCommand; +import org.zmlx.hg4idea.command.HgMergeCommand; +import org.zmlx.hg4idea.command.HgUpdateCommand; +import org.zmlx.hg4idea.execution.HgCommandException; +import org.zmlx.hg4idea.repo.HgRepository; +import org.zmlx.hg4idea.repo.HgRepositoryManager; +import org.zmlx.hg4idea.util.HgErrorUtil; +import org.zmlx.hg4idea.util.HgUtil; + +import java.util.Collection; +import java.util.List; + +public class HgTaskHandler extends DvcsTaskHandler { + + public HgTaskHandler(@NotNull HgRepositoryManager repositoryManager, + @NotNull Project project) { + super(repositoryManager, project, "bookmark"); + } + + @Override + protected void checkout(@NotNull String taskName, @NotNull List repos, @Nullable Runnable callInAwtLater) { + HgUpdateCommand.updateTo( + !HgBranchUtil.getCommonBookmarks(repos).contains(taskName) ? "head() and not bookmark() and branch(" + taskName + ")" : taskName, + repos, callInAwtLater); + } + + @Override + protected void checkoutAsNewBranch(@NotNull String name, @NotNull List repositories) { + HgBookmarkCommand.createBookmark(repositories, name, true); + } + + @NotNull + @Override + protected Collection getCommonBranchNames(@NotNull List repositories) { + //be careful with equality names of branches/bookmarks =( + return ContainerUtil.concat(HgBranchUtil.getCommonBookmarks(repositories), HgBranchUtil.getCommonBranches(repositories)); + } + + @Override + protected void mergeAndClose(@NotNull final String branch, @NotNull final List repositories) { + String bookmarkRevisionArg = "bookmark(\"" + branch + "\")"; + FileDocumentManager.getInstance().saveAllDocuments(); + final UpdatedFiles updatedFiles = UpdatedFiles.create(); + for (final HgRepository repository : repositories) { + HgMergeCommand.mergeWith(repository, bookmarkRevisionArg, updatedFiles, new Runnable() { + + @Override + public void run() { + Project project = repository.getProject(); + VirtualFile repositoryRoot = repository.getRoot(); + try { + new HgCommitCommand(project, repositoryRoot, "Automated merge with " + branch).execute(); + new HgBookmarkCommand(project, repositoryRoot, branch).deleteBookmark(); + } + catch (HgCommandException e) { + HgErrorUtil.handleException(project, e); + } + catch (VcsException e) { + VcsNotifier.getInstance(project) + .notifyError("Exception during merge commit with " + branch, e.getMessage()); + } + } + }); + } + } + + @Override + protected boolean hasBranch(@NotNull HgRepository repository, @NotNull String name) { + return HgUtil.getNamesWithoutHashes(repository.getBookmarks()).contains(name) || repository.getOpenedBranches().contains(name); + } +} diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgMergeCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgMergeCommand.java index 57751791c053..776a7c4eaea1 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgMergeCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgMergeCommand.java @@ -93,10 +93,17 @@ public class HgMergeCommand { } public static void mergeWith(@NotNull final HgRepository repository, - @NotNull String branchName, + @NotNull final String branchName, @NotNull final UpdatedFiles updatedFiles) { + mergeWith(repository, branchName, updatedFiles, null); + } + + public static void mergeWith(@NotNull final HgRepository repository, + @NotNull final String branchName, + @NotNull final UpdatedFiles updatedFiles, @Nullable final Runnable onSuccessHandler) { final Project project = repository.getProject(); - final HgMergeCommand hgMergeCommand = new HgMergeCommand(project, repository.getRoot()); + final VirtualFile repositoryRoot = repository.getRoot(); + final HgMergeCommand hgMergeCommand = new HgMergeCommand(project, repositoryRoot); hgMergeCommand.setRevision(branchName);//there is no difference between branch or revision or bookmark as parameter to merge, // we need just a string new Task.Backgroundable(project, "Merging changes...") { @@ -104,7 +111,10 @@ public class HgMergeCommand { public void run(@NotNull ProgressIndicator indicator) { try { hgMergeCommand.merge(); - new HgConflictResolver(project, updatedFiles).resolve(repository.getRoot()); + new HgConflictResolver(project, updatedFiles).resolve(repositoryRoot); + if (HgConflictResolver.findConflicts(project, repositoryRoot).isEmpty() && onSuccessHandler != null) { + onSuccessHandler.run(); // for example commit changes + } } catch (VcsException exception) { if (exception.isWarning()) { diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgUtil.java b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgUtil.java index 3204be8ff52c..3fc8db7f08f3 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgUtil.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgUtil.java @@ -454,7 +454,7 @@ public abstract class HgUtil { } public static void executeOnPooledThreadIfNeeded(Runnable runnable) { - if (EventQueue.isDispatchThread()) { + if (EventQueue.isDispatchThread() && !ApplicationManager.getApplication().isUnitTestMode()) { ApplicationManager.getApplication().executeOnPooledThread(runnable); } else { runnable.run(); diff --git a/plugins/hg4idea/testSrc/hg4idea/test/HgPlatformTest.java b/plugins/hg4idea/testSrc/hg4idea/test/HgPlatformTest.java index 5cb02e06ca62..7099c3bbf88d 100644 --- a/plugins/hg4idea/testSrc/hg4idea/test/HgPlatformTest.java +++ b/plugins/hg4idea/testSrc/hg4idea/test/HgPlatformTest.java @@ -128,7 +128,7 @@ public abstract class HgPlatformTest extends UsefulTestCase { initRepo(root.getPath()); } - private static void initRepo(String repoRoot) { + public static void initRepo(String repoRoot) { cd(repoRoot); hg("init"); touch("file.txt"); diff --git a/plugins/hg4idea/testSrc/hg4idea/test/branch/HgTaskBranchesTest.java b/plugins/hg4idea/testSrc/hg4idea/test/branch/HgTaskBranchesTest.java new file mode 100644 index 000000000000..08398f1c48df --- /dev/null +++ b/plugins/hg4idea/testSrc/hg4idea/test/branch/HgTaskBranchesTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2000-2014 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.branch; + +import com.intellij.dvcs.repo.Repository; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vcs.ProjectLevelVcsManager; +import com.intellij.openapi.vcs.impl.ProjectLevelVcsManagerImpl; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.tasks.vcs.TaskBranchesTest; +import com.intellij.util.ObjectUtils; +import hg4idea.test.HgExecutor; +import hg4idea.test.HgPlatformTest; +import org.jetbrains.annotations.NotNull; +import org.zmlx.hg4idea.HgVcs; +import org.zmlx.hg4idea.repo.HgRepository; +import org.zmlx.hg4idea.util.HgUtil; + +import java.io.File; + +import static com.intellij.openapi.vcs.Executor.cd; +import static com.intellij.openapi.vcs.Executor.touch; +import static hg4idea.test.HgExecutor.hg; + +public class HgTaskBranchesTest extends TaskBranchesTest { + + @Override + protected void setUp() throws Exception { + super.setUp(); + HgVcs hgVcs = ObjectUtils.assertNotNull(HgVcs.getInstance(myProject)); + hgVcs.getGlobalSettings().setHgExecutable(HgExecutor.getHgExecutable()); + } + + @NotNull + @Override + protected Repository initRepository(@NotNull String name) { + String tempDirectory = FileUtil.getTempDirectory(); + String root = tempDirectory + "/" + name; + assertTrue(new File(root).mkdirs()); + HgPlatformTest.initRepo(root); + touch("a.txt"); + hg("add a.txt"); + hg("commit -m another"); + hg("up -r 0"); + ProjectLevelVcsManagerImpl vcsManager = (ProjectLevelVcsManagerImpl)ProjectLevelVcsManager.getInstance(myProject); + vcsManager.setDirectoryMapping(root, HgVcs.VCS_NAME); + VirtualFile file = LocalFileSystem.getInstance().findFileByIoFile(new File(root)); + HgRepository repository = HgUtil.getRepositoryManager(myProject).getRepositoryForRoot(file); + assertNotNull("Couldn't find repository for root " + root, repository); + return repository; + } + + @NotNull + @Override + protected String getDefaultBranchName() { + return "default"; + } + + @Override + protected int getNumberOfBranches(@NotNull Repository repository) { + if (!(repository instanceof HgRepository)) return 0; + return ((HgRepository)repository).getOpenedBranches().size() + + ((HgRepository)repository).getBookmarks().size(); + } + + @Override + protected void addFiles(@NotNull Project project, @NotNull VirtualFile root, @NotNull VirtualFile file) { + cd(root); + hg("add " + file.getPath()); + } +} + diff --git a/plugins/tasks/tasks-tests/tasks-tests.iml b/plugins/tasks/tasks-tests/tasks-tests.iml index d09122f88424..4e9157f0112d 100644 --- a/plugins/tasks/tasks-tests/tasks-tests.iml +++ b/plugins/tasks/tasks-tests/tasks-tests.iml @@ -17,10 +17,9 @@ - + - - + \ No newline at end of file diff --git a/plugins/tasks/tasks-tests/test/com/intellij/tasks/vcs/TaskBranchesTest.java b/plugins/tasks/tasks-tests/test/com/intellij/tasks/vcs/TaskBranchesTest.java index 54c00c38b83e..95d93a5a3dcb 100644 --- a/plugins/tasks/tasks-tests/test/com/intellij/tasks/vcs/TaskBranchesTest.java +++ b/plugins/tasks/tasks-tests/test/com/intellij/tasks/vcs/TaskBranchesTest.java @@ -15,9 +15,11 @@ */ package com.intellij.tasks.vcs; +import com.intellij.dvcs.repo.Repository; +import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Disposer; -import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vcs.ProjectLevelVcsManager; +import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vcs.VcsTaskHandler; import com.intellij.openapi.vcs.changes.*; import com.intellij.openapi.vfs.LocalFileSystem; @@ -31,14 +33,10 @@ import com.intellij.tasks.impl.TaskManagerImpl; import com.intellij.testFramework.PlatformTestCase; import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; -import git4idea.branch.GitBranchesCollection; -import git4idea.config.GitVcsSettings; -import git4idea.repo.GitRepository; -import git4idea.test.GitExecutor; -import git4idea.test.GitTestUtil; -import git4idea.util.GitFileUtils; +import org.jetbrains.annotations.NotNull; import java.io.File; +import java.io.IOException; import java.util.Collections; import java.util.List; @@ -47,17 +45,18 @@ import java.util.List; * Date: 18.07.13 */ @SuppressWarnings("ConstantConditions") -public class TaskBranchesTest extends PlatformTestCase { +public abstract class TaskBranchesTest extends PlatformTestCase { private TaskManagerImpl myTaskManager; private ChangeListManagerImpl myChangeListManager; private VcsDirtyScopeManagerImpl myDirtyScopeManager; - public void testGitTaskHandler() throws Exception { + public void testVcsTaskHandler() throws Exception { - List repositories = initRepositories("community", "idea"); - GitRepository repository = repositories.get(0); - assertEquals("master", repository.getCurrentBranch().getName()); + List repositories = initRepositories("community", "idea"); + Repository repository = repositories.get(0); + String defaultBranchName = getDefaultBranchName(); + assertEquals(defaultBranchName, repository.getCurrentBranchName()); VcsTaskHandler[] handlers = VcsTaskHandler.getAllHandlers(getProject()); assertEquals(1, handlers.length); @@ -66,29 +65,30 @@ public class TaskBranchesTest extends PlatformTestCase { VcsTaskHandler.TaskInfo defaultInfo = handler.getActiveTask(); final String first = "first"; VcsTaskHandler.TaskInfo firstInfo = handler.startNewTask(first); - assertEquals(2, repository.getBranches().getLocalBranches().size()); - assertEquals(first, repository.getCurrentBranch().getName()); + assertEquals(2, getNumberOfBranches(repository)); + assertEquals(first, repository.getCurrentBranchName()); handler.switchToTask(defaultInfo, null); - assertEquals("master", repository.getCurrentBranch().getName()); + assertEquals(defaultBranchName, repository.getCurrentBranchName()); final String second = "second"; VcsTaskHandler.TaskInfo secondInfo = handler.startNewTask(second); - assertEquals(3, repository.getBranches().getLocalBranches().size()); - assertEquals(second, repository.getCurrentBranch().getName()); - + assertEquals(3, getNumberOfBranches(repository)); + assertEquals(second, repository.getCurrentBranchName()); + handler.switchToTask(firstInfo, null); + commitChanges(repository); handler.closeTask(secondInfo, firstInfo); - repository.update(); - assertEquals(2, repository.getBranches().getLocalBranches().size()); + assertEquals(2, getNumberOfBranches(repository)); } public void testTaskManager() throws Exception { - List repositories = initRepositories("community", "idea"); + List repositories = initRepositories("community", "idea"); LocalTask defaultTask = myTaskManager.getActiveTask(); assertNotNull(defaultTask); LocalTaskImpl foo = myTaskManager.createLocalTask("foo"); LocalTask localTask = myTaskManager.activateTask(foo, false); myTaskManager.createBranch(localTask, defaultTask, myTaskManager.suggestBranchName(localTask)); + String defaultBranchName = getDefaultBranchName(); assertEquals(4, localTask.getBranches().size()); assertEquals(2, localTask.getBranches(true).size()); @@ -98,50 +98,53 @@ public class TaskBranchesTest extends PlatformTestCase { myTaskManager.activateTask(defaultTask, false); - GitRepository repository = repositories.get(0); - assertEquals("master", repository.getCurrentBranch().getName()); + Repository repository = repositories.get(0); + assertEquals(defaultBranchName, repository.getCurrentBranchName()); foo = myTaskManager.createLocalTask("foo"); localTask = myTaskManager.activateTask(foo, false); myTaskManager.createBranch(localTask, defaultTask, myTaskManager.suggestBranchName(localTask)); - assertEquals("foo", repository.getCurrentBranch().getName()); + assertEquals("foo", repository.getCurrentBranchName()); + commitChanges(repository); myTaskManager.mergeBranch(localTask); - repository.update(); - assertEquals("master", repository.getCurrentBranch().getName()); - assertEquals(1, repository.getBranches().getLocalBranches().size()); + assertEquals(defaultBranchName, repository.getCurrentBranchName()); + assertEquals(1, getNumberOfBranches(repository)); myTaskManager.activateTask(defaultTask, false); myTaskManager.activateTask(foo, false); } - public void testCommit() throws Exception { - GitRepository repository = initRepository("foo"); - LocalTask defaultTask = myTaskManager.getActiveTask(); - LocalTaskImpl foo = myTaskManager.createLocalTask("foo"); - final LocalTask localTask = myTaskManager.activateTask(foo, false); - myTaskManager.createBranch(localTask, defaultTask, myTaskManager.suggestBranchName(localTask)); - + private void commitChanges(@NotNull Repository repository) throws IOException, VcsException { VirtualFile root = repository.getRoot(); File file = new File(root.getPath(), "foo.txt"); assertTrue(file.createNewFile()); final VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file); - GitFileUtils.addFiles(getProject(), root, virtualFile); + addFiles(getProject(), root, virtualFile); myDirtyScopeManager.fileDirty(virtualFile); myChangeListManager.ensureUpToDate(false); Change change = myChangeListManager.getChange(virtualFile); assertNotNull(change); ProjectLevelVcsManager.getInstance(getProject()).getAllActiveVcss()[0].getCheckinEnvironment() .commit(Collections.singletonList(change), "foo"); + } + + public void testCommit() throws Exception { + Repository repository = initRepository("foo"); + LocalTask defaultTask = myTaskManager.getActiveTask(); + LocalTaskImpl foo = myTaskManager.createLocalTask("foo"); + final LocalTask localTask = myTaskManager.activateTask(foo, false); + myTaskManager.createBranch(localTask, defaultTask, myTaskManager.suggestBranchName(localTask)); + commitChanges(repository); myTaskManager.mergeBranch(localTask); - repository.update(); - assertEquals("master", repository.getCurrentBranch().getName()); - assertEquals(1, repository.getBranches().getLocalBranches().size()); + assertEquals(getDefaultBranchName(), repository.getCurrentBranchName()); + assertEquals(1, getNumberOfBranches(repository)); } public void testOpenTaskDialog() throws Exception { initRepository("foo"); + String defaultBranchName = getDefaultBranchName(); LocalTaskImpl task = myTaskManager.createLocalTask("foo"); OpenTaskDialog dialog = new OpenTaskDialog(getProject(), task); Disposer.register(myTestRootDisposable, dialog.getDisposable()); @@ -149,7 +152,7 @@ public class TaskBranchesTest extends PlatformTestCase { assertEquals("foo", myTaskManager.getActiveTask().getSummary()); List branches = task.getBranches(true); assertEquals(1, branches.size()); - assertEquals("master", branches.get(0).name); + assertEquals(defaultBranchName, branches.get(0).name); branches = task.getBranches(false); assertEquals(1, branches.size()); assertEquals("foo", branches.get(0).name); @@ -173,8 +176,8 @@ public class TaskBranchesTest extends PlatformTestCase { assertEquals(1, defaultTask.getBranches().size()); } - public void testCleanupRemovedBranch() { - GitRepository repository = initRepository("foo"); + public void testCleanupRemovedBranch() throws InterruptedException { + Repository repository = initRepository("foo"); LocalTask defaultTask = myTaskManager.getActiveTask(); assertNotNull(defaultTask); assertEquals(0, defaultTask.getBranches().size()); @@ -189,42 +192,39 @@ public class TaskBranchesTest extends PlatformTestCase { info.name = "non-existing"; info.repository = defaultTask.getBranches().get(0).repository; defaultTask.addBranch(info); - - assertEquals("foo", repository.getCurrentBranch().getName()); + assertEquals("foo", repository.getCurrentBranchName()); myTaskManager.activateTask(defaultTask, false); - assertEquals("master", repository.getCurrentBranch().getName()); + assertEquals(getDefaultBranchName(), repository.getCurrentBranchName()); // do not re-create "non-existing" - assertEquals(2, repository.getBranches().getLocalBranches().size()); + assertEquals(2, getNumberOfBranches(repository)); } - private List initRepositories(String... names) { - return ContainerUtil.map(names, new Function() { + private List initRepositories(String... names) { + return ContainerUtil.map(names, new Function() { @Override - public GitRepository fun(String s) { + public Repository fun(String s) { return initRepository(s); } }); } - private GitRepository initRepository(String name) { - String tempDirectory = FileUtil.getTempDirectory(); - String root = tempDirectory + "/" + name; - assertTrue(new File(root).mkdirs()); - GitRepository repository = GitTestUtil.createRepository(getProject(), root); - GitBranchesCollection branches = repository.getBranches(); - assertEquals(1, branches.getLocalBranches().size()); - return repository; - } - @Override protected void setUp() throws Exception { super.setUp(); myTaskManager = (TaskManagerImpl)TaskManager.getManager(getProject()); - GitVcsSettings.getInstance(myProject).getAppSettings().setPathToGit(GitExecutor.PathHolder.GIT_EXECUTABLE); - myChangeListManager = (ChangeListManagerImpl)ChangeListManager.getInstance(getProject()); myChangeListManager.projectOpened(); myDirtyScopeManager = ((VcsDirtyScopeManagerImpl)VcsDirtyScopeManager.getInstance(getProject())); myDirtyScopeManager.projectOpened(); } + + @NotNull + protected abstract Repository initRepository(@NotNull String name); + + @NotNull + protected abstract String getDefaultBranchName(); + + protected abstract int getNumberOfBranches(@NotNull Repository repository); + + protected abstract void addFiles(@NotNull Project project, @NotNull VirtualFile root, @NotNull VirtualFile file) throws VcsException; }