diff options
author | Shawn O. Pearce <spearce@spearce.org> | 2010-08-23 10:59:30 -0700 |
---|---|---|
committer | Shawn O. Pearce <spearce@spearce.org> | 2010-08-23 10:59:30 -0700 |
commit | 32466c33bae19aff03b95a02e0b7e72d4e2f11b8 (patch) | |
tree | ea652da99f86f53f8db72c40be8b027b75276ed3 /org.eclipse.jgit | |
parent | 9d5b926ed164e8ee88d3b8b1e525d699adda01ba (diff) | |
download | jgit-32466c33bae19aff03b95a02e0b7e72d4e2f11b8.tar.gz jgit-32466c33bae19aff03b95a02e0b7e72d4e2f11b8.zip |
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 <spearce@spearce.org>
Diffstat (limited to 'org.eclipse.jgit')
5 files changed, 138 insertions, 327 deletions
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<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) { 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 <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(); - } - } -} 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 { |