Search in sources :

Example 6 with GitException

use of alien4cloud.exception.GitException in project alien4cloud by alien4cloud.

the class RepositoryManager method renameBranches.

public static void renameBranches(Path repositoryDirectory, Map<String, String> branchOldNameToNewName) {
    Git git = null;
    try {
        git = Git.open(repositoryDirectory.toFile());
        for (Map.Entry<String, String> entry : branchOldNameToNewName.entrySet()) {
            String oldBranchName = entry.getKey();
            String newBranchName = entry.getValue();
            if (branchExistsLocally(git, oldBranchName)) {
                git.branchRename().setOldName(oldBranchName).setNewName(newBranchName).call();
            }
        }
    } catch (IOException | GitAPIException e) {
        throw new GitException("Unable to rename all branches", e);
    } finally {
        close(git);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException) IOException(java.io.IOException)

Example 7 with GitException

use of alien4cloud.exception.GitException in project alien4cloud by alien4cloud.

the class RepositoryManager method pull.

/**
 * Trigger a Pull Request on an existing repository.
 *
 * @param repository The git repository to pull.
 * @param username The username to use for the repository connection.
 * @param password The password to use for the repository connection.
 * @return True if the pull request has updated the data, false if the repository was already up to date.
 */
public static boolean pull(Git repository, String username, String password) {
    try {
        PullCommand pullCommand = repository.pull();
        setCredentials(pullCommand, username, password);
        PullResult result = pullCommand.call();
        MergeResult mergeResult = result.getMergeResult();
        return mergeResult == null || MergeResult.MergeStatus.ALREADY_UP_TO_DATE != mergeResult.getMergeStatus();
    } catch (GitAPIException e) {
        throw new GitException("Failed to pull git repository", e);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException)

Example 8 with GitException

use of alien4cloud.exception.GitException in project alien4cloud by alien4cloud.

the class RepositoryManager method cloneOrCheckout.

/**
 * @param targetDirectory The root directory that will contains the localDirectory in which to checkout the archives.
 * @param repositoryUrl The url of the repository to checkout or clone.
 * @param username The username to use for the repository connection.
 * @param password The password to use for the repository connection.
 * @param branch The branch to checkout or clone.
 * @param localDirectory The path, relative to targetDirectory, in which to checkout or clone the git directory.
 */
public static Git cloneOrCheckout(Path targetDirectory, String repositoryUrl, String username, String password, String branch, String localDirectory) {
    try {
        Files.createDirectories(targetDirectory);
        Path targetPath = targetDirectory.resolve(localDirectory);
        Git repository;
        if (Files.exists(targetPath)) {
            try {
                repository = Git.open(targetPath.toFile());
                fetch(repository, username, password);
                checkoutRepository(repository, branch);
            } catch (RepositoryNotFoundException e) {
                // TODO delete the folder
                FileUtil.delete(targetPath);
                repository = cloneRepository(repositoryUrl, username, password, branch, targetPath);
            }
        } else {
            Files.createDirectories(targetPath);
            repository = cloneRepository(repositoryUrl, username, password, branch, targetPath);
        }
        return repository;
    } catch (IOException e) {
        throw new GitException("Error while creating target directory", e);
    }
}
Also used : Path(java.nio.file.Path) GitException(alien4cloud.exception.GitException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException)

Example 9 with GitException

use of alien4cloud.exception.GitException in project alien4cloud by alien4cloud.

the class RepositoryManager method commitAll.

/**
 * Commit all changes in the given repository.
 *
 * @param targetDirectory The target directory.
 */
public static void commitAll(Path targetDirectory, String userName, String userEmail, String commitMessage) {
    Git repository = null;
    try {
        repository = Git.open(targetDirectory.toFile());
        repository.add().addFilepattern(".").call();
        String name = defaultUsernameIfNull(userName);
        String email = defaultEmailIfNull(name, userEmail);
        repository.commit().setCommitter(name, email).setMessage(commitMessage).call();
    } catch (GitAPIException | IOException e) {
        throw new GitException("Unable to commit to the git repository", e);
    } finally {
        close(repository);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException) IOException(java.io.IOException)

Example 10 with GitException

use of alien4cloud.exception.GitException in project alien4cloud by alien4cloud.

the class DeploymentConfigurationDao method deleteAllByTopologyVersionId.

public void deleteAllByTopologyVersionId(String applicationId, String versionId) {
    // delete local branch + related stash
    List<Path> paths = localGitRepositoryPathResolver.findAllEnvironmentSetupLocalPath(applicationId);
    for (Path path : paths) {
        try {
            RepositoryManager.dropStash(path, "a4c_stash_" + versionId);
            RepositoryManager.deleteBranch(path, versionId, false);
        } catch (GitException e) {
            log.error("Error when deleting data related to the topology version <" + versionId + "> from local git.", e);
        }
    }
}
Also used : Path(java.nio.file.Path) GitException(alien4cloud.exception.GitException)

Aggregations

GitException (alien4cloud.exception.GitException)21 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)16 IOException (java.io.IOException)15 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 Path (java.nio.file.Path)3 GitConflictException (alien4cloud.exception.GitConflictException)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 NoHeadException (org.eclipse.jgit.api.errors.NoHeadException)1 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)1 Repository (org.eclipse.jgit.lib.Repository)1 StoredConfig (org.eclipse.jgit.lib.StoredConfig)1