use of alien4cloud.exception.GitException in project alien4cloud by alien4cloud.
the class RepositoryManager method cloneRepository.
private static Git cloneRepository(String url, String username, String password, String branch, Path targetPath) throws IOException {
log.debug("Cloning from [{}] branch [{}] to [{}]", url, branch, targetPath.toString());
Git result;
try {
CloneCommand cloneCommand = Git.cloneRepository().setURI(url).setBranch(branch).setDirectory(targetPath.toFile());
setCredentials(cloneCommand, username, password);
result = cloneCommand.call();
log.debug("Cloned repository to [{}]: ", result.getRepository().getDirectory());
return result;
} catch (GitAPIException e) {
// if the import fails then we should try to remove the created directory
FileUtil.delete(targetPath);
throw new GitException("Failed to clone git repository", e);
}
}
use of alien4cloud.exception.GitException in project alien4cloud by alien4cloud.
the class RepositoryManager method setRemote.
/**
* Set a remote repository path.
*
* @param repositoryDirectory The directory in which the git repo exists.
* @param remoteName The name of the remote (i.e. 'origin')
* @param remoteUrl The url of the repository.
*/
public static void setRemote(Path repositoryDirectory, String remoteName, String remoteUrl) {
Git git = null;
try {
git = Git.open(repositoryDirectory.toFile());
StoredConfig config = git.getRepository().getConfig();
config.unsetSection("remote", remoteName);
RemoteConfig remoteConfig = new RemoteConfig(config, remoteName);
remoteConfig.addURI(new URIish(remoteUrl));
remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + remoteName + "/*"));
remoteConfig.update(config);
config.save();
} catch (URISyntaxException | IOException e) {
throw new GitException("Unable to set the remote repository", e);
} finally {
close(git);
}
}
use of alien4cloud.exception.GitException in project alien4cloud by alien4cloud.
the class RepositoryManager method getHistory.
/**
* Return a simplified git commit history list.
*
* @param repositoryDirectory The directory in which the git repo exists.
* @param from Start to query from the given history.
* @param count The number of history entries to retrieve.
* @return A list of simplified history entries.
*/
public static List<SimpleGitHistoryEntry> getHistory(Path repositoryDirectory, int from, int count) {
Git repository = null;
try {
repository = Git.open(repositoryDirectory.toFile());
Iterable<RevCommit> commits = repository.log().setSkip(from).setMaxCount(count).call();
List<SimpleGitHistoryEntry> historyEntries = Lists.newArrayList();
for (RevCommit commit : commits) {
historyEntries.add(new SimpleGitHistoryEntry(commit.getId().getName(), commit.getAuthorIdent().getName(), commit.getAuthorIdent().getEmailAddress(), commit.getFullMessage(), new Date(commit.getCommitTime() * 1000L)));
}
return historyEntries;
} catch (NoHeadException e) {
log.debug("Your repository has no head, you need to save your topology before using the git history.");
return Lists.newArrayList();
} catch (GitAPIException | IOException e) {
throw new GitException("Unable to get history from the git repository", e);
} finally {
close(repository);
}
}
use of alien4cloud.exception.GitException in project alien4cloud by alien4cloud.
the class RepositoryManager method push.
/**
* Git push to a remote.
*
* @param git The git repository.
* @param username The username to use for the repository connection.
* @param password The password to use for the repository connection.
* @param localBranch The name of the local branch to push.
* @param remoteBranch The name of the remote branch to push to.
* @return <code>true</code> pushed, <code>false</code> otherwise.
*/
public static boolean push(Git git, String username, String password, String localBranch, String remoteBranch) {
try {
if (git.getRepository().getRemoteNames().isEmpty()) {
throw new GitException("No remote found for the repository");
}
PushCommand pushCommand = git.push();
setCredentials(pushCommand, username, password);
RefSpec refSpec = new RefSpec(String.format("refs/heads/%s:refs/heads/%s", localBranch, remoteBranch));
pushCommand.setRefSpecs(refSpec);
Iterable<PushResult> call = pushCommand.call();
return isPushed(call);
} catch (GitAPIException e) {
throw new GitException(String.format("Error when trying to git push: %s", e.getMessage()), e);
}
}
use of alien4cloud.exception.GitException in project alien4cloud by alien4cloud.
the class RepositoryManager method dropStash.
public static void dropStash(Path repositoryDirectory, String stashId) {
Git git = null;
try {
git = Git.open(repositoryDirectory.toFile());
int stashIndex = 0;
Collection<RevCommit> stashes = git.stashList().call();
for (RevCommit stash : stashes) {
if (stash.getFullMessage().equals(stashId)) {
git.stashDrop().setStashRef(stashIndex).call();
log.debug("Stash <" + stashId + "> has been dropped on <" + repositoryDirectory + ">");
}
stashIndex++;
}
} catch (IOException | GitAPIException e) {
throw new GitException("Failed to apply then drop stash", e);
} finally {
close(git);
}
}
Aggregations