Search in sources :

Example 41 with RemoteRefUpdate

use of org.eclipse.jgit.transport.RemoteRefUpdate in project egit by eclipse.

the class PushOperationResult method deriveSpecification.

/**
 * Derive push operation specification from this push operation result.
 * <p>
 * Specification is created basing on URIs of remote repositories in this
 * result that completed without connection errors, and remote ref updates
 * from push results.
 * <p>
 * This method is targeted to provide support for 2-stage push, where first
 * operation is dry run for user confirmation and second one is a real
 * operation.
 *
 * @param requireUnchanged
 *            if true, newly created copies of remote ref updates have
 *            expected old object id set to previously advertised ref value
 *            (remote ref won't be updated if it change in the mean time),
 *            if false, newly create copies of remote ref updates have
 *            expected object id set up as in this result source
 *            specification.
 * @return derived specification for another push operation.
 * @throws IOException
 *             when some previously locally available source ref is not
 *             available anymore, or some error occurred during creation
 *             locally tracking ref update.
 */
public PushOperationSpecification deriveSpecification(final boolean requireUnchanged) throws IOException {
    final PushOperationSpecification spec = new PushOperationSpecification();
    for (final URIish uri : getURIs()) {
        final PushResult pr = getPushResult(uri);
        if (pr == null)
            continue;
        final Collection<RemoteRefUpdate> oldUpdates = pr.getRemoteUpdates();
        final ArrayList<RemoteRefUpdate> newUpdates = new ArrayList<RemoteRefUpdate>(oldUpdates.size());
        for (final RemoteRefUpdate rru : oldUpdates) {
            final ObjectId expectedOldObjectId;
            if (requireUnchanged) {
                final Ref advertisedRef = getPushResult(uri).getAdvertisedRef(rru.getRemoteName());
                if (advertisedRef == null)
                    expectedOldObjectId = ObjectId.zeroId();
                else
                    expectedOldObjectId = advertisedRef.getObjectId();
            } else
                expectedOldObjectId = rru.getExpectedOldObjectId();
            final RemoteRefUpdate newRru = new RemoteRefUpdate(rru, expectedOldObjectId);
            newUpdates.add(newRru);
        }
        spec.addURIRefUpdates(uri, newUpdates);
    }
    return spec;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) PushResult(org.eclipse.jgit.transport.PushResult)

Example 42 with RemoteRefUpdate

use of org.eclipse.jgit.transport.RemoteRefUpdate in project egit by eclipse.

the class ConfirmationPage method revalidateImpl.

private void revalidateImpl() {
    if (getControl().isDisposed() || !isCurrentPage())
        return;
    final List<RefSpec> fetchSpecs;
    if (displayedRepoSelection.isConfigSelected())
        fetchSpecs = displayedRepoSelection.getConfig().getFetchRefSpecs();
    else
        fetchSpecs = null;
    final PushOperation operation;
    try {
        final Collection<RemoteRefUpdate> updates = Transport.findRemoteRefUpdatesFor(local, displayedRefSpecs, fetchSpecs);
        if (updates.isEmpty()) {
            // It can happen only when local refs changed in the mean time.
            setErrorMessage(UIText.ConfirmationPage_errorRefsChangedNoMatch);
            setPageComplete(false);
            return;
        }
        final PushOperationSpecification spec = new PushOperationSpecification();
        for (final URIish uri : displayedRepoSelection.getPushURIs()) spec.addURIRefUpdates(uri, copyUpdates(updates));
        int timeout = Activator.getDefault().getPreferenceStore().getInt(UIPreferences.REMOTE_CONNECTION_TIMEOUT);
        operation = new PushOperation(local, spec, true, timeout);
        if (credentials != null)
            operation.setCredentialsProvider(new EGitCredentialsProvider(credentials.getUser(), credentials.getPassword()));
        getContainer().run(true, true, new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                operation.run(monitor);
            }
        });
    } catch (final IOException e) {
        setErrorMessage(NLS.bind(UIText.ConfirmationPage_errorCantResolveSpecs, e.getMessage()));
        return;
    } catch (final InvocationTargetException e) {
        setErrorMessage(NLS.bind(UIText.ConfirmationPage_errorUnexpected, e.getCause().getMessage()));
        return;
    } catch (final InterruptedException e) {
        setErrorMessage(UIText.ConfirmationPage_errorInterrupted);
        setPageComplete(true);
        displayedRefSpecs = null;
        displayedRepoSelection = null;
        return;
    }
    final PushOperationResult result = operation.getOperationResult();
    resultPanel.setData(local, result);
    if (result.isSuccessfulConnectionForAnyURI()) {
        setPageComplete(true);
        confirmedResult = result;
    } else {
        final String message = NLS.bind(UIText.ConfirmationPage_cantConnectToAny, result.getErrorStringForAllURis());
        setErrorMessage(message);
        ErrorDialog.openError(getShell(), UIText.ConfirmationPage_cantConnectToAnyTitle, null, new Status(IStatus.ERROR, Activator.getPluginId(), message));
    }
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) URIish(org.eclipse.jgit.transport.URIish) PushOperationResult(org.eclipse.egit.core.op.PushOperationResult) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IOException(java.io.IOException) PushOperation(org.eclipse.egit.core.op.PushOperation) EGitCredentialsProvider(org.eclipse.egit.ui.internal.credentials.EGitCredentialsProvider) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RefSpec(org.eclipse.jgit.transport.RefSpec) PushOperationSpecification(org.eclipse.egit.core.op.PushOperationSpecification)

Example 43 with RemoteRefUpdate

use of org.eclipse.jgit.transport.RemoteRefUpdate in project egit by eclipse.

the class PushToGerritPage method doPush.

void doPush() {
    try {
        URIish uri = new URIish(uriCombo.getText());
        Ref currentHead = repository.exactRef(Constants.HEAD);
        String ref = prefixCombo.getItem(prefixCombo.getSelectionIndex()) + branchText.getText().trim();
        if (topicText.isEnabled()) {
            ref = setTopicInRef(ref, topicText.getText().trim());
        }
        RemoteRefUpdate update = new RemoteRefUpdate(repository, currentHead, ref, false, null, null);
        PushOperationSpecification spec = new PushOperationSpecification();
        spec.addURIRefUpdates(uri, Arrays.asList(update));
        final PushOperationUI op = new PushOperationUI(repository, spec, false);
        storeLastUsedUri(uriCombo.getText());
        storeLastUsedBranch(branchText.getText());
        storeLastUsedTopic(topicText.isEnabled(), topicText.getText().trim(), repository.getBranch());
        op.setPushMode(PushMode.GERRIT);
        op.start();
    } catch (URISyntaxException | IOException e) {
        Activator.handleError(e.getMessage(), e, true);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) Ref(org.eclipse.jgit.lib.Ref) PushOperationSpecification(org.eclipse.egit.core.op.PushOperationSpecification) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 44 with RemoteRefUpdate

use of org.eclipse.jgit.transport.RemoteRefUpdate in project gerrit by GerritCodeReview.

the class AbstractPushForReview method pushForReview.

private static void pushForReview(TestRepository<?> testRepo, RemoteRefUpdate.Status expectedStatus, String expectedMessage) throws GitAPIException {
    String ref = "refs/for/master";
    PushResult r = pushHead(testRepo, ref);
    RemoteRefUpdate refUpdate = r.getRemoteUpdate(ref);
    assertThat(refUpdate.getStatus()).isEqualTo(expectedStatus);
    if (expectedMessage != null) {
        assertThat(refUpdate.getMessage()).contains(expectedMessage);
    }
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) PushResult(org.eclipse.jgit.transport.PushResult)

Example 45 with RemoteRefUpdate

use of org.eclipse.jgit.transport.RemoteRefUpdate in project gerrit by GerritCodeReview.

the class AbstractPushForReview method errorMessageFormat.

@Test
public void errorMessageFormat() throws Exception {
    RevCommit c = createCommit(testRepo, "Message without Change-Id");
    assertThat(GitUtil.getChangeId(testRepo, c)).isEmpty();
    String ref = "refs/for/master";
    PushResult r = pushHead(testRepo, ref);
    RemoteRefUpdate refUpdate = r.getRemoteUpdate(ref);
    assertThat(refUpdate.getStatus()).isEqualTo(RemoteRefUpdate.Status.REJECTED_OTHER_REASON);
    String reason = String.format("commit %s: missing Change-Id in message footer", abbreviateName(c));
    assertThat(refUpdate.getMessage()).isEqualTo(reason);
    assertThat(r.getMessages()).contains("\nERROR: " + reason);
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) PushResult(org.eclipse.jgit.transport.PushResult) RevCommit(org.eclipse.jgit.revwalk.RevCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)73 PushResult (org.eclipse.jgit.transport.PushResult)55 Git (org.eclipse.jgit.api.Git)23 Test (org.junit.Test)19 File (java.io.File)18 RefSpec (org.eclipse.jgit.transport.RefSpec)17 IOException (java.io.IOException)15 CloneCommand (org.eclipse.jgit.api.CloneCommand)14 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)14 URIish (org.eclipse.jgit.transport.URIish)14 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)14 BufferedWriter (java.io.BufferedWriter)11 FileOutputStream (java.io.FileOutputStream)11 OutputStreamWriter (java.io.OutputStreamWriter)11 Date (java.util.Date)11 PushCommand (org.eclipse.jgit.api.PushCommand)11 RepositoryModel (com.gitblit.models.RepositoryModel)10 Repository (org.eclipse.jgit.lib.Repository)10 RevCommit (org.eclipse.jgit.revwalk.RevCommit)10 ArrayList (java.util.ArrayList)9