Search in sources :

Example 1 with IGitCoreHeadSnapshot

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

the class GitCoreRepository method deriveHead.

@Override
public IGitCoreHeadSnapshot deriveHead() throws GitCoreException {
    Ref ref = Try.of(() -> jgitRepo.getRefDatabase().findRef(Constants.HEAD)).getOrElseThrow(e -> new GitCoreException("Cannot get current branch", e));
    if (ref == null) {
        throw new GitCoreException("Error occurred while getting current branch ref");
    }
    val reflog = deriveReflogByRefFullName(Constants.HEAD);
    String currentBranchName = null;
    if (ref.isSymbolic()) {
        currentBranchName = Repository.shortenRefName(ref.getTarget().getName());
    } else {
        Option<Path> headNamePath = Stream.of("rebase-apply", "rebase-merge").map(dir -> jgitRepo.getDirectory().toPath().resolve(dir).resolve("head-name")).find(path -> path.toFile().isFile());
        if (headNamePath.isDefined()) {
            currentBranchName = Try.of(() -> Stream.ofAll(Files.readAllLines(headNamePath.get()))).getOrElseThrow(e -> new GitCoreException("Error occurred while getting current branch ref", e)).headOption().map(Repository::shortenRefName).getOrNull();
        }
    }
    IGitCoreLocalBranchSnapshot targetBranch;
    if (currentBranchName != null) {
        targetBranch = deriveLocalBranchByName(currentBranchName).getOrNull();
    } else {
        targetBranch = null;
    }
    return new GitCoreHeadSnapshot(targetBranch, reflog);
}
Also used : lombok.val(lombok.val) Path(java.nio.file.Path) 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) Ref(org.eclipse.jgit.lib.Ref) IGitCoreRepository(com.virtuslab.gitcore.api.IGitCoreRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) GitCoreException(com.virtuslab.gitcore.api.GitCoreException) ToString(lombok.ToString) IGitCoreLocalBranchSnapshot(com.virtuslab.gitcore.api.IGitCoreLocalBranchSnapshot) IGitCoreHeadSnapshot(com.virtuslab.gitcore.api.IGitCoreHeadSnapshot)

Aggregations

GitCoreCannotAccessGitDirectoryException (com.virtuslab.gitcore.api.GitCoreCannotAccessGitDirectoryException)1 GitCoreException (com.virtuslab.gitcore.api.GitCoreException)1 GitCoreNoSuchRevisionException (com.virtuslab.gitcore.api.GitCoreNoSuchRevisionException)1 GitCoreRelativeCommitCount (com.virtuslab.gitcore.api.GitCoreRelativeCommitCount)1 GitCoreRepositoryState (com.virtuslab.gitcore.api.GitCoreRepositoryState)1 IGitCoreCommit (com.virtuslab.gitcore.api.IGitCoreCommit)1 IGitCoreHeadSnapshot (com.virtuslab.gitcore.api.IGitCoreHeadSnapshot)1 IGitCoreLocalBranchSnapshot (com.virtuslab.gitcore.api.IGitCoreLocalBranchSnapshot)1 IGitCoreReflogEntry (com.virtuslab.gitcore.api.IGitCoreReflogEntry)1 IGitCoreRepository (com.virtuslab.gitcore.api.IGitCoreRepository)1 BranchFullNameUtils.getLocalBranchFullName (com.virtuslab.gitcore.impl.jgit.BranchFullNameUtils.getLocalBranchFullName)1 BranchFullNameUtils.getRemoteBranchFullName (com.virtuslab.gitcore.impl.jgit.BranchFullNameUtils.getRemoteBranchFullName)1 API.$ (io.vavr.API.$)1 Case (io.vavr.API.Case)1 Match (io.vavr.API.Match)1 CheckedFunction1 (io.vavr.CheckedFunction1)1 Predicates.isIn (io.vavr.Predicates.isIn)1 Tuple (io.vavr.Tuple)1 Tuple2 (io.vavr.Tuple2)1 Iterator (io.vavr.collection.Iterator)1