Example usage for org.eclipse.jgit.lib ObjectId equals

List of usage examples for org.eclipse.jgit.lib ObjectId equals

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectId equals.

Prototype

@SuppressWarnings({ "NonOverridingEquals", "AmbiguousMethodReference" })
public final boolean equals(AnyObjectId other) 

Source Link

Document

Determine if this ObjectId has exactly the same value as another.

Usage

From source file:actors.PostReceiveActor.java

License:Apache License

protected Collection<? extends RevCommit> parseCommitsFrom(ReceiveCommand command, Project project) {
    Repository repository = GitRepository.buildGitRepository(project);
    List<RevCommit> list = new ArrayList<>();

    try {/*from  w  w w.j  a  v a 2 s .c  o  m*/
        ObjectId endRange = command.getNewId();
        ObjectId startRange = command.getOldId();

        RevWalk rw = new RevWalk(repository);
        rw.markStart(rw.parseCommit(endRange));
        if (startRange.equals(ObjectId.zeroId())) {
            // maybe this is a tag or an orphan branch
            list.add(rw.parseCommit(endRange));
            rw.dispose();
            return list;
        } else {
            rw.markUninteresting(rw.parseCommit(startRange));
        }

        for (RevCommit rev : rw) {
            list.add(rev);
        }
        rw.dispose();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return list;
}

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Returns a list of commits for the repository within the range specified
 * by startRangeId and endRangeId. If the repository does not exist or is
 * empty, an empty list is returned./*from w  w w . j a v  a  2 s .  com*/
 *
 * @param repository
 * @param startRangeId
 *            the first commit (not included in results)
 * @param endRangeId
 *            the end commit (included in results)
 * @return a list of commits
 */
public static List<RevCommit> getRevLog(Repository repository, String startRangeId, String endRangeId) {
    List<RevCommit> list = new ArrayList<RevCommit>();
    if (!hasCommits(repository)) {
        return list;
    }
    try {
        ObjectId endRange = repository.resolve(endRangeId);
        ObjectId startRange = repository.resolve(startRangeId);

        RevWalk rw = new RevWalk(repository);
        rw.markStart(rw.parseCommit(endRange));
        if (startRange.equals(ObjectId.zeroId())) {
            // maybe this is a tag or an orphan branch
            list.add(rw.parseCommit(endRange));
            rw.dispose();
            return list;
        } else {
            rw.markUninteresting(rw.parseCommit(startRange));
        }

        Iterable<RevCommit> revlog = rw;
        for (RevCommit rev : revlog) {
            list.add(rev);
        }
        rw.dispose();
    } catch (Throwable t) {
        error(t, repository, "{0} failed to get revlog for {1}..{2}", startRangeId, endRangeId);
    }
    return list;
}

From source file:com.google.devtools.build.lib.bazel.repository.GitCloneFunction.java

License:Open Source License

private boolean isUpToDate(GitRepositoryDescriptor descriptor) {
    // Initializing/checking status of/etc submodules cleanly is hard, so don't try for now.
    if (descriptor.initSubmodules) {
        return false;
    }/*www  . j a  v a 2  s .  co  m*/
    Repository repository = null;
    try {
        repository = new FileRepositoryBuilder()
                .setGitDir(descriptor.directory.getChild(Constants.DOT_GIT).getPathFile()).setMustExist(true)
                .build();
        ObjectId head = repository.resolve(Constants.HEAD);
        ObjectId checkout = repository.resolve(descriptor.checkout);
        if (head != null && checkout != null && head.equals(checkout)) {
            Status status = Git.wrap(repository).status().call();
            if (!status.hasUncommittedChanges()) {
                // new_git_repository puts (only) BUILD and WORKSPACE, and
                // git_repository doesn't add any files.
                Set<String> untracked = status.getUntracked();
                if (untracked.isEmpty() || (untracked.size() == 2 && untracked.contains("BUILD")
                        && untracked.contains("WORKSPACE"))) {
                    return true;
                }
            }
        }
    } catch (GitAPIException | IOException e) {
        // Any exceptions here, we'll just blow it away and try cloning fresh.
        // The fresh clone avoids any weirdness due to what's there and has nicer
        // error reporting.
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return false;
}

From source file:com.google.devtools.build.lib.bazel.repository.GitCloner.java

License:Open Source License

private static boolean isUpToDate(GitRepositoryDescriptor descriptor) {
    // Initializing/checking status of/etc submodules cleanly is hard, so don't try for now.
    if (descriptor.initSubmodules) {
        return false;
    }//from   w  ww  .  j a  va  2 s .  co m
    Repository repository = null;
    try {
        repository = new FileRepositoryBuilder()
                .setGitDir(descriptor.directory.getChild(Constants.DOT_GIT).getPathFile()).setMustExist(true)
                .build();
        ObjectId head = repository.resolve(Constants.HEAD);
        ObjectId checkout = repository.resolve(descriptor.checkout);
        if (head != null && checkout != null && head.equals(checkout)) {
            Status status = Git.wrap(repository).status().call();
            if (!status.hasUncommittedChanges()) {
                // new_git_repository puts (only) BUILD and WORKSPACE, and
                // git_repository doesn't add any files.
                Set<String> untracked = status.getUntracked();
                if (untracked.isEmpty() || (untracked.size() == 2 && untracked.contains("BUILD")
                        && untracked.contains("WORKSPACE"))) {
                    return true;
                }
            }
        }
    } catch (GitAPIException | IOException e) {
        // Any exceptions here, we'll just blow it away and try cloning fresh.
        // The fresh clone avoids any weirdness due to what's there and has nicer
        // error reporting.
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return false;
}

From source file:com.google.gerrit.pgm.init.AllProjectsConfig.java

License:Apache License

private void save(PersonIdent ident, String msg) throws IOException {
    File path = getPath();//from w w  w . ja  v a 2s  . c  om
    if (path == null) {
        throw new IOException("All-Projects does not exist.");
    }

    Repository repo = new FileRepository(path);
    try {
        inserter = repo.newObjectInserter();
        reader = repo.newObjectReader();
        try {
            RevWalk rw = new RevWalk(reader);
            try {
                RevTree srcTree = revision != null ? rw.parseTree(revision) : null;
                newTree = readTree(srcTree);
                saveConfig(ProjectConfig.PROJECT_CONFIG, cfg);
                ObjectId res = newTree.writeTree(inserter);
                if (res.equals(srcTree)) {
                    // If there are no changes to the content, don't create the commit.
                    return;
                }

                CommitBuilder commit = new CommitBuilder();
                commit.setAuthor(ident);
                commit.setCommitter(ident);
                commit.setMessage(msg);
                commit.setTreeId(res);
                if (revision != null) {
                    commit.addParentId(revision);
                }
                ObjectId newRevision = inserter.insert(commit);
                updateRef(repo, ident, newRevision, "commit: " + msg);
                revision = newRevision;
            } finally {
                rw.release();
            }
        } finally {
            if (inserter != null) {
                inserter.release();
                inserter = null;
            }
            if (reader != null) {
                reader.release();
                reader = null;
            }
        }
    } finally {
        repo.close();
    }
}

From source file:com.google.gerrit.pgm.init.api.AllProjectsConfig.java

License:Apache License

private void save(PersonIdent ident, String msg) throws IOException {
    File path = getPath();//w  w w. j a  va 2s . co m
    if (path == null) {
        throw new IOException("All-Projects does not exist.");
    }

    try (Repository repo = new FileRepository(path)) {
        inserter = repo.newObjectInserter();
        reader = repo.newObjectReader();
        try (RevWalk rw = new RevWalk(reader)) {
            RevTree srcTree = revision != null ? rw.parseTree(revision) : null;
            newTree = readTree(srcTree);
            saveConfig(ProjectConfig.PROJECT_CONFIG, cfg);
            saveGroupList();
            ObjectId res = newTree.writeTree(inserter);
            if (res.equals(srcTree)) {
                // If there are no changes to the content, don't create the commit.
                return;
            }

            CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(ident);
            commit.setCommitter(ident);
            commit.setMessage(msg);
            commit.setTreeId(res);
            if (revision != null) {
                commit.addParentId(revision);
            }
            ObjectId newRevision = inserter.insert(commit);
            updateRef(repo, ident, newRevision, "commit: " + msg);
            revision = newRevision;
        } finally {
            if (inserter != null) {
                inserter.close();
                inserter = null;
            }
            if (reader != null) {
                reader.close();
                reader = null;
            }
        }
    }

    // we need to invalidate the JGit cache if the group list is invalidated in
    // an unattended init step
    RepositoryCache.clear();
}

From source file:com.google.gerrit.pgm.init.api.VersionedMetaDataOnInit.java

License:Apache License

protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException {
    File path = getPath();/*  w  ww. j  ava  2s.c  om*/
    if (path == null) {
        throw new IOException(project + " does not exist.");
    }

    try (Repository repo = new FileRepository(path);
            ObjectInserter i = repo.newObjectInserter();
            ObjectReader r = repo.newObjectReader();
            RevWalk rw = new RevWalk(r)) {
        inserter = i;
        reader = r;

        RevTree srcTree = revision != null ? rw.parseTree(revision) : null;
        newTree = readTree(srcTree);

        CommitBuilder commit = new CommitBuilder();
        commit.setAuthor(ident);
        commit.setCommitter(ident);
        commit.setMessage(msg);

        onSave(commit);

        ObjectId res = newTree.writeTree(inserter);
        if (res.equals(srcTree)) {
            return;
        }
        commit.setTreeId(res);

        if (revision != null) {
            commit.addParentId(revision);
        }
        ObjectId newRevision = inserter.insert(commit);
        updateRef(repo, ident, newRevision, "commit: " + msg);
        revision = rw.parseCommit(newRevision);
    } finally {
        inserter = null;
        reader = null;
    }
}

From source file:com.google.gerrit.pgm.init.VersionedAuthorizedKeysOnInit.java

License:Apache License

private void save(PersonIdent ident, String msg) throws IOException {
    File path = getPath();//ww  w  . j  a va2  s  .  c  o  m
    if (path == null) {
        throw new IOException(project + " does not exist.");
    }

    try (Repository repo = new FileRepository(path);
            ObjectInserter i = repo.newObjectInserter();
            ObjectReader r = repo.newObjectReader();
            RevWalk rw = new RevWalk(reader)) {
        inserter = i;
        reader = r;

        RevTree srcTree = revision != null ? rw.parseTree(revision) : null;
        newTree = readTree(srcTree);

        CommitBuilder commit = new CommitBuilder();
        commit.setAuthor(ident);
        commit.setCommitter(ident);
        commit.setMessage(msg);

        onSave(commit);
        ObjectId res = newTree.writeTree(inserter);
        if (res.equals(srcTree)) {
            return;
        }

        commit.setTreeId(res);
        if (revision != null) {
            commit.addParentId(revision);
        }
        ObjectId newRevision = inserter.insert(commit);
        updateRef(repo, ident, newRevision, "commit: " + msg);
        revision = newRevision;
    } finally {
        inserter = null;
        reader = null;
    }
}

From source file:com.google.gerrit.server.account.externalids.ExternalIdReader.java

License:Apache License

public static NoteMap readNoteMap(RevWalk rw, ObjectId rev) throws IOException {
    if (!rev.equals(ObjectId.zeroId())) {
        return NoteMap.read(rw.getObjectReader(), rw.parseCommit(rev));
    }/*from www  . j  a  v  a 2  s. co  m*/
    return NoteMap.newEmptyMap();
}

From source file:com.google.gerrit.server.account.externalids.ExternalIdReader.java

License:Apache License

/** Reads and returns all external IDs. */
private Set<ExternalId> all(Repository repo, ObjectId rev) throws IOException {
    if (rev.equals(ObjectId.zeroId())) {
        return ImmutableSet.of();
    }//from  w w w .  ja  v a2 s  . c  o m

    try (Timer0.Context ctx = readAllLatency.start(); RevWalk rw = new RevWalk(repo)) {
        NoteMap noteMap = readNoteMap(rw, rev);
        Set<ExternalId> extIds = new HashSet<>();
        for (Note note : noteMap) {
            byte[] raw = rw.getObjectReader().open(note.getData(), OBJ_BLOB).getCachedBytes(MAX_NOTE_SZ);
            try {
                extIds.add(ExternalId.parse(note.getName(), raw));
            } catch (Exception e) {
                log.error(String.format("Ignoring invalid external ID note %s", note.getName()), e);
            }
        }
        return extIds;
    }
}