Search in sources :

Example 31 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project gerrit by GerritCodeReview.

the class ChangeEditModifier method rebase.

private void rebase(Repository repository, ChangeEdit changeEdit, PatchSet currentPatchSet) throws IOException, MergeConflictException, InvalidChangeOperationException, OrmException {
    RevCommit currentEditCommit = changeEdit.getEditCommit();
    if (currentEditCommit.getParentCount() == 0) {
        throw new InvalidChangeOperationException("Rebase change edit against root commit not supported");
    }
    Change change = changeEdit.getChange();
    RevCommit basePatchSetCommit = lookupCommit(repository, currentPatchSet);
    RevTree basePatchSetTree = basePatchSetCommit.getTree();
    ObjectId newTreeId = merge(repository, changeEdit, basePatchSetTree);
    Timestamp nowTimestamp = TimeUtil.nowTs();
    String commitMessage = currentEditCommit.getFullMessage();
    ObjectId newEditCommitId = createCommit(repository, basePatchSetCommit, newTreeId, commitMessage, nowTimestamp);
    String newEditRefName = getEditRefName(change, currentPatchSet);
    updateReferenceWithNameChange(repository, changeEdit.getRefName(), currentEditCommit, newEditRefName, newEditCommitId, nowTimestamp);
    reindex(change);
}
Also used : InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.reviewdb.client.Change) Timestamp(java.sql.Timestamp) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 32 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project gitiles by GerritCodeReview.

the class TimeCacheTest method taggedTreeAndBlobTime.

@Test
public void taggedTreeAndBlobTime() throws Exception {
    RevBlob blob = repo.blob("contents");
    RevTree tree = repo.tree(repo.file("foo", blob));
    repo.tick(1);
    RevTag blobTag = repo.tag("blob", blob);
    repo.tick(1);
    RevTag treeTag = repo.tag("tree", tree);
    assertThat(getTime(blobTag)).isEqualTo(start + 1);
    assertThat(getTime(treeTag)).isEqualTo(start + 2);
}
Also used : RevTag(org.eclipse.jgit.revwalk.RevTag) RevBlob(org.eclipse.jgit.revwalk.RevBlob) RevTree(org.eclipse.jgit.revwalk.RevTree) Test(org.junit.Test)

Example 33 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project gitiles by GerritCodeReview.

the class TimeCacheTest method treeAndBlobTime.

@Test
public void treeAndBlobTime() throws Exception {
    RevBlob blob = repo.blob("contents");
    RevTree tree = repo.tree(repo.file("foo", blob));
    assertThat(getTime(blob)).isEqualTo(Long.MIN_VALUE);
    assertThat(getTime(tree)).isEqualTo(Long.MIN_VALUE);
}
Also used : RevBlob(org.eclipse.jgit.revwalk.RevBlob) RevTree(org.eclipse.jgit.revwalk.RevTree) Test(org.junit.Test)

Example 34 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project indy by Commonjava.

the class GitManager method verifyChangesExist.

private boolean verifyChangesExist(final Collection<String> paths) throws GitSubsystemException {
    return lockAnd(me -> {
        try {
            final DiffFormatter formatter = new DiffFormatter(System.out);
            formatter.setRepository(repo);
            final ObjectId oid = repo.resolve(Constants.HEAD);
            if (oid == null) {
                return true;
            }
            final RevWalk walk = new RevWalk(repo);
            final RevCommit commit = walk.parseCommit(oid);
            final RevTree treeWalk = walk.parseTree(commit);
            final List<TreeFilter> filters = new ArrayList<>();
            for (final String path : paths) {
                filters.add(PathFilter.create(path));
            }
            filters.add(TreeFilter.ANY_DIFF);
            walk.setTreeFilter(AndTreeFilter.create(filters));
            final CanonicalTreeParser tree = new CanonicalTreeParser();
            final ObjectReader oldReader = repo.newObjectReader();
            try {
                tree.reset(oldReader, treeWalk.getId());
            } finally {
                oldReader.release();
            }
            walk.dispose();
            final FileTreeIterator files = new FileTreeIterator(repo);
            final List<DiffEntry> entries = formatter.scan(tree, files);
            return entries != null && !entries.isEmpty();
        } catch (final IOException e) {
            throw new GitSubsystemException("Failed to scan for actual changes among: %s. Reason: %s", e, paths, e.getMessage());
        }
    });
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) ObjectReader(org.eclipse.jgit.lib.ObjectReader) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 35 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project searchcode-server by boyter.

the class GitService method fetchFileRevision.

/**
     * Given a repository location, revision and file path will retrieve that files contents. N.B. it returns the whole
     * file so you MAY end up running into serious memory issues, and should be aware of this
     */
public String fetchFileRevision(String repoLocation, String revision, String filePath) throws MissingObjectException, IncorrectObjectTypeException, IOException {
    Repository localRepository = new FileRepository(new File(repoLocation));
    ObjectId id = localRepository.resolve(revision);
    ObjectReader reader = localRepository.newObjectReader();
    try {
        RevWalk walk = new RevWalk(reader);
        RevCommit commit = walk.parseCommit(id);
        RevTree tree = commit.getTree();
        TreeWalk treewalk = TreeWalk.forPath(reader, filePath, tree);
        if (treewalk != null) {
            byte[] data = reader.open(treewalk.getObjectId(0)).getBytes();
            return new String(data, "utf-8");
        } else {
            return "";
        }
    } finally {
        reader.close();
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

RevTree (org.eclipse.jgit.revwalk.RevTree)35 RevWalk (org.eclipse.jgit.revwalk.RevWalk)24 ObjectId (org.eclipse.jgit.lib.ObjectId)17 RevCommit (org.eclipse.jgit.revwalk.RevCommit)17 ObjectReader (org.eclipse.jgit.lib.ObjectReader)11 IOException (java.io.IOException)8 DiffEntry (org.eclipse.jgit.diff.DiffEntry)8 Repository (org.eclipse.jgit.lib.Repository)7 RevObject (org.eclipse.jgit.revwalk.RevObject)6 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)6 File (java.io.File)4 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)4 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)4 ArrayList (java.util.ArrayList)3 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)3 RawTextComparator (org.eclipse.jgit.diff.RawTextComparator)3 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)3 Ref (org.eclipse.jgit.lib.Ref)3 RevBlob (org.eclipse.jgit.revwalk.RevBlob)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2