[git] Branch API, step 2: move selectedRepository parameter to compare()

selectedRepository is needed only for the branch comparing dialog, so better to pass it to the compare() method instead of GitBrancherImpl.
This commit is contained in:
Kirill Likhodedov
2012-10-04 13:26:29 +04:00
parent 94ea085c37
commit 1ed532e592
6 changed files with 46 additions and 68 deletions
@@ -15,6 +15,7 @@
*/
package git4idea.branch;
import git4idea.repo.GitRepository;
import org.jetbrains.annotations.NotNull;
/**
@@ -85,9 +86,12 @@ public interface GitBrancher {
/**
* Compares the HEAD with the specified branch - shows a dialog with the differences.
*
* @param branchName name of the branch to compare with.
* @param branchName name of the branch to compare with.
* @param selectedRepository current or selected repository.
* The list of commits is displayed for the repository selected from the combobox.
* This parameter tells which repository should be pre-selected in the combobox.
*/
void compare(@NotNull String branchName);
void compare(@NotNull String branchName, @NotNull GitRepository selectedRepository);
/**
* <p>Merges the given branch to the HEAD.</p>
@@ -62,7 +62,6 @@ public final class GitBrancherImpl implements GitBrancher {
private final Project myProject;
private final List<GitRepository> myRepositories;
private final @Nullable Runnable myCallInAwtAfterExecution;
private final GitRepository mySelectedRepository;
private final Git myGit;
public GitBrancherImpl(@NotNull GitRepository repository) {
@@ -70,21 +69,16 @@ public final class GitBrancherImpl implements GitBrancher {
}
public GitBrancherImpl(@NotNull GitRepository repository, @Nullable Runnable callInAwtAfterExecution) {
this(repository.getProject(), Collections.singletonList(repository), repository, callInAwtAfterExecution);
this(repository.getProject(), Collections.singletonList(repository), callInAwtAfterExecution);
}
public GitBrancherImpl(@NotNull Project project, @NotNull List<GitRepository> repositories,
@NotNull GitRepository selectedRepository) {
this(project, repositories, selectedRepository, null);
public GitBrancherImpl(@NotNull Project project, @NotNull List<GitRepository> repositories) {
this(project, repositories, null);
}
public GitBrancherImpl(@NotNull Project project,
@NotNull List<GitRepository> repositories,
@NotNull GitRepository selectedRepository,
@Nullable Runnable callInAwtAfterExecution) {
public GitBrancherImpl(@NotNull Project project, @NotNull List<GitRepository> repositories, @Nullable Runnable callInAwtAfterExecution) {
myProject = project;
myRepositories = repositories;
mySelectedRepository = selectedRepository;
myCallInAwtAfterExecution = callInAwtAfterExecution;
myGit = ServiceManager.getService(Git.class);
}
@@ -345,7 +339,7 @@ public final class GitBrancherImpl implements GitBrancher {
}
@Override
public void compare(@NotNull final String branchName) {
public void compare(@NotNull final String branchName, @NotNull final GitRepository selectedRepository) {
new CommonBackgroundTask(myProject, "Comparing with " + branchName, myCallInAwtAfterExecution) {
private GitCommitCompareInfo myCompareInfo;
@@ -361,7 +355,7 @@ public final class GitBrancherImpl implements GitBrancher {
LOG.error("The task to get compare info didn't finish. Repositories: \n" + myRepositories + "\nbranch name: " + branchName);
return;
}
displayCompareDialog(branchName, getCurrentBranchOrRev(), myCompareInfo);
displayCompareDialog(branchName, getCurrentBranchOrRev(), myCompareInfo, selectedRepository);
}
}.runInBackground();
}
@@ -401,13 +395,14 @@ public final class GitBrancherImpl implements GitBrancher {
return Pair.create(headToBranch, branchToHead);
}
private void displayCompareDialog(@NotNull String branchName, @NotNull String currentBranch, @NotNull GitCommitCompareInfo compareInfo) {
private void displayCompareDialog(@NotNull String branchName, @NotNull String currentBranch, @NotNull GitCommitCompareInfo compareInfo,
@NotNull GitRepository selectedRepository) {
if (compareInfo.isEmpty()) {
Messages.showInfoMessage(myProject, String.format("<html>There are no changes between <code>%s</code> and <code>%s</code></html>",
currentBranch, branchName), "No Changes Detected");
}
else {
new GitCompareBranchesDialog(myProject, branchName, currentBranch, compareInfo, mySelectedRepository).show();
new GitCompareBranchesDialog(myProject, branchName, currentBranch, compareInfo, selectedRepository).show();
}
}
@@ -426,7 +421,7 @@ public final class GitBrancherImpl implements GitBrancher {
revisions.put(repository, repository.getCurrentRevision());
}
new GitMergeOperation(myProject, myGit, myRepositories, branchName, localBranch, getCurrentBranchOrRev(),
mySelectedRepository, revisions, indicator).execute();
revisions, indicator).execute();
}
/**
@@ -56,7 +56,6 @@ class GitMergeOperation extends GitBranchOperation {
@NotNull private final String myBranchToMerge;
private final boolean myLocalBranch;
@NotNull private final String myCurrentBranch;
@NotNull private final GitRepository myCurrentRepository;
@NotNull private final Map<GitRepository, String> myCurrentRevisionsBeforeMerge;
// true in value, if we've stashed local changes before merge and will need to unstash after resolving conflicts.
@@ -65,13 +64,11 @@ class GitMergeOperation extends GitBranchOperation {
GitMergeOperation(@NotNull Project project, @NotNull Git git, @NotNull Collection<GitRepository> repositories,
@NotNull String branchToMerge, boolean localBranch, @NotNull String currentBranch,
@NotNull GitRepository currentRepository, @NotNull Map<GitRepository, String> currentRevisionsBeforeMerge,
@NotNull ProgressIndicator indicator) {
@NotNull Map<GitRepository, String> currentRevisionsBeforeMerge, @NotNull ProgressIndicator indicator) {
super(project, git, repositories, currentBranch, indicator);
myBranchToMerge = branchToMerge;
myLocalBranch = localBranch;
myCurrentBranch = currentBranch;
myCurrentRepository = currentRepository;
myCurrentRevisionsBeforeMerge = currentRevisionsBeforeMerge;
myChangeListManager = ChangeListManager.getInstance(myProject);
}
@@ -381,8 +378,7 @@ class GitMergeOperation extends GitBranchOperation {
public void hyperlinkUpdate(@NotNull Notification notification,
@NotNull HyperlinkEvent event) {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED && event.getDescription().equalsIgnoreCase("delete")) {
new GitBrancherImpl(myProject, new ArrayList<GitRepository>(getRepositories()), myCurrentRepository).
deleteBranch(myBranchToMerge);
new GitBrancherImpl(myProject, new ArrayList<GitRepository>(getRepositories())).deleteBranch(myBranchToMerge);
}
}
}
@@ -220,7 +220,7 @@ class GitBranchPopup {
List<GitRepository> repositories = repositoryManager.getRepositories();
String currentBranch = myMultiRootBranchConfig.getCurrentBranch();
assert currentBranch != null : "Current branch can't be null if branches have not diverged";
popupGroup.add(new GitBranchPopupActions.NewBranchAction(myProject, repositories, myCurrentRepository));
popupGroup.add(new GitBranchPopupActions.NewBranchAction(myProject, repositories));
popupGroup.addAll(createRepositoriesActions());
@@ -53,7 +53,7 @@ class GitBranchPopupActions {
ActionGroup createActions(@Nullable DefaultActionGroup toInsert) {
DefaultActionGroup popupGroup = new DefaultActionGroup(null, false);
popupGroup.addAction(new NewBranchAction(myProject, Collections.singletonList(myRepository), myRepository));
popupGroup.addAction(new NewBranchAction(myProject, Collections.singletonList(myRepository)));
popupGroup.addAction(new CheckoutRevisionActions(myProject, myRepository));
if (toInsert != null) {
@@ -82,20 +82,18 @@ class GitBranchPopupActions {
static class NewBranchAction extends DumbAwareAction {
private final Project myProject;
private final List<GitRepository> myRepositories;
@NotNull private final GitRepository mySelectedRepository;
NewBranchAction(@NotNull Project project, @NotNull List<GitRepository> repositories, @NotNull GitRepository selectedRepository) {
NewBranchAction(@NotNull Project project, @NotNull List<GitRepository> repositories) {
super("New Branch", "Create and checkout new branch", IconUtil.getAddIcon());
myProject = project;
myRepositories = repositories;
mySelectedRepository = selectedRepository;
}
@Override
public void actionPerformed(AnActionEvent e) {
final String name = GitBranchUiUtil.getNewBranchNameFromUser(myProject, myRepositories, "Create New Branch");
if (name != null) {
new GitBrancherImpl(myProject, myRepositories, mySelectedRepository).checkoutNewBranch(name);
new GitBrancherImpl(myProject, myRepositories).checkoutNewBranch(name);
}
}
@@ -194,11 +192,11 @@ class GitBranchPopupActions {
@Override
public AnAction[] getChildren(@Nullable AnActionEvent e) {
return new AnAction[] {
new CheckoutAction(myProject, myRepositories, myBranchName, mySelectedRepository),
new CheckoutAsNewBranch(myProject, myRepositories, myBranchName, mySelectedRepository),
new CheckoutAction(myProject, myRepositories, myBranchName),
new CheckoutAsNewBranch(myProject, myRepositories, myBranchName),
new CompareAction(myProject, myRepositories, myBranchName, mySelectedRepository),
new MergeAction(myProject, myRepositories, myBranchName, mySelectedRepository, true),
new DeleteAction(myProject, myRepositories, myBranchName, mySelectedRepository)
new MergeAction(myProject, myRepositories, myBranchName, true),
new DeleteAction(myProject, myRepositories, myBranchName)
};
}
@@ -206,20 +204,17 @@ class GitBranchPopupActions {
private final Project myProject;
private final List<GitRepository> myRepositories;
private final String myBranchName;
@NotNull private final GitRepository mySelectedRepository;
CheckoutAction(@NotNull Project project, @NotNull List<GitRepository> repositories, @NotNull String branchName,
@NotNull GitRepository selectedRepository) {
CheckoutAction(@NotNull Project project, @NotNull List<GitRepository> repositories, @NotNull String branchName) {
super("Checkout");
myProject = project;
myRepositories = repositories;
myBranchName = branchName;
mySelectedRepository = selectedRepository;
}
@Override
public void actionPerformed(AnActionEvent e) {
new GitBrancherImpl(myProject, myRepositories, mySelectedRepository).checkout(myBranchName);
new GitBrancherImpl(myProject, myRepositories).checkout(myBranchName);
}
}
@@ -228,15 +223,12 @@ class GitBranchPopupActions {
private final Project myProject;
private final List<GitRepository> myRepositories;
private final String myBranchName;
@NotNull private final GitRepository mySelectedRepository;
CheckoutAsNewBranch(@NotNull Project project, @NotNull List<GitRepository> repositories, @NotNull String branchName,
@NotNull GitRepository selectedRepository) {
CheckoutAsNewBranch(@NotNull Project project, @NotNull List<GitRepository> repositories, @NotNull String branchName) {
super("Checkout as new branch");
myProject = project;
myRepositories = repositories;
myBranchName = branchName;
mySelectedRepository = selectedRepository;
}
@Override
@@ -245,7 +237,7 @@ class GitBranchPopupActions {
.showInputDialog(myProject, "Enter name of new branch", "Checkout New Branch From " + myBranchName,
Messages.getQuestionIcon(), "", GitNewBranchNameValidator.newInstance(myRepositories));
if (name != null) {
new GitBrancherImpl(myProject, myRepositories, mySelectedRepository).checkoutNewBranchStartingFrom(name, myBranchName);
new GitBrancherImpl(myProject, myRepositories).checkoutNewBranchStartingFrom(name, myBranchName);
}
}
@@ -255,19 +247,17 @@ class GitBranchPopupActions {
private final Project myProject;
private final List<GitRepository> myRepositories;
private final String myBranchName;
private final GitRepository mySelectedRepository;
DeleteAction(Project project, List<GitRepository> repositories, String branchName, GitRepository selectedRepository) {
DeleteAction(Project project, List<GitRepository> repositories, String branchName) {
super("Delete");
myProject = project;
myRepositories = repositories;
myBranchName = branchName;
mySelectedRepository = selectedRepository;
}
@Override
public void actionPerformed(AnActionEvent e) {
new GitBrancherImpl(myProject, myRepositories, mySelectedRepository).deleteBranch(myBranchName);
new GitBrancherImpl(myProject, myRepositories).deleteBranch(myBranchName);
}
}
}
@@ -296,10 +286,10 @@ class GitBranchPopupActions {
@Override
public AnAction[] getChildren(@Nullable AnActionEvent e) {
return new AnAction[] {
new CheckoutRemoteBranchAction(myProject, myRepositories, myBranchName, mySelectedRepository),
new CheckoutRemoteBranchAction(myProject, myRepositories, myBranchName),
new CompareAction(myProject, myRepositories, myBranchName, mySelectedRepository),
new MergeAction(myProject, myRepositories, myBranchName, mySelectedRepository, false),
new RemoteDeleteAction(myProject, myRepositories, myBranchName, mySelectedRepository)
new MergeAction(myProject, myRepositories, myBranchName, false),
new RemoteDeleteAction(myProject, myRepositories, myBranchName)
};
}
@@ -307,15 +297,13 @@ class GitBranchPopupActions {
private final Project myProject;
private final List<GitRepository> myRepositories;
private final String myRemoteBranchName;
@NotNull private final GitRepository mySelectedRepository;
public CheckoutRemoteBranchAction(@NotNull Project project, @NotNull List<GitRepository> repositories,
@NotNull String remoteBranchName, @NotNull GitRepository selectedRepository) {
@NotNull String remoteBranchName) {
super("Checkout as new local branch");
myProject = project;
myRepositories = repositories;
myRemoteBranchName = remoteBranchName;
mySelectedRepository = selectedRepository;
}
@Override
@@ -323,7 +311,7 @@ class GitBranchPopupActions {
final String name = Messages.showInputDialog(myProject, "Enter name of new branch", "Checkout Remote Branch", Messages.getQuestionIcon(),
guessBranchName(), GitNewBranchNameValidator.newInstance(myRepositories));
if (name != null) {
new GitBrancherImpl(myProject, myRepositories, mySelectedRepository).checkoutNewBranchStartingFrom(name, myRemoteBranchName);
new GitBrancherImpl(myProject, myRepositories).checkoutNewBranchStartingFrom(name, myRemoteBranchName);
}
}
@@ -339,20 +327,17 @@ class GitBranchPopupActions {
private final Project myProject;
private final List<GitRepository> myRepositories;
private final String myBranchName;
private final GitRepository mySelectedRepository;
RemoteDeleteAction(@NotNull Project project, @NotNull List<GitRepository> repositories, @NotNull String branchName,
@NotNull GitRepository selectedRepository) {
RemoteDeleteAction(@NotNull Project project, @NotNull List<GitRepository> repositories, @NotNull String branchName) {
super("Delete");
myProject = project;
myRepositories = repositories;
myBranchName = branchName;
mySelectedRepository = selectedRepository;
}
@Override
public void actionPerformed(AnActionEvent e) {
new GitBrancherImpl(myProject, myRepositories, mySelectedRepository).deleteRemoteBranch(myBranchName);
new GitBrancherImpl(myProject, myRepositories).deleteRemoteBranch(myBranchName);
}
}
@@ -376,7 +361,7 @@ class GitBranchPopupActions {
@Override
public void actionPerformed(AnActionEvent e) {
new GitBrancherImpl(myProject, myRepositories, mySelectedRepository).compare(myBranchName);
new GitBrancherImpl(myProject, myRepositories).compare(myBranchName, mySelectedRepository);
}
}
@@ -386,22 +371,20 @@ class GitBranchPopupActions {
private final Project myProject;
private final List<GitRepository> myRepositories;
private final String myBranchName;
private final GitRepository mySelectedRepository;
private final boolean myLocalBranch;
public MergeAction(@NotNull Project project, @NotNull List<GitRepository> repositories, @NotNull String branchName,
@NotNull GitRepository selectedRepository, boolean localBranch) {
boolean localBranch) {
super("Merge");
myProject = project;
myRepositories = repositories;
myBranchName = branchName;
mySelectedRepository = selectedRepository;
myLocalBranch = localBranch;
}
@Override
public void actionPerformed(AnActionEvent e) {
new GitBrancherImpl(myProject, myRepositories, mySelectedRepository).merge(myBranchName, myLocalBranch);
new GitBrancherImpl(myProject, myRepositories).merge(myBranchName, myLocalBranch);
}
}
@@ -461,7 +461,7 @@ public class GitBranchOperationsTest extends AbstractVcsTestCase {
return DialogWrapper.CANCEL_EXIT_CODE;
}
});
GitBrancher processor = new GitBrancherImpl(myProject, singletonList(myCommunity), myCommunity);
GitBrancher processor = new GitBrancherImpl(myProject, singletonList(myCommunity));
Method method = GitBrancherImpl.class.getDeclaredMethod("doDelete", String.class, ProgressIndicator.class);
method.setAccessible(true);
method.invoke(processor, "feature", new EmptyProgressIndicator());
@@ -670,7 +670,7 @@ public class GitBranchOperationsTest extends AbstractVcsTestCase {
}
private void doMerge(String branch) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
GitBrancher processor = new GitBrancherImpl(myProject, myRepositories, myCommunity);
GitBrancher processor = new GitBrancherImpl(myProject, myRepositories);
Method method = GitBrancherImpl.class.getDeclaredMethod("doMerge", String.class, Boolean.TYPE, ProgressIndicator.class);
method.setAccessible(true);
method.invoke(processor, branch, true, new EmptyProgressIndicator());
@@ -707,7 +707,7 @@ public class GitBranchOperationsTest extends AbstractVcsTestCase {
private void callPrivateBranchOperationsProcessorMethod(String methodName, String branchName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// call private doCheckoutNewBranch instead of public checkoutNewBranch to avoid dealing with background process creation
// same for other branch operations
GitBrancher processor = new GitBrancherImpl(myProject, myRepositories, myCommunity);
GitBrancher processor = new GitBrancherImpl(myProject, myRepositories);
Method method = GitBrancherImpl.class.getDeclaredMethod(methodName, String.class, ProgressIndicator.class);
method.setAccessible(true);
method.invoke(processor, branchName, new EmptyProgressIndicator());
@@ -718,7 +718,7 @@ public class GitBranchOperationsTest extends AbstractVcsTestCase {
}
private void doCheckout(@NotNull String branchName, @Nullable String newBranch) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
GitBrancher processor = new GitBrancherImpl(myProject, myRepositories, myCommunity);
GitBrancher processor = new GitBrancherImpl(myProject, myRepositories);
Method doCheckout = GitBrancherImpl.class.getDeclaredMethod("doCheckout", ProgressIndicator.class, String.class, String.class);
doCheckout.setAccessible(true);
doCheckout.invoke(processor, new EmptyProgressIndicator(), branchName, newBranch);