diff options
author | Shawn Pearce <spearce@spearce.org> | 2015-11-27 19:34:36 -0800 |
---|---|---|
committer | Shawn Pearce <spearce@spearce.org> | 2016-01-07 15:43:50 -0800 |
commit | 52eb040c559345777592b28187d4b0753505323e (patch) | |
tree | 072923717f5456352f649bca5e16ecb0fdf79e7f | |
parent | 73eb32be829c0f2167c2796ce1ba60bdf4a6b88a (diff) | |
download | jgit-52eb040c559345777592b28187d4b0753505323e.tar.gz jgit-52eb040c559345777592b28187d4b0753505323e.zip |
debug-rebuild-ref-tree: Simple program to build a RefTree
This tool scans all references in the repository and writes out a new
reference pointing to a single commit whose root tree is a RefTree
containing the current refs of this repository.
It alway skips storing the reference it will write to, avoiding the
obvious cycle.
Change-Id: I20b1eeb81c55dc49dd600eac3bf8f90297394113
4 files changed, 134 insertions, 0 deletions
diff --git a/org.eclipse.jgit.pgm/META-INF/MANIFEST.MF b/org.eclipse.jgit.pgm/META-INF/MANIFEST.MF index 9cbd377feb..9dc6aea16f 100644 --- a/org.eclipse.jgit.pgm/META-INF/MANIFEST.MF +++ b/org.eclipse.jgit.pgm/META-INF/MANIFEST.MF @@ -21,6 +21,7 @@ Import-Package: org.apache.commons.compress.archivers;version="[1.3,2.0)", org.eclipse.jgit.gitrepo;version="[4.2.0,4.3.0)", org.eclipse.jgit.internal.storage.file;version="[4.2.0,4.3.0)", org.eclipse.jgit.internal.storage.pack;version="[4.2.0,4.3.0)", + org.eclipse.jgit.internal.storage.reftree;version="[4.2.0,4.3.0)", org.eclipse.jgit.lib;version="[4.2.0,4.3.0)", org.eclipse.jgit.merge;version="4.2.0", org.eclipse.jgit.nls;version="[4.2.0,4.3.0)", diff --git a/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin b/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin index c13f63e80f..6aa20041b5 100644 --- a/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin +++ b/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin @@ -41,6 +41,7 @@ org.eclipse.jgit.pgm.debug.DiffAlgorithms org.eclipse.jgit.pgm.debug.MakeCacheTree org.eclipse.jgit.pgm.debug.ReadDirCache org.eclipse.jgit.pgm.debug.RebuildCommitGraph +org.eclipse.jgit.pgm.debug.RebuildRefTree org.eclipse.jgit.pgm.debug.ShowCacheTree org.eclipse.jgit.pgm.debug.ShowCommands org.eclipse.jgit.pgm.debug.ShowDirCache diff --git a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties index a24b817949..b4b1261b37 100644 --- a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties +++ b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties @@ -228,6 +228,7 @@ usage_MergeBase=Find as good common ancestors as possible for a merge usage_MergesTwoDevelopmentHistories=Merges two development histories usage_ReadDirCache= Read the DirCache 100 times usage_RebuildCommitGraph=Recreate a repository from another one's commit graph +usage_RebuildRefTree=Copy references into a RefTree usage_Remote=Manage set of tracked repositories usage_RepositoryToReadFrom=Repository to read from usage_RepositoryToReceiveInto=Repository to receive into @@ -359,6 +360,7 @@ usage_tags=fetch all tags usage_notags=do not fetch tags usage_tagMessage=tag message usage_untrackedFilesMode=show untracked files +usage_updateRef=reference to update usage_updateRemoteRefsFromAnotherRepository=Update remote refs from another repository usage_useNameInsteadOfOriginToTrackUpstream=use <name> instead of 'origin' to track upstream usage_checkoutBranchAfterClone=checkout named branch instead of remotes's HEAD diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildRefTree.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildRefTree.java new file mode 100644 index 0000000000..6af4ce1dd1 --- /dev/null +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildRefTree.java @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2015, 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.pgm.debug; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.eclipse.jgit.internal.storage.reftree.RefTree; +import org.eclipse.jgit.lib.CommitBuilder; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ObjectInserter; +import org.eclipse.jgit.lib.ObjectReader; +import org.eclipse.jgit.lib.PersonIdent; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.RefDatabase; +import org.eclipse.jgit.lib.RefUpdate; +import org.eclipse.jgit.pgm.Command; +import org.eclipse.jgit.pgm.TextBuiltin; +import org.eclipse.jgit.revwalk.RevWalk; +import org.kohsuke.args4j.Argument; + +@Command(usage = "usage_RebuildRefTree") +class RebuildRefTree extends TextBuiltin { + @Argument(index = 0, required = true, metaVar = "metaVar_ref", usage = "usage_updateRef") + String refName; + + @Override + protected void run() throws Exception { + try (ObjectReader reader = db.newObjectReader(); + RevWalk rw = new RevWalk(reader); + ObjectInserter inserter = db.newObjectInserter()) { + RefDatabase refDb = db.getRefDatabase(); + + CommitBuilder b = new CommitBuilder(); + Ref ref = db.getRefDatabase().exactRef(refName); + RefUpdate update = db.updateRef(refName); + ObjectId oldTreeId; + + if (ref != null && ref.getObjectId() != null) { + ObjectId oldId = ref.getObjectId(); + update.setExpectedOldObjectId(oldId); + b.setParentId(oldId); + oldTreeId = rw.parseCommit(oldId).getTree(); + } else { + update.setExpectedOldObjectId(ObjectId.zeroId()); + oldTreeId = ObjectId.zeroId(); + } + + RefTree tree = rebuild(refDb.getRefs(RefDatabase.ALL)); + b.setTreeId(tree.writeTree(inserter)); + b.setAuthor(new PersonIdent(db)); + b.setCommitter(b.getAuthor()); + if (b.getTreeId().equals(oldTreeId)) { + return; + } + + update.setNewObjectId(inserter.insert(b)); + inserter.flush(); + + RefUpdate.Result result = update.update(rw); + switch (result) { + case NEW: + case FAST_FORWARD: + break; + default: + throw die(String.format("%s: %s", update.getName(), result)); //$NON-NLS-1$ + } + } + } + + private RefTree rebuild(Map<String, Ref> refMap) { + RefTree tree = RefTree.newEmptyTree(); + List<org.eclipse.jgit.internal.storage.reftree.Command> cmds + = new ArrayList<>(); + + for (Ref r : refMap.values()) { + if (refName.equals(r.getName())) { + continue; + } + + cmds.add(new org.eclipse.jgit.internal.storage.reftree.Command( + null, + db.peel(r))); + } + tree.apply(cmds); + return tree; + } +} |