Search in sources :

Example 1 with GitRevisionNumber

use of git4idea.GitRevisionNumber in project intellij-community by JetBrains.

the class GitAnnotationProvider method parseAnnotations.

@NotNull
private GitFileAnnotation parseAnnotations(@Nullable VcsRevisionNumber revision, @NotNull VirtualFile file, @NotNull VirtualFile root, @NotNull String output) throws VcsException {
    Interner<FilePath> pathInterner = new Interner<>();
    try {
        List<LineInfo> lines = new ArrayList<>();
        HashMap<String, LineInfo> commits = new HashMap<>();
        for (StringScanner s = new StringScanner(output); s.hasMoreData(); ) {
            // parse header line
            String commitHash = s.spaceToken();
            if (commitHash.equals(GitRevisionNumber.NOT_COMMITTED_HASH)) {
                commitHash = null;
            }
            // skip revision line number
            s.spaceToken();
            String s1 = s.spaceToken();
            int lineNum = Integer.parseInt(s1);
            s.nextLine();
            // parse commit information
            LineInfo commit = commits.get(commitHash);
            if (commit != null || commitHash == null) {
                while (s.hasMoreData() && !s.startsWith('\t')) {
                    s.nextLine();
                }
            } else {
                Date committerDate = null;
                FilePath filePath = null;
                String subject = null;
                String authorName = null;
                String authorEmail = null;
                String previousRevision = null;
                FilePath previousFilePath = null;
                while (s.hasMoreData() && !s.startsWith('\t')) {
                    String key = s.spaceToken();
                    String value = s.line();
                    if (SUBJECT_KEY.equals(key)) {
                        subject = value;
                    } else if (AUTHOR_KEY.equals(key)) {
                        authorName = value;
                    } else if (COMMITTER_TIME_KEY.equals(key)) {
                        committerDate = GitUtil.parseTimestamp(value);
                    } else if (FILENAME_KEY.equals(key)) {
                        filePath = VcsUtil.getFilePath(root, value);
                    } else if (AUTHOR_EMAIL_KEY.equals(key)) {
                        authorEmail = value;
                        if (authorEmail.startsWith("<") && authorEmail.endsWith(">")) {
                            authorEmail = authorEmail.substring(1, authorEmail.length() - 1);
                        }
                    } else if (PREVIOUS_KEY.equals(key)) {
                        int index = value.indexOf(' ');
                        if (index != -1) {
                            previousRevision = value.substring(0, index);
                            previousFilePath = VcsUtil.getFilePath(root, value.substring(index + 1, value.length()));
                        }
                    }
                }
                if (committerDate == null || filePath == null || authorName == null || authorEmail == null || subject == null) {
                    throw new VcsException("Output for line " + lineNum + " lacks necessary data");
                }
                GitRevisionNumber revisionNumber = new GitRevisionNumber(commitHash, committerDate);
                VcsUser author = myUserRegistry.createUser(authorName, authorEmail);
                GitRevisionNumber previousRevisionNumber = previousRevision != null ? new GitRevisionNumber(previousRevision) : null;
                filePath = pathInterner.intern(filePath);
                if (previousFilePath != null)
                    previousFilePath = pathInterner.intern(previousFilePath);
                commit = new LineInfo(myProject, revisionNumber, filePath, committerDate, author, subject, previousRevisionNumber, previousFilePath);
                commits.put(commitHash, commit);
            }
            s.nextLine();
            int expectedLineNum = lines.size() + 1;
            if (lineNum != expectedLineNum) {
                throw new VcsException("Adding for info for line " + lineNum + " but we are expecting it to be for " + expectedLineNum);
            }
            lines.add(commit);
        }
        return new GitFileAnnotation(myProject, file, revision, lines);
    } catch (Exception e) {
        LOG.error("Couldn't parse annotation: " + e, new Attachment("output.txt", output));
        throw new VcsException(e);
    }
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) Interner(com.intellij.util.containers.Interner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Attachment(com.intellij.openapi.diagnostic.Attachment) LineInfo(git4idea.annotate.GitFileAnnotation.LineInfo) Date(java.util.Date) VcsException(com.intellij.openapi.vcs.VcsException) GitRevisionNumber(git4idea.GitRevisionNumber) VcsUser(com.intellij.vcs.log.VcsUser) VcsException(com.intellij.openapi.vcs.VcsException) StringScanner(git4idea.util.StringScanner) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GitRevisionNumber

use of git4idea.GitRevisionNumber in project intellij-community by JetBrains.

the class GitFileAnnotation method getToolTip.

@Nullable
@Override
public String getToolTip(int lineNumber) {
    LineInfo lineInfo = getLineInfo(lineNumber);
    if (lineInfo == null)
        return null;
    GitRevisionNumber revisionNumber = lineInfo.getRevisionNumber();
    VcsFileRevision fileRevision = null;
    if (myRevisions != null && myRevisionMap != null && myRevisionMap.contains(revisionNumber)) {
        fileRevision = myRevisions.get(myRevisionMap.get(revisionNumber));
    }
    String commitMessage = fileRevision != null ? fileRevision.getCommitMessage() : lineInfo.getSubject() + "\n...";
    return GitBundle.message("annotation.tool.tip", revisionNumber.asString(), lineInfo.getAuthor(), DateFormatUtil.formatDateTime(lineInfo.getDate()), commitMessage);
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GitRevisionNumber

use of git4idea.GitRevisionNumber in project intellij-community by JetBrains.

the class GitFileAnnotation method getPreviousFileRevisionProvider.

@Nullable
@Override
public PreviousFileRevisionProvider getPreviousFileRevisionProvider() {
    return new PreviousFileRevisionProvider() {

        @Nullable
        @Override
        public VcsFileRevision getPreviousRevision(int lineNumber) {
            LineInfo lineInfo = getLineInfo(lineNumber);
            if (lineInfo == null)
                return null;
            VcsFileRevision previousFileRevision = lineInfo.getPreviousFileRevision();
            if (previousFileRevision != null)
                return previousFileRevision;
            GitRevisionNumber revisionNumber = lineInfo.getRevisionNumber();
            if (myRevisions != null && myRevisionMap != null && myRevisionMap.contains(revisionNumber)) {
                int index = myRevisionMap.get(revisionNumber);
                if (index + 1 < myRevisions.size()) {
                    return myRevisions.get(index + 1);
                }
            }
            return null;
        }

        @Nullable
        @Override
        public VcsFileRevision getLastRevision() {
            if (myBaseRevision instanceof GitRevisionNumber) {
                return new GitFileRevision(myProject, VcsUtil.getFilePath(myFile), (GitRevisionNumber) myBaseRevision);
            } else {
                return ContainerUtil.getFirstItem(getRevisions());
            }
        }
    };
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) GitFileRevision(git4idea.GitFileRevision) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GitRevisionNumber

use of git4idea.GitRevisionNumber in project intellij-community by JetBrains.

the class GitOutgoingChangesProvider method filterLocalChangesBasedOnLocalCommits.

public Collection<Change> filterLocalChangesBasedOnLocalCommits(final Collection<Change> localChanges, final VirtualFile vcsRoot) throws VcsException {
    final GitBranchesSearcher searcher = new GitBranchesSearcher(myProject, vcsRoot, true);
    if (searcher.getLocal() == null || searcher.getRemote() == null) {
        // no information, better strict approach (see getOutgoingChanges() code)
        return new ArrayList<>(localChanges);
    }
    final GitRevisionNumber base;
    try {
        base = getMergeBase(myProject, vcsRoot, searcher.getLocal(), searcher.getRemote());
    } catch (VcsException e) {
        LOG.info(e);
        return new ArrayList<>(localChanges);
    }
    if (base == null) {
        // no information, better strict approach (see getOutgoingChanges() code)
        return new ArrayList<>(localChanges);
    }
    final List<Pair<SHAHash, Date>> hashes = GitHistoryUtils.onlyHashesHistory(myProject, VcsUtil.getFilePath(vcsRoot), vcsRoot, (base.asString() + "..HEAD"));
    // no local commits
    if (hashes.isEmpty())
        return Collections.emptyList();
    // optimization
    final String first = hashes.get(0).getFirst().getValue();
    final Set<String> localHashes = new HashSet<>();
    for (Pair<SHAHash, Date> hash : hashes) {
        localHashes.add(hash.getFirst().getValue());
    }
    final Collection<Change> result = new ArrayList<>();
    for (Change change : localChanges) {
        if (change.getBeforeRevision() != null) {
            final String changeBeforeRevision = change.getBeforeRevision().getRevisionNumber().asString().trim();
            if (first.equals(changeBeforeRevision) || localHashes.contains(changeBeforeRevision)) {
                result.add(change);
            }
        }
    }
    return result;
}
Also used : SHAHash(git4idea.history.browser.SHAHash) Change(com.intellij.openapi.vcs.changes.Change) GitBranchesSearcher(git4idea.GitBranchesSearcher) GitRevisionNumber(git4idea.GitRevisionNumber) Pair(com.intellij.openapi.util.Pair)

Example 5 with GitRevisionNumber

use of git4idea.GitRevisionNumber in project intellij-community by JetBrains.

the class GitOutgoingChangesProvider method getOutgoingChanges.

public Pair<VcsRevisionNumber, List<CommittedChangeList>> getOutgoingChanges(final VirtualFile vcsRoot, final boolean findRemote) throws VcsException {
    LOG.debug("getOutgoingChanges root: " + vcsRoot.getPath());
    final GitBranchesSearcher searcher = new GitBranchesSearcher(myProject, vcsRoot, findRemote);
    if (searcher.getLocal() == null || searcher.getRemote() == null) {
        return new Pair<>(null, Collections.<CommittedChangeList>emptyList());
    }
    final GitRevisionNumber base = getMergeBase(myProject, vcsRoot, searcher.getLocal(), searcher.getRemote());
    if (base == null) {
        return new Pair<>(null, Collections.<CommittedChangeList>emptyList());
    }
    final List<GitCommittedChangeList> lists = GitUtil.getLocalCommittedChanges(myProject, vcsRoot, new Consumer<GitSimpleHandler>() {

        public void consume(final GitSimpleHandler handler) {
            handler.addParameters(base.asString() + "..HEAD");
        }
    });
    return new Pair<>(base, ObjectsConvertor.convert(lists, new Convertor<GitCommittedChangeList, CommittedChangeList>() {

        @Override
        public CommittedChangeList convert(GitCommittedChangeList o) {
            return o;
        }
    }));
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) GitSimpleHandler(git4idea.commands.GitSimpleHandler) GitBranchesSearcher(git4idea.GitBranchesSearcher) Pair(com.intellij.openapi.util.Pair) Convertor(com.intellij.util.containers.Convertor)

Aggregations

GitRevisionNumber (git4idea.GitRevisionNumber)29 VcsException (com.intellij.openapi.vcs.VcsException)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 Change (com.intellij.openapi.vcs.changes.Change)8 NotNull (org.jetbrains.annotations.NotNull)8 Nullable (org.jetbrains.annotations.Nullable)8 FilePath (com.intellij.openapi.vcs.FilePath)4 GitSingleRepoTest (git4idea.test.GitSingleRepoTest)4 Test (org.testng.annotations.Test)4 ContentRevision (com.intellij.openapi.vcs.changes.ContentRevision)3 ObjectUtils.assertNotNull (com.intellij.util.ObjectUtils.assertNotNull)3 GitBranchesSearcher (git4idea.GitBranchesSearcher)3 GitSimpleHandler (git4idea.commands.GitSimpleHandler)3 GitRepository (git4idea.repo.GitRepository)3 StringScanner (git4idea.util.StringScanner)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Task (com.intellij.openapi.progress.Task)2 Pair (com.intellij.openapi.util.Pair)2