Example usage for org.eclipse.jgit.transport RemoteRefUpdate getSrcRef

List of usage examples for org.eclipse.jgit.transport RemoteRefUpdate getSrcRef

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport RemoteRefUpdate getSrcRef.

Prototype

public String getSrcRef() 

Source Link

Document

Get source revision as specified by user (in constructor)

Usage

From source file:com.googlesource.gerrit.plugins.replication.PushOne.java

License:Apache License

private void updateStates(Collection<RemoteRefUpdate> refUpdates) throws LockFailureException {
    Set<String> doneRefs = new HashSet<>();
    boolean anyRefFailed = false;
    RemoteRefUpdate.Status lastRefStatusError = RemoteRefUpdate.Status.OK;

    for (RemoteRefUpdate u : refUpdates) {
        RefPushResult pushStatus = RefPushResult.SUCCEEDED;
        Set<ReplicationState> logStates = new HashSet<>();

        logStates.addAll(stateMap.get(u.getSrcRef()));
        logStates.addAll(stateMap.get(ALL_REFS));
        ReplicationState[] logStatesArray = logStates.toArray(new ReplicationState[logStates.size()]);

        doneRefs.add(u.getSrcRef());//from  w w w.  ja va 2 s.co m
        switch (u.getStatus()) {
        case OK:
        case UP_TO_DATE:
        case NON_EXISTING:
            break;

        case NOT_ATTEMPTED:
        case AWAITING_REPORT:
        case REJECTED_NODELETE:
        case REJECTED_NONFASTFORWARD:
        case REJECTED_REMOTE_CHANGED:
            stateLog.error(String.format("Failed replicate of %s to %s: status %s", u.getRemoteName(), uri,
                    u.getStatus()), logStatesArray);
            pushStatus = RefPushResult.FAILED;
            anyRefFailed = true;
            lastRefStatusError = u.getStatus();
            break;

        case REJECTED_OTHER_REASON:
            if ("non-fast-forward".equals(u.getMessage())) {
                stateLog.error(
                        String.format(
                                "Failed replicate of %s to %s" + ", remote rejected non-fast-forward push."
                                        + "  Check receive.denyNonFastForwards variable in config file"
                                        + " of destination repository.",
                                u.getRemoteName(), uri),
                        logStatesArray);
            } else if ("failed to lock".equals(u.getMessage())) {
                throw new LockFailureException(uri, u.getMessage());
            } else {
                stateLog.error(String.format("Failed replicate of %s to %s, reason: %s", u.getRemoteName(), uri,
                        u.getMessage()), logStatesArray);
            }
            pushStatus = RefPushResult.FAILED;
            anyRefFailed = true;
            lastRefStatusError = u.getStatus();
            break;
        }

        for (ReplicationState rs : getStatesByRef(u.getSrcRef())) {
            rs.notifyRefReplicated(projectName.get(), u.getSrcRef(), uri, pushStatus, u.getStatus());
        }
    }

    doneRefs.add(ALL_REFS);
    for (ReplicationState rs : getStatesByRef(ALL_REFS)) {
        rs.notifyRefReplicated(projectName.get(), ALL_REFS, uri,
                anyRefFailed ? RefPushResult.FAILED : RefPushResult.SUCCEEDED, lastRefStatusError);
    }
    for (Map.Entry<String, ReplicationState> entry : stateMap.entries()) {
        if (!doneRefs.contains(entry.getKey())) {
            entry.getValue().notifyRefReplicated(projectName.get(), entry.getKey(), uri,
                    RefPushResult.NOT_ATTEMPTED, null);
        }
    }
    stateMap.clear();
}

From source file:jbyoshi.gitupdate.processor.Push.java

License:Apache License

private static void process(Repository repo, Git git, String remote, Collection<String> branches, Report report)
        throws Exception {
    // Figure out if anything needs to be pushed.
    Map<String, ObjectId> oldIds = new HashMap<>();
    boolean canPush = false;
    for (String branch : branches) {
        BranchConfig config = new BranchConfig(repo.getConfig(), branch);
        ObjectId target = repo.getRef(branch).getObjectId();

        Ref remoteRef = repo.getRef(config.getRemoteTrackingBranch());
        if (remoteRef == null || !target.equals(remoteRef.getObjectId())) {
            canPush = true;//from  ww  w .  ja va2s.  c  o  m
        }
        oldIds.put(branch, remoteRef == null ? ObjectId.zeroId() : remoteRef.getObjectId());
    }

    if (!canPush) {
        return;
    }

    PushCommand push = git.push().setCredentialsProvider(Prompts.INSTANCE).setTimeout(5).setRemote(remote);
    for (String branch : branches) {
        push.add(Constants.R_HEADS + branch);
    }
    for (PushResult result : push.call()) {
        for (RemoteRefUpdate update : result.getRemoteUpdates()) {
            if (update.getStatus() == RemoteRefUpdate.Status.OK) {
                String branchName = Utils.getShortBranch(update.getSrcRef());
                ObjectId oldId = oldIds.get(branchName);
                String old = oldId.equals(ObjectId.zeroId()) ? "new branch" : oldId.name();
                report.newChild(branchName + ": " + old + " -> " + update.getNewObjectId().name()).modified();
            }
        }
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

private PushResponse addCommandOutputUpdates(PushResponse pushResponseDto, final PushRequest request,
        final PushResult result) throws GitException {
    String commandOutput = result.getMessages();
    final Collection<RemoteRefUpdate> refUpdates = result.getRemoteUpdates();
    final List<Map<String, String>> updates = new ArrayList<>();
    final String currentBranch = getCurrentBranch();

    for (RemoteRefUpdate remoteRefUpdate : refUpdates) {
        final String remoteRefName = remoteRefUpdate.getRemoteName();
        // check status only for branch given in the URL or tags - (handle special "refs/for" case)
        String shortenRefFor = remoteRefName.startsWith("refs/for/")
                ? remoteRefName.substring("refs/for/".length())
                : remoteRefName;/*from  w ww .  j a  va  2s  .c o m*/
        if (currentBranch.equals(Repository.shortenRefName(remoteRefName))
                || currentBranch.equals(shortenRefFor) || remoteRefName.startsWith(Constants.R_TAGS)) {
            Map<String, String> update = new HashMap<>();
            RemoteRefUpdate.Status status = remoteRefUpdate.getStatus();
            if (status != RemoteRefUpdate.Status.UP_TO_DATE || !remoteRefName.startsWith(Constants.R_TAGS)) {
                update.put(KEY_COMMIT_MESSAGE, remoteRefUpdate.getMessage());
                update.put(KEY_RESULT, status.name());
                TrackingRefUpdate refUpdate = remoteRefUpdate.getTrackingRefUpdate();
                if (refUpdate != null) {
                    update.put(KEY_REMOTENAME, Repository.shortenRefName(refUpdate.getLocalName()));
                    update.put(KEY_LOCALNAME, Repository.shortenRefName(refUpdate.getRemoteName()));
                } else {
                    update.put(KEY_REMOTENAME, Repository.shortenRefName(remoteRefUpdate.getSrcRef()));
                    update.put(KEY_LOCALNAME, Repository.shortenRefName(remoteRefUpdate.getRemoteName()));
                }
                updates.add(update);
            }
            if (status != RemoteRefUpdate.Status.OK) {
                commandOutput = buildPushFailedMessage(request, remoteRefUpdate, currentBranch);
            }
        }
    }
    return pushResponseDto.withCommandOutput(commandOutput).withUpdates(updates);
}

From source file:org.eclipse.egit.ui.internal.push.RefUpdateContentProvider.java

License:Open Source License

public Object[] getElements(final Object inputElement) {
    if (inputElement == null)
        return new RefUpdateElement[0];

    final PushOperationResult result = (PushOperationResult) inputElement;

    final SortedMap<String, String> dstToSrc = new TreeMap<String, String>();
    for (final URIish uri : result.getURIs()) {
        if (result.isSuccessfulConnection(uri)) {
            for (final RemoteRefUpdate rru : result.getPushResult(uri).getRemoteUpdates())
                dstToSrc.put(rru.getRemoteName(), rru.getSrcRef());
            // Assuming that each repository received the same ref updates,
            // we need only one to get these ref names.
            break;
        }/*w ww  . ja va 2 s .  c o  m*/
    }

    // Transforming PushOperationResult model to row-wise one.
    final RefUpdateElement elements[] = new RefUpdateElement[dstToSrc.size()];
    int i = 0;
    for (final Entry<String, String> entry : dstToSrc.entrySet())
        elements[i++] = new RefUpdateElement(result, entry.getValue(), entry.getKey());
    return elements;
}

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

License:Open Source License

private static String getErrors(PushOperationResult pushResult) {
    StringBuilder builder = new StringBuilder();
    for (RemoteRefUpdate failedUpdate : getFailedUpdates(pushResult)) {
        builder.append(MessageFormat.format("push from {0} to {1} was {2}", failedUpdate.getSrcRef(),
                failedUpdate.getRemoteName(), failedUpdate.getStatus()));
    }/*w  w  w .  j  a v  a2 s.  com*/
    return builder.toString();

}