All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.codacy.scoobydoo.GitWrapper Maven / Gradle / Ivy
package com.codacy.scoobydoo;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import java.io.File;
import java.io.IOException;
// TODO: create GitWrapperTests.
public class GitWrapper {
private org.eclipse.jgit.api.Git git;
private final String localPath;
public GitWrapper(String localPath, String remotePath) throws GitAPIException, IOException {
this.localPath = localPath;
File tempGitDirectory = new File(localPath);
CloneCommand git = org.eclipse.jgit.api.Git.cloneRepository()
.setURI(remotePath)
.setDirectory(new File(localPath));
if (tempGitDirectory.exists()) {
FileUtils.deleteDirectory(tempGitDirectory);
}
this.git = git.call();
}
public String getLatestCommitHash() throws GitAPIException {
RevCommit latestCommit = git
.log()
.setMaxCount(1)
.call()
.iterator()
.next();
return latestCommit.getName();
}
public GitWrapper gitCreateBranch(String branchName, String username, String pwd) throws GitAPIException {
if (git
.branchList()
.setListMode(ListBranchCommand.ListMode.ALL)
.call()
.stream()
.map(Ref::getName)
.toList()
.contains("refs/remotes/origin/" + branchName
)){
LoggingHelper.warn("Branch already exists. Deleting branch [" + branchName + "].");
try {
gitDeleteBranchWithForce(branchName, username, pwd, true);
} catch (Exception e) {
LoggingHelper.error("Error deleting branch.", e);
throw e;
}
}
LoggingHelper.info("Creating branch [" + branchName + "].");
git.branchCreate()
.setName(branchName)
.call();
return this;
}
public GitWrapper gitCheckout(String branchName) throws GitAPIException {
LoggingHelper.info("Checking out to branch [" + branchName + "].");
git.checkout()
.setName(branchName)
.call();
return this;
}
public void gitAddFile(String fileName) throws GitAPIException {
LoggingHelper.info("Adding file [" + fileName + "].");
git.add()
.addFilepattern(fileName)
.call();
}
public void gitCommit(String message) throws GitAPIException {
LoggingHelper.info("Committing.");
git.commit()
.setMessage(message)
.call();
}
public void gitPush(String username, String password) throws GitAPIException {
gitPush(username, password, false);
}
public void gitPushForce(String username, String password) throws GitAPIException {
LoggingHelper.info("You are pushing with force, I hope you know what you're doing.");
gitPush(username, password, true);
}
private void gitPush(String username, String password, Boolean withForce) throws GitAPIException {
LoggingHelper.info("Pushing.");
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
pushCommand.setForce(withForce);
pushCommand.call();
}
public GitWrapper gitDeleteBranchWithForce(String branchName, String username, String password, Boolean withForce) throws GitAPIException {
//delete Local
LoggingHelper.info("Deleting branch [" + branchName + "].");
git.branchDelete()
.setBranchNames("refs/heads/" + branchName)
.setForce(withForce)
.call();
//delete remote
RefSpec refSpec = new RefSpec()
.setSource(null)
.setDestination("refs/heads/" + branchName);
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
pushCommand.setRefSpecs(refSpec).setRemote("origin").call();
return this;
}
public void gitDeleteBranch(String branchName, String username, String password) throws GitAPIException {
gitDeleteBranchWithForce(branchName, username, password, false);
}
public Git gitClient() {
if (git == null) {
final String errorMessage = "Git client is not defined.";
NullPointerException exception = new NullPointerException(errorMessage);
LoggingHelper.error(errorMessage, exception);
throw exception;
}
return git;
}
public void destroyAndClean() {
this.git = null;
try {
FileUtils.deleteDirectory(new File(localPath));
} catch(Exception e) {
LoggingHelper.warn("Couldn't delete directory [" + localPath + "], but this will be ignored and the test will continue. Exception was: " + e);
}
}
}