Search in sources :

Example 21 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project gitiles by GerritCodeReview.

the class DescribeServlet method describe.

private String describe(Repository repo, GitilesView view, HttpServletRequest req, HttpServletResponse res) throws IOException {
    if (!getBooleanParam(view, CONTAINS_PARAM)) {
        res.setStatus(SC_BAD_REQUEST);
        return null;
    }
    ObjectId id = resolve(repo, view, req, res);
    if (id == null) {
        return null;
    }
    String name;
    try (Git git = new Git(repo)) {
        NameRevCommand cmd = nameRevCommand(git, id, req, res);
        if (cmd == null) {
            return null;
        }
        name = cmd.call().get(id);
    } catch (GitAPIException e) {
        throw new IOException(e);
    }
    if (name == null) {
        res.setStatus(SC_NOT_FOUND);
        return null;
    }
    return name;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) ObjectId(org.eclipse.jgit.lib.ObjectId) NameRevCommand(org.eclipse.jgit.api.NameRevCommand) IOException(java.io.IOException)

Example 22 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project gerrit by GerritCodeReview.

the class UploadArchive method runImpl.

@Override
protected void runImpl() throws IOException, PermissionBackendException, Failure {
    PacketLineOut packetOut = new PacketLineOut(out);
    packetOut.setFlushOnEnd(true);
    packetOut.writeString("ACK");
    packetOut.end();
    try {
        // Parse Git arguments
        readArguments();
        ArchiveFormat f = allowedFormats.getExtensions().get("." + options.format);
        if (f == null) {
            throw new Failure(3, "fatal: upload-archive not permitted");
        }
        // Find out the object to get from the specified reference and paths
        ObjectId treeId = repo.resolve(options.treeIsh);
        if (treeId == null) {
            throw new Failure(4, "fatal: reference not found");
        }
        // Verify the user has permissions to read the specified tree.
        if (!canRead(treeId)) {
            throw new Failure(5, "fatal: cannot perform upload-archive operation");
        }
        // The archive is sent in DATA sideband channel
        try (SideBandOutputStream sidebandOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA, SideBandOutputStream.MAX_BUF, out)) {
            new ArchiveCommand(repo).setFormat(f.name()).setFormatOptions(getFormatOptions(f)).setTree(treeId).setPaths(options.path.toArray(new String[0])).setPrefix(options.prefix).setOutputStream(sidebandOut).call();
            sidebandOut.flush();
        } catch (GitAPIException e) {
            throw new Failure(7, "fatal: git api exception, " + e);
        }
    } catch (Failure f) {
        // Report the error in ERROR sideband channel
        try (SideBandOutputStream sidebandError = new SideBandOutputStream(SideBandOutputStream.CH_ERROR, SideBandOutputStream.MAX_BUF, out)) {
            sidebandError.write(f.getMessage().getBytes(UTF_8));
            sidebandError.flush();
        }
        throw f;
    } finally {
        // In any case, cleanly close the packetOut channel
        packetOut.end();
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) PacketLineOut(org.eclipse.jgit.transport.PacketLineOut) ArchiveFormat(com.google.gerrit.server.change.ArchiveFormat) ObjectId(org.eclipse.jgit.lib.ObjectId) SideBandOutputStream(org.eclipse.jgit.transport.SideBandOutputStream) ArchiveCommand(org.eclipse.jgit.api.ArchiveCommand)

Example 23 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project indy by Commonjava.

the class GitManager method addAndCommitPaths.

public GitManager addAndCommitPaths(final ChangeSummary summary, final Collection<String> paths) throws GitSubsystemException {
    lockAnd(me -> {
        if (!verifyChangesExist(paths)) {
            logger.info("No actual changes in:\n  {}\n\nSkipping commit.", join(paths, "\n  "));
            return this;
        }
        try {
            final AddCommand add = git.add();
            final CommitCommand commit = git.commit();
            for (final String filepath : paths) {
                add.addFilepattern(filepath);
            }
            logger.info("Adding:\n  " + join(paths, "\n  ") + "\n\nSummary: " + summary);
            add.call();
            commit.setMessage(buildMessage(summary, paths)).setAuthor(summary.getUser(), email).call();
        } catch (final NoFilepatternException e) {
            throw new GitSubsystemException("Cannot add to git: " + e.getMessage(), e);
        } catch (final GitAPIException e) {
            throw new GitSubsystemException("Cannot add to git: " + e.getMessage(), e);
        }
        return me;
    });
    return this;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) NoFilepatternException(org.eclipse.jgit.api.errors.NoFilepatternException) CommitCommand(org.eclipse.jgit.api.CommitCommand) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) AddCommand(org.eclipse.jgit.api.AddCommand)

Example 24 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project compiler by boalang.

the class RepositoryCloner method main.

public static void main(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    String localpath = args[1];
    String url = args[0];
    REMOTE_URL = url;
    File localPath = new File(localpath);
    if (!localPath.exists())
        localPath.mkdir();
    // then clone
    Git result = null;
    try {
        result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call();
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=474093
        result.getRepository().close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (result != null && result.getRepository() != null)
            result.getRepository().close();
    }
}
Also used : Git(org.eclipse.jgit.api.Git) File(java.io.File) TransportException(org.eclipse.jgit.api.errors.TransportException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) IOException(java.io.IOException)

Example 25 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project compiler by boalang.

the class RepositoryCloner method clone.

public static void clone(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    String localpaths = args[1];
    String url = args[0];
    REMOTE_URL = url;
    File localPath = new File(localpaths);
    if (!localPath.exists())
        localPath.mkdir();
    // then clone
    Git result = null;
    try {
        result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call();
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=474093
        result.getRepository().close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (result != null && result.getRepository() != null)
            result.getRepository().close();
    }
}
Also used : Git(org.eclipse.jgit.api.Git) File(java.io.File) TransportException(org.eclipse.jgit.api.errors.TransportException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) IOException(java.io.IOException)

Aggregations

GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)49 IOException (java.io.IOException)23 GitException (org.eclipse.che.api.git.exception.GitException)18 Git (org.eclipse.jgit.api.Git)18 File (java.io.File)14 RevCommit (org.eclipse.jgit.revwalk.RevCommit)12 ArrayList (java.util.ArrayList)11 ObjectId (org.eclipse.jgit.lib.ObjectId)11 Ref (org.eclipse.jgit.lib.Ref)10 Repository (org.eclipse.jgit.lib.Repository)9 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)8 RevWalk (org.eclipse.jgit.revwalk.RevWalk)7 RefSpec (org.eclipse.jgit.transport.RefSpec)7 PushResult (org.eclipse.jgit.transport.PushResult)6 HashMap (java.util.HashMap)5 DiffCommitFile (org.eclipse.che.api.git.shared.DiffCommitFile)5 CheckoutConflictException (org.eclipse.jgit.api.errors.CheckoutConflictException)5 GitUser (org.eclipse.che.api.git.shared.GitUser)4 AddCommand (org.eclipse.jgit.api.AddCommand)4 CloneCommand (org.eclipse.jgit.api.CloneCommand)4