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 <spearce@spearce.org>
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"));
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);
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();
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);
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);
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();
+ }
+ }
}
private Tree buildTree(HashMap<String, String> 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<String, String> 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<String, String> 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;
}
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);
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 {
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());
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;
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");
package org.eclipse.jgit.storage.file;
-import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
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;
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()
}
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");
}
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");
}
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");
}
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));
}
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));
}
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));
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());
}
}
+ 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();
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 {
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;
}
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;
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);
}
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;
}
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;
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;
{
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.
{
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 });
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());
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());
}
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;
}
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;
}
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 {
*/
public ObjectId writeTree() throws IOException {
checkWriteOk();
- ObjectWriter writer = new ObjectWriter(db);
- Tree current = new Tree(db);
- Stack<Tree> trees = new Stack<Tree>();
- 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<Tree> trees = new Stack<Tree>();
+ 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) {
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}.
* 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();
- }
}
+++ /dev/null
-/*
- * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
- * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
- * 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();
- }
- }
-}
package org.eclipse.jgit.lib;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.MessageFormat;
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;
/**
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()));
package org.eclipse.jgit.lib;
import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.text.MessageFormat;
*/
@Deprecated
public class WriteTree extends TreeVisitorWithCurrentDirectory {
- private final ObjectWriter ow;
+ private final ObjectInserter inserter;
/**
* Construct a WriteTree for a given directory
*/
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 {
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 {