Search in sources :

Example 6 with GitCoreException

use of com.virtuslab.gitcore.api.GitCoreException in project git-machete-intellij-plugin by VirtusLab.

the class GitCoreRepository method deriveAllLocalBranches.

@Override
public List<IGitCoreLocalBranchSnapshot> deriveAllLocalBranches() throws GitCoreException {
    LOG.debug(() -> "Entering: this = ${this}");
    LOG.debug("List of local branches:");
    List<Try<GitCoreLocalBranchSnapshot>> result = Try.of(() -> jgitRepo.getRefDatabase().getRefsByPrefix(Constants.R_HEADS)).getOrElseThrow(e -> new GitCoreException("Error while getting list of local branches", e)).stream().filter(branch -> !branch.getName().equals(Constants.HEAD)).map(ref -> Try.of(() -> {
        String localBranchFullName = ref.getName();
        LOG.debug(() -> "* " + localBranchFullName);
        String localBranchName = localBranchFullName.replace(Constants.R_HEADS, /* replacement */
        "");
        val objectId = ref.getObjectId();
        if (objectId == null) {
            throw new GitCoreException("Cannot access git object id corresponding to ${localBranchFullName}");
        }
        val pointedCommit = convertObjectIdToGitCoreCommit(objectId);
        val reflog = deriveReflogByRefFullName(localBranchFullName);
        val remoteBranch = deriveRemoteBranchForLocalBranch(localBranchName).getOrNull();
        return new GitCoreLocalBranchSnapshot(localBranchName, pointedCommit, reflog, remoteBranch);
    })).collect(List.collector());
    return List.narrow(Try.sequence(result).getOrElseThrow(GitCoreException::getOrWrap).toList().sortBy(b -> b.getName()));
}
Also used : Match(io.vavr.API.Match) RevCommit(org.eclipse.jgit.revwalk.RevCommit) SneakyThrows(lombok.SneakyThrows) ReflogReader(org.eclipse.jgit.lib.ReflogReader) GitCoreRepositoryState(com.virtuslab.gitcore.api.GitCoreRepositoryState) IGitCoreHeadSnapshot(com.virtuslab.gitcore.api.IGitCoreHeadSnapshot) CheckedFunction1(io.vavr.CheckedFunction1) RepositoryState(org.eclipse.jgit.lib.RepositoryState) List(io.vavr.collection.List) IGitCoreLocalBranchSnapshot(com.virtuslab.gitcore.api.IGitCoreLocalBranchSnapshot) Predicates.isIn(io.vavr.Predicates.isIn) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CONFIG_KEY_MERGE(org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_MERGE) Option(io.vavr.control.Option) IGitCoreRepository(com.virtuslab.gitcore.api.IGitCoreRepository) ToString(lombok.ToString) Iterator(io.vavr.collection.Iterator) Stream(io.vavr.collection.Stream) Path(java.nio.file.Path) GitCoreNoSuchRevisionException(com.virtuslab.gitcore.api.GitCoreNoSuchRevisionException) API.$(io.vavr.API.$) BranchFullNameUtils.getLocalBranchFullName(com.virtuslab.gitcore.impl.jgit.BranchFullNameUtils.getLocalBranchFullName) Tuple(io.vavr.Tuple) RevFilter(org.eclipse.jgit.revwalk.filter.RevFilter) Files(java.nio.file.Files) CONFIG_BRANCH_SECTION(org.eclipse.jgit.lib.ConfigConstants.CONFIG_BRANCH_SECTION) CONFIG_KEY_REMOTE(org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_REMOTE) GitCoreCannotAccessGitDirectoryException(com.virtuslab.gitcore.api.GitCoreCannotAccessGitDirectoryException) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) lombok.val(lombok.val) BranchFullNameUtils.getRemoteBranchFullName(com.virtuslab.gitcore.impl.jgit.BranchFullNameUtils.getRemoteBranchFullName) RevSort(org.eclipse.jgit.revwalk.RevSort) Case(io.vavr.API.Case) IOException(java.io.IOException) CustomLog(lombok.CustomLog) Constants(org.eclipse.jgit.lib.Constants) IGitCoreCommit(com.virtuslab.gitcore.api.IGitCoreCommit) GitCoreRelativeCommitCount(com.virtuslab.gitcore.api.GitCoreRelativeCommitCount) RevWalkUtils(org.eclipse.jgit.revwalk.RevWalkUtils) ObjectId(org.eclipse.jgit.lib.ObjectId) Try(io.vavr.control.Try) Tuple2(io.vavr.Tuple2) Ref(org.eclipse.jgit.lib.Ref) Unique(org.checkerframework.common.aliasing.qual.Unique) Repository(org.eclipse.jgit.lib.Repository) GitCoreException(com.virtuslab.gitcore.api.GitCoreException) IGitCoreReflogEntry(com.virtuslab.gitcore.api.IGitCoreReflogEntry) lombok.val(lombok.val) GitCoreException(com.virtuslab.gitcore.api.GitCoreException) IGitCoreLocalBranchSnapshot(com.virtuslab.gitcore.api.IGitCoreLocalBranchSnapshot) Try(io.vavr.control.Try) ToString(lombok.ToString)

Aggregations

GitCoreException (com.virtuslab.gitcore.api.GitCoreException)6 lombok.val (lombok.val)5 IGitCoreCommit (com.virtuslab.gitcore.api.IGitCoreCommit)3 GitMacheteException (com.virtuslab.gitmachete.backend.api.GitMacheteException)3 UIThreadUnsafe (com.virtuslab.qual.guieffect.UIThreadUnsafe)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 GitCoreCannotAccessGitDirectoryException (com.virtuslab.gitcore.api.GitCoreCannotAccessGitDirectoryException)2 GitCoreNoSuchRevisionException (com.virtuslab.gitcore.api.GitCoreNoSuchRevisionException)2 GitCoreRelativeCommitCount (com.virtuslab.gitcore.api.GitCoreRelativeCommitCount)2 GitCoreRepositoryState (com.virtuslab.gitcore.api.GitCoreRepositoryState)2 IGitCoreHeadSnapshot (com.virtuslab.gitcore.api.IGitCoreHeadSnapshot)2 IGitCoreLocalBranchSnapshot (com.virtuslab.gitcore.api.IGitCoreLocalBranchSnapshot)2 IGitCoreReflogEntry (com.virtuslab.gitcore.api.IGitCoreReflogEntry)2 IGitCoreRepository (com.virtuslab.gitcore.api.IGitCoreRepository)2 BranchFullNameUtils.getLocalBranchFullName (com.virtuslab.gitcore.impl.jgit.BranchFullNameUtils.getLocalBranchFullName)2 BranchFullNameUtils.getRemoteBranchFullName (com.virtuslab.gitcore.impl.jgit.BranchFullNameUtils.getRemoteBranchFullName)2 API.$ (io.vavr.API.$)2 Case (io.vavr.API.Case)2 Match (io.vavr.API.Match)2