From 6190a392e35136540a2a51a36f95a76fa25d74fa Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Wed, 27 May 2015 16:11:54 +0300 Subject: [PATCH] [git] IDEA-95846 Check & define user name under progress + extract methods and DRY a similar check --- .../checkin/GitCheckinHandlerFactory.java | 129 ++++++++++-------- 1 file changed, 74 insertions(+), 55 deletions(-) diff --git a/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java b/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java index fda443a2186f..3b644caa04cc 100644 --- a/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java +++ b/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java @@ -27,6 +27,7 @@ import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Couple; import com.intellij.openapi.util.Pair; +import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vcs.CheckinProjectPanel; import com.intellij.openapi.vcs.FilePath; @@ -38,6 +39,7 @@ import com.intellij.openapi.vcs.checkin.CheckinHandler; import com.intellij.openapi.vcs.checkin.VcsCheckinHandlerFactory; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.PairConsumer; +import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.UIUtil; import com.intellij.xml.util.XmlStringUtil; import git4idea.GitPlatformFacade; @@ -176,33 +178,16 @@ public class GitCheckinHandlerFactory extends VcsCheckinHandlerFactory { } private ReturnResult checkUserName() { - Project project = myPanel.getProject(); + final Project project = myPanel.getProject(); GitVcs vcs = GitVcs.getInstance(project); assert vcs != null; - Collection notDefined = new ArrayList(); - Map> defined = new HashMap>(); - Collection allRoots = new ArrayList(Arrays.asList( - ProjectLevelVcsManager.getInstance(project).getRootsUnderVcs(vcs))); - Collection affectedRoots = getSelectedRoots(); - for (VirtualFile root : affectedRoots) { - try { - Couple nameAndEmail = getUserNameAndEmailFromGitConfig(project, root); - String name = nameAndEmail.getFirst(); - String email = nameAndEmail.getSecond(); - if (name == null || email == null) { - notDefined.add(root); - } - else { - defined.put(root, nameAndEmail); - } - } - catch (VcsException e) { - LOG.error("Couldn't get user.name and user.email for root " + root, e); - // doing nothing - let commit with possibly empty user.name/email - } - } + Map> defined = getDefinedUserNames(project, affectedRoots, false); + + Collection allRoots = new ArrayList(Arrays.asList(ProjectLevelVcsManager.getInstance(project).getRootsUnderVcs(vcs))); + Collection notDefined = new ArrayList(affectedRoots); + notDefined.removeAll(defined.keySet()); if (notDefined.isEmpty()) { return ReturnResult.COMMIT; @@ -219,48 +204,82 @@ public class GitCheckinHandlerFactory extends VcsCheckinHandlerFactory { return ReturnResult.CANCEL; } + // try to find a root with defined user name among other roots - to propose this user name in the dialog if (defined.isEmpty() && allRoots.size() > affectedRoots.size()) { allRoots.removeAll(affectedRoots); - for (VirtualFile root : allRoots) { + defined.putAll(getDefinedUserNames(project, allRoots, true)); + } + + final GitUserNameNotDefinedDialog dialog = new GitUserNameNotDefinedDialog(project, notDefined, affectedRoots, defined); + if (dialog.showAndGet()) { + return setUserNameUnderProgress(project, notDefined, dialog) ? ReturnResult.COMMIT : ReturnResult.CANCEL; + } + return ReturnResult.CLOSE_WINDOW; + } + + @NotNull + private Map> getDefinedUserNames(@NotNull final Project project, + @NotNull final Collection roots, + final boolean stopWhenFoundFirst) { + final Map> defined = ContainerUtil.newHashMap(); + ProgressManager.getInstance().run(new Task.Modal(project, "Checking Git user name...", false) { + @Override + public void run(@NotNull ProgressIndicator pi) { + for (VirtualFile root : roots) { + try { + Couple nameAndEmail = getUserNameAndEmailFromGitConfig(project, root); + String name = nameAndEmail.getFirst(); + String email = nameAndEmail.getSecond(); + if (name != null && email != null) { + defined.put(root, nameAndEmail); + if (stopWhenFoundFirst) { + return; + } + } + } + catch (VcsException e) { + LOG.error("Couldn't get user.name and user.email for root " + root, e); + // doing nothing - let commit with possibly empty user.name/email + } + } + } + }); + return defined; + } + + private boolean setUserNameUnderProgress(@NotNull final Project project, + @NotNull final Collection notDefined, + @NotNull final GitUserNameNotDefinedDialog dialog) { + final Ref error = Ref.create(); + ProgressManager.getInstance().run(new Task.Modal(project, "Setting Git User Name...", false) { + @Override + public void run(@NotNull ProgressIndicator pi) { try { - Couple nameAndEmail = getUserNameAndEmailFromGitConfig(project, root); - String name = nameAndEmail.getFirst(); - String email = nameAndEmail.getSecond(); - if (name != null && email != null) { - defined.put(root, nameAndEmail); - break; + if (dialog.isGlobal()) { + GitConfigUtil.setValue(project, notDefined.iterator().next(), GitConfigUtil.USER_NAME, dialog.getUserName(), "--global"); + GitConfigUtil.setValue(project, notDefined.iterator().next(), GitConfigUtil.USER_EMAIL, dialog.getUserEmail(), "--global"); + } + else { + for (VirtualFile root : notDefined) { + GitConfigUtil.setValue(project, root, GitConfigUtil.USER_NAME, dialog.getUserName()); + GitConfigUtil.setValue(project, root, GitConfigUtil.USER_EMAIL, dialog.getUserEmail()); + } } } catch (VcsException e) { - LOG.error("Couldn't get user.name and user.email for root " + root, e); - // doing nothing - not critical not to find the values for other roots not affected by commit + String message = "Couldn't set user.name and user.email"; + LOG.error(message, e); + error.set(message); } } + }); + if (error.isNull()) { + return true; } - - GitUserNameNotDefinedDialog dialog = new GitUserNameNotDefinedDialog(project, notDefined, affectedRoots, defined); - if (dialog.showAndGet()) { - try { - if (dialog.isGlobal()) { - GitConfigUtil.setValue(project, notDefined.iterator().next(), GitConfigUtil.USER_NAME, dialog.getUserName(), "--global"); - GitConfigUtil.setValue(project, notDefined.iterator().next(), GitConfigUtil.USER_EMAIL, dialog.getUserEmail(), "--global"); - } - else { - for (VirtualFile root : notDefined) { - GitConfigUtil.setValue(project, root, GitConfigUtil.USER_NAME, dialog.getUserName()); - GitConfigUtil.setValue(project, root, GitConfigUtil.USER_EMAIL, dialog.getUserEmail()); - } - } - } - catch (VcsException e) { - String message = "Couldn't set user.name and user.email"; - LOG.error(message, e); - Messages.showErrorDialog(myPanel.getComponent(), message); - return ReturnResult.CANCEL; - } - return ReturnResult.COMMIT; + else { + Messages.showErrorDialog(myPanel.getComponent(), error.get()); + return false; } - return ReturnResult.CLOSE_WINDOW; } @NotNull