From 32466c33bae19aff03b95a02e0b7e72d4e2f11b8 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 23 Aug 2010 10:59:30 -0700 Subject: [PATCH] Delete deprecated ObjectWriter ObjectWriter is a deprecated API that people shouldn't be using. So get rid of it in favor of the ObjectInserter API. Change-Id: I6218bcb26b6b9ffb64e3e470dba5dca2e0a62fd4 Signed-off-by: Shawn O. Pearce --- .../org/eclipse/jgit/lib/IndexDiffTest.java | 29 ++- .../org/eclipse/jgit/lib/ReadTreeTest.java | 22 +- .../eclipse/jgit/lib/ReflogConfigTest.java | 9 +- .../eclipse/jgit/revwalk/ObjectWalkTest.java | 18 +- .../storage/file/ConcurrentRepackTest.java | 12 +- .../jgit/storage/file/T0003_Basic.java | 52 ++-- .../treewalk/NameConflictTreeWalkTest.java | 9 +- .../jgit/treewalk/PostOrderTreeWalkTest.java | 13 +- .../jgit/treewalk/TreeWalkBasicDiffTest.java | 32 ++- .../src/org/eclipse/jgit/lib/GitIndex.java | 147 +++++++---- .../org/eclipse/jgit/lib/ObjectInserter.java | 31 --- .../org/eclipse/jgit/lib/ObjectWriter.java | 238 ------------------ .../src/org/eclipse/jgit/lib/Tree.java | 26 ++ .../src/org/eclipse/jgit/lib/WriteTree.java | 23 +- 14 files changed, 262 insertions(+), 399 deletions(-) delete mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectWriter.java diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java index c439baccf6..3d19576758 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java @@ -56,7 +56,7 @@ public class IndexDiffTest extends RepositoryTestCase { writeTrashFile("file1", "file1"); writeTrashFile("dir/subfile", "dir/subfile"); Tree tree = new Tree(db); - tree.setId(new ObjectWriter(db).writeTree(tree)); + tree.setId(insertTree(tree)); index.add(trash, new File(trash, "file1")); index.add(trash, new File(trash, "dir/subfile")); @@ -83,8 +83,8 @@ public class IndexDiffTest extends RepositoryTestCase { tree.findBlobMember("file2").setId(ObjectId.fromString("30d67d4672d5c05833b7192cc77a79eaafb5c7ad")); Tree tree2 = (Tree) tree.findTreeMember("dir"); tree2.findBlobMember("file3").setId(ObjectId.fromString("873fb8d667d05436d728c52b1d7a09528e6eb59b")); - tree2.setId(new ObjectWriter(db).writeTree(tree2)); - tree.setId(new ObjectWriter(db).writeTree(tree)); + tree2.setId(insertTree(tree2)); + tree.setId(insertTree(tree)); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.getId(), iterator); @@ -113,8 +113,8 @@ public class IndexDiffTest extends RepositoryTestCase { assertEquals(2, tree.memberCount()); Tree tree2 = (Tree) tree.findTreeMember("dir"); - tree2.setId(new ObjectWriter(db).writeTree(tree2)); - tree.setId(new ObjectWriter(db).writeTree(tree)); + tree2.setId(insertTree(tree2)); + tree.setId(insertTree(tree)); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.getId(), iterator); diff.diff(); @@ -144,7 +144,7 @@ public class IndexDiffTest extends RepositoryTestCase { tree.addFile("a=c").setId(ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714")); tree.addFile("a=d").setId(ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2")); - tree.setId(new ObjectWriter(db).writeTree(tree)); + tree.setId(insertTree(tree)); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.getId(), iterator); @@ -185,10 +185,10 @@ public class IndexDiffTest extends RepositoryTestCase { tree.addFile("a=d").setId(ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2")); Tree tree3 = (Tree) tree.findTreeMember("a/b.b"); - tree3.setId(new ObjectWriter(db).writeTree(tree3)); + tree3.setId(insertTree(tree3)); Tree tree2 = (Tree) tree.findTreeMember("a"); - tree2.setId(new ObjectWriter(db).writeTree(tree2)); - tree.setId(new ObjectWriter(db).writeTree(tree)); + tree2.setId(insertTree(tree2)); + tree.setId(insertTree(tree)); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, tree.getId(), iterator); @@ -199,4 +199,15 @@ public class IndexDiffTest extends RepositoryTestCase { assertEquals(0, diff.getMissing().size()); assertEquals(0, diff.getModified().size()); } + + private ObjectId insertTree(Tree tree) throws IOException { + ObjectInserter oi = db.newObjectInserter(); + try { + ObjectId id = oi.insert(Constants.OBJ_TREE, tree.format()); + oi.flush(); + return id; + } finally { + oi.release(); + } + } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java index fe2d0b248c..f2bb66fd30 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java @@ -110,21 +110,25 @@ public abstract class ReadTreeTest extends RepositoryTestCase { private Tree buildTree(HashMap headEntries) throws IOException { Tree tree = new Tree(db); - ObjectWriter ow = new ObjectWriter(db); if (headEntries == null) return tree; FileTreeEntry fileEntry; Tree parent; - for (java.util.Map.Entry e : headEntries.entrySet()) { - fileEntry = tree.addFile(e.getKey()); - fileEntry.setId(genSha1(e.getValue())); - parent = fileEntry.getParent(); - while (parent != null) { - parent.setId(ow.writeTree(parent)); - parent = parent.getParent(); + ObjectInserter oi = db.newObjectInserter(); + try { + for (java.util.Map.Entry e : headEntries.entrySet()) { + fileEntry = tree.addFile(e.getKey()); + fileEntry.setId(genSha1(e.getValue())); + parent = fileEntry.getParent(); + while (parent != null) { + parent.setId(oi.insert(Constants.OBJ_TREE, parent.format())); + parent = parent.getParent(); + } } + oi.flush(); + } finally { + oi.release(); } - return tree; } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java index ebe2db111d..92c4b9cc04 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java @@ -109,8 +109,13 @@ public class ReflogConfigTest extends RepositoryTestCase { commit.setCommitter(committer); commit.setMessage(commitMsg); commit.setTreeId(t.getTreeId()); - ObjectWriter writer = new ObjectWriter(db); - commit.setCommitId(writer.writeCommit(commit)); + ObjectInserter inserter = db.newObjectInserter(); + try { + inserter.insert(commit); + inserter.flush(); + } finally { + inserter.release(); + } int nl = commitMsg.indexOf('\n'); final RefUpdate ru = db.updateRef(Constants.HEAD); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkTest.java index 0ff4d512e8..840bee8466 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkTest.java @@ -43,9 +43,10 @@ package org.eclipse.jgit.revwalk; +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileTreeEntry; import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.ObjectWriter; +import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.Tree; public class ObjectWalkTest extends RevWalkTestCase { @@ -214,11 +215,16 @@ public class ObjectWalkTest extends RevWalkTestCase { Tree A_A = A.addTree("A"); Tree A_B = A.addTree("B"); - final ObjectWriter ow = new ObjectWriter(db); - A_A.setId(ow.writeTree(A_A)); - A_B.setId(ow.writeTree(A_B)); - A.setId(ow.writeTree(A)); - root.setId(ow.writeTree(root)); + final ObjectInserter inserter = db.newObjectInserter(); + try { + A_A.setId(inserter.insert(Constants.OBJ_TREE, A_A.format())); + A_B.setId(inserter.insert(Constants.OBJ_TREE, A_B.format())); + A.setId(inserter.insert(Constants.OBJ_TREE, A.format())); + root.setId(inserter.insert(Constants.OBJ_TREE, root.format())); + inserter.flush(); + } finally { + inserter.release(); + } tree_root = rw.parseTree(root.getId()); tree_A = rw.parseTree(A.getId()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/ConcurrentRepackTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/ConcurrentRepackTest.java index c8f2aad759..7608a51f22 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/ConcurrentRepackTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/ConcurrentRepackTest.java @@ -57,8 +57,8 @@ import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.NullProgressMonitor; import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectLoader; -import org.eclipse.jgit.lib.ObjectWriter; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryTestCase; import org.eclipse.jgit.revwalk.RevObject; @@ -265,8 +265,14 @@ public class ConcurrentRepackTest extends RepositoryTestCase { throws IOException { final RevWalk revWalk = new RevWalk(repo); final byte[] bytes = Constants.encode(data); - final ObjectWriter ow = new ObjectWriter(repo); - final ObjectId id = ow.writeBlob(bytes); + final ObjectInserter inserter = repo.newObjectInserter(); + final ObjectId id; + try { + id = inserter.insert(Constants.OBJ_BLOB, bytes); + inserter.flush(); + } finally { + inserter.release(); + } try { parse(id); fail("Object " + id.name() + " should not exist in test repository"); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0003_Basic.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0003_Basic.java index 0352a40bfd..baa35f274b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0003_Basic.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0003_Basic.java @@ -46,7 +46,6 @@ package org.eclipse.jgit.storage.file; -import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; @@ -67,7 +66,6 @@ import org.eclipse.jgit.lib.FileTreeEntry; import org.eclipse.jgit.lib.ObjectDatabase; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectInserter; -import org.eclipse.jgit.lib.ObjectWriter; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.Repository; @@ -294,7 +292,7 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { public void test003_WriteShouldBeEmptyTree() throws IOException { final Tree t = new Tree(db); - final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]); + final ObjectId emptyId = insertEmptyBlob(); t.addFile("should-be-empty").setId(emptyId); t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY); assertEquals("7bb943559a305bdd6bdee2cef6e5df2413c3d30a", t.getId() @@ -419,7 +417,7 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { } public void test012_SubtreeExternalSorting() throws IOException { - final ObjectId emptyBlob = new ObjectWriter(db).writeBlob(new byte[0]); + final ObjectId emptyBlob = insertEmptyBlob(); final Tree t = new Tree(db); final FileTreeEntry e0 = t.addFile("a-"); final FileTreeEntry e1 = t.addFile("a-b"); @@ -439,7 +437,7 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { } public void test020_createBlobTag() throws IOException { - final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]); + final ObjectId emptyId = insertEmptyBlob(); final TagBuilder t = new TagBuilder(); t.setObjectId(emptyId, Constants.OBJ_BLOB); t.setTag("test020"); @@ -456,10 +454,10 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { } public void test021_createTreeTag() throws IOException { - final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]); + final ObjectId emptyId = insertEmptyBlob(); final Tree almostEmptyTree = new Tree(db); almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false)); - final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree); + final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree); final TagBuilder t = new TagBuilder(); t.setObjectId(almostEmptyTreeId, Constants.OBJ_TREE); t.setTag("test021"); @@ -476,10 +474,10 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { } public void test022_createCommitTag() throws IOException { - final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]); + final ObjectId emptyId = insertEmptyBlob(); final Tree almostEmptyTree = new Tree(db); almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false)); - final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree); + final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree); final CommitBuilder almostEmptyCommit = new CommitBuilder(); almostEmptyCommit.setAuthor(new PersonIdent(author, 1154236443000L, -2 * 60)); // not exactly the same almostEmptyCommit.setCommitter(new PersonIdent(author, 1154236443000L, -2 * 60)); @@ -502,10 +500,10 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { } public void test023_createCommitNonAnullii() throws IOException { - final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]); + final ObjectId emptyId = insertEmptyBlob(); final Tree almostEmptyTree = new Tree(db); almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false)); - final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree); + final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree); CommitBuilder commit = new CommitBuilder(); commit.setTreeId(almostEmptyTreeId); commit.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60)); @@ -520,10 +518,10 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { } public void test024_createCommitNonAscii() throws IOException { - final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]); + final ObjectId emptyId = insertEmptyBlob(); final Tree almostEmptyTree = new Tree(db); almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false)); - final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree); + final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree); CommitBuilder commit = new CommitBuilder(); commit.setTreeId(almostEmptyTreeId); commit.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60)); @@ -537,9 +535,8 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { public void test025_computeSha1NoStore() throws IOException { byte[] data = "test025 some data, more than 16 bytes to get good coverage" .getBytes("ISO-8859-1"); - // TODO: but we do not test legacy header writing - final ObjectId id = new ObjectWriter(db).computeBlobSha1(data.length, - new ByteArrayInputStream(data)); + final ObjectId id = new ObjectInserter.Formatter().idFor( + Constants.OBJ_BLOB, data); assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id.name()); } @@ -693,6 +690,29 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { } + private ObjectId insertEmptyBlob() throws IOException { + final ObjectId emptyId; + ObjectInserter oi = db.newObjectInserter(); + try { + emptyId = oi.insert(Constants.OBJ_BLOB, new byte[] {}); + oi.flush(); + } finally { + oi.release(); + } + return emptyId; + } + + private ObjectId insertTree(Tree tree) throws IOException { + ObjectInserter oi = db.newObjectInserter(); + try { + ObjectId id = oi.insert(Constants.OBJ_TREE, tree.format()); + oi.flush(); + return id; + } finally { + oi.release(); + } + } + private ObjectId insertCommit(final CommitBuilder builder) throws IOException, UnsupportedEncodingException { ObjectInserter oi = db.newObjectInserter(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/NameConflictTreeWalkTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/NameConflictTreeWalkTest.java index e59b7c18d3..7e3dea6714 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/NameConflictTreeWalkTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/NameConflictTreeWalkTest.java @@ -43,15 +43,13 @@ package org.eclipse.jgit.treewalk; -import java.io.ByteArrayInputStream; - import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheBuilder; import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileMode; -import org.eclipse.jgit.lib.ObjectWriter; +import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.RepositoryTestCase; public class NameConflictTreeWalkTest extends RepositoryTestCase { @@ -248,11 +246,10 @@ public class NameConflictTreeWalkTest extends RepositoryTestCase { private DirCacheEntry makeEntry(final String path, final FileMode mode) throws Exception { - final byte[] pathBytes = Constants.encode(path); final DirCacheEntry ent = new DirCacheEntry(path); ent.setFileMode(mode); - ent.setObjectId(new ObjectWriter(db).computeBlobSha1(pathBytes.length, - new ByteArrayInputStream(pathBytes))); + ent.setObjectId(new ObjectInserter.Formatter().idFor( + Constants.OBJ_BLOB, Constants.encode(path))); return ent; } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/PostOrderTreeWalkTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/PostOrderTreeWalkTest.java index 274df5bec0..8f813a806b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/PostOrderTreeWalkTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/PostOrderTreeWalkTest.java @@ -43,7 +43,8 @@ package org.eclipse.jgit.treewalk; -import java.io.ByteArrayInputStream; +import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE; +import static org.eclipse.jgit.lib.FileMode.TREE; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheBuilder; @@ -51,12 +52,9 @@ import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileMode; -import org.eclipse.jgit.lib.ObjectWriter; +import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.RepositoryTestCase; -import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE; -import static org.eclipse.jgit.lib.FileMode.TREE; - public class PostOrderTreeWalkTest extends RepositoryTestCase { public void testInitialize_NoPostOrder() throws Exception { final TreeWalk tw = new TreeWalk(db); @@ -178,11 +176,10 @@ public class PostOrderTreeWalkTest extends RepositoryTestCase { } private DirCacheEntry makeFile(final String path) throws Exception { - final byte[] pathBytes = Constants.encode(path); final DirCacheEntry ent = new DirCacheEntry(path); ent.setFileMode(REGULAR_FILE); - ent.setObjectId(new ObjectWriter(db).computeBlobSha1(pathBytes.length, - new ByteArrayInputStream(pathBytes))); + ent.setObjectId(new ObjectInserter.Formatter().idFor( + Constants.OBJ_BLOB, Constants.encode(path))); return ent; } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/TreeWalkBasicDiffTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/TreeWalkBasicDiffTest.java index 581683e34f..e0957401c5 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/TreeWalkBasicDiffTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/TreeWalkBasicDiffTest.java @@ -43,9 +43,13 @@ package org.eclipse.jgit.treewalk; +import static org.eclipse.jgit.lib.Constants.OBJ_BLOB; +import static org.eclipse.jgit.lib.Constants.OBJ_TREE; +import static org.eclipse.jgit.lib.Constants.encode; + import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.ObjectWriter; +import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.RepositoryTestCase; import org.eclipse.jgit.lib.Tree; import org.eclipse.jgit.treewalk.filter.TreeFilter; @@ -53,11 +57,11 @@ import org.eclipse.jgit.treewalk.filter.TreeFilter; public class TreeWalkBasicDiffTest extends RepositoryTestCase { public void testMissingSubtree_DetectFileAdded_FileModified() throws Exception { - final ObjectWriter ow = new ObjectWriter(db); - final ObjectId aFileId = ow.writeBlob("a".getBytes()); - final ObjectId bFileId = ow.writeBlob("b".getBytes()); - final ObjectId cFileId1 = ow.writeBlob("c-1".getBytes()); - final ObjectId cFileId2 = ow.writeBlob("c-2".getBytes()); + final ObjectInserter inserter = db.newObjectInserter(); + final ObjectId aFileId = inserter.insert(OBJ_BLOB, encode("a")); + final ObjectId bFileId = inserter.insert(OBJ_BLOB, encode("b")); + final ObjectId cFileId1 = inserter.insert(OBJ_BLOB, encode("c-1")); + final ObjectId cFileId2 = inserter.insert(OBJ_BLOB, encode("c-2")); // Create sub-a/empty, sub-c/empty = hello. final ObjectId oldTree; @@ -66,14 +70,14 @@ public class TreeWalkBasicDiffTest extends RepositoryTestCase { { final Tree subA = root.addTree("sub-a"); subA.addFile("empty").setId(aFileId); - subA.setId(ow.writeTree(subA)); + subA.setId(inserter.insert(OBJ_TREE, subA.format())); } { final Tree subC = root.addTree("sub-c"); subC.addFile("empty").setId(cFileId1); - subC.setId(ow.writeTree(subC)); + subC.setId(inserter.insert(OBJ_TREE, subC.format())); } - oldTree = ow.writeTree(root); + oldTree = inserter.insert(OBJ_TREE, root.format()); } // Create sub-a/empty, sub-b/empty, sub-c/empty. @@ -83,20 +87,22 @@ public class TreeWalkBasicDiffTest extends RepositoryTestCase { { final Tree subA = root.addTree("sub-a"); subA.addFile("empty").setId(aFileId); - subA.setId(ow.writeTree(subA)); + subA.setId(inserter.insert(OBJ_TREE, subA.format())); } { final Tree subB = root.addTree("sub-b"); subB.addFile("empty").setId(bFileId); - subB.setId(ow.writeTree(subB)); + subB.setId(inserter.insert(OBJ_TREE, subB.format())); } { final Tree subC = root.addTree("sub-c"); subC.addFile("empty").setId(cFileId2); - subC.setId(ow.writeTree(subC)); + subC.setId(inserter.insert(OBJ_TREE, subC.format())); } - newTree = ow.writeTree(root); + newTree = inserter.insert(OBJ_TREE, root.format()); } + inserter.flush(); + inserter.release(); final TreeWalk tw = new TreeWalk(db); tw.reset(new ObjectId[] { oldTree, newTree }); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java index bf293d190b..5e6e616e51 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java @@ -424,8 +424,18 @@ public class GitIndex { uid = -1; gid = -1; size = (int) f.length(); - ObjectWriter writer = new ObjectWriter(db); - sha1 = writer.writeBlob(f); + ObjectInserter inserter = db.newObjectInserter(); + try { + InputStream in = new FileInputStream(f); + try { + sha1 = inserter.insert(Constants.OBJ_BLOB, f.length(), in); + } finally { + in.close(); + } + inserter.flush(); + } finally { + inserter.release(); + } name = key; flags = (short) ((stage << 12) | name.length); // TODO: fix flags stages = (1 >> getStage()); @@ -444,8 +454,18 @@ public class GitIndex { uid = -1; gid = -1; size = newContent.length; - ObjectWriter writer = new ObjectWriter(db); - sha1 = writer.writeBlob(newContent); + ObjectInserter inserter = db.newObjectInserter(); + try { + InputStream in = new FileInputStream(f); + try { + sha1 = inserter.insert(Constants.OBJ_BLOB, newContent); + } finally { + in.close(); + } + inserter.flush(); + } finally { + inserter.release(); + } name = key; flags = (short) ((stage << 12) | name.length); // TODO: fix flags stages = (1 >> getStage()); @@ -517,11 +537,22 @@ public class GitIndex { } if (modified) { size = (int) f.length(); - ObjectWriter writer = new ObjectWriter(db); - ObjectId newsha1 = writer.writeBlob(f); - if (!newsha1.equals(sha1)) - modified = true; - sha1 = newsha1; + ObjectInserter oi = db.newObjectInserter(); + try { + InputStream in = new FileInputStream(f); + try { + ObjectId newsha1 = oi.insert(Constants.OBJ_BLOB, f + .length(), in); + oi.flush(); + if (!newsha1.equals(sha1)) + modified = true; + sha1 = newsha1; + } finally { + in.close(); + } + } finally { + oi.release(); + } } return modified; } @@ -540,11 +571,16 @@ public class GitIndex { public boolean update(File f, byte[] newContent) throws IOException { boolean modified = false; size = newContent.length; - ObjectWriter writer = new ObjectWriter(db); - ObjectId newsha1 = writer.writeBlob(newContent); - if (!newsha1.equals(sha1)) - modified = true; - sha1 = newsha1; + ObjectInserter oi = db.newObjectInserter(); + try { + ObjectId newsha1 = oi.insert(Constants.OBJ_BLOB, newContent); + oi.flush(); + if (!newsha1.equals(sha1)) + modified = true; + sha1 = newsha1; + } finally { + oi.release(); + } return modified; } @@ -661,11 +697,9 @@ public class GitIndex { try { InputStream is = new FileInputStream(file); try { - ObjectWriter objectWriter = new ObjectWriter(db); - ObjectId newId = objectWriter.computeBlobSha1(file - .length(), is); - boolean ret = !newId.equals(sha1); - return ret; + ObjectId newId = new ObjectInserter.Formatter().idFor( + Constants.OBJ_BLOB, file.length(), is); + return !newId.equals(sha1); } catch (IOException e) { e.printStackTrace(); } finally { @@ -932,44 +966,49 @@ public class GitIndex { */ public ObjectId writeTree() throws IOException { checkWriteOk(); - ObjectWriter writer = new ObjectWriter(db); - Tree current = new Tree(db); - Stack trees = new Stack(); - trees.push(current); - String[] prevName = new String[0]; - for (Entry e : entries.values()) { - if (e.getStage() != 0) - continue; - String[] newName = splitDirPath(e.getName()); - int c = longestCommonPath(prevName, newName); - while (c < trees.size() - 1) { - current.setId(writer.writeTree(current)); - trees.pop(); - current = trees.isEmpty() ? null : (Tree) trees.peek(); - } - while (trees.size() < newName.length) { - if (!current.existsTree(newName[trees.size() - 1])) { - current = new Tree(current, Constants.encode(newName[trees.size() - 1])); - current.getParent().addEntry(current); - trees.push(current); - } else { - current = (Tree) current.findTreeMember(newName[trees - .size() - 1]); - trees.push(current); + ObjectInserter inserter = db.newObjectInserter(); + try { + Tree current = new Tree(db); + Stack trees = new Stack(); + trees.push(current); + String[] prevName = new String[0]; + for (Entry e : entries.values()) { + if (e.getStage() != 0) + continue; + String[] newName = splitDirPath(e.getName()); + int c = longestCommonPath(prevName, newName); + while (c < trees.size() - 1) { + current.setId(inserter.insert(Constants.OBJ_TREE, current.format())); + trees.pop(); + current = trees.isEmpty() ? null : (Tree) trees.peek(); + } + while (trees.size() < newName.length) { + if (!current.existsTree(newName[trees.size() - 1])) { + current = new Tree(current, Constants.encode(newName[trees.size() - 1])); + current.getParent().addEntry(current); + trees.push(current); + } else { + current = (Tree) current.findTreeMember(newName[trees + .size() - 1]); + trees.push(current); + } } + FileTreeEntry ne = new FileTreeEntry(current, e.sha1, + Constants.encode(newName[newName.length - 1]), + (e.mode & FileMode.EXECUTABLE_FILE.getBits()) == FileMode.EXECUTABLE_FILE.getBits()); + current.addEntry(ne); } - FileTreeEntry ne = new FileTreeEntry(current, e.sha1, - Constants.encode(newName[newName.length - 1]), - (e.mode & FileMode.EXECUTABLE_FILE.getBits()) == FileMode.EXECUTABLE_FILE.getBits()); - current.addEntry(ne); - } - while (!trees.isEmpty()) { - current.setId(writer.writeTree(current)); - trees.pop(); - if (!trees.isEmpty()) - current = trees.peek(); + while (!trees.isEmpty()) { + current.setId(inserter.insert(Constants.OBJ_TREE, current.format())); + trees.pop(); + if (!trees.isEmpty()) + current = trees.peek(); + } + inserter.flush(); + return current.getTreeId(); + } finally { + inserter.release(); } - return current.getTreeId(); } String[] splitDirPath(String name) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java index 1024459225..adc20b5b08 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java @@ -47,15 +47,10 @@ package org.eclipse.jgit.lib; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; -import java.text.MessageFormat; - -import org.eclipse.jgit.JGitText; -import org.eclipse.jgit.errors.ObjectWritingException; /** * Inserts objects into an existing {@code ObjectDatabase}. @@ -286,30 +281,4 @@ public abstract class ObjectInserter { * released after the subsequent usage. */ public abstract void release(); - - /** - * Format a Tree in canonical format. - * - * @param tree - * the tree object to format - * @return canonical encoding of the tree object. - * @throws IOException - * the tree cannot be loaded, or its not in a writable state. - */ - public final byte[] format(Tree tree) throws IOException { - ByteArrayOutputStream o = new ByteArrayOutputStream(); - for (TreeEntry e : tree.members()) { - ObjectId id = e.getId(); - if (id == null) - throw new ObjectWritingException(MessageFormat.format(JGitText - .get().objectAtPathDoesNotHaveId, e.getFullName())); - - e.getMode().copyTo(o); - o.write(' '); - o.write(e.getNameUTF8()); - o.write(0); - id.copyRawTo(o); - } - return o.toByteArray(); - } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectWriter.java deleted file mode 100644 index 339986be07..0000000000 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectWriter.java +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright (C) 2007, Robin Rosenberg - * Copyright (C) 2006-2008, Shawn O. Pearce - * Copyright (C) 2009, Google Inc. - * and other copyright owners as documented in the project's IP log. - * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.eclipse.jgit.lib; - -import static org.eclipse.jgit.lib.Constants.OBJ_BLOB; -import static org.eclipse.jgit.lib.Constants.OBJ_TREE; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; - -/** - * A class for writing loose objects. - * - * @deprecated Use {@link Repository#newObjectInserter()}. - */ -public class ObjectWriter { - private final ObjectInserter inserter; - - /** - * Construct an Object writer for the specified repository - * - * @param d - */ - public ObjectWriter(final Repository d) { - inserter = d.newObjectInserter(); - } - - /** - * Write a blob with the specified data - * - * @param b - * bytes of the blob - * @return SHA-1 of the blob - * @throws IOException - */ - public ObjectId writeBlob(final byte[] b) throws IOException { - try { - ObjectId id = inserter.insert(OBJ_BLOB, b); - inserter.flush(); - return id; - } finally { - inserter.release(); - } - } - - /** - * Write a blob with the data in the specified file - * - * @param f - * a file containing blob data - * @return SHA-1 of the blob - * @throws IOException - */ - public ObjectId writeBlob(final File f) throws IOException { - final FileInputStream is = new FileInputStream(f); - try { - return writeBlob(f.length(), is); - } finally { - is.close(); - } - } - - /** - * Write a blob with data from a stream - * - * @param len - * number of bytes to consume from the stream - * @param is - * stream with blob data - * @return SHA-1 of the blob - * @throws IOException - */ - public ObjectId writeBlob(final long len, final InputStream is) - throws IOException { - try { - ObjectId id = inserter.insert(OBJ_BLOB, len, is); - inserter.flush(); - return id; - } finally { - inserter.release(); - } - } - - /** - * Write a Tree to the object database. - * - * @param tree - * Tree - * @return SHA-1 of the tree - * @throws IOException - */ - public ObjectId writeTree(Tree tree) throws IOException { - try { - ObjectId id = inserter.insert(OBJ_TREE, inserter.format(tree)); - inserter.flush(); - return id; - } finally { - inserter.release(); - } - } - - /** - * Write a canonical tree to the object database. - * - * @param treeData - * the canonical encoding of the tree object. - * @return SHA-1 of the tree - * @throws IOException - */ - public ObjectId writeCanonicalTree(byte[] treeData) throws IOException { - try { - ObjectId id = inserter.insert(OBJ_TREE, treeData); - inserter.flush(); - return id; - } finally { - inserter.release(); - } - } - - /** - * Write a Commit to the object database - * - * @param commit - * Commit to store - * @return SHA-1 of the commit - * @throws IOException - */ - public ObjectId writeCommit(CommitBuilder commit) throws IOException { - try { - ObjectId id = inserter.insert(commit); - inserter.flush(); - return id; - } finally { - inserter.release(); - } - } - - /** - * Write an annotated Tag to the object database - * - * @param tag - * Tag - * @return SHA-1 of the tag - * @throws IOException - */ - public ObjectId writeTag(TagBuilder tag) throws IOException { - try { - ObjectId id = inserter.insert(tag); - inserter.flush(); - return id; - } finally { - inserter.release(); - } - } - - /** - * Compute the SHA-1 of a blob without creating an object. This is for - * figuring out if we already have a blob or not. - * - * @param len - * number of bytes to consume - * @param is - * stream for read blob data from - * @return SHA-1 of a looked for blob - * @throws IOException - */ - public ObjectId computeBlobSha1(long len, InputStream is) - throws IOException { - return computeObjectSha1(OBJ_BLOB, len, is); - } - - /** - * Compute the SHA-1 of an object without actually creating an object in the - * database - * - * @param type - * kind of object - * @param len - * number of bytes to consume - * @param is - * stream for read data from - * @return SHA-1 of data combined with type information - * @throws IOException - */ - public ObjectId computeObjectSha1(int type, long len, InputStream is) - throws IOException { - try { - return inserter.idFor(type, len, is); - } finally { - inserter.release(); - } - } -} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java index d68b9f6380..2eb578b88e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java @@ -45,6 +45,7 @@ package org.eclipse.jgit.lib; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.text.MessageFormat; @@ -52,6 +53,7 @@ import org.eclipse.jgit.JGitText; import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.EntryExistsException; import org.eclipse.jgit.errors.MissingObjectException; +import org.eclipse.jgit.errors.ObjectWritingException; import org.eclipse.jgit.util.RawParseUtils; /** @@ -604,6 +606,30 @@ public class Tree extends TreeEntry implements Treeish { contents = temp; } + /** + * Format this Tree in canonical format. + * + * @return canonical encoding of the tree object. + * @throws IOException + * the tree cannot be loaded, or its not in a writable state. + */ + public byte[] format() throws IOException { + ByteArrayOutputStream o = new ByteArrayOutputStream(); + for (TreeEntry e : members()) { + ObjectId id = e.getId(); + if (id == null) + throw new ObjectWritingException(MessageFormat.format(JGitText + .get().objectAtPathDoesNotHaveId, e.getFullName())); + + e.getMode().copyTo(o); + o.write(' '); + o.write(e.getNameUTF8()); + o.write(0); + id.copyRawTo(o); + } + return o.toByteArray(); + } + public String toString() { final StringBuilder r = new StringBuilder(); r.append(ObjectId.toString(getId())); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/WriteTree.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/WriteTree.java index c24fcd9fd7..13cb74218e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/WriteTree.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/WriteTree.java @@ -46,6 +46,7 @@ package org.eclipse.jgit.lib; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.text.MessageFormat; @@ -61,7 +62,7 @@ import org.eclipse.jgit.errors.SymlinksNotSupportedException; */ @Deprecated public class WriteTree extends TreeVisitorWithCurrentDirectory { - private final ObjectWriter ow; + private final ObjectInserter inserter; /** * Construct a WriteTree for a given directory @@ -71,11 +72,20 @@ public class WriteTree extends TreeVisitorWithCurrentDirectory { */ public WriteTree(final File sourceDirectory, final Repository db) { super(sourceDirectory); - ow = new ObjectWriter(db); + inserter = db.newObjectInserter(); } public void visitFile(final FileTreeEntry f) throws IOException { - f.setId(ow.writeBlob(new File(getCurrentDirectory(), f.getName()))); + File path = new File(getCurrentDirectory(), f.getName()); + FileInputStream in = new FileInputStream(path); + try { + long sz = in.getChannel().size(); + f.setId(inserter.insert(Constants.OBJ_BLOB, sz, in)); + inserter.flush(); + } finally { + inserter.release(); + in.close(); + } } public void visitSymlink(final SymlinkTreeEntry s) throws IOException { @@ -87,7 +97,12 @@ public class WriteTree extends TreeVisitorWithCurrentDirectory { public void endVisitTree(final Tree t) throws IOException { super.endVisitTree(t); - t.setId(ow.writeTree(t)); + try { + t.setId(inserter.insert(Constants.OBJ_TREE, t.format())); + inserter.flush(); + } finally { + inserter.release(); + } } public void visitGitlink(GitlinkTreeEntry s) throws IOException { -- 2.39.5