Search in sources :

Example 16 with GitException

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

the class RepositoryManager method clean.

/**
 * Clean the current modifications of the repository
 *
 * @param repositoryDirectory
 */
public static void clean(Path repositoryDirectory) {
    Git repository = null;
    try {
        repository = Git.open(repositoryDirectory.resolve(".git").toFile());
        CleanCommand cleanCommand = repository.clean();
        cleanCommand.setIgnore(true);
        cleanCommand.call();
    } catch (IOException e) {
        throw new GitException("Unable to open the git repository", e);
    } catch (GitAPIException e) {
        throw new GitException("Unable to clean 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 17 with GitException

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

the class RepositoryManager method checkoutExistingBranchOrCreateOrphan.

public static void checkoutExistingBranchOrCreateOrphan(Path repositoryDirectory, boolean isLocalOnly, String username, String password, String branch) {
    Git git = null;
    try {
        git = Git.open(repositoryDirectory.toFile());
        checkoutExistingBranchOrCreateOrphan(git, isLocalOnly, username, password, branch);
    } catch (IOException | GitAPIException e) {
        throw new GitException("Git repository related issue", e);
    } finally {
        close(git);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException) IOException(java.io.IOException)

Example 18 with GitException

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

the class RepositoryManager method applyStashThenDrop.

public static void applyStashThenDrop(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.stashApply().setStashRef(stash.getName()).call();
                git.stashDrop().setStashRef(stashIndex).call();
                log.debug("Stash <" + stashId + ">  applied/dropped on <" + repositoryDirectory + ">");
                break;
            }
            stashIndex++;
        }
    } catch (IOException | GitAPIException e) {
        throw new GitException("Failed to apply then drop stash", e);
    } finally {
        close(git);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 19 with GitException

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

Example 20 with GitException

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

the class RepositoryManager method stash.

public static void stash(Path repositoryDirectory, String stashId) {
    Git git = null;
    try {
        log.debug("Stashing change from <" + repositoryDirectory + "> to stash <" + stashId + ">");
        git = Git.open(repositoryDirectory.toFile());
        Collection<RevCommit> stashes = git.stashList().call();
        int stashIndex = 0;
        for (RevCommit stash : stashes) {
            if (stash.getFullMessage().equals(stashId)) {
                git.stashDrop().setStashRef(stashIndex).call();
                log.warn("Stash <" + stashId + "> was already existing in <" + repositoryDirectory + ">. It has been deleted.");
                break;
            }
            stashIndex++;
        }
        git.stashCreate().setIncludeUntracked(true).setWorkingDirectoryMessage(stashId).call();
    } catch (IOException | GitAPIException e) {
        throw new GitException("Failed to stash data", e);
    } finally {
        close(git);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(alien4cloud.exception.GitException) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

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