From d7eee72ec0d60a49056c045638aac9875d48e336 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Sat, 8 Jun 2019 11:31:48 +0100 Subject: [PATCH] git: extract calculation of ranges of commits received during Update Project GitOrigin-RevId: dc500e34261a80878b68cb26e647b12565e9e321 --- .../src/git4idea/update/GitUpdateProcess.java | 42 +----------- .../src/git4idea/update/GitUpdatedRanges.kt | 67 +++++++++++++++++++ 2 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 plugins/git4idea/src/git4idea/update/GitUpdatedRanges.kt diff --git a/plugins/git4idea/src/git4idea/update/GitUpdateProcess.java b/plugins/git4idea/src/git4idea/update/GitUpdateProcess.java index a03ea2a20e28..b482621f4819 100644 --- a/plugins/git4idea/src/git4idea/update/GitUpdateProcess.java +++ b/plugins/git4idea/src/git4idea/update/GitUpdateProcess.java @@ -169,7 +169,7 @@ public class GitUpdateProcess { return GitUpdateResult.NOTHING_TO_UPDATE; } - Map previousPublishedTipPositions = calcPublishedTipPositions(trackedBranches); + GitUpdatedRanges updatedRanges = GitUpdatedRanges.calcInitialPositions(myProject, trackedBranches); try { updaters = tryFastForwardMergeForRebaseUpdaters(updaters); @@ -248,15 +248,7 @@ public class GitUpdateProcess { return ObjectUtils.notNull(compoundResult.get(), GitUpdateResult.ERROR); } finally { - Map newPublishedTipPositions = calcPublishedTipPositions(trackedBranches); - myUpdatedRanges = new LinkedHashMap<>(); - for (GitRepository repository : newPublishedTipPositions.keySet()) { - Hash before = previousPublishedTipPositions.get(repository); - if (before != null) { - Hash after = newPublishedTipPositions.get(repository); - myUpdatedRanges.put(repository, new HashRange(before, after)); - } - } + myUpdatedRanges = updatedRanges.calcCurrentPositions(); } } @@ -324,36 +316,6 @@ public class GitUpdateProcess { return mySkippedRoots; } - @NotNull - private Map calcPublishedTipPositions(@NotNull Map trackedBranches) { - Map result = new LinkedHashMap<>(); - for (GitRepository repository : trackedBranches.keySet()) { - GitLocalBranch localBranch = trackedBranches.get(repository).getSource(); - GitRemoteBranch trackedBranch = trackedBranches.get(repository).getTarget(); - if (trackedBranch != null) { - Hash mergeBase = getMergeBase(repository.getRoot(), localBranch.getFullName(), trackedBranch.getFullName()); - if (mergeBase != null) { - result.put(repository, mergeBase); - } - } - } - return result; - } - - @Nullable - private Hash getMergeBase(@NotNull VirtualFile root, @NotNull String firstRef, @NotNull String secondRef) { - GitLineHandler h = new GitLineHandler(myProject, root, GitCommand.MERGE_BASE); - h.addParameters(firstRef, secondRef); - try { - String output = Git.getInstance().runCommand(h).getOutputOrThrow().trim(); - return HashImpl.build(output); - } - catch (Throwable t) { - LOG.warn("Couldn't find merge-base between " + firstRef + " and " + secondRef); - return null; - } - } - @Nullable public Map getUpdatedRanges() { return myUpdatedRanges; diff --git a/plugins/git4idea/src/git4idea/update/GitUpdatedRanges.kt b/plugins/git4idea/src/git4idea/update/GitUpdatedRanges.kt new file mode 100644 index 000000000000..d5c4a976ef0d --- /dev/null +++ b/plugins/git4idea/src/git4idea/update/GitUpdatedRanges.kt @@ -0,0 +1,67 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package git4idea.update + +import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.vcs.log.Hash +import com.intellij.vcs.log.impl.HashImpl +import git4idea.branch.GitBranchPair +import git4idea.commands.Git +import git4idea.commands.GitCommand +import git4idea.commands.GitLineHandler +import git4idea.repo.GitRepository +import java.util.* + +private val LOG = logger() + +class GitUpdatedRanges private constructor( + private val project: Project, + private val sourceAndTargetBranches: Map) { + + private val initialPositions = calcPublishedTipPositions(sourceAndTargetBranches) + + fun calcCurrentPositions(): Map { + val newPositions = calcPublishedTipPositions(sourceAndTargetBranches) + val result = LinkedHashMap() + for ((repository, newHash) in newPositions.entries) { + val before = initialPositions[repository] + if (before != null) { + result[repository] = HashRange(before, newHash) + } + } + return result + } + + private fun calcPublishedTipPositions(trackedBranches: Map): Map { + val result = LinkedHashMap() + for ((repository, branchPair) in trackedBranches.entries) { + val (localBranch, trackedBranch) = branchPair + val mergeBase = getMergeBase(repository.root, localBranch.fullName, trackedBranch.fullName) + if (mergeBase != null) { + result[repository] = mergeBase + } + } + return result + } + + private fun getMergeBase(root: VirtualFile, firstRef: String, secondRef: String): Hash? { + val h = GitLineHandler(project, root, GitCommand.MERGE_BASE) + h.addParameters(firstRef, secondRef) + try { + val output = Git.getInstance().runCommand(h).getOutputOrThrow().trim() + return HashImpl.build(output) + } + catch (t: Throwable) { + LOG.warn("Couldn't find merge-base between $firstRef and $secondRef") + return null + } + } + + companion object { + @JvmStatic + fun calcInitialPositions(project: Project, trackedBranches: Map): GitUpdatedRanges { + return GitUpdatedRanges(project, trackedBranches) + } + } +} \ No newline at end of file