Search in sources :

Example 1 with GitException

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

the class RepositoryManager method rebranch.

/**
 * Rebranch the local and remote branch.
 *
 * @param git The git repository.
 * @param localBranch The name of the local branch.
 * @param remoteBranch The name of the remote branch.
 */
public static void rebranch(Git git, String localBranch, String remoteBranch) {
    String tmpBranchName = "a4c-switch";
    try {
        log.debug(String.format("Prepare git repository=%s to re-branch=%s on remote branch=%s", git.getRepository().getDirectory(), localBranch, remoteBranch));
        CheckoutCommand checkoutCommand = git.checkout();
        checkoutCommand.setStartPoint("origin/" + remoteBranch);
        checkoutCommand.setName(tmpBranchName);
        checkoutCommand.setCreateBranch(true);
        checkoutCommand.call();
        log.debug(String.format("Delete branch=%s from git repository=%s", localBranch, git.getRepository().getDirectory()));
        DeleteBranchCommand deleteBranchCommand = git.branchDelete();
        deleteBranchCommand.setBranchNames(localBranch);
        deleteBranchCommand.setForce(true);
        deleteBranchCommand.call();
        log.debug(String.format("Finalize git re-branch=%s for repository=%s", localBranch, git.getRepository().getDirectory()));
        RenameBranchCommand renameBranchCommand = git.branchRename();
        renameBranchCommand.setOldName(tmpBranchName);
        renameBranchCommand.setNewName(localBranch);
        renameBranchCommand.call();
    } catch (GitAPIException e) {
        throw new GitException("Couldn't rebranch to origin common branch", e);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException)

Example 2 with GitException

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

the class RepositoryManager method create.

/**
 * Create a git repository that includes an optional readme file.
 *
 * @param targetDirectory The path of the repository to create.
 * @param readmeContentIfEmpty
 */
public static void create(Path targetDirectory, String readmeContentIfEmpty) {
    Git repository = null;
    try {
        repository = Git.init().setDirectory(targetDirectory.toFile()).call();
        if (readmeContentIfEmpty != null) {
            Path readmePath = targetDirectory.resolve("readme.txt");
            File file = readmePath.toFile();
            file.createNewFile();
            try (BufferedWriter writer = Files.newBufferedWriter(readmePath)) {
                writer.write(readmeContentIfEmpty);
            }
        }
    } catch (GitAPIException | IOException e) {
        throw new GitException("Error while creating git repository", e);
    } finally {
        close(repository);
    }
}
Also used : Path(java.nio.file.Path) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 3 with GitException

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

the class RepositoryManager method deleteBranch.

public static void deleteBranch(Path targetDirectory, String branch, boolean deleteRemoteBranch) {
    Git repository = null;
    try {
        repository = Git.open(targetDirectory.toFile());
        // delete locally
        if (branchExistsLocally(repository, branch)) {
            // is current branch?
            if (repository.getRepository().getBranch().equals(branch)) {
                checkoutExistingBranchOrCreateOrphan(repository, true, null, null, "tmp");
            }
            repository.branchDelete().setForce(true).setBranchNames("refs/heads/" + branch).call();
        }
        // delete remote branch
        if (deleteRemoteBranch) {
            RefSpec refSpec = new RefSpec().setSource(null).setDestination("refs/heads/" + branch);
            repository.push().setRefSpecs(refSpec).setRemote("origin").call();
        }
    } catch (IOException | GitAPIException e) {
        throw new GitException("Error while deleting branch <" + branch + ">", e);
    } finally {
        close(repository);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException) IOException(java.io.IOException)

Example 4 with GitException

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

the class RepositoryManager method fetch.

/**
 * Fetch a git repository.
 *
 * @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.
 */
public static void fetch(Git git, String username, String password) {
    try {
        FetchCommand fetchCommand = git.fetch();
        setCredentials(fetchCommand, username, password);
        FetchResult fetchResult = fetchCommand.call();
        log.debug(String.format("Fetched git repository=%s messages=%s", git.getRepository().getDirectory(), fetchResult.getMessages()));
    } catch (GitAPIException e) {
        throw new GitException("Unable to fetch git repository", e);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException)

Example 5 with GitException

use of alien4cloud.exception.GitException 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);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException) IOException(java.io.IOException) GitConflictException(alien4cloud.exception.GitConflictException)

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