Example usage for org.eclipse.jgit.lib BranchTrackingStatus getAheadCount

List of usage examples for org.eclipse.jgit.lib BranchTrackingStatus getAheadCount

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib BranchTrackingStatus getAheadCount.

Prototype

public int getAheadCount() 

Source Link

Document

Get number of commits that the local branch is ahead of the remote-tracking branch

Usage

From source file:com.buildautomation.jgit.api.ShowBranchTrackingStatus.java

License:Apache License

private static List<Integer> getCounts(org.eclipse.jgit.lib.Repository repository, String branchName)
        throws IOException {
    BranchTrackingStatus trackingStatus = BranchTrackingStatus.of(repository, branchName);
    List<Integer> counts = new ArrayList<>();
    if (trackingStatus != null) {
        counts.add(trackingStatus.getAheadCount());
        counts.add(trackingStatus.getBehindCount());
    } else {/*from   w w  w  .j a v a 2 s.  c  o m*/
        System.out.println("Returned null, likely no remote tracking of branch " + branchName);
        counts.add(0);
        counts.add(0);
    }
    return counts;
}

From source file:org.eclipse.egit.ui.internal.components.SimplePushSpecPage.java

License:Open Source License

/**
 * pre-fills the destination box with a remote ref name if one exists that
 * matches the local branch name./*from w w w. j  ava 2s  . com*/
 */
protected void updateDestinationField() {
    setMessage(NLS.bind(UIText.SimplePushSpecPage_message, sourceName));
    String checkRemote = sourceName;

    if (sourceName.startsWith(Constants.R_HEADS)) {
        try {
            BranchTrackingStatus status = BranchTrackingStatus.of(repository,
                    sourceName.substring(Constants.R_HEADS.length()));

            if (status != null) {
                // calculate the name of the branch on the other side.
                checkRemote = status.getRemoteTrackingBranch();
                checkRemote = Constants.R_HEADS
                        + checkRemote.substring(checkRemote.indexOf('/', Constants.R_REMOTES.length() + 1) + 1);

                setMessage(
                        NLS.bind(UIText.SimplePushSpecPage_pushAheadInfo, new Object[] { sourceName,
                                Integer.valueOf(status.getAheadCount()), status.getRemoteTrackingBranch() }),
                        IStatus.INFO);
            }
        } catch (Exception e) {
            // ignore and continue...
        }

        if (assist == null) {
            if (checkRemote != null)
                remoteRefName.setText(checkRemote);
            return;
        }

        if (checkRemote == null)
            checkRemote = sourceName;

        for (Ref ref : assist.getRefsForContentAssist(false, true))
            if (ref.getName().equals(checkRemote))
                remoteRefName.setText(checkRemote);
    }
}

From source file:org.eclipse.egit.ui.internal.decorators.DecoratableResourceHelper.java

License:Open Source License

static String getBranchStatus(Repository repo) throws IOException {
    String branchName = repo.getBranch();
    if (branchName == null)
        return null;

    BranchTrackingStatus status = BranchTrackingStatus.of(repo, branchName);
    if (status == null)
        return null;

    if (status.getAheadCount() == 0 && status.getBehindCount() == 0)
        return null;

    String formattedStatus = GitLabelProvider.formatBranchTrackingStatus(status);
    return formattedStatus;
}

From source file:org.eclipse.egit.ui.internal.GitLabelProvider.java

License:Open Source License

/**
 * Format the branch tracking status suitable for displaying in decorations and labels.
 *
 * @param status/*from   w  ww. j ava 2 s. c om*/
 * @return the branch tracking status as a string
 */
public static String formatBranchTrackingStatus(BranchTrackingStatus status) {
    StringBuilder sb = new StringBuilder();
    int ahead = status.getAheadCount();
    int behind = status.getBehindCount();
    if (ahead != 0) {
        // UPWARDS ARROW
        sb.append('\u2191');
        sb.append(ahead);
    }
    if (behind != 0) {
        if (sb.length() != 0)
            sb.append(' ');
        // DOWNWARDS ARROW
        sb.append('\u2193');
        sb.append(status.getBehindCount());
    }
    return sb.toString();
}

From source file:org.eclipse.egit.ui.internal.GitLabelProvider.java

License:Open Source License

/**
 * @param repository/*from w  w  w.j a va  2s .  c o  m*/
 * @return a styled string for the repository
 * @throws IOException
 */
protected StyledString getStyledTextFor(Repository repository) throws IOException {
    File directory = repository.getDirectory();
    StyledString string = new StyledString();
    if (!repository.isBare())
        string.append(directory.getParentFile().getName());
    else
        string.append(directory.getName());

    String branch = Activator.getDefault().getRepositoryUtil().getShortBranch(repository);
    if (branch != null) {
        string.append(' ');
        string.append('[', StyledString.DECORATIONS_STYLER);
        string.append(branch, StyledString.DECORATIONS_STYLER);

        RepositoryState repositoryState = repository.getRepositoryState();
        if (repositoryState != RepositoryState.SAFE) {
            string.append(" - ", StyledString.DECORATIONS_STYLER); //$NON-NLS-1$
            string.append(repositoryState.getDescription(), StyledString.DECORATIONS_STYLER);
        }

        BranchTrackingStatus trackingStatus = BranchTrackingStatus.of(repository, branch);
        if (trackingStatus != null
                && (trackingStatus.getAheadCount() != 0 || trackingStatus.getBehindCount() != 0)) {
            String formattedTrackingStatus = formatBranchTrackingStatus(trackingStatus);
            string.append(' ');
            string.append(formattedTrackingStatus, StyledString.DECORATIONS_STYLER);
        }
        string.append(']', StyledString.DECORATIONS_STYLER);
    }

    string.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
    string.append(directory.getAbsolutePath(), StyledString.QUALIFIER_STYLER);

    return string;
}

From source file:org.eclipse.egit.ui.internal.GitLabels.java

License:Open Source License

/**
 * Computes detailed repository label that consists of repository name,
 * state, checked-out branch and it's status (returned by
 * {@linkplain #formatBranchTrackingStatus(BranchTrackingStatus)})
 *
 * @param repository//from   ww  w  .  ja  va2 s.c om
 * @return a styled string for the repository
 * @throws IOException
 */
public static @NonNull StyledString getStyledLabel(@NonNull Repository repository) throws IOException {
    RepositoryUtil repositoryUtil = Activator.getDefault().getRepositoryUtil();

    StyledString string = getChangedPrefix(repository);

    string.append(repositoryUtil.getRepositoryName(repository));

    String branch = repositoryUtil.getShortBranch(repository);
    if (branch != null) {
        string.append(' ');
        string.append('[', StyledString.DECORATIONS_STYLER);
        string.append(branch, StyledString.DECORATIONS_STYLER);

        BranchTrackingStatus trackingStatus = BranchTrackingStatus.of(repository, branch);
        if (trackingStatus != null
                && (trackingStatus.getAheadCount() != 0 || trackingStatus.getBehindCount() != 0)) {
            String formattedTrackingStatus = GitLabels.formatBranchTrackingStatus(trackingStatus);
            string.append(' ');
            string.append(formattedTrackingStatus, StyledString.DECORATIONS_STYLER);
        }

        RepositoryState repositoryState = repository.getRepositoryState();
        if (repositoryState != RepositoryState.SAFE) {
            string.append(" - ", StyledString.DECORATIONS_STYLER); //$NON-NLS-1$
            string.append(repositoryState.getDescription(), StyledString.DECORATIONS_STYLER);
        }
        string.append(']', StyledString.DECORATIONS_STYLER);
    }

    return string;
}

From source file:org.eclipse.oomph.gitbash.decorators.BranchDecorator.java

License:Open Source License

private String getDecoration(org.eclipse.egit.ui.internal.repository.tree.RefNode node) {
    String branchName = Repository.shortenRefName(node.getObject().getName());
    Repository repository = node.getRepository();
    StoredConfig config = repository.getConfig();

    String branch = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
            ConfigConstants.CONFIG_KEY_MERGE);

    if (branch != null) {
        String remote = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
                ConfigConstants.CONFIG_KEY_REMOTE);

        boolean rebaseFlag = config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
                ConfigConstants.CONFIG_KEY_REBASE, false);

        if (branch.startsWith(DEFAULT_PATH)) {
            branch = branch.substring(DEFAULT_PATH.length());
        }//  w ww.j a v  a  2 s . co  m

        String prefix = ".".equals(remote) ? "" : remote + "/";
        String result = (rebaseFlag ? "REBASE" : "MERGE") + ": " + prefix + branch;

        try {
            BranchTrackingStatus trackingStatus = BranchTrackingStatus.of(repository, branchName);
            if (trackingStatus != null
                    && (trackingStatus.getAheadCount() != 0 || trackingStatus.getBehindCount() != 0)) {
                result += " " + formatBranchTrackingStatus(trackingStatus);
            }
        } catch (Throwable t) {
            //$FALL-THROUGH$
        }

        return result;
    }

    return null;
}

From source file:org.jboss.tools.openshift.egit.core.EGitUtils.java

License:Open Source License

/**
 * Returns <code>true</code> if the given branch in the given repository has
 * local commits that were not pushed to its remote yet.
 * //from   ww w.j  a  v  a 2s  .  c  o m
 * @param repository
 * @param branchName
 * @return
 * @throws IOException
 */
public static boolean hasCommitsToBePushed(Repository repository) throws IOException {
    BranchTrackingStatus status = BranchTrackingStatus.of(repository, Constants.MASTER);
    if (status == null) {
        return false;
    }
    return status.getAheadCount() > 0;
}

From source file:org.jboss.tools.openshift.egit.core.EGitUtils.java

License:Open Source License

/**
 * Returns <code>true</code> if the given repo has commits that are not
 * contained withing the repo attached to it via the given remote. It is
 * ahead of the given remote config./*  ww  w. java  2s. co m*/
 * This will work for non{@link BranchTrackingStatus#of(Repository, String)} will tell you if the
 * given branch is ahead of it's tracking branch. It only works with a
 * branch that is tracking another branch. 
 * 
 * @param repo
 *            the repo to check
 * @param remote
 *            the name of the remote to check against
 * @param monitor
 *            the monitor to report progress to
 * @return
 * @throws IOException
 * @throws InvocationTargetException
 * @throws URISyntaxException
 * 
 * @see BranchTrackingStatus#of
 */
public static boolean isAhead(Repository repo, String remote, IProgressMonitor monitor)
        throws IOException, InvocationTargetException, URISyntaxException {
    Assert.isLegal(remote != null);
    Assert.isLegal(repo != null);
    if (remote.equals(getRemote(repo.getBranch(), repo.getConfig()))) {
        BranchTrackingStatus status = BranchTrackingStatus.of(repo, repo.getBranch());
        if (status != null) {
            return status.getAheadCount() > 0;
        }
    }
    return isNonTrackingBranchAhead(repo, remote, monitor);
}