Search in sources :

Example 36 with RemoteRefUpdate

use of org.eclipse.jgit.transport.RemoteRefUpdate in project omegat by omegat-org.

the class GITRemoteRepository2 method commit.

@Override
public String commit(String[] onVersions, String comment) throws Exception {
    if (onVersions != null) {
        // check versions
        String currentVersion = getCurrentVersion();
        boolean hasVersion = false;
        for (String v : onVersions) {
            if (v != null) {
                hasVersion = true;
                break;
            }
        }
        if (hasVersion) {
            boolean found = false;
            for (String v : onVersions) {
                if (v != null) {
                    if (v.equals(currentVersion)) {
                        found = true;
                        break;
                    }
                }
            }
            if (!found) {
                throw new RuntimeException("Version changed from " + Arrays.toString(onVersions) + " to " + currentVersion);
            }
        }
    }
    if (indexIsEmpty(DirCache.read(repository))) {
        // Nothing was actually added to the index so we can just return.
        Log.logInfoRB("GIT_NO_CHANGES", "upload");
        return null;
    }
    Log.logInfoRB("GIT_START", "upload");
    try (Git git = new Git(repository)) {
        CommitCommand commitCommand = git.commit();
        commitCommand.setMessage(comment);
        // read from git config
        commitCommand.setSign(null);
        RevCommit commit = commitCommand.call();
        Iterable<PushResult> results = git.push().setTimeout(TIMEOUT).setRemote(REMOTE).add(getDefaultBranchName(repository)).call();
        List<Status> statuses = StreamSupport.stream(results.spliterator(), false).flatMap(r -> r.getRemoteUpdates().stream()).map(RemoteRefUpdate::getStatus).collect(Collectors.toList());
        String result;
        if (statuses.isEmpty() || statuses.stream().anyMatch(s -> s != RemoteRefUpdate.Status.OK)) {
            Log.logWarningRB("GIT_CONFLICT");
            result = null;
        } else {
            result = commit.getName();
        }
        Log.logDebug(LOGGER, "GIT committed into new version {0} ", result);
        Log.logInfoRB("GIT_FINISH", "upload");
        return result;
    } catch (Exception ex) {
        Log.logErrorRB("GIT_ERROR", "upload", ex.getMessage());
        if (ex instanceof TransportException) {
            throw new NetworkException(ex);
        } else {
            throw ex;
        }
    }
}
Also used : Status(org.eclipse.jgit.transport.RemoteRefUpdate.Status) Arrays(java.util.Arrays) Status(org.eclipse.jgit.transport.RemoteRefUpdate.Status) IRemoteRepository2(org.omegat.core.team2.IRemoteRepository2) RepositoryDefinition(gen.core.project.RepositoryDefinition) RevCommit(org.eclipse.jgit.revwalk.RevCommit) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) GpgSigner(org.eclipse.jgit.lib.GpgSigner) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) Log(org.omegat.util.Log) CloneCommand(org.eclipse.jgit.api.CloneCommand) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) ResetType(org.eclipse.jgit.api.ResetCommand.ResetType) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Collection(java.util.Collection) CommitCommand(org.eclipse.jgit.api.CommitCommand) ConfigConstants(org.eclipse.jgit.lib.ConfigConstants) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) IOException(java.io.IOException) Constants(org.eclipse.jgit.lib.Constants) AutoCRLF(org.eclipse.jgit.lib.CoreConfig.AutoCRLF) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) RevTree(org.eclipse.jgit.revwalk.RevTree) ObjectId(org.eclipse.jgit.lib.ObjectId) TransportException(org.eclipse.jgit.api.errors.TransportException) List(java.util.List) ProjectTeamSettings(org.omegat.core.team2.ProjectTeamSettings) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) Ref(org.eclipse.jgit.lib.Ref) PushResult(org.eclipse.jgit.transport.PushResult) LsRemoteCommand(org.eclipse.jgit.api.LsRemoteCommand) DirCache(org.eclipse.jgit.dircache.DirCache) StoredConfig(org.eclipse.jgit.lib.StoredConfig) AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) QName(javax.xml.namespace.QName) Git(org.eclipse.jgit.api.Git) DiffEntry(org.eclipse.jgit.diff.DiffEntry) ObjectReader(org.eclipse.jgit.lib.ObjectReader) Repository(org.eclipse.jgit.lib.Repository) PushResult(org.eclipse.jgit.transport.PushResult) TransportException(org.eclipse.jgit.api.errors.TransportException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) TransportException(org.eclipse.jgit.api.errors.TransportException) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) Git(org.eclipse.jgit.api.Git) CommitCommand(org.eclipse.jgit.api.CommitCommand) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 37 with RemoteRefUpdate

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

the class PushTagIT method pushTag.

private String pushTag(TagType tagType, String tagName, boolean newCommit, boolean force, Status expectedStatus) throws Exception {
    if (force) {
        testRepo.reset(initialHead);
    }
    commit(user.getIdent(), "subject");
    boolean createTag = tagName == null;
    tagName = MoreObjects.firstNonNull(tagName, "v1_" + System.nanoTime());
    switch(tagType) {
        case LIGHTWEIGHT:
            break;
        case ANNOTATED:
            if (createTag) {
                createAnnotatedTag(testRepo, tagName, user.getIdent());
            } else {
                updateAnnotatedTag(testRepo, tagName, user.getIdent());
            }
            break;
        default:
            throw new IllegalStateException("unexpected tag type: " + tagType);
    }
    if (!newCommit) {
        grant(project, "refs/for/refs/heads/master", Permission.SUBMIT, false, REGISTERED_USERS);
        pushHead(testRepo, "refs/for/master%submit");
    }
    String tagRef = tagRef(tagName);
    PushResult r = tagType == LIGHTWEIGHT ? pushHead(testRepo, tagRef, false, force) : GitUtil.pushTag(testRepo, tagName, !createTag);
    RemoteRefUpdate refUpdate = r.getRemoteUpdate(tagRef);
    assertThat(refUpdate.getStatus()).named(tagType.name()).isEqualTo(expectedStatus);
    return tagName;
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) PushResult(org.eclipse.jgit.transport.PushResult)

Example 38 with RemoteRefUpdate

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

the class PushOperationTest method testUpdateTrackingBranchIfSpecifiedInRemoteRefUpdate.

@Test
public void testUpdateTrackingBranchIfSpecifiedInRemoteRefUpdate() throws Exception {
    // Commit on repository 2
    IProject project = importProject(repository2, projectName);
    RevCommit commit = repository2.addAndCommit(project, new File(workdir2, "test.txt"), "Commit in repository 2");
    project.delete(false, false, null);
    // We want to push from repository 2 to 1 (because repository 2 already
    // has tracking set up)
    URIish remote = repository1.getUri();
    String trackingRef = "refs/remotes/origin/master";
    RemoteRefUpdate update = new RemoteRefUpdate(repository2.getRepository(), "HEAD", "refs/heads/master", false, trackingRef, null);
    PushOperationSpecification spec = new PushOperationSpecification();
    spec.addURIRefUpdates(remote, Arrays.asList(update));
    PushOperation push = new PushOperation(repository2.getRepository(), spec, false, 0);
    push.run(null);
    PushOperationResult result = push.getOperationResult();
    PushResult pushResult = result.getPushResult(remote);
    assertNotNull("Expected result to have tracking ref update", pushResult.getTrackingRefUpdate(trackingRef));
    ObjectId trackingId = repository2.getRepository().resolve(trackingRef);
    assertEquals("Expected tracking branch to be updated", commit.getId(), trackingId);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) PushOperationResult(org.eclipse.egit.core.op.PushOperationResult) ObjectId(org.eclipse.jgit.lib.ObjectId) PushOperationSpecification(org.eclipse.egit.core.op.PushOperationSpecification) PushResult(org.eclipse.jgit.transport.PushResult) PushOperation(org.eclipse.egit.core.op.PushOperation) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IProject(org.eclipse.core.resources.IProject) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 39 with RemoteRefUpdate

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

the class PushOperationTest method testPushWithReusedSpec.

/**
 * We should get an {@link IllegalStateException} if the spec was re-used
 *
 * @throws Exception
 */
@Test
public void testPushWithReusedSpec() throws Exception {
    PushOperationSpecification spec = new PushOperationSpecification();
    // the remote is repo2
    URIish remote = repository2.getUri();
    // update master upon master
    List<RemoteRefUpdate> refUpdates = new ArrayList<RemoteRefUpdate>();
    RemoteRefUpdate update = new RemoteRefUpdate(repository1.getRepository(), "HEAD", "refs/heads/test", false, null, null);
    refUpdates.add(update);
    spec.addURIRefUpdates(remote, refUpdates);
    PushOperation pop = new PushOperation(repository1.getRepository(), spec, false, 0);
    pop.run(null);
    pop = new PushOperation(repository1.getRepository(), spec, false, 0);
    try {
        pop.run(null);
        fail("Expected Exception not thrown");
    } catch (IllegalStateException e) {
    // expected
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) ArrayList(java.util.ArrayList) PushOperationSpecification(org.eclipse.egit.core.op.PushOperationSpecification) PushOperation(org.eclipse.egit.core.op.PushOperation) Test(org.junit.Test)

Example 40 with RemoteRefUpdate

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

the class PushOperationResult method equals.

/**
 * This implementation returns true if all following conditions are met:
 * <ul>
 * <li>both objects result have the same set successfully connected
 * repositories (URIs) - unsuccessful connections are discarded, AND <li>
 * remote ref updates must match for each successful connection in sense of
 * equal remoteName, equal status and equal newObjectId value.</li>
 * </ul>
 *
 * @see Object#equals(Object)
 * @param obj
 *            other push operation result to compare to.
 * @return true if object is equal to this one in terms of conditions
 *         described above, false otherwise.
 */
@Override
public boolean equals(final Object obj) {
    if (obj == this)
        return true;
    if (!(obj instanceof PushOperationResult))
        return false;
    final PushOperationResult other = (PushOperationResult) obj;
    // Check successful connections/URIs two-ways:
    final Set<URIish> otherURIs = other.getURIs();
    for (final URIish uri : getURIs()) {
        if (isSuccessfulConnection(uri) && (!otherURIs.contains(uri) || !other.isSuccessfulConnection(uri)))
            return false;
    }
    for (final URIish uri : other.getURIs()) {
        if (other.isSuccessfulConnection(uri) && (!urisEntries.containsKey(uri) || !isSuccessfulConnection(uri)))
            return false;
    }
    for (final URIish uri : getURIs()) {
        if (!isSuccessfulConnection(uri))
            continue;
        final PushResult otherPushResult = other.getPushResult(uri);
        for (final RemoteRefUpdate rru : getPushResult(uri).getRemoteUpdates()) {
            final RemoteRefUpdate otherRru = otherPushResult.getRemoteUpdate(rru.getRemoteName());
            if (otherRru == null)
                return false;
            if (otherRru.getStatus() != rru.getStatus() || otherRru.getNewObjectId() != rru.getNewObjectId())
                return false;
        }
    }
    return true;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) PushResult(org.eclipse.jgit.transport.PushResult)

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