Example usage for org.eclipse.jgit.api LogCommand not

List of usage examples for org.eclipse.jgit.api LogCommand not

Introduction

In this page you can find the example usage for org.eclipse.jgit.api LogCommand not.

Prototype

public LogCommand not(AnyObjectId start) throws MissingObjectException, IncorrectObjectTypeException 

Source Link

Document

Same as --not start , or ^start

Usage

From source file:org.eclipse.orion.server.git.servlets.GitCommitHandlerV1.java

License:Open Source License

private boolean handleGetCommitLog(HttpServletRequest request, HttpServletResponse response, Repository db,
        String refIdsRange, String path) throws AmbiguousObjectException, IOException, ServletException,
        JSONException, URISyntaxException, CoreException {
    int page = request.getParameter("page") != null ? new Integer(request.getParameter("page")).intValue() : 0; //$NON-NLS-1$ //$NON-NLS-2$
    int pageSize = request.getParameter("pageSize") != null //$NON-NLS-1$
            ? new Integer(request.getParameter("pageSize")).intValue() //$NON-NLS-1$
            : PAGE_SIZE;/*  w w  w  . j a  va2 s.c om*/

    ObjectId toObjectId = null;
    ObjectId fromObjectId = null;

    Ref toRefId = null;
    Ref fromRefId = null;

    Git git = new Git(db);
    LogCommand log = git.log();

    if (refIdsRange != null) {
        // git log <since>..<until>
        if (refIdsRange.contains("..")) { //$NON-NLS-1$
            String[] commits = refIdsRange.split("\\.\\."); //$NON-NLS-1$
            if (commits.length != 2) {
                String msg = NLS.bind("Failed to generate commit log for ref {0}", refIdsRange);
                return statusHandler.handleRequest(request, response,
                        new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
            }

            fromObjectId = db.resolve(commits[0]);
            fromRefId = db.getRef(commits[0]);
            if (fromObjectId == null) {
                String msg = NLS.bind("Failed to generate commit log for ref {0}", commits[0]);
                return statusHandler.handleRequest(request, response,
                        new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
            }

            toObjectId = db.resolve(commits[1]);
            toRefId = db.getRef(commits[1]);
            if (toObjectId == null) {
                String msg = NLS.bind("No ref or commit found: {0}", commits[1]);
                return statusHandler.handleRequest(request, response,
                        new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
            }
        } else {
            toObjectId = db.resolve(refIdsRange);
            toRefId = db.getRef(refIdsRange);
            if (toObjectId == null) {
                String msg = NLS.bind("No ref or commit found: {0}", refIdsRange);
                return statusHandler.handleRequest(request, response,
                        new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
            }
        }

        // set the commit range
        log.add(toObjectId);

        if (fromObjectId != null)
            log.not(fromObjectId);
    } else {
        // git log --all
        // workaround for git log --all - see bug 353310
        List<Ref> branches = git.branchList().setListMode(ListMode.ALL).call();
        for (Ref branch : branches) {
            log.add(branch.getObjectId());
        }
    }

    // set the path filter
    TreeFilter filter = null;

    boolean isRoot = true;
    if (path != null && !"".equals(path)) { //$NON-NLS-1$
        filter = AndTreeFilter.create(PathFilterGroup.createFromStrings(Collections.singleton(path)),
                TreeFilter.ANY_DIFF);
        log.addPath(path);
        isRoot = false;
    }

    try {
        Iterable<RevCommit> commits = log.call();
        Map<ObjectId, JSONArray> commitToBranchMap = getCommitToBranchMap(db);
        JSONObject result = toJSON(db, OrionServlet.getURI(request), commits, commitToBranchMap, page, pageSize,
                filter, isRoot);

        result.put(GitConstants.KEY_REPOSITORY_PATH, isRoot ? "" : path); //$NON-NLS-1$
        if (refIdsRange == null)
            result.put(GitConstants.KEY_CLONE,
                    BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.COMMIT));
        else
            result.put(GitConstants.KEY_CLONE, BaseToCloneConverter.getCloneLocation(getURI(request),
                    BaseToCloneConverter.COMMIT_REFRANGE));

        if (toRefId != null) {
            result.put(GitConstants.KEY_REMOTE, BaseToRemoteConverter.getRemoteBranchLocation(getURI(request),
                    Repository.shortenRefName(toRefId.getName()), db, BaseToRemoteConverter.REMOVE_FIRST_3));

            String refTargetName = toRefId.getTarget().getName();
            if (refTargetName.startsWith(Constants.R_HEADS)) {
                // this is a branch
                result.put(GitConstants.KEY_LOG_TO_REF,
                        BranchToJSONConverter.toJSON(toRefId.getTarget(), db, getURI(request), 3));
            }
        }
        if (fromRefId != null) {
            String refTargetName = fromRefId.getTarget().getName();
            if (refTargetName.startsWith(Constants.R_HEADS)) {
                // this is a branch
                result.put(GitConstants.KEY_LOG_FROM_REF,
                        BranchToJSONConverter.toJSON(fromRefId.getTarget(), db, getURI(request), 3));
            }
        }

        OrionServlet.writeJSONResponse(request, response, result);
        return true;
    } catch (NoHeadException e) {
        String msg = NLS.bind("No HEAD reference found when generating log for ref {0}", refIdsRange);
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e));
    } catch (JGitInternalException e) {
        String msg = NLS.bind("An internal error occured when generating log for ref {0}", refIdsRange);
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e));
    }
}