git: extract calculation of ranges of commits received during Update Project

GitOrigin-RevId: dc500e34261a80878b68cb26e647b12565e9e321
This commit is contained in:
Kirill Likhodedov
2019-06-09 15:08:51 +03:00
committed by intellij-monorepo-bot
parent 85b9a10ccd
commit d7eee72ec0
2 changed files with 69 additions and 40 deletions
@@ -169,7 +169,7 @@ public class GitUpdateProcess {
return GitUpdateResult.NOTHING_TO_UPDATE;
}
Map<GitRepository, Hash> 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<GitRepository, Hash> 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<GitRepository, Hash> calcPublishedTipPositions(@NotNull Map<GitRepository, GitBranchPair> trackedBranches) {
Map<GitRepository, Hash> 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<GitRepository, HashRange> getUpdatedRanges() {
return myUpdatedRanges;
@@ -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<GitUpdatedRanges>()
class GitUpdatedRanges private constructor(
private val project: Project,
private val sourceAndTargetBranches: Map<GitRepository, GitBranchPair>) {
private val initialPositions = calcPublishedTipPositions(sourceAndTargetBranches)
fun calcCurrentPositions(): Map<GitRepository, HashRange> {
val newPositions = calcPublishedTipPositions(sourceAndTargetBranches)
val result = LinkedHashMap<GitRepository, HashRange>()
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<GitRepository, GitBranchPair>): Map<GitRepository, Hash> {
val result = LinkedHashMap<GitRepository, Hash>()
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<GitRepository, GitBranchPair>): GitUpdatedRanges {
return GitUpdatedRanges(project, trackedBranches)
}
}
}