Search in sources :

Example 1 with CommitsAheadAndBehind

use of com.oxygenxml.git.view.history.CommitsAheadAndBehind in project oxygen-git-client-addon by oxygenxml.

the class RevCommitUtil method getCommitsAheadAndBehind.

/**
 * Get commits ahead and behind.
 *
 * @param repository Current repo.
 * @param branchName Current branch.
 *
 * @return a structure that contains the lists of commits ahead and behind or <code>null</code>.
 *
 * @throws IOException
 */
public static CommitsAheadAndBehind getCommitsAheadAndBehind(Repository repository, String branchName) throws IOException {
    String shortBranchName = Repository.shortenRefName(branchName);
    String fullBranchName = Constants.R_HEADS + shortBranchName;
    BranchConfig branchConfig = new BranchConfig(repository.getConfig(), shortBranchName);
    String trackingBranch = branchConfig.getTrackingBranch();
    if (trackingBranch == null) {
        return null;
    }
    Ref tracking = repository.exactRef(trackingBranch);
    if (tracking == null) {
        return null;
    }
    Ref local = repository.exactRef(fullBranchName);
    if (local == null) {
        return null;
    }
    try (RevWalk walk = new RevWalk(repository)) {
        RevCommit localCommit = walk.parseCommit(local.getObjectId());
        RevCommit trackingCommit = walk.parseCommit(tracking.getObjectId());
        walk.setRevFilter(RevFilter.MERGE_BASE);
        walk.markStart(localCommit);
        walk.markStart(trackingCommit);
        RevCommit mergeBase = walk.next();
        walk.reset();
        walk.setRevFilter(RevFilter.ALL);
        List<RevCommit> commitsAhead = RevWalkUtils.find(walk, localCommit, mergeBase);
        List<RevCommit> commitsBehind = RevWalkUtils.find(walk, trackingCommit, mergeBase);
        return new CommitsAheadAndBehind(commitsAhead, commitsBehind);
    }
}
Also used : CommitsAheadAndBehind(com.oxygenxml.git.view.history.CommitsAheadAndBehind) Ref(org.eclipse.jgit.lib.Ref) BranchConfig(org.eclipse.jgit.lib.BranchConfig) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with CommitsAheadAndBehind

use of com.oxygenxml.git.view.history.CommitsAheadAndBehind in project oxygen-git-client-addon by oxygenxml.

the class ToolbarPanel method updatePushToolTip.

/**
 * Updates the tool tip of "Push" Button.
 *
 * @param isAnUpstreamBranchDefinedInConfig              <code>true</code> if is an upstream branch defined in configurations
 * @param existsRemoteBranchForUpstreamDefinedInConfig   <code>true</code> if exists remote branch for upstream defined in configurations
 * @param upstreamBranchFromConfig                       The upstream branch from configurations
 * @param commitsAheadMessage                            The commits ahead message
 * @param currentBranchName                              The name of the current branch
 * @param repo                                           The current repository
 *
 * @return updated "Push" button tool tip text.
 */
private String updatePushToolTip(boolean isAnUpstreamBranchDefinedInConfig, boolean existsRemoteBranchForUpstreamDefinedInConfig, String upstreamBranchFromConfig, String commitsAheadMessage, String currentBranchName, Repository repo) {
    StringBuilder pushButtonTooltip = new StringBuilder();
    pushButtonTooltip.append("<html>");
    if (isAnUpstreamBranchDefinedInConfig) {
        if (existsRemoteBranchForUpstreamDefinedInConfig) {
            // The "normal" case. The upstream branch defined in "config" exists in the remote repository.
            String pushToMsg = MessageFormat.format(TRANSLATOR.getTranslation(Tags.PUSH_TO), upstreamBranchFromConfig);
            pushButtonTooltip.append(pushToMsg).append(".<br>").append(commitsAheadMessage);
            try {
                CommitsAheadAndBehind commitsAheadAndBehind = RevCommitUtil.getCommitsAheadAndBehind(repo, currentBranchName);
                if (commitsAheadAndBehind != null && commitsAheadAndBehind.getCommitsAhead() != null) {
                    List<RevCommit> commitsAhead = commitsAheadAndBehind.getCommitsAhead();
                    pushButtonTooltip.append("<br><br>");
                    addCommitsToTooltip(commitsAhead, pushButtonTooltip);
                    if (commitsAhead.size() > MAX_NO_OF_COMMITS_IN_PUSH_AND_PULL_TOOLTIPS) {
                        pushButtonTooltip.append("<br>").append(TRANSLATOR.getTranslation(Tags.SEE_ALL_COMMITS_IN_GIT_HISTORY));
                    }
                }
            } catch (IOException | GitAPIException e) {
                LOGGER.error(e.getMessage(), e);
            }
        } else {
            // There is an upstream branch defined in "config",
            // but that branch does not exist in the remote repository.
            pushButtonTooltip.append(MessageFormat.format(TRANSLATOR.getTranslation(Tags.PUSH_TO_CREATE_AND_TRACK_REMOTE_BRANCH), currentBranchName));
        }
    } else {
        updatePushTooltipWhenNoUpstream(currentBranchName, pushButtonTooltip);
    }
    pushButtonTooltip.append("</html>");
    return pushButtonTooltip.toString();
}
Also used : CommitsAheadAndBehind(com.oxygenxml.git.view.history.CommitsAheadAndBehind) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with CommitsAheadAndBehind

use of com.oxygenxml.git.view.history.CommitsAheadAndBehind in project oxygen-git-client-addon by oxygenxml.

the class ToolbarPanel method updatePullToolTip.

/**
 * Updates the tool tip of "Push" Button.
 *
 * @param isAnUpstreamBranchDefinedInConfig              <code>true</code> if is an upstream branch defined in configurations
 * @param existsRemoteBranchForUpstreamDefinedInConfig   <code>true</code> if exists remote branch for upstream defined in configurations
 * @param upstreamBranchFromConfig                       The upstream branch from configurations
 * @param commitsBehindMessage                           The commits behind message
 * @param remoteBranchRefForUpstreamFromConfig           The remote branch reference for upstream from configurations.
 * @param repo                                           Current repo.
 *
 * @return updated "Push" button tool tip text.
 */
private String updatePullToolTip(boolean isAnUpstreamBranchDefinedInConfig, boolean existsRemoteBranchForUpstreamDefinedInConfig, String upstreamBranchFromConfig, String commitsBehindMessage, Ref remoteBranchRefForUpstreamFromConfig, Repository repo) {
    StringBuilder pullButtonTooltip = new StringBuilder();
    pullButtonTooltip.append("<html>");
    String currentBranchName = GitAccess.getInstance().getBranchInfo().getBranchName();
    if (isAnUpstreamBranchDefinedInConfig) {
        if (existsRemoteBranchForUpstreamDefinedInConfig) {
            // The "normal" case. The upstream branch defined in "config" exists in the remote repository.
            String pullFromMsg = MessageFormat.format(TRANSLATOR.getTranslation(getPullFromTranslationTag()), Repository.shortenRefName(remoteBranchRefForUpstreamFromConfig.getName()));
            pullButtonTooltip.append(pullFromMsg).append(".<br>").append(commitsBehindMessage);
            try {
                CommitsAheadAndBehind commitsAheadAndBehind = RevCommitUtil.getCommitsAheadAndBehind(repo, currentBranchName);
                if (commitsAheadAndBehind != null && commitsAheadAndBehind.getCommitsBehind() != null) {
                    List<RevCommit> commitsBehind = commitsAheadAndBehind.getCommitsBehind();
                    pullButtonTooltip.append("<br><br>");
                    addCommitsToTooltip(commitsBehind, pullButtonTooltip);
                    if (commitsBehind.size() > MAX_NO_OF_COMMITS_IN_PUSH_AND_PULL_TOOLTIPS) {
                        pullButtonTooltip.append("<br>").append(TRANSLATOR.getTranslation(Tags.SEE_ALL_COMMITS_IN_GIT_HISTORY));
                    }
                }
            } catch (IOException | GitAPIException e) {
                LOGGER.error(e.getMessage(), e);
            }
        } else {
            // The upstream branch defined in "config" does not exists in the remote repository.
            String upstreamDoesNotExistMsg = MessageFormat.format(StringUtils.capitalize(TRANSLATOR.getTranslation(Tags.UPSTREAM_BRANCH_DOES_NOT_EXIST)), upstreamBranchFromConfig);
            pullButtonTooltip.append(TRANSLATOR.getTranslation(Tags.CANNOT_PULL)).append("<br>").append(upstreamDoesNotExistMsg);
        }
    } else {
        updatePullTooltipWhenNoUpstream(pullButtonTooltip, currentBranchName);
    }
    pullButtonTooltip.append("</html>");
    return pullButtonTooltip.toString();
}
Also used : CommitsAheadAndBehind(com.oxygenxml.git.view.history.CommitsAheadAndBehind) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with CommitsAheadAndBehind

use of com.oxygenxml.git.view.history.CommitsAheadAndBehind in project oxygen-git-client-addon by oxygenxml.

the class RemoteRepositoryChangeWatcher method checkForRemoteCommits.

/**
 * Checks in the remote repository if there are new commits.
 * @param fetch <code>true</code> to execute a fetch before making the checks.
 *
 * @return <code>commitsAhead</code> a list with all new commits
 */
private static List<RevCommit> checkForRemoteCommits(boolean fetch) {
    List<RevCommit> commitsBehind = Collections.emptyList();
    try {
        GitAccess gitAccess = GitAccess.getInstance();
        if (fetch) {
            gitAccess.fetch();
        }
        Repository repository = gitAccess.getRepository();
        CommitsAheadAndBehind commitsAheadAndBehind = RevCommitUtil.getCommitsAheadAndBehind(repository, repository.getFullBranch());
        if (commitsAheadAndBehind != null) {
            commitsBehind = commitsAheadAndBehind.getCommitsBehind();
        }
    } catch (NoRepositorySelected | IOException | SSHPassphraseRequiredException | PrivateRepositoryException | RepositoryUnavailableException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    return commitsBehind;
}
Also used : CommitsAheadAndBehind(com.oxygenxml.git.view.history.CommitsAheadAndBehind) Repository(org.eclipse.jgit.lib.Repository) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

CommitsAheadAndBehind (com.oxygenxml.git.view.history.CommitsAheadAndBehind)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 IOException (java.io.IOException)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 BranchConfig (org.eclipse.jgit.lib.BranchConfig)1 Ref (org.eclipse.jgit.lib.Ref)1 Repository (org.eclipse.jgit.lib.Repository)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1