diff options
Diffstat (limited to 'org.eclipse.jgit')
10 files changed, 217 insertions, 133 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java index 8623bb76a4..8564570d34 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/Git.java @@ -258,7 +258,7 @@ public class Git { /** * Returns a command object to execute a {@code rm} command - * + * * @see <a * href="http://www.kernel.org/pub/software/scm/git/docs/git-rm.html" * >Git documentation about rm</a> @@ -283,6 +283,19 @@ public class Git { } /** + * Returns a command object to execute a {@code init} command + * + * @see <a + * href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html" + * >Git documentation about init</a> + * @return a {@link InitCommand} used to collect all optional parameters and + * to finally execute the {@code init} command + */ + static public InitCommand init() { + return new InitCommand(); + } + + /** * @return the git repository this class is interacting with */ public Repository getRepository() { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java new file mode 100644 index 0000000000..fdadf9690d --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com> + * 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.api; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.Callable; + +import org.eclipse.jgit.api.errors.JGitInternalException; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.RepositoryBuilder; + +/** + * Create an empty git repository + * + * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html" + * >Git documentation about init</a> + */ +public class InitCommand implements Callable<Git> { + private File directory; + + private boolean bare; + + /** + * Executes the {@code Init} command. + * + * @throws JGitInternalException + * if the repository can't be created + * @return the newly created {@code Git} object with associated repository + */ + public Git call() throws JGitInternalException { + try { + RepositoryBuilder builder = new RepositoryBuilder(); + if (bare) + builder.setBare(); + builder.readEnvironment(); + if (directory != null) { + File d = directory; + if (!bare) + d = new File(d, Constants.DOT_GIT); + builder.setGitDir(d); + } else if (builder.getGitDir() == null) { + File d = new File("."); + if (!bare) + d = new File(d, Constants.DOT_GIT); + builder.setGitDir(d); + } + Repository repository = builder.build(); + repository.create(bare); + return new Git(repository); + } catch (IOException e) { + throw new JGitInternalException(e.getMessage(), e); + } + } + + /** + * The optional directory associated with the init operation. If no + * directory is set, we'll use the current directory + * + * @param directory + * the directory to init to + * @return this instance + */ + public InitCommand setDirectory(File directory) { + this.directory = directory; + return this; + } + + /** + * @param bare + * whether the repository is bare or not + * @return this instance + */ + public InitCommand setBare(boolean bare) { + this.bare = bare; + return this; + } + +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/TagCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/TagCommand.java index 45b0ca4ed0..95474c8bc4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/TagCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/TagCommand.java @@ -140,7 +140,7 @@ public class TagCommand extends GitCommand<RevTag> { RevWalk revWalk = new RevWalk(repo); try { - RevTag revTag = revWalk.parseTag(newTag.getTagId()); + RevTag revTag = revWalk.parseTag(tagId); String refName = Constants.R_TAGS + newTag.getTag(); RefUpdate tagRef = repo.updateRef(refName); tagRef.setNewObjectId(tagId); 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/CommitBuilder.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java index 288b1d4831..8c6f809cba 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java @@ -76,8 +76,6 @@ public class CommitBuilder { private static final byte[] hencoding = Constants.encodeASCII("encoding"); - private ObjectId commitId; - private ObjectId treeId; private ObjectId[] parentIds; @@ -96,21 +94,6 @@ public class CommitBuilder { encoding = Constants.CHARSET; } - /** @return this commit's object id. */ - public ObjectId getCommitId() { - return commitId; - } - - /** - * Set the id of this commit object. - * - * @param id - * the id that we calculated for this object. - */ - public void setCommitId(final ObjectId id) { - commitId = id; - } - /** @return id of the root tree listing this commit's snapshot. */ public ObjectId getTreeId() { return treeId; @@ -124,7 +107,6 @@ public class CommitBuilder { */ public void setTreeId(AnyObjectId id) { treeId = id.copy(); - commitId = null; } /** @return the author of this commit (who wrote it). */ @@ -140,7 +122,6 @@ public class CommitBuilder { */ public void setAuthor(PersonIdent newAuthor) { author = newAuthor; - commitId = null; } /** @return the committer and commit time for this object. */ @@ -156,7 +137,6 @@ public class CommitBuilder { */ public void setCommitter(PersonIdent newCommitter) { committer = newCommitter; - commitId = null; } /** @return the ancestors of this commit. Never null. */ @@ -172,7 +152,6 @@ public class CommitBuilder { */ public void setParentId(AnyObjectId newParent) { parentIds = new ObjectId[] { newParent.copy() }; - commitId = null; } /** @@ -188,7 +167,6 @@ public class CommitBuilder { */ public void setParentIds(AnyObjectId parent1, AnyObjectId parent2) { parentIds = new ObjectId[] { parent1.copy(), parent2.copy() }; - commitId = null; } /** @@ -201,7 +179,6 @@ public class CommitBuilder { parentIds = new ObjectId[newParents.length]; for (int i = 0; i < newParents.length; i++) parentIds[i] = newParents[i].copy(); - commitId = null; } /** @@ -214,7 +191,6 @@ public class CommitBuilder { parentIds = new ObjectId[newParents.size()]; for (int i = 0; i < newParents.size(); i++) parentIds[i] = newParents.get(i).copy(); - commitId = null; } /** @@ -232,7 +208,6 @@ public class CommitBuilder { newParents[i] = parentIds[i]; newParents[parentIds.length] = additionalParent.copy(); parentIds = newParents; - commitId = null; } } @@ -279,9 +254,6 @@ public class CommitBuilder { /** * Format this builder's state as a commit object. * - * As a side effect, {@link #getCommitId()} will be populated with the - * proper ObjectId for the formatted content. - * * @return this object in the canonical commit format, suitable for storage * in a repository. * @throws UnsupportedEncodingException @@ -289,26 +261,6 @@ public class CommitBuilder { * supported by this Java runtime. */ public byte[] build() throws UnsupportedEncodingException { - return build(new ObjectInserter.Formatter()); - } - - /** - * Format this builder's state as a commit object. - * - * As a side effect, {@link #getCommitId()} will be populated with the - * proper ObjectId for the formatted content. - * - * @param oi - * the inserter whose formatting support will be reused. The - * inserter itself is not affected, and the commit is not - * actually inserted into the repository. - * @return this object in the canonical commit format, suitable for storage - * in a repository. - * @throws UnsupportedEncodingException - * the encoding specified by {@link #getEncoding()} is not - * supported by this Java runtime. - */ - public byte[] build(ObjectInserter oi) throws UnsupportedEncodingException { ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStreamWriter w = new OutputStreamWriter(os, getEncoding()); try { @@ -355,18 +307,26 @@ public class CommitBuilder { // throw new RuntimeException(err); } + return os.toByteArray(); + } - byte[] content = os.toByteArray(); - setCommitId(oi.idFor(Constants.OBJ_COMMIT, content)); - return content; + /** + * Format this builder's state as a commit object. + * + * @return this object in the canonical commit format, suitable for storage + * in a repository. + * @throws UnsupportedEncodingException + * the encoding specified by {@link #getEncoding()} is not + * supported by this Java runtime. + */ + public byte[] toByteArray() throws UnsupportedEncodingException { + return build(); } @Override public String toString() { StringBuilder r = new StringBuilder(); r.append("Commit"); - if (commitId != null) - r.append("[" + commitId.name() + "]"); r.append("={\n"); r.append("tree "); 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 7d02c86ddd..de0c55f651 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java @@ -177,10 +177,23 @@ public abstract class ObjectInserter { } /** - * Insert a single commit into the store, returning its unique name. + * Insert a single tree into the store, returning its unique name. * - * As a side effect, {@link CommitBuilder#getCommitId()} will also be - * populated with the returned ObjectId. + * @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. * * @param builder * the builder containing the proposed commit's data. @@ -189,15 +202,12 @@ public abstract class ObjectInserter { * the object could not be stored. */ public final ObjectId insert(CommitBuilder builder) throws IOException { - return insert(Constants.OBJ_COMMIT, builder.build(this)); + return insert(Constants.OBJ_COMMIT, builder.build()); } /** * Insert a single annotated tag into the store, returning its unique name. * - * As a side effect, {@link TagBuilder#getTagId()} will also be populated - * with the returned ObjectId. - * * @param builder * the builder containing the proposed tag's data. * @return the name of the tag object. @@ -205,7 +215,7 @@ public abstract class ObjectInserter { * the object could not be stored. */ public final ObjectId insert(TagBuilder builder) throws IOException { - return insert(Constants.OBJ_TAG, builder.build(this)); + return insert(Constants.OBJ_TAG, builder.build()); } /** diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/TagBuilder.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/TagBuilder.java index 8eadb6145c..82cd0747a4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/TagBuilder.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/TagBuilder.java @@ -62,8 +62,6 @@ import org.eclipse.jgit.revwalk.RevObject; * {@link org.eclipse.jgit.revwalk.RevWalk#parseTag(AnyObjectId)}. */ public class TagBuilder { - private ObjectId tagId; - private ObjectId object; private int type = Constants.OBJ_BAD; @@ -74,21 +72,6 @@ public class TagBuilder { private String message; - /** @return this tag's object id. */ - public ObjectId getTagId() { - return tagId; - } - - /** - * Set the id of this tag object. - * - * @param id - * the id that we calculated for this object. - */ - public void setTagId(ObjectId id) { - tagId = id; - } - /** @return the type of object this tag refers to. */ public int getObjectType() { return type; @@ -110,7 +93,6 @@ public class TagBuilder { public void setObjectId(AnyObjectId obj, int objType) { object = obj.copy(); type = objType; - tagId = null; } /** @@ -138,7 +120,6 @@ public class TagBuilder { */ public void setTag(String shortName) { this.tag = shortName; - tagId = null; } /** @return creator of this tag. May be null. */ @@ -154,7 +135,6 @@ public class TagBuilder { */ public void setTagger(PersonIdent taggerIdent) { tagger = taggerIdent; - tagId = null; } /** @return the complete commit message. */ @@ -170,36 +150,15 @@ public class TagBuilder { */ public void setMessage(final String newMessage) { message = newMessage; - tagId = null; } /** * Format this builder's state as an annotated tag object. * - * As a side effect, {@link #getTagId()} will be populated with the proper - * ObjectId for the formatted content. - * * @return this object in the canonical annotated tag format, suitable for * storage in a repository. */ public byte[] build() { - return build(new ObjectInserter.Formatter()); - } - - /** - * Format this builder's state as an annotated tag object. - * - * As a side effect, {@link #getTagId()} will be populated with the proper - * ObjectId for the formatted content. - * - * @param oi - * the inserter whose formatting support will be reused. The - * inserter itself is not affected, and the annotated tag is not - * actually inserted into the repository. - * @return this object in the canonical annotated tag format, suitable for - * storage in a repository. - */ - public byte[] build(ObjectInserter oi) { ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStreamWriter w = new OutputStreamWriter(os, Constants.CHARSET); try { @@ -231,18 +190,23 @@ public class TagBuilder { // throw new RuntimeException(err); } + return os.toByteArray(); + } - byte[] content = os.toByteArray(); - setTagId(oi.idFor(Constants.OBJ_TAG, content)); - return content; + /** + * Format this builder's state as an annotated tag object. + * + * @return this object in the canonical annotated tag format, suitable for + * storage in a repository. + */ + public byte[] toByteArray() { + return build(); } @Override public String toString() { StringBuilder r = new StringBuilder(); r.append("Tag"); - if (tagId != null) - r.append("[" + tagId.name() + "]"); r.append("={\n"); r.append("object "); 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 e14e81f6e0..737a1c3fc1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/TreeFormatter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/TreeFormatter.java @@ -52,9 +52,11 @@ import static org.eclipse.jgit.lib.FileMode.TREE; import java.io.IOException; +import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.revwalk.RevBlob; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevTree; +import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.util.TemporaryBuffer; /** @@ -271,25 +273,6 @@ public class TreeFormatter { } /** - * Compute the current tree's ObjectId. - * - * @return computed ObjectId of the tree - */ - public ObjectId getTreeId() { - final ObjectInserter.Formatter fmt = new ObjectInserter.Formatter(); - if (buf != null) - return fmt.idFor(OBJ_TREE, buf, 0, ptr); - - try { - final long len = overflowBuffer.length(); - return fmt.idFor(OBJ_TREE, len, overflowBuffer.openInputStream()); - } catch (IOException err) { - // This should never happen, its read failure on a byte array. - throw new RuntimeException(err); - } - } - - /** * Insert this tree and obtain its ObjectId. * * @param ins @@ -298,7 +281,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); @@ -312,7 +295,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. */ @@ -330,4 +313,38 @@ public class TreeFormatter { throw new RuntimeException(err); } } + + @Override + public String toString() { + byte[] raw = toByteArray(); + + CanonicalTreeParser p = new CanonicalTreeParser(); + p.reset(raw); + + StringBuilder r = new StringBuilder(); + r.append("Tree={"); + if (!p.eof()) { + r.append('\n'); + try { + new ObjectChecker().checkTree(raw); + } catch (CorruptObjectException error) { + r.append("*** ERROR: ").append(error.getMessage()).append("\n"); + r.append('\n'); + } + } + while (!p.eof()) { + final FileMode mode = p.getEntryFileMode(); + r.append(mode); + r.append(' '); + r.append(Constants.typeString(mode.getObjectType())); + r.append(' '); + r.append(p.getEntryObjectId().name()); + r.append(' '); + r.append(p.getEntryPathString()); + r.append('\n'); + p.next(); + } + r.append("}"); + return r.toString(); + } } 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) { |