From 8d4c95a64511951e98bc4c168a4e306fedc22165 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Fri, 3 Dec 2010 13:02:15 -0800 Subject: [PATCH] Add insert(TreeFormatter) to ObjectInserter This makes usage of a TreeFormatter more similar to a CommitBuilder or a TagBuilder: populate the formatter and pass to the ObjectInserter. Change-Id: I5a45ef3a35cc73f4905a34bc6f6228510df8eb2c Signed-off-by: Shawn O. Pearce Reviewed-by: Chris Aniszczyk --- .../org/eclipse/jgit/dircache/DirCacheTree.java | 2 +- .../src/org/eclipse/jgit/lib/ObjectInserter.java | 16 ++++++++++++++++ .../src/org/eclipse/jgit/lib/TreeFormatter.java | 4 ++-- .../src/org/eclipse/jgit/notes/FanoutBucket.java | 2 +- .../src/org/eclipse/jgit/notes/LeafBucket.java | 2 +- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheTree.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheTree.java index 4d97e7cd84..2995e17c98 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheTree.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheTree.java @@ -338,7 +338,7 @@ public class DirCacheTree { entryIdx++; } - id = fmt.insert(ow); + id = ow.insert(fmt); } return id; } 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 6b2eeaaca1..de0c55f651 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java @@ -176,6 +176,22 @@ public abstract class ObjectInserter { return ObjectId.fromRaw(md.digest()); } + /** + * Insert a single tree into the store, returning its unique name. + * + * @param formatter + * the formatter containing the proposed tree's data. + * @return the name of the tree object. + * @throws IOException + * the object could not be stored. + */ + public final ObjectId insert(TreeFormatter formatter) throws IOException { + // Delegate to the formatter, as then it can pass the raw internal + // buffer back to this inserter, avoiding unnecessary data copying. + // + return formatter.insertTo(this); + } + /** * Insert a single commit into the store, returning its unique name. * diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/TreeFormatter.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/TreeFormatter.java index 1b2627e01b..6e53cc7403 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/TreeFormatter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/TreeFormatter.java @@ -279,7 +279,7 @@ public class TreeFormatter { * @throws IOException * the tree could not be stored. */ - public ObjectId insert(ObjectInserter ins) throws IOException { + public ObjectId insertTo(ObjectInserter ins) throws IOException { if (buf != null) return ins.insert(OBJ_TREE, buf, 0, ptr); @@ -293,7 +293,7 @@ public class TreeFormatter { * This method is not efficient, as it needs to create a copy of the * internal buffer in order to supply an array of the correct size to the * caller. If the buffer is just to pass to an ObjectInserter, consider - * using {@link #insert(ObjectInserter)} instead. + * using {@link ObjectInserter#insert(TreeFormatter)} instead. * * @return a copy of this formatter's buffer. */ diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/notes/FanoutBucket.java b/org.eclipse.jgit/src/org/eclipse/jgit/notes/FanoutBucket.java index e1b96eaae9..944e575008 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/notes/FanoutBucket.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/notes/FanoutBucket.java @@ -254,7 +254,7 @@ class FanoutBucket extends InMemoryNoteBucket { for (; e != null; e = e.next) e.format(fmt); - return fmt.insert(inserter); + return inserter.insert(fmt); } private int treeSize() { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java b/org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java index af6c6f455e..db56eda2b1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java @@ -190,7 +190,7 @@ class LeafBucket extends InMemoryNoteBucket { for (; e != null; e = e.next) e.format(fmt); - return fmt.insert(inserter); + return inserter.insert(fmt); } private int treeSize(final int nameLen) { -- 2.39.5