Search in sources :

Example 1 with MergeResult

use of org.eclipse.che.api.git.shared.MergeResult in project che by eclipse.

the class JGitConnection method merge.

@Override
public MergeResult merge(String commit) throws GitException {
    org.eclipse.jgit.api.MergeResult jGitMergeResult;
    MergeResult.MergeStatus status;
    try {
        Ref ref = repository.findRef(commit);
        if (ref == null) {
            throw new GitException("Invalid reference to commit for merge " + commit);
        }
        // Shorten local branch names by removing '/refs/heads/' from the beginning
        String name = ref.getName();
        if (name.startsWith(Constants.R_HEADS)) {
            name = name.substring(Constants.R_HEADS.length());
        }
        jGitMergeResult = getGit().merge().include(name, ref.getObjectId()).call();
    } catch (CheckoutConflictException exception) {
        jGitMergeResult = new org.eclipse.jgit.api.MergeResult(exception.getConflictingPaths());
    } catch (IOException | GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
    switch(jGitMergeResult.getMergeStatus()) {
        case ALREADY_UP_TO_DATE:
            status = MergeResult.MergeStatus.ALREADY_UP_TO_DATE;
            break;
        case CONFLICTING:
            status = MergeResult.MergeStatus.CONFLICTING;
            break;
        case FAILED:
            status = MergeResult.MergeStatus.FAILED;
            break;
        case FAST_FORWARD:
            status = MergeResult.MergeStatus.FAST_FORWARD;
            break;
        case MERGED:
            status = MergeResult.MergeStatus.MERGED;
            break;
        case NOT_SUPPORTED:
            status = MergeResult.MergeStatus.NOT_SUPPORTED;
            break;
        case CHECKOUT_CONFLICT:
            status = MergeResult.MergeStatus.CONFLICTING;
            break;
        default:
            throw new IllegalStateException("Unknown merge status " + jGitMergeResult.getMergeStatus());
    }
    ObjectId[] jGitMergedCommits = jGitMergeResult.getMergedCommits();
    List<String> mergedCommits = new ArrayList<>();
    if (jGitMergedCommits != null) {
        for (ObjectId jGitMergedCommit : jGitMergedCommits) {
            mergedCommits.add(jGitMergedCommit.getName());
        }
    }
    List<String> conflicts;
    if (org.eclipse.jgit.api.MergeResult.MergeStatus.CHECKOUT_CONFLICT.equals(jGitMergeResult.getMergeStatus())) {
        conflicts = jGitMergeResult.getCheckoutConflicts();
    } else {
        Map<String, int[][]> jGitConflicts = jGitMergeResult.getConflicts();
        conflicts = jGitConflicts != null ? new ArrayList<>(jGitConflicts.keySet()) : Collections.emptyList();
    }
    Map<String, ResolveMerger.MergeFailureReason> jGitFailing = jGitMergeResult.getFailingPaths();
    ObjectId newHead = jGitMergeResult.getNewHead();
    return newDto(MergeResult.class).withFailed(jGitFailing != null ? new ArrayList<>(jGitFailing.keySet()) : Collections.emptyList()).withNewHead(newHead != null ? newHead.getName() : null).withMergeStatus(status).withConflicts(conflicts).withMergedCommits(mergedCommits);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) GitException(org.eclipse.che.api.git.exception.GitException) MergeResult(org.eclipse.che.api.git.shared.MergeResult) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CheckoutConflictException(org.eclipse.jgit.api.errors.CheckoutConflictException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Ref(org.eclipse.jgit.lib.Ref)

Example 2 with MergeResult

use of org.eclipse.che.api.git.shared.MergeResult in project che by eclipse.

the class MergeTest method testMergeNoChanges.

@Test(dataProvider = "GitConnectionFactory", dataProviderClass = org.eclipse.che.git.impl.GitConnectionFactoryProvider.class)
public void testMergeNoChanges(GitConnectionFactory connectionFactory) throws Exception {
    //given
    GitConnection connection = connectToGitRepositoryWithContent(connectionFactory, repository);
    connection.branchCreate(branchName, null);
    //when
    MergeResult mergeResult = connection.merge(branchName);
    //then
    assertEquals(mergeResult.getMergeStatus(), MergeResult.MergeStatus.ALREADY_UP_TO_DATE);
}
Also used : MergeResult(org.eclipse.che.api.git.shared.MergeResult) GitConnection(org.eclipse.che.api.git.GitConnection) Test(org.testng.annotations.Test)

Example 3 with MergeResult

use of org.eclipse.che.api.git.shared.MergeResult in project che by eclipse.

the class MergeTest method testMerge.

@Test(dataProvider = "GitConnectionFactory", dataProviderClass = org.eclipse.che.git.impl.GitConnectionFactoryProvider.class)
public void testMerge(GitConnectionFactory connectionFactory) throws Exception {
    //given
    GitConnection connection = connectToGitRepositoryWithContent(connectionFactory, repository);
    connection.checkout(CheckoutParams.create(branchName).withCreateNew(true));
    File file = addFile(connection, "t-merge", "aaa\n");
    connection.add(AddParams.create(new ArrayList<>(singletonList("."))));
    connection.commit(CommitParams.create("add file in new branch"));
    connection.checkout(CheckoutParams.create("master"));
    //when
    MergeResult mergeResult = connection.merge(branchName);
    //then
    assertEquals(mergeResult.getMergeStatus(), MergeResult.MergeStatus.FAST_FORWARD);
    assertTrue(file.exists());
    assertEquals(Files.toString(file, Charsets.UTF_8), "aaa\n");
    assertEquals(connection.log(LogParams.create()).getCommits().get(0).getMessage(), "add file in new branch");
}
Also used : ArrayList(java.util.ArrayList) MergeResult(org.eclipse.che.api.git.shared.MergeResult) GitConnection(org.eclipse.che.api.git.GitConnection) File(java.io.File) GitTestUtil.addFile(org.eclipse.che.git.impl.GitTestUtil.addFile) Test(org.testng.annotations.Test)

Example 4 with MergeResult

use of org.eclipse.che.api.git.shared.MergeResult in project che by eclipse.

the class MergeResultWriter method writeTo.

/**
     * @see MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType,
     *      javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)
     */
@Override
public void writeTo(MergeResult mergeResult, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    Writer writer = new OutputStreamWriter(entityStream);
    MergeResult.MergeStatus status = mergeResult.getMergeStatus();
    switch(mergeResult.getMergeStatus()) {
        case FAST_FORWARD:
        case ALREADY_UP_TO_DATE:
        case MERGED:
            writer.write(status.toString());
            writer.write('\n');
            break;
        case FAILED:
            writer.write("error: Failed to merge:");
            for (String failed : mergeResult.getFailed()) {
                writer.write("        ");
                writer.write(failed);
                writer.write('\n');
            }
            break;
        case CONFLICTING:
            for (String conflict : mergeResult.getConflicts()) {
                writer.write("CONFLICT(content): Merge conflict in: " + conflict);
                writer.write('\n');
            }
            writer.write("Automatic merge failed; fix conflicts and then commit the result");
            writer.write('\n');
            break;
        case NOT_SUPPORTED:
            writer.write("Operation not supported");
            writer.write('\n');
            break;
        default:
            break;
    }
    writer.flush();
}
Also used : MergeResult(org.eclipse.che.api.git.shared.MergeResult) OutputStreamWriter(java.io.OutputStreamWriter) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 5 with MergeResult

use of org.eclipse.che.api.git.shared.MergeResult in project che by eclipse.

the class MergeTest method testMergeConflict.

@Test(dataProvider = "GitConnectionFactory", dataProviderClass = org.eclipse.che.git.impl.GitConnectionFactoryProvider.class)
public void testMergeConflict(GitConnectionFactory connectionFactory) throws Exception {
    //given
    GitConnection connection = connectToGitRepositoryWithContent(connectionFactory, repository);
    connection.checkout(CheckoutParams.create(branchName).withCreateNew(true));
    addFile(connection, "t-merge-conflict", "aaa\n");
    connection.add(AddParams.create(new ArrayList<>(singletonList("."))));
    connection.commit(CommitParams.create("add file in new branch"));
    connection.checkout(CheckoutParams.create("master"));
    addFile(connection, "t-merge-conflict", "bbb\n");
    connection.add(AddParams.create(new ArrayList<>(singletonList("."))));
    connection.commit(CommitParams.create("add file in new branch"));
    //when
    MergeResult mergeResult = connection.merge(branchName);
    //then
    List<String> conflicts = mergeResult.getConflicts();
    assertEquals(conflicts.size(), 1);
    assertEquals(conflicts.get(0), "t-merge-conflict");
    assertEquals(mergeResult.getMergeStatus(), MergeResult.MergeStatus.CONFLICTING);
    String expContent = //
    "<<<<<<< HEAD\n" + //
    "bbb\n" + //
    "=======\n" + //
    "aaa\n" + ">>>>>>> MergeTestBranch\n";
    String actual = Files.toString(new File(connection.getWorkingDir(), "t-merge-conflict"), Charsets.UTF_8);
    assertEquals(actual, expContent);
}
Also used : ArrayList(java.util.ArrayList) MergeResult(org.eclipse.che.api.git.shared.MergeResult) GitConnection(org.eclipse.che.api.git.GitConnection) File(java.io.File) GitTestUtil.addFile(org.eclipse.che.git.impl.GitTestUtil.addFile) Test(org.testng.annotations.Test)

Aggregations

MergeResult (org.eclipse.che.api.git.shared.MergeResult)7 ArrayList (java.util.ArrayList)3 GitConnection (org.eclipse.che.api.git.GitConnection)3 Test (org.testng.annotations.Test)3 File (java.io.File)2 GitTestUtil.addFile (org.eclipse.che.git.impl.GitTestUtil.addFile)2 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)1 GitException (org.eclipse.che.api.git.exception.GitException)1 MergeRequest (org.eclipse.che.api.git.shared.MergeRequest)1 Operation (org.eclipse.che.api.promises.client.Operation)1 OperationException (org.eclipse.che.api.promises.client.OperationException)1 PromiseError (org.eclipse.che.api.promises.client.PromiseError)1 ConfirmCallback (org.eclipse.che.ide.api.dialogs.ConfirmCallback)1 ServerException (org.eclipse.che.ide.commons.exception.ServerException)1 GitOutputConsole (org.eclipse.che.ide.ext.git.client.outputconsole.GitOutputConsole)1 CheckoutConflictException (org.eclipse.jgit.api.errors.CheckoutConflictException)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1