Search in sources :

Example 1 with FormatResult

use of org.eclipse.egit.ui.internal.history.FormatJob.FormatResult in project egit by eclipse.

the class CommitInfoBuilder method format.

/**
 * Retrieves and formats the commit info.
 *
 * @param monitor
 *            for progress reporting and cancellation
 * @return formatted commit info
 * @throws IOException
 */
public FormatResult format(IProgressMonitor monitor) throws IOException {
    boolean trace = GitTraceLocation.HISTORYVIEW.isActive();
    if (trace)
        GitTraceLocation.getTrace().traceEntry(GitTraceLocation.HISTORYVIEW.getLocation());
    monitor.setTaskName(UIText.CommitMessageViewer_FormattingMessageTaskName);
    final StringBuilder d = new StringBuilder();
    final PersonIdent author = commit.getAuthorIdent();
    final PersonIdent committer = commit.getCommitterIdent();
    List<GitCommitReference> hyperlinks = new ArrayList<>();
    d.append(UIText.CommitMessageViewer_commit);
    d.append(' ');
    d.append(commit.getId().name());
    d.append(LF);
    addPersonIdent(d, author, UIText.CommitMessageViewer_author);
    addPersonIdent(d, committer, UIText.CommitMessageViewer_committer);
    for (int i = 0; i < commit.getParentCount(); i++) {
        addCommit(d, (SWTCommit) commit.getParent(i), UIText.CommitMessageViewer_parent, hyperlinks);
    }
    for (int i = 0; i < commit.getChildCount(); i++) {
        addCommit(d, (SWTCommit) commit.getChild(i), UIText.CommitMessageViewer_child, hyperlinks);
    }
    if (Activator.getDefault().getPreferenceStore().getBoolean(UIPreferences.HISTORY_SHOW_BRANCH_SEQUENCE)) {
        try (RevWalk rw = new RevWalk(db)) {
            List<Ref> branches = getBranches(commit, allRefs, db);
            if (!branches.isEmpty()) {
                d.append(UIText.CommitMessageViewer_branches);
                // $NON-NLS-1$
                d.append(": ");
                int count = 0;
                for (Iterator<Ref> i = branches.iterator(); i.hasNext(); ) {
                    Ref head = i.next();
                    RevCommit p;
                    p = rw.parseCommit(head.getObjectId());
                    addLink(d, formatHeadRef(head), hyperlinks, p);
                    if (i.hasNext()) {
                        if (count++ <= MAXBRANCHES) {
                            // $NON-NLS-1$
                            d.append(", ");
                        } else {
                            d.append(NLS.bind(UIText.CommitMessageViewer_MoreBranches, Integer.valueOf(branches.size() - MAXBRANCHES)));
                            break;
                        }
                    }
                }
                d.append(LF);
            }
        } catch (IOException e) {
            Activator.logError(e.getMessage(), e);
        }
    }
    String tagsString = getTagsString();
    if (tagsString.length() > 0) {
        d.append(UIText.CommitMessageViewer_tags);
        // $NON-NLS-1$
        d.append(": ");
        d.append(tagsString);
        d.append(LF);
    }
    if (Activator.getDefault().getPreferenceStore().getBoolean(UIPreferences.HISTORY_SHOW_TAG_SEQUENCE)) {
        try (RevWalk rw = new RevWalk(db)) {
            monitor.setTaskName(UIText.CommitMessageViewer_GettingPreviousTagTaskName);
            addTag(d, UIText.CommitMessageViewer_follows, rw, getNextTag(false, monitor), hyperlinks);
        } catch (IOException e) {
            Activator.logError(e.getMessage(), e);
        }
        try (RevWalk rw = new RevWalk(db)) {
            monitor.setTaskName(UIText.CommitMessageViewer_GettingNextTagTaskName);
            addTag(d, UIText.CommitMessageViewer_precedes, rw, getNextTag(true, monitor), hyperlinks);
        } catch (IOException e) {
            Activator.logError(e.getMessage(), e);
        }
    }
    d.append(LF);
    int headerEnd = d.length();
    String msg = commit.getFullMessage().trim();
    // Find start of footer:
    int footerStart = CommonUtils.getFooterOffset(msg);
    if (footerStart >= 0) {
        if (fill) {
            String footer = msg.substring(footerStart);
            msg = msg.substring(0, footerStart);
            msg = // $NON-NLS-1$ //$NON-NLS-2$
            msg.replaceAll("([\\w.,; \t])\n(\\w)", "$1 $2") + footer;
            footerStart = headerEnd + msg.length() - footer.length();
        } else {
            footerStart = headerEnd + footerStart;
        }
    } else if (fill) {
        // $NON-NLS-1$ //$NON-NLS-2$
        msg = msg.replaceAll("([\\w.,; \t])\n(\\w)", "$1 $2");
    }
    d.append(msg);
    if (!msg.endsWith(LF))
        d.append(LF);
    if (trace)
        GitTraceLocation.getTrace().traceExit(GitTraceLocation.HISTORYVIEW.getLocation());
    return new FormatResult(d.toString(), hyperlinks, headerEnd, footerStart >= 0 ? footerStart : d.length());
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Ref(org.eclipse.jgit.lib.Ref) PersonIdent(org.eclipse.jgit.lib.PersonIdent) RevCommit(org.eclipse.jgit.revwalk.RevCommit) FormatResult(org.eclipse.egit.ui.internal.history.FormatJob.FormatResult)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 FormatResult (org.eclipse.egit.ui.internal.history.FormatJob.FormatResult)1 PersonIdent (org.eclipse.jgit.lib.PersonIdent)1 Ref (org.eclipse.jgit.lib.Ref)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1