Example usage for org.eclipse.jgit.errors AmbiguousObjectException getCandidates

List of usage examples for org.eclipse.jgit.errors AmbiguousObjectException getCandidates

Introduction

In this page you can find the example usage for org.eclipse.jgit.errors AmbiguousObjectException getCandidates.

Prototype

public Collection<ObjectId> getCandidates() 

Source Link

Document

Get the matching candidates (or at least a subset of them)

Usage

From source file:com.google.gerrit.plugins.GitLogCommand.java

License:Apache License

@Override
public void run() throws UnloggedFailure, Failure, Exception {
    ObjectId from = null;/*w  w w.  ja v a 2 s  .c  o  m*/
    ObjectId to = null;
    Repository repository = null;
    RevCommit rev = null;
    RevWalk walk = null;
    Project.NameKey project = null;
    Pair<String, String> range = null;
    ArrayList<Map<String, String>> cmts = new ArrayList<Map<String, String>>();

    // Check that a project was specified.
    if (projectName == null) {
        this.resultPrinter(this.format, GitLogReturnCode.WRONG_PROJECT_NAME, null);
        return;
    }

    // Check that we have something to parse.
    if (input == null) {
        this.resultPrinter(this.format, GitLogReturnCode.WRONG_RANGE, null);
        return;
    }

    // Remove .git if project name has it.
    if (projectName.endsWith(".git")) {
        projectName = projectName.substring(0, projectName.length() - 4);
    }

    // Check that project/repository exists.
    project = Project.NameKey.parse(projectName);
    if (!repoManager.list().contains(project)) {
        this.resultPrinter(this.format, GitLogReturnCode.WRONG_PROJECT_NAME, null);
        return;
    }

    repository = repoManager.openRepository(project);
    walk = new RevWalk(repository);

    // Parse provided input to get range of revisions.
    range = GitLogInputParser.parse(input);

    /*
     * If "from" and "to" revisions are null then it means that we got faulty
     * input and we need to notify user about it
     */
    if (range.getValue0() == null && range.getValue1() == null) {
        this.resultPrinter(this.format, GitLogReturnCode.WRONG_RANGE, null);
        return;
    }

    /*
     * If "from" value is null then it means that we have internal problem with
     * input parser because such situation should never happen
     */
    if (range.getValue0() == null) {
        this.resultPrinter(this.format, GitLogReturnCode.INTERNAL_ERROR, null);
        return;
    }

    // Get "from" commit object.
    try {
        from = repository.resolve(range.getValue0());
    } catch (AmbiguousObjectException e) {
        // Few commits corresponds to provided reference. Return all of them
        Map<String, String> candidates = new HashMap<String, String>();
        for (Iterator<ObjectId> iter = e.getCandidates().iterator(); iter.hasNext();) {
            candidates.put("candidate", iter.next().getName());
        }
        cmts.add(candidates);
        this.resultPrinter(this.format, GitLogReturnCode.AMBIGUOUS_COMMIT_REF, cmts);
        return;
    } catch (IncorrectObjectTypeException e) {
        this.resultPrinter(this.format, GitLogReturnCode.INTERNAL_ERROR, null);
        return;
    } catch (RevisionSyntaxException e) {
        this.resultPrinter(this.format, GitLogReturnCode.INTERNAL_ERROR, null);
        return;
    } catch (IOException e) {
        this.resultPrinter(this.format, GitLogReturnCode.ACCESS_ERROR, null);
        return;
    }

    if (from == null) {
        this.resultPrinter(this.format, GitLogReturnCode.FIRST_REF_NOT_FOUND, null);
        return;
    }

    // Get "to" commit object if commit reference is not null.
    if (range.getValue1() != null) {
        try {
            to = repository.resolve(range.getValue1());
        } catch (AmbiguousObjectException e) {
            // Few commits corresponds to provided reference. Return all of them
            Map<String, String> candidates = new HashMap<String, String>();
            for (Iterator<ObjectId> iter = e.getCandidates().iterator(); iter.hasNext();) {
                candidates.put("candidate", iter.next().getName());
            }
            cmts.add(candidates);
            this.resultPrinter(this.format, GitLogReturnCode.AMBIGUOUS_COMMIT_REF, cmts);
            return;
        } catch (IncorrectObjectTypeException e) {
            this.resultPrinter(this.format, GitLogReturnCode.INTERNAL_ERROR, null);
            return;
        } catch (RevisionSyntaxException e) {
            this.resultPrinter(this.format, GitLogReturnCode.INTERNAL_ERROR, null);
            return;
        } catch (IOException e) {
            this.resultPrinter(this.format, GitLogReturnCode.ACCESS_ERROR, null);
            return;
        }
        if (to == null) {
            this.resultPrinter(this.format, GitLogReturnCode.SECOND_REF_NOT_FOUND, null);
            return;
        }
    }

    if (from == to) {
        // We asked to show only one commit.
        try {
            rev = walk.parseCommit(from);
        } catch (MissingObjectException e) {
            this.resultPrinter(this.format, GitLogReturnCode.FIRST_REF_NOT_FOUND, null);
            return;
        } catch (IncorrectObjectTypeException e) {
            this.resultPrinter(this.format, GitLogReturnCode.INTERNAL_ERROR, null);
            return;
        } catch (IOException e) {
            this.resultPrinter(this.format, GitLogReturnCode.ACCESS_ERROR, null);
            return;
        }
        cmts.add(this.revCommitToMap(rev));
    } else if (from != null && to != null) {
        /*
         * we asked to show log between two commits we want our search to be
         * inclusive so first we want to save "to" into result
         */
        try {
            rev = walk.parseCommit(to);
        } catch (MissingObjectException e) {
            this.resultPrinter(this.format, GitLogReturnCode.SECOND_REF_NOT_FOUND, null);
            return;
        } catch (IncorrectObjectTypeException e) {
            this.resultPrinter(this.format, GitLogReturnCode.INTERNAL_ERROR, null);
            return;
        } catch (IOException e) {
            this.resultPrinter(this.format, GitLogReturnCode.ACCESS_ERROR, null);
            return;
        }
        Map<String, String> last = this.revCommitToMap(rev);
        /*
         * Set filter for revision walk. It is important that we got "to" before
         * this moment, otherwise it will be filtered out.
         */
        try {
            rev = walk.parseCommit(from);
        } catch (MissingObjectException e) {
            this.resultPrinter(this.format, GitLogReturnCode.FIRST_REF_NOT_FOUND, null);
            return;
        } catch (IncorrectObjectTypeException e) {
            this.resultPrinter(this.format, GitLogReturnCode.INTERNAL_ERROR, null);
            return;
        } catch (IOException e) {
            this.resultPrinter(this.format, GitLogReturnCode.ACCESS_ERROR, null);
            return;
        }

        walk.markUninteresting(walk.parseCommit(to));
        for (RevCommit next : walk) {
            cmts.add(this.revCommitToMap(next));
        }
        // Insert "to" to the end of array
        cmts.add(last);
    } else if (from != null && to == null) {
        // If we asked to show log for the entire history to the root commit.
        walk.markStart(walk.parseCommit(from));
        for (RevCommit next : walk) {
            cmts.add(this.revCommitToMap(next));
        }
    }

    this.resultPrinter(this.format, GitLogReturnCode.OK, cmts);
}