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);
}
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);
}
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");
}
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();
}
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);
}
Aggregations