use of alien4cloud.exception.GitConflictException in project alien4cloud by alien4cloud.
the class RepositoryManager method pull.
/**
* Pull modifications a git repository.
*
* @param repositoryDirectory The directory in which the git repo exists.
* @param username The username for the git repository connection, null if none.
* @param password The password for the git repository connection, null if none.
* @param remoteBranch The name of the remote branch to pull from.
*/
public static void pull(Path repositoryDirectory, String username, String password, String remoteBranch) {
Git git = null;
try {
git = Git.open(repositoryDirectory.resolve(".git").toFile());
if (git.getRepository().getRemoteNames().isEmpty()) {
throw new GitException("No remote found for the repository");
}
checkRepositoryState(git.getRepository().getRepositoryState(), "Git pull operation failed");
PullCommand pullCommand = git.pull();
setCredentials(pullCommand, username, password);
pullCommand.setRemoteBranchName(remoteBranch);
PullResult call = pullCommand.call();
if (call.getMergeResult() != null && call.getMergeResult().getConflicts() != null && !call.getMergeResult().getConflicts().isEmpty()) {
throw new GitConflictException(git.getRepository().getBranch());
}
log.debug(String.format("Successfully pulled from %s", call.getFetchedFrom()));
} catch (IOException e) {
throw new GitException("Unable to open the git repository", e);
} catch (GitAPIException e) {
throw new GitException("Unable to pull the git repository", e);
} finally {
close(git);
}
}
use of alien4cloud.exception.GitConflictException in project alien4cloud by alien4cloud.
the class RepositoryManager method push.
/**
* Git push to a remote.
*
* @param repositoryDirectory The directory in which the git repo exists.
* @param username The username to use for the repository connection.
* @param password The password to use for the repository connection.
* @return <code>true</code> pushed, <code>false</code> otherwise.
*/
public static boolean push(Path repositoryDirectory, String username, String password, String remoteBranch) {
Git git = null;
try {
git = Git.open(repositoryDirectory.toFile());
checkRepositoryState(git.getRepository().getRepositoryState(), "Git push operation failed.");
Repository repository = git.getRepository();
// If no given remoteBranch, use the default one (i.e. master).
String targetRemoteBranch = remoteBranch == null ? repository.getBranch() : remoteBranch;
boolean isPushed = push(git, username, password, repository.getBranch(), targetRemoteBranch);
if (!isPushed) {
// If not pushed, then we have a conflict.
// Push the current commit into a new alien branch.
// Then rebranch to the current branch.
// Only handle one remote (default: 'origin')
String remoteName = repository.getRemoteNames().iterator().next();
log.debug(String.format("Couldn't push git repository=%s to remote=%s on the branch=%s", git.getRepository().getDirectory(), remoteName, repository.getBranch()));
fetch(git, username, password);
String conflictBranchName = generateConflictBranchName(repository, remoteName);
isPushed = push(git, username, password, repository.getBranch(), conflictBranchName);
if (isPushed) {
log.debug(String.format("Pushed git repository=%s on branch=%s", git.getRepository().getDirectory(), conflictBranchName));
rebranch(git, repository.getBranch(), targetRemoteBranch);
}
throw new GitConflictException(remoteName, repository.getBranch(), conflictBranchName);
} else {
log.debug(String.format("Pushed git repository=%s on branch=%s", git.getRepository().getDirectory(), targetRemoteBranch));
}
return isPushed;
} catch (IOException e) {
throw new GitException("Unable to open the remote repository", e);
} finally {
close(git);
}
}
Aggregations