Example usage for org.eclipse.jgit.internal.storage.reftree RefTree newEmptyTree

List of usage examples for org.eclipse.jgit.internal.storage.reftree RefTree newEmptyTree

Introduction

In this page you can find the example usage for org.eclipse.jgit.internal.storage.reftree RefTree newEmptyTree.

Prototype

public static RefTree newEmptyTree() 

Source Link

Document

Create an empty reference tree.

Usage

From source file:org.uberfire.java.nio.fs.jgit.util.commands.RefTreeUpdateCommand.java

License:Apache License

private void commit(final Repository repo, final RevCommit original, final BiFunction fun) throws IOException {
    try (final ObjectReader reader = repo.newObjectReader();
            final ObjectInserter inserter = repo.newObjectInserter();
            final RevWalk rw = new RevWalk(reader)) {

        final RefTreeDatabase refdb = (RefTreeDatabase) repo.getRefDatabase();
        final RefDatabase bootstrap = refdb.getBootstrap();
        final RefUpdate refUpdate = bootstrap.newUpdate(refdb.getTxnCommitted(), false);

        final CommitBuilder cb = new CommitBuilder();
        final Ref ref = bootstrap.exactRef(refdb.getTxnCommitted());
        final RefTree tree;
        if (ref != null && ref.getObjectId() != null) {
            tree = RefTree.read(reader, rw.parseTree(ref.getObjectId()));
            cb.setParentId(ref.getObjectId());
            refUpdate.setExpectedOldObjectId(ref.getObjectId());
        } else {/*from  w w w  .  j  a v a 2s.c o m*/
            tree = RefTree.newEmptyTree();
            refUpdate.setExpectedOldObjectId(ObjectId.zeroId());
        }

        if (fun.apply(reader, tree)) {
            final Ref ref2 = bootstrap.exactRef(refdb.getTxnCommitted());
            if (ref2 == null || ref2.getObjectId().equals(ref != null ? ref.getObjectId() : null)) {
                cb.setTreeId(tree.writeTree(inserter));
                if (original != null) {
                    cb.setAuthor(original.getAuthorIdent());
                    cb.setCommitter(original.getAuthorIdent());
                } else {
                    final PersonIdent personIdent = new PersonIdent("user", "user@example.com");
                    cb.setAuthor(personIdent);
                    cb.setCommitter(personIdent);
                }
                refUpdate.setNewObjectId(inserter.insert(cb));
                inserter.flush();
                final RefUpdate.Result result = refUpdate.update(rw);
                switch (result) {
                case NEW:
                case FAST_FORWARD:
                    break;
                default:
                    throw new RuntimeException(
                            repo.getDirectory() + " -> " + result.toString() + " : " + refUpdate.getName());
                }
                final File commited = new File(repo.getDirectory(), refdb.getTxnCommitted());
                final File accepted = new File(repo.getDirectory(), refdb.getTxnNamespace() + "accepted");
                Files.copy(commited.toPath(), accepted.toPath(), StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }
}