From cda1048161e558e3db7aeff5fc35fa4474447f83 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Thu, 19 Jul 2018 14:25:49 +0300 Subject: [PATCH] git: extract method to get unmerged files --- .../src/git4idea/changes/GitChangeUtils.java | 24 +++++++++++ .../git4idea/merge/GitConflictResolver.java | 43 ++----------------- 2 files changed, 27 insertions(+), 40 deletions(-) diff --git a/plugins/git4idea/src/git4idea/changes/GitChangeUtils.java b/plugins/git4idea/src/git4idea/changes/GitChangeUtils.java index a04ddeaf5e3c..0f50ca7a4703 100644 --- a/plugins/git4idea/src/git4idea/changes/GitChangeUtils.java +++ b/plugins/git4idea/src/git4idea/changes/GitChangeUtils.java @@ -26,6 +26,7 @@ import com.intellij.openapi.vcs.changes.Change; import com.intellij.openapi.vcs.changes.ContentRevision; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.ArrayUtil; +import com.intellij.util.containers.ContainerUtil; import git4idea.GitContentRevision; import git4idea.GitRevisionNumber; import git4idea.GitUtil; @@ -417,6 +418,29 @@ public class GitChangeUtils { return changes; } + @NotNull + public static List getUnmergedFiles(@NotNull GitRepository repository) throws VcsException { + GitCommandResult result = Git.getInstance().getUnmergedFiles(repository); + if (!result.success()) { + throw new VcsException(result.getErrorOutputAsJoinedString()); + } + + String output = StringUtil.join(result.getOutput(), "\n"); + HashSet unmergedPaths = ContainerUtil.newHashSet(); + for (StringScanner s = new StringScanner(output); s.hasMoreData(); ) { + if (s.isEol()) { + s.nextLine(); + continue; + } + s.boundedToken('\t'); + String relative = s.line(); + unmergedPaths.add(GitUtil.unescapePath(relative)); + } + + VirtualFile root = repository.getRoot(); + return ContainerUtil.map(unmergedPaths, path -> new File(root.getPath(), path)); + } + @NotNull public static Collection getDiffWithWorkingDir(@NotNull Project project, @NotNull VirtualFile root, diff --git a/plugins/git4idea/src/git4idea/merge/GitConflictResolver.java b/plugins/git4idea/src/git4idea/merge/GitConflictResolver.java index 5e25097b15e7..d57c6cb5c817 100644 --- a/plugins/git4idea/src/git4idea/merge/GitConflictResolver.java +++ b/plugins/git4idea/src/git4idea/merge/GitConflictResolver.java @@ -17,14 +17,12 @@ import com.intellij.openapi.vcs.VcsNotifier; import com.intellij.openapi.vcs.merge.MergeDialogCustomizer; import com.intellij.openapi.vcs.merge.MergeProvider; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.util.containers.ContainerUtil; import git4idea.GitUtil; import git4idea.GitVcs; +import git4idea.changes.GitChangeUtils; import git4idea.commands.Git; -import git4idea.commands.GitCommandResult; import git4idea.repo.GitRepository; import git4idea.repo.GitRepositoryManager; -import git4idea.util.StringScanner; import org.jetbrains.annotations.CalledInBackground; import org.jetbrains.annotations.NotNull; @@ -260,48 +258,13 @@ public class GitConflictResolver { * @see #getUnmergedFiles(java.util.Collection */ private Collection getUnmergedFiles(@NotNull VirtualFile root) throws VcsException { - return unmergedFiles(root); - } - - /** - * Parse changes from lines - * - * - * @param root the git root - * @return a set of unmerged files - * @throws com.intellij.openapi.vcs.VcsException if the input format does not matches expected format - */ - private List unmergedFiles(final VirtualFile root) throws VcsException { GitRepository repository = myRepositoryManager.getRepositoryForRoot(root); if (repository == null) { LOG.error("Repository not found for root " + root); return Collections.emptyList(); } - GitCommandResult result = myGit.getUnmergedFiles(repository); - if (!result.success()) { - throw new VcsException(result.getErrorOutputAsJoinedString()); - } - - String output = StringUtil.join(result.getOutput(), "\n"); - HashSet unmergedPaths = ContainerUtil.newHashSet(); - for (StringScanner s = new StringScanner(output); s.hasMoreData();) { - if (s.isEol()) { - s.nextLine(); - continue; - } - s.boundedToken('\t'); - String relative = s.line(); - unmergedPaths.add(GitUtil.unescapePath(relative)); - } - - if (unmergedPaths.size() == 0) { - return Collections.emptyList(); - } - else { - List files = ContainerUtil.map(unmergedPaths, path -> new File(root.getPath(), path)); - return sortVirtualFilesByPresentation(findVirtualFilesWithRefresh(files)); - } + List files = GitChangeUtils.getUnmergedFiles(repository); + return sortVirtualFilesByPresentation(findVirtualFilesWithRefresh(files)); } - }