Search in sources :

Example 16 with GitException

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

the class PushTest method testPushWhenLocalRepositoryIsNotSynchronisedWithRemote.

@Test(dataProvider = "GitConnectionFactory", dataProviderClass = org.eclipse.che.git.impl.GitConnectionFactoryProvider.class)
public void testPushWhenLocalRepositoryIsNotSynchronisedWithRemote(GitConnectionFactory connectionFactory) throws IOException, ServerException, URISyntaxException, UnauthorizedException {
    //given
    GitConnection remoteConnection = connectToGitRepositoryWithContent(connectionFactory, repository);
    GitConnection localConnection = connectionFactory.getConnection(remoteRepo.getAbsolutePath());
    localConnection.clone(CloneParams.create(remoteConnection.getWorkingDir().getAbsolutePath()));
    addFile(remoteConnection, "newfile", "content");
    remoteConnection.add(AddParams.create(singletonList(".")));
    remoteConnection.commit(CommitParams.create("Fake commit"));
    //when
    String errorMessage = "";
    try {
        localConnection.push(PushParams.create("origin").withTimeout(-1));
    } catch (GitException exception) {
        errorMessage = exception.getMessage();
    }
    //then
    assertTrue(errorMessage.contains("master -> master"));
    assertTrue(errorMessage.contains(remoteConnection.getWorkingDir().getAbsolutePath()));
}
Also used : GitException(org.eclipse.che.api.git.exception.GitException) GitConnection(org.eclipse.che.api.git.GitConnection) Test(org.testng.annotations.Test)

Example 17 with GitException

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

the class GitHubService method updateSSHKey.

@POST
@Path("ssh/generate")
public void updateSSHKey() throws ApiException {
    final String host = "github.com";
    SshPair sshPair = null;
    try {
        sshPair = sshServiceClient.getPair("vcs", host);
    } catch (NotFoundException ignored) {
    }
    if (sshPair != null) {
        if (sshPair.getPublicKey() == null) {
            sshServiceClient.removePair("vcs", host);
            sshPair = sshServiceClient.generatePair(newDto(GenerateSshPairRequest.class).withService("vcs").withName(host));
        }
    } else {
        sshPair = sshServiceClient.generatePair(newDto(GenerateSshPairRequest.class).withService("vcs").withName(host));
    }
    // update public key
    try {
        githubKeyUploader.uploadKey(sshPair.getPublicKey());
    } catch (IOException e) {
        LOG.error("Upload github ssh key fail", e);
        throw new GitException(e.getMessage(), e);
    }
}
Also used : SshPair(org.eclipse.che.api.ssh.shared.model.SshPair) GitException(org.eclipse.che.api.git.exception.GitException) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 18 with GitException

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

the class JGitConnection method cloneWithSparseCheckout.

@Override
public void cloneWithSparseCheckout(String directory, String remoteUrl) throws GitException, UnauthorizedException {
    //TODO rework this code when jgit will support sparse-checkout. Tracked issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=383772
    if (directory == null) {
        throw new GitException("Subdirectory for sparse-checkout is not specified");
    }
    clone(CloneParams.create(remoteUrl));
    final String sourcePath = getWorkingDir().getPath();
    final String keepDirectoryPath = sourcePath + "/" + directory;
    IOFileFilter folderFilter = new DirectoryFileFilter() {

        public boolean accept(File dir) {
            String directoryPath = dir.getPath();
            return !(directoryPath.startsWith(keepDirectoryPath) || directoryPath.startsWith(sourcePath + "/.git"));
        }
    };
    Collection<File> files = org.apache.commons.io.FileUtils.listFilesAndDirs(getWorkingDir(), TrueFileFilter.INSTANCE, folderFilter);
    try {
        DirCache index = getRepository().lockDirCache();
        int sourcePathLength = sourcePath.length() + 1;
        files.stream().filter(File::isFile).forEach(file -> index.getEntry(file.getPath().substring(sourcePathLength)).setAssumeValid(true));
        index.write();
        index.commit();
        for (File file : files) {
            if (keepDirectoryPath.startsWith(file.getPath())) {
                continue;
            }
            if (file.exists()) {
                FileUtils.delete(file, FileUtils.RECURSIVE);
            }
        }
    } catch (IOException exception) {
        String message = generateExceptionMessage(exception);
        throw new GitException(message, exception);
    }
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) GitException(org.eclipse.che.api.git.exception.GitException) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) IOException(java.io.IOException) DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) File(java.io.File) DirectoryFileFilter(org.apache.commons.io.filefilter.DirectoryFileFilter)

Example 19 with GitException

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

the class JGitConnection method init.

@Override
public void init(boolean isBare) throws GitException {
    File workDir = repository.getWorkTree();
    if (!workDir.exists()) {
        throw new GitException(format(ERROR_INIT_FOLDER_MISSING, workDir));
    }
    // If create fails and the .git folder didn't exist we want to remove it.
    // We have to do this here because the create command doesn't revert its own changes in case of failure.
    boolean removeIfFailed = !repository.getDirectory().exists();
    try {
        repository.create(isBare);
    } catch (IOException exception) {
        if (removeIfFailed) {
            deleteRepositoryFolder();
        }
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : GitException(org.eclipse.che.api.git.exception.GitException) IOException(java.io.IOException) DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) File(java.io.File)

Example 20 with GitException

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

the class JGitConnection method rm.

@Override
public void rm(RmParams params) throws GitException {
    List<String> files = params.getItems();
    RmCommand rmCommand = getGit().rm();
    rmCommand.setCached(params.isCached());
    if (files != null) {
        files.forEach(rmCommand::addFilepattern);
    }
    try {
        rmCommand.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) RmCommand(org.eclipse.jgit.api.RmCommand)

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