Search in sources :

Example 11 with DirCacheEntry

use of org.eclipse.jgit.dircache.DirCacheEntry in project gitblit by gitblit.

the class BranchTicketService method deleteTicketImpl.

/**
	 * Deletes a ticket from the repository.
	 *
	 * @param ticket
	 * @return true if successful
	 */
@Override
protected synchronized boolean deleteTicketImpl(RepositoryModel repository, TicketModel ticket, String deletedBy) {
    if (ticket == null) {
        throw new RuntimeException("must specify a ticket!");
    }
    boolean success = false;
    Repository db = repositoryManager.getRepository(ticket.repository);
    try {
        RefModel ticketsBranch = getTicketsBranch(db);
        if (ticketsBranch == null) {
            throw new RuntimeException(BRANCH + " does not exist!");
        }
        String ticketPath = toTicketPath(ticket.number);
        TreeWalk treeWalk = null;
        try {
            ObjectId treeId = db.resolve(BRANCH + "^{tree}");
            // Create the in-memory index of the new/updated ticket
            DirCache index = DirCache.newInCore();
            DirCacheBuilder builder = index.builder();
            // Traverse HEAD to add all other paths
            treeWalk = new TreeWalk(db);
            int hIdx = -1;
            if (treeId != null) {
                hIdx = treeWalk.addTree(treeId);
            }
            treeWalk.setRecursive(true);
            while (treeWalk.next()) {
                String path = treeWalk.getPathString();
                CanonicalTreeParser hTree = null;
                if (hIdx != -1) {
                    hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
                }
                if (!path.startsWith(ticketPath)) {
                    // add entries from HEAD for all other paths
                    if (hTree != null) {
                        final DirCacheEntry entry = new DirCacheEntry(path);
                        entry.setObjectId(hTree.getEntryObjectId());
                        entry.setFileMode(hTree.getEntryFileMode());
                        // add to temporary in-core index
                        builder.add(entry);
                    }
                }
            }
            // finish temporary in-core index used for this commit
            builder.finish();
            success = commitIndex(db, index, deletedBy, "- " + ticket.number);
        } catch (Throwable t) {
            log.error(MessageFormat.format("Failed to delete ticket {0,number,0} from {1}", ticket.number, db.getDirectory()), t);
        } finally {
            // release the treewalk
            if (treeWalk != null) {
                treeWalk.close();
            }
        }
    } finally {
        db.close();
    }
    return success;
}
Also used : RefModel(com.gitblit.models.RefModel) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) ObjectId(org.eclipse.jgit.lib.ObjectId) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DirCache(org.eclipse.jgit.dircache.DirCache) Repository(org.eclipse.jgit.lib.Repository) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 12 with DirCacheEntry

use of org.eclipse.jgit.dircache.DirCacheEntry in project gitiles by GerritCodeReview.

the class PathServletTest method treeJsonLinkTarget.

@Test
public void treeJsonLinkTarget() throws Exception {
    final ObjectId targetID = repo.blob("target");
    RevCommit c = repo.parseBody(repo.branch("master").commit().edit(new PathEdit("link") {

        @Override
        public void apply(DirCacheEntry ent) {
            ent.setFileMode(FileMode.SYMLINK);
            ent.setObjectId(targetID);
        }
    }).create());
    Tree tree = buildJson(Tree.class, "/repo/+/master/", "long=1");
    assertThat(tree.id).isEqualTo(c.getTree().name());
    assertThat(tree.entries).hasSize(1);
    TreeJsonData.Entry e = tree.entries.get(0);
    assertThat(e.mode).isEqualTo(0120000);
    assertThat(e.type).isEqualTo("blob");
    assertThat(e.name).isEqualTo("link");
    assertThat(e.id).isEqualTo(targetID.name());
    assertThat(e.target).isEqualTo("target");
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) ObjectId(org.eclipse.jgit.lib.ObjectId) PathEdit(org.eclipse.jgit.dircache.DirCacheEditor.PathEdit) Tree(com.google.gitiles.TreeJsonData.Tree) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 13 with DirCacheEntry

use of org.eclipse.jgit.dircache.DirCacheEntry in project gerrit by GerritCodeReview.

the class CommitMsgHookTest method file.

private DirCacheEntry file(final String name) throws IOException {
    try (ObjectInserter oi = repository.newObjectInserter()) {
        final DirCacheEntry e = new DirCacheEntry(name);
        e.setFileMode(FileMode.REGULAR_FILE);
        e.setObjectId(oi.insert(Constants.OBJ_BLOB, Constants.encode(name)));
        oi.flush();
        return e;
    }
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter)

Aggregations

DirCacheEntry (org.eclipse.jgit.dircache.DirCacheEntry)13 DirCache (org.eclipse.jgit.dircache.DirCache)6 DirCacheBuilder (org.eclipse.jgit.dircache.DirCacheBuilder)6 ObjectId (org.eclipse.jgit.lib.ObjectId)6 PathEdit (org.eclipse.jgit.dircache.DirCacheEditor.PathEdit)5 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)5 IOException (java.io.IOException)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)3 Test (org.junit.Test)3 TreeSet (java.util.TreeSet)2 DeletePath (org.eclipse.jgit.dircache.DirCacheEditor.DeletePath)2 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)2 Ref (org.eclipse.jgit.lib.Ref)2 Repository (org.eclipse.jgit.lib.Repository)2 RevBlob (org.eclipse.jgit.revwalk.RevBlob)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)2 RefModel (com.gitblit.models.RefModel)1 Attachment (com.gitblit.models.TicketModel.Attachment)1