Search in sources :

Example 31 with GitException

use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.

the class JGitConnection method clone.

public void clone(CloneParams params) throws GitException, UnauthorizedException {
    String remoteUri = params.getRemoteUrl();
    boolean removeIfFailed = false;
    try {
        if (params.getRemoteName() == null) {
            params.setRemoteName(Constants.DEFAULT_REMOTE_NAME);
        }
        if (params.getWorkingDir() == null) {
            params.setWorkingDir(repository.getWorkTree().getCanonicalPath());
        }
        // If clone fails and the .git folder didn't exist we want to remove it.
        // We have to do this here because the clone command doesn't revert its own changes in case of failure.
        removeIfFailed = !repository.getDirectory().exists();
        CloneCommand cloneCommand = Git.cloneRepository().setDirectory(new File(params.getWorkingDir())).setRemote(params.getRemoteName()).setCloneSubmodules(params.isRecursive()).setURI(remoteUri);
        if (params.getBranchesToFetch().isEmpty()) {
            cloneCommand.setCloneAllBranches(true);
        } else {
            cloneCommand.setBranchesToClone(params.getBranchesToFetch());
        }
        LineConsumer lineConsumer = lineConsumerFactory.newLineConsumer();
        cloneCommand.setProgressMonitor(new BatchingProgressMonitor() {

            @Override
            protected void onUpdate(String taskName, int workCurr) {
                try {
                    lineConsumer.writeLine(taskName + ": " + workCurr + " completed");
                } catch (IOException exception) {
                    LOG.error(exception.getMessage(), exception);
                }
            }

            @Override
            protected void onEndTask(String taskName, int workCurr) {
            }

            @Override
            protected void onUpdate(String taskName, int workCurr, int workTotal, int percentDone) {
                try {
                    lineConsumer.writeLine(taskName + ": " + workCurr + " of " + workTotal + " completed, " + percentDone + "% done");
                } catch (IOException exception) {
                    LOG.error(exception.getMessage(), exception);
                }
            }

            @Override
            protected void onEndTask(String taskName, int workCurr, int workTotal, int percentDone) {
            }
        });
        ((Git) executeRemoteCommand(remoteUri, cloneCommand, params.getUsername(), params.getPassword())).close();
        StoredConfig repositoryConfig = getRepository().getConfig();
        GitUser gitUser = getUser();
        if (gitUser != null) {
            repositoryConfig.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME, gitUser.getName());
            repositoryConfig.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL, gitUser.getEmail());
        }
        repositoryConfig.save();
    } catch (IOException | GitAPIException exception) {
        // Delete .git directory in case it was created
        if (removeIfFailed) {
            deleteRepositoryFolder();
        }
        //try to clone repository by replacing http to https in the url if HTTP 301 redirect happened
        if (exception.getMessage().contains(": 301 Moved Permanently")) {
            remoteUri = "https" + remoteUri.substring(4);
            try {
                clone(params.withRemoteUrl(remoteUri));
            } catch (UnauthorizedException | GitException e) {
                throw new GitException("Failed to clone the repository", e);
            }
            return;
        }
        String message = generateExceptionMessage(exception);
        throw new GitException(message, exception);
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) GitException(org.eclipse.che.api.git.exception.GitException) IOException(java.io.IOException) GitUser(org.eclipse.che.api.git.shared.GitUser) StoredConfig(org.eclipse.jgit.lib.StoredConfig) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) Git(org.eclipse.jgit.api.Git) BatchingProgressMonitor(org.eclipse.jgit.lib.BatchingProgressMonitor) DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) File(java.io.File)

Example 32 with GitException

use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.

the class JGitConnection method mv.

@Override
public void mv(String source, String target) throws GitException {
    try {
        getGit().add().addFilepattern(target).call();
        getGit().rm().addFilepattern(source).call();
    } catch (GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(org.eclipse.che.api.git.exception.GitException)

Example 33 with GitException

use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.

the class RemoteDeleteTest method testRemoteDelete.

@Test(dataProvider = "GitConnectionFactory", dataProviderClass = org.eclipse.che.git.impl.GitConnectionFactoryProvider.class)
public void testRemoteDelete(GitConnectionFactory connectionFactory) throws GitException, IOException {
    GitConnection connection = connectToInitializedGitRepository(connectionFactory, repository);
    connection.remoteAdd(RemoteAddParams.create("origin", "host.com:username/Repo.git"));
    //now it is 1 remote
    assertEquals(connection.remoteList(null, false).size(), 1);
    //try delete not existing remote
    try {
        connection.remoteDelete("donotexists");
        fail("should be exception");
    } catch (GitException ignored) {
    }
    connection.remoteDelete("origin");
    //now it is 0 remotes
    assertEquals(connection.remoteList(null, false).size(), 0);
}
Also used : GitException(org.eclipse.che.api.git.exception.GitException) GitConnection(org.eclipse.che.api.git.GitConnection) Test(org.testng.annotations.Test)

Aggregations

GitException (org.eclipse.che.api.git.exception.GitException)33 IOException (java.io.IOException)20 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)18 ArrayList (java.util.ArrayList)10 File (java.io.File)9 DiffCommitFile (org.eclipse.che.api.git.shared.DiffCommitFile)9 StoredConfig (org.eclipse.jgit.lib.StoredConfig)9 Ref (org.eclipse.jgit.lib.Ref)8 URISyntaxException (java.net.URISyntaxException)6 RefSpec (org.eclipse.jgit.transport.RefSpec)6 GitConnection (org.eclipse.che.api.git.GitConnection)5 CheckoutConflictException (org.eclipse.jgit.api.errors.CheckoutConflictException)5 Branch (org.eclipse.che.api.git.shared.Branch)4 GitUser (org.eclipse.che.api.git.shared.GitUser)4 MergeResult (org.eclipse.che.api.git.shared.MergeResult)4 Remote (org.eclipse.che.api.git.shared.Remote)4 FetchCommand (org.eclipse.jgit.api.FetchCommand)4 RebaseResult (org.eclipse.jgit.api.RebaseResult)4 FetchResult (org.eclipse.jgit.transport.FetchResult)4 PushResult (org.eclipse.jgit.transport.PushResult)4