Add first integration tests for merge detection

This commit is contained in:
René Pfeuffer
2020-08-06 14:40:43 +02:00
parent 072d8f15c9
commit 6f20781812
21 changed files with 1071 additions and 7 deletions

View File

@@ -0,0 +1,49 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository.client.spi;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import sonia.scm.repository.client.api.RepositoryClientException;
import java.io.IOException;
public class GitCheckoutCommand implements CheckoutCommand {
private Git git;
GitCheckoutCommand(Git git) {
this.git = git;
}
@Override
public void checkout(String name) throws IOException {
try {
git.checkout().setName(name).call();
} catch (GitAPIException ex) {
throw new RepositoryClientException("could not checkout branch or revision", ex);
}
}
}

View File

@@ -0,0 +1,64 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository.client.spi;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
import sonia.scm.repository.client.api.RepositoryClientException;
import java.io.IOException;
public class GitDeleteRemoteBranchCommand implements DeleteRemoteBranchCommand {
private final Git git;
private final CredentialsProvider credentialsProvider;
GitDeleteRemoteBranchCommand(Git git, CredentialsProvider credentialsProvider) {
this.git = git;
this.credentialsProvider = credentialsProvider;
}
@Override
public void delete(String name) throws IOException {
try {
git.branchDelete().setBranchNames("refs/heads/" + name).call();
RefSpec refSpec = new RefSpec()
.setSource(null)
.setDestination("refs/heads/" + name);
PushCommand push = git.push();
if (credentialsProvider != null) {
push.setCredentialsProvider(credentialsProvider);
}
push.setRefSpecs(refSpec).call();
// List<String> result = git.branchDelete().setBranchNames(name).call();
} catch (GitAPIException ex) {
throw new RepositoryClientException("could not delete branch", ex);
}
}
}

View File

@@ -0,0 +1,64 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository.client.spi;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.GitChangesetConverter;
import sonia.scm.repository.client.api.RepositoryClientException;
import java.io.IOException;
public class GitMergeCommand implements MergeCommand {
private final Git git;
GitMergeCommand(Git git) {
this.git = git;
}
@Override
public Changeset merge(MergeRequest request) throws IOException {
try (GitChangesetConverter converter = new GitChangesetConverter(git.getRepository())) {
ObjectId resolved = git.getRepository().resolve(request.getBranch());
MergeResult mergeResult = git.merge()
.include(request.getBranch(), resolved)
.setMessage(request.getMessage())
.call();
try (RevWalk revWalk = new RevWalk(git.getRepository())) {
RevCommit commit = revWalk.parseCommit(mergeResult.getNewHead());
return converter.createChangeset(commit);
}
} catch (GitAPIException ex) {
throw new RepositoryClientException("could not commit changes to repository", ex);
}
}
}

View File

@@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository.client.spi;
//~--- non-JDK imports --------------------------------------------------------
@@ -49,7 +49,7 @@ public class GitRepositoryClientProvider extends RepositoryClientProvider
private static final Set<ClientCommand> SUPPORTED_COMMANDS =
ImmutableSet.of(ClientCommand.ADD, ClientCommand.REMOVE,
ClientCommand.COMMIT, ClientCommand.TAG, ClientCommand.BRANCH,
ClientCommand.PUSH);
ClientCommand.DELETE_REMOTE_BRANCH, ClientCommand.MERGE, ClientCommand.PUSH);
//~--- constructors ---------------------------------------------------------
@@ -118,6 +118,16 @@ public class GitRepositoryClientProvider extends RepositoryClientProvider
return new GitBranchCommand(git);
}
@Override
public DeleteRemoteBranchCommand getDeleteRemoteBranchCommand() {
return new GitDeleteRemoteBranchCommand(git, credentialsProvider);
}
@Override
public CheckoutCommand getCheckoutCommand() {
return new GitCheckoutCommand(git);
}
/**
* Method description
*
@@ -178,6 +188,11 @@ public class GitRepositoryClientProvider extends RepositoryClientProvider
return new GitTagCommand(git);
}
@Override
public MergeCommand getMergeCommand() {
return new GitMergeCommand(git);
}
@Override
public File getWorkingCopy() {
return git.getRepository().getWorkTree();