All of lore.kernel.org
 help / color / mirror / Atom feed
* [JGIT PATCH] Create a debugging tool "jgit rebuild-commitgraph"
@ 2009-03-13 14:35 Shawn O. Pearce
  2009-03-13 14:49 ` Johannes Schindelin
  2009-03-15 11:34 ` Robin Rosenberg
  0 siblings, 2 replies; 8+ messages in thread
From: Shawn O. Pearce @ 2009-03-13 14:35 UTC (permalink / raw)
  To: Robin Rosenberg; +Cc: git

This is a command line tool which can be used to import only
the commit graph of another Git repository, to help debug a
topology related bug, without actually needing a clone of the
problematic repository.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---

 I used this yesterday to obtain a DAG from someone's private
 repository and recreate the same structure on my own system,
 to debug why RevWalk was busted in their case.

 Its only meant for debugging weird cases, I can't see many
 uses of it.  But now that its written, maybe we should add
 it to the project and keep it around.

 .../services/org.spearce.jgit.pgm.TextBuiltin      |    1 +
 .../org/spearce/jgit/pgm/RebuildCommitGraph.java   |  276 ++++++++++++++++++++
 2 files changed, 277 insertions(+), 0 deletions(-)
 create mode 100644 org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java

diff --git a/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin b/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin
index 1ba29e6..9bba156 100644
--- a/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin
+++ b/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin
@@ -11,6 +11,7 @@ org.spearce.jgit.pgm.LsRemote
 org.spearce.jgit.pgm.LsTree
 org.spearce.jgit.pgm.MergeBase
 org.spearce.jgit.pgm.Push
+org.spearce.jgit.pgm.RebuildCommitGraph
 org.spearce.jgit.pgm.ReceivePack
 org.spearce.jgit.pgm.RevList
 org.spearce.jgit.pgm.Rm
diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java
new file mode 100644
index 0000000..ccee997
--- /dev/null
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java
@@ -0,0 +1,276 @@
+/*
+ * Copyright (C) 2009, Google Inc.
+ *
+ * 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 Git Development Community 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.spearce.jgit.pgm;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+
+import org.kohsuke.args4j.Argument;
+import org.spearce.jgit.errors.MissingObjectException;
+import org.spearce.jgit.errors.ObjectWritingException;
+import org.spearce.jgit.lib.Commit;
+import org.spearce.jgit.lib.Constants;
+import org.spearce.jgit.lib.LockFile;
+import org.spearce.jgit.lib.ObjectId;
+import org.spearce.jgit.lib.ObjectWriter;
+import org.spearce.jgit.lib.PersonIdent;
+import org.spearce.jgit.lib.ProgressMonitor;
+import org.spearce.jgit.lib.Ref;
+import org.spearce.jgit.lib.RefUpdate;
+import org.spearce.jgit.lib.RefWriter;
+import org.spearce.jgit.lib.TextProgressMonitor;
+import org.spearce.jgit.lib.Tree;
+import org.spearce.jgit.revwalk.RevWalk;
+
+/**
+ * Recreates a repository from another one's commit graph.
+ * <p>
+ * <b>Do not run this on a repository unless you want to destroy it.</b>
+ * <p>
+ * To create the input files, in the source repository use:
+ * 
+ * <pre>
+ * git for-each-ref &gt;in.refs
+ * git log --all '--pretty=format:%H %ct %P' &gt;in.dag
+ * </pre>
+ * <p>
+ * Run the rebuild in either an empty repository, or a clone of the source. Any
+ * missing commits (which might be the entire graph) will be created. All refs
+ * will be modified to match the input exactly, which means some refs may be
+ * deleted from the current repository.
+ * <p>
+ */
+class RebuildCommitGraph extends TextBuiltin {
+	@Argument(index = 0, required = true, metaVar = "REFS", usage = "for-each-ref output")
+	File refList;
+
+	@Argument(index = 1, required = true, metaVar = "DAG", usage = "log --all '--pretty=format:%H %ct %P' output")
+	File graph;
+
+	private final ProgressMonitor pm = new TextProgressMonitor();
+
+	private Map<ObjectId, ObjectId> rewrites = new HashMap<ObjectId, ObjectId>();
+
+	@Override
+	protected void run() throws Exception {
+		recreateCommitGraph();
+		detachHead();
+		deleteAllRefs();
+		recreateRefs();
+	}
+
+	private void recreateCommitGraph() throws IOException {
+		final RevWalk rw = new RevWalk(db);
+		final Map<ObjectId, ToRewrite> toRewrite = new HashMap<ObjectId, ToRewrite>();
+		List<ToRewrite> queue = new ArrayList<ToRewrite>();
+		final BufferedReader br = new BufferedReader(new InputStreamReader(
+				new FileInputStream(graph), Constants.CHARSET));
+		try {
+			String line;
+			while ((line = br.readLine()) != null) {
+				final String[] parts = line.split("[ \t]{1,}");
+				final ObjectId oldId = ObjectId.fromString(parts[0]);
+				try {
+					rw.parseCommit(oldId);
+					// We have it already. Don't rewrite it.
+					continue;
+				} catch (MissingObjectException mue) {
+					// Fall through and rewrite it.
+				}
+
+				final long time = Long.parseLong(parts[1]) * 1000L;
+				final ObjectId[] parents = new ObjectId[parts.length - 2];
+				for (int i = 0; i < parents.length; i++) {
+					parents[i] = ObjectId.fromString(parts[2 + i]);
+				}
+
+				final ToRewrite t = new ToRewrite(oldId, time, parents);
+				toRewrite.put(oldId, t);
+				queue.add(t);
+			}
+		} finally {
+			br.close();
+		}
+
+		pm.beginTask("Rewriting commits", queue.size());
+		final ObjectWriter ow = new ObjectWriter(db);
+		final ObjectId emptyTree = ow.writeTree(new Tree(db));
+		final PersonIdent me = new PersonIdent("jgit rebuild-commitgraph",
+				"rebuild-commitgraph@localhost");
+		while (!queue.isEmpty()) {
+			final ListIterator<ToRewrite> itr = queue
+					.listIterator(queue.size());
+			queue = new ArrayList<ToRewrite>();
+			REWRITE: while (itr.hasPrevious()) {
+				final ToRewrite t = itr.previous();
+				final ObjectId[] newParents = new ObjectId[t.oldParents.length];
+				for (int k = 0; k < t.oldParents.length; k++) {
+					final ToRewrite p = toRewrite.get(t.oldParents[k]);
+					if (p != null) {
+						if (p.newId == null) {
+							// Must defer until after the parent is rewritten.
+							queue.add(t);
+							continue REWRITE;
+						} else {
+							newParents[k] = p.newId;
+						}
+					} else {
+						// We have the old parent object. Use it.
+						//
+						newParents[k] = t.oldParents[k];
+					}
+				}
+
+				final Commit newc = new Commit(db);
+				newc.setTreeId(emptyTree);
+				newc.setAuthor(new PersonIdent(me, new Date(t.commitTime)));
+				newc.setCommitter(newc.getAuthor());
+				newc.setParentIds(newParents);
+				newc.setMessage("ORIGINAL " + t.oldId.name() + "\n");
+				t.newId = ow.writeCommit(newc);
+				rewrites.put(t.oldId, t.newId);
+				pm.update(1);
+			}
+		}
+		pm.endTask();
+	}
+
+	private static class ToRewrite {
+		final ObjectId oldId;
+
+		final long commitTime;
+
+		final ObjectId[] oldParents;
+
+		ObjectId newId;
+
+		ToRewrite(final ObjectId o, final long t, final ObjectId[] p) {
+			oldId = o;
+			commitTime = t;
+			oldParents = p;
+		}
+	}
+
+	private void detachHead() throws IOException {
+		final String head = db.getFullBranch();
+		final ObjectId id = db.resolve(Constants.HEAD);
+		if (!ObjectId.isId(head) && id != null) {
+			final LockFile lf;
+			lf = new LockFile(new File(db.getDirectory(), Constants.HEAD));
+			if (!lf.lock())
+				throw new IOException("Cannot lock HEAD");
+			lf.write(id);
+			if (!lf.commit())
+				throw new IOException("Cannot deatch HEAD");
+		}
+	}
+
+	private void deleteAllRefs() throws Exception {
+		final RevWalk rw = new RevWalk(db);
+		for (final Ref r : db.getAllRefs().values()) {
+			if (Constants.HEAD.equals(r.getName()))
+				continue;
+			final RefUpdate u = db.updateRef(r.getName());
+			u.setForceUpdate(true);
+			u.delete(rw);
+		}
+	}
+
+	private void recreateRefs() throws Exception {
+		final Map<String, Ref> refs = computeNewRefs();
+		new RefWriter(refs.values()) {
+			@Override
+			protected void writeFile(final String name, final byte[] content)
+					throws IOException {
+				final File file = new File(db.getDirectory(), name);
+				final LockFile lck = new LockFile(file);
+				if (!lck.lock())
+					throw new ObjectWritingException("Can't write " + file);
+				try {
+					lck.write(content);
+				} catch (IOException ioe) {
+					throw new ObjectWritingException("Can't write " + file);
+				}
+				if (!lck.commit())
+					throw new ObjectWritingException("Can't write " + file);
+			}
+		}.writePackedRefs();
+	}
+
+	private Map<String, Ref> computeNewRefs() throws IOException {
+		final RevWalk rw = new RevWalk(db);
+		final Map<String, Ref> refs = new HashMap<String, Ref>();
+		final BufferedReader br = new BufferedReader(new InputStreamReader(
+				new FileInputStream(refList), Constants.CHARSET));
+		try {
+			String line;
+			while ((line = br.readLine()) != null) {
+				final String[] parts = line.split("[ \t]{1,}");
+				final ObjectId origId = ObjectId.fromString(parts[0]);
+				final String type = parts[1];
+				final String name = parts[2];
+
+				ObjectId id = rewrites.get(origId);
+				if (id == null)
+					id = origId;
+				try {
+					rw.parseAny(id);
+				} catch (MissingObjectException mue) {
+					if (!Constants.TYPE_COMMIT.equals(type)) {
+						System.err.println("skipping " + type + " " + name);
+						continue;
+					}
+					throw new MissingObjectException(id, type);
+				}
+				refs.put(name, new Ref(Ref.Storage.PACKED, name, id));
+			}
+		} finally {
+			br.close();
+		}
+		return refs;
+	}
+}
-- 
1.6.2.288.gc3f22

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [JGIT PATCH] Create a debugging tool "jgit rebuild-commitgraph"
  2009-03-13 14:35 [JGIT PATCH] Create a debugging tool "jgit rebuild-commitgraph" Shawn O. Pearce
@ 2009-03-13 14:49 ` Johannes Schindelin
  2009-03-13 14:52   ` Shawn O. Pearce
  2009-03-15 11:34 ` Robin Rosenberg
  1 sibling, 1 reply; 8+ messages in thread
From: Johannes Schindelin @ 2009-03-13 14:49 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: Robin Rosenberg, git

Hi,

On Fri, 13 Mar 2009, Shawn O. Pearce wrote:

> This is a command line tool which can be used to import only
> the commit graph of another Git repository, to help debug a
> topology related bug, without actually needing a clone of the
> problematic repository.
> 
> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
> ---
> 
>  I used this yesterday to obtain a DAG from someone's private
>  repository and recreate the same structure on my own system,
>  to debug why RevWalk was busted in their case.

Heh:

http://repo.or.cz/w/git/dscho.git?a=blob;f=my-fake-history.sh;h=e108ce99a5ebd75230c3cfc56b88d9769fec8bc1;hb=master

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [JGIT PATCH] Create a debugging tool "jgit rebuild-commitgraph"
  2009-03-13 14:49 ` Johannes Schindelin
@ 2009-03-13 14:52   ` Shawn O. Pearce
  0 siblings, 0 replies; 8+ messages in thread
From: Shawn O. Pearce @ 2009-03-13 14:52 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Robin Rosenberg, git

Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> On Fri, 13 Mar 2009, Shawn O. Pearce wrote:
> 
> > This is a command line tool which can be used to import only
> > the commit graph of another Git repository, to help debug a
> > topology related bug, without actually needing a clone of the
> > problematic repository.
> > 
> > Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
> > ---
> > 
> >  I used this yesterday to obtain a DAG from someone's private
> >  repository and recreate the same structure on my own system,
> >  to debug why RevWalk was busted in their case.
> 
> Heh:
> 
> http://repo.or.cz/w/git/dscho.git?a=blob;f=my-fake-history.sh;h=e108ce99a5ebd75230c3cfc56b88d9769fec8bc1;hb=master

Heh.  I knew both you and Nico have written something like this in
the past, but I couldn't find it.

Your script is almost what I needed.  But I also needed to force
GIT_COMMITTER_DATE to match the timestamp, as I suspected the commit
times had something to do with the problem I was seeing.

I also whack the entire ref space to match the source repository,
which mattered because I was trying to debug:

  git rev-list $insha1 --not --all

in JGit...

-- 
Shawn.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [JGIT PATCH] Create a debugging tool "jgit rebuild-commitgraph"
  2009-03-13 14:35 [JGIT PATCH] Create a debugging tool "jgit rebuild-commitgraph" Shawn O. Pearce
  2009-03-13 14:49 ` Johannes Schindelin
@ 2009-03-15 11:34 ` Robin Rosenberg
  2009-03-16 14:44   ` Shawn O. Pearce
  1 sibling, 1 reply; 8+ messages in thread
From: Robin Rosenberg @ 2009-03-15 11:34 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: git


I'd hate to put such a dangerous thing in the list of normal tools. If the user
want to shoot him/her-self in the foot they should get a license first.

-- robin

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [JGIT PATCH] Create a debugging tool "jgit rebuild-commitgraph"
  2009-03-15 11:34 ` Robin Rosenberg
@ 2009-03-16 14:44   ` Shawn O. Pearce
  2009-03-16 20:09     ` Robin Rosenberg
  0 siblings, 1 reply; 8+ messages in thread
From: Shawn O. Pearce @ 2009-03-16 14:44 UTC (permalink / raw)
  To: Robin Rosenberg; +Cc: git

Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
> 
> I'd hate to put such a dangerous thing in the list of normal tools. If the user
> want to shoot him/her-self in the foot they should get a license first.

How about squashing this in?

diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java
index ccee997..cc23438 100644
--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java
@@ -50,6 +50,7 @@
 import java.util.Map;
 
 import org.kohsuke.args4j.Argument;
+import org.kohsuke.args4j.Option;
 import org.spearce.jgit.errors.MissingObjectException;
 import org.spearce.jgit.errors.ObjectWritingException;
 import org.spearce.jgit.lib.Commit;
@@ -85,6 +86,11 @@
  * <p>
  */
 class RebuildCommitGraph extends TextBuiltin {
+	private final String REALLY = "--destroy-this-repository";
+
+	@Option(name = REALLY, usage = "approve destruction of repository")
+	boolean really;
+
 	@Argument(index = 0, required = true, metaVar = "REFS", usage = "for-each-ref output")
 	File refList;
 
@@ -97,6 +103,30 @@
 
 	@Override
 	protected void run() throws Exception {
+		if (!really && !db.getAllRefs().isEmpty()) {
+			final StringBuilder m = new StringBuilder();
+			m.append("fatal: ");
+			m.append("This program will destroy the repository:");
+			m.append("\n");
+			m.append("fatal:\n");
+			m.append("fatal:    ");
+			m.append(db.getDirectory().getAbsolutePath());
+			m.append("\n");
+			m.append("fatal:\n");
+			m.append("fatal: ");
+			m.append("To continue, add ");
+			m.append(REALLY);
+			m.append(" to the command line");
+			m.append("\n");
+			m.append("fatal:");
+			System.err.println(m);
+			throw die("Need approval to destroy current repository");
+		}
+		if (!refList.isFile())
+			throw die("no such file: " + refList.getPath());
+		if (!graph.isFile())
+			throw die("no such file: " + graph.getPath());
+
 		recreateCommitGraph();
 		detachHead();
 		deleteAllRefs();
-- 
Shawn.

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [JGIT PATCH] Create a debugging tool "jgit rebuild-commitgraph"
  2009-03-16 14:44   ` Shawn O. Pearce
@ 2009-03-16 20:09     ` Robin Rosenberg
  2009-03-16 20:13       ` Shawn O. Pearce
  0 siblings, 1 reply; 8+ messages in thread
From: Robin Rosenberg @ 2009-03-16 20:09 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: git

måndag 16 mars 2009 15:44:50 skrev "Shawn O. Pearce" <spearce@spearce.org>:
> Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
> > 
> > I'd hate to put such a dangerous thing in the list of normal tools. If the user
> > want to shoot him/her-self in the foot they should get a license first.
> 
> How about squashing this in?

Does it have to be a utility accessible through the pgm interface? Why not 
just run ut as java org.spearce.jgit.pgm.debug.RebuildCommitGraph ? 

> 
> diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java
> index ccee997..cc23438 100644
> --- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java
> +++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/RebuildCommitGraph.java
> @@ -50,6 +50,7 @@
>  import java.util.Map;
>  
>  import org.kohsuke.args4j.Argument;
> +import org.kohsuke.args4j.Option;
>  import org.spearce.jgit.errors.MissingObjectException;
>  import org.spearce.jgit.errors.ObjectWritingException;
>  import org.spearce.jgit.lib.Commit;
> @@ -85,6 +86,11 @@
>   * <p>
>   */
>  class RebuildCommitGraph extends TextBuiltin {
> +	private final String REALLY = "--destroy-this-repository";
> +
> +	@Option(name = REALLY, usage = "approve destruction of repository")
> +	boolean really;
> +
>  	@Argument(index = 0, required = true, metaVar = "REFS", usage = "for-each-ref output")
>  	File refList;
>  
> @@ -97,6 +103,30 @@
>  
>  	@Override
>  	protected void run() throws Exception {
> +		if (!really && !db.getAllRefs().isEmpty()) {
> +			final StringBuilder m = new StringBuilder();
> +			m.append("fatal: ");
> +			m.append("This program will destroy the repository:");
> +			m.append("\n");
> +			m.append("fatal:\n");
> +			m.append("fatal:    ");
> +			m.append(db.getDirectory().getAbsolutePath());
> +			m.append("\n");
> +			m.append("fatal:\n");
> +			m.append("fatal: ");
> +			m.append("To continue, add ");
> +			m.append(REALLY);
> +			m.append(" to the command line");
> +			m.append("\n");
> +			m.append("fatal:");
> +			System.err.println(m);
> +			throw die("Need approval to destroy current repository");

What's wrong with old fashioned '+' ? (which just translated to exactly this series
of StringBuilder calls anyway?

> +		}
> +		if (!refList.isFile())
> +			throw die("no such file: " + refList.getPath());
> +		if (!graph.isFile())
> +			throw die("no such file: " + graph.getPath());
> +
>  		recreateCommitGraph();
>  		detachHead();
>  		deleteAllRefs();

-- robin

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [JGIT PATCH] Create a debugging tool "jgit rebuild-commitgraph"
  2009-03-16 20:09     ` Robin Rosenberg
@ 2009-03-16 20:13       ` Shawn O. Pearce
  2009-03-16 23:58         ` [JGIT PATCH v3] Create a debugging tool "jgit debug-rebuild-commitgraph" Shawn O. Pearce
  0 siblings, 1 reply; 8+ messages in thread
From: Shawn O. Pearce @ 2009-03-16 20:13 UTC (permalink / raw)
  To: Robin Rosenberg; +Cc: git

Robin Rosenberg <robin.rosenberg.lists@dewire.com> wrote:
> måndag 16 mars 2009 15:44:50 skrev "Shawn O. Pearce" <spearce@spearce.org>:
> > Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
> > > 
> > > I'd hate to put such a dangerous thing in the list of normal tools. If the user
> > > want to shoot him/her-self in the foot they should get a license first.
> > 
> > How about squashing this in?
> 
> Does it have to be a utility accessible through the pgm interface? Why not 
> just run ut as java org.spearce.jgit.pgm.debug.RebuildCommitGraph ? 

Yea, that's fine too.  Then its "debug-rebuildcommitgraph" I think,
but that's still a reasonable name for it.
 
> >  	@Override
> >  	protected void run() throws Exception {
> > +		if (!really && !db.getAllRefs().isEmpty()) {
> > +			final StringBuilder m = new StringBuilder();
> > +			m.append("fatal: ");
...
> > +			System.err.println(m);
> > +			throw die("Need approval to destroy current repository");
> 
> What's wrong with old fashioned '+' ? (which just translated to exactly this series
> of StringBuilder calls anyway?

Nothing, other than formatting.  + line wraps somewhat ugly sometimes
under the Eclipse formatter.  Using StringBuilder with one append per
line and a short name for the StringBuilder variable makes it easier
to format.

-- 
Shawn.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [JGIT PATCH v3] Create a debugging tool "jgit debug-rebuild-commitgraph"
  2009-03-16 20:13       ` Shawn O. Pearce
@ 2009-03-16 23:58         ` Shawn O. Pearce
  0 siblings, 0 replies; 8+ messages in thread
From: Shawn O. Pearce @ 2009-03-16 23:58 UTC (permalink / raw)
  To: Robin Rosenberg; +Cc: git

This is a command line tool which can be used to import only
the commit graph of another Git repository, to help debug a
topology related bug, without actually needing a clone of the
problematic repository.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
 > Does it have to be a utility accessible through the pgm interface? Why not 
 > just run ut as java org.spearce.jgit.pgm.debug.RebuildCommitGraph ? 

 Moved.  Prior squash request was also squashed.

 .../services/org.spearce.jgit.pgm.TextBuiltin      |    1 +
 .../spearce/jgit/pgm/debug/RebuildCommitGraph.java |  307 ++++++++++++++++++++
 2 files changed, 308 insertions(+), 0 deletions(-)
 create mode 100644 org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/debug/RebuildCommitGraph.java

diff --git a/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin b/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin
index 1ba29e6..2fe6612 100644
--- a/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin
+++ b/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin
@@ -21,6 +21,7 @@ org.spearce.jgit.pgm.Version
 
 org.spearce.jgit.pgm.debug.MakeCacheTree
 org.spearce.jgit.pgm.debug.ReadDirCache
+org.spearce.jgit.pgm.debug.RebuildCommitGraph
 org.spearce.jgit.pgm.debug.ShowCacheTree
 org.spearce.jgit.pgm.debug.ShowCommands
 org.spearce.jgit.pgm.debug.ShowDirCache
diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/debug/RebuildCommitGraph.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/debug/RebuildCommitGraph.java
new file mode 100644
index 0000000..63e189c
--- /dev/null
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/debug/RebuildCommitGraph.java
@@ -0,0 +1,307 @@
+/*
+ * Copyright (C) 2009, Google Inc.
+ *
+ * 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 Git Development Community 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.spearce.jgit.pgm.debug;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+
+import org.kohsuke.args4j.Argument;
+import org.kohsuke.args4j.Option;
+import org.spearce.jgit.errors.MissingObjectException;
+import org.spearce.jgit.errors.ObjectWritingException;
+import org.spearce.jgit.lib.Commit;
+import org.spearce.jgit.lib.Constants;
+import org.spearce.jgit.lib.LockFile;
+import org.spearce.jgit.lib.ObjectId;
+import org.spearce.jgit.lib.ObjectWriter;
+import org.spearce.jgit.lib.PersonIdent;
+import org.spearce.jgit.lib.ProgressMonitor;
+import org.spearce.jgit.lib.Ref;
+import org.spearce.jgit.lib.RefUpdate;
+import org.spearce.jgit.lib.RefWriter;
+import org.spearce.jgit.lib.TextProgressMonitor;
+import org.spearce.jgit.lib.Tree;
+import org.spearce.jgit.pgm.TextBuiltin;
+import org.spearce.jgit.revwalk.RevWalk;
+
+/**
+ * Recreates a repository from another one's commit graph.
+ * <p>
+ * <b>Do not run this on a repository unless you want to destroy it.</b>
+ * <p>
+ * To create the input files, in the source repository use:
+ * 
+ * <pre>
+ * git for-each-ref &gt;in.refs
+ * git log --all '--pretty=format:%H %ct %P' &gt;in.dag
+ * </pre>
+ * <p>
+ * Run the rebuild in either an empty repository, or a clone of the source. Any
+ * missing commits (which might be the entire graph) will be created. All refs
+ * will be modified to match the input exactly, which means some refs may be
+ * deleted from the current repository.
+ * <p>
+ */
+class RebuildCommitGraph extends TextBuiltin {
+	private final String REALLY = "--destroy-this-repository";
+
+	@Option(name = REALLY, usage = "approve destruction of repository")
+	boolean really;
+
+	@Argument(index = 0, required = true, metaVar = "REFS", usage = "for-each-ref output")
+	File refList;
+
+	@Argument(index = 1, required = true, metaVar = "DAG", usage = "log --all '--pretty=format:%H %ct %P' output")
+	File graph;
+
+	private final ProgressMonitor pm = new TextProgressMonitor();
+
+	private Map<ObjectId, ObjectId> rewrites = new HashMap<ObjectId, ObjectId>();
+
+	@Override
+	protected void run() throws Exception {
+		if (!really && !db.getAllRefs().isEmpty()) {
+			final StringBuilder m = new StringBuilder();
+			m.append("fatal: ");
+			m.append("This program will destroy the repository:");
+			m.append("\n");
+			m.append("fatal:\n");
+			m.append("fatal:    ");
+			m.append(db.getDirectory().getAbsolutePath());
+			m.append("\n");
+			m.append("fatal:\n");
+			m.append("fatal: ");
+			m.append("To continue, add ");
+			m.append(REALLY);
+			m.append(" to the command line");
+			m.append("\n");
+			m.append("fatal:");
+			System.err.println(m);
+			throw die("Need approval to destroy current repository");
+		}
+		if (!refList.isFile())
+			throw die("no such file: " + refList.getPath());
+		if (!graph.isFile())
+			throw die("no such file: " + graph.getPath());
+
+		recreateCommitGraph();
+		detachHead();
+		deleteAllRefs();
+		recreateRefs();
+	}
+
+	private void recreateCommitGraph() throws IOException {
+		final RevWalk rw = new RevWalk(db);
+		final Map<ObjectId, ToRewrite> toRewrite = new HashMap<ObjectId, ToRewrite>();
+		List<ToRewrite> queue = new ArrayList<ToRewrite>();
+		final BufferedReader br = new BufferedReader(new InputStreamReader(
+				new FileInputStream(graph), Constants.CHARSET));
+		try {
+			String line;
+			while ((line = br.readLine()) != null) {
+				final String[] parts = line.split("[ \t]{1,}");
+				final ObjectId oldId = ObjectId.fromString(parts[0]);
+				try {
+					rw.parseCommit(oldId);
+					// We have it already. Don't rewrite it.
+					continue;
+				} catch (MissingObjectException mue) {
+					// Fall through and rewrite it.
+				}
+
+				final long time = Long.parseLong(parts[1]) * 1000L;
+				final ObjectId[] parents = new ObjectId[parts.length - 2];
+				for (int i = 0; i < parents.length; i++) {
+					parents[i] = ObjectId.fromString(parts[2 + i]);
+				}
+
+				final ToRewrite t = new ToRewrite(oldId, time, parents);
+				toRewrite.put(oldId, t);
+				queue.add(t);
+			}
+		} finally {
+			br.close();
+		}
+
+		pm.beginTask("Rewriting commits", queue.size());
+		final ObjectWriter ow = new ObjectWriter(db);
+		final ObjectId emptyTree = ow.writeTree(new Tree(db));
+		final PersonIdent me = new PersonIdent("jgit rebuild-commitgraph",
+				"rebuild-commitgraph@localhost");
+		while (!queue.isEmpty()) {
+			final ListIterator<ToRewrite> itr = queue
+					.listIterator(queue.size());
+			queue = new ArrayList<ToRewrite>();
+			REWRITE: while (itr.hasPrevious()) {
+				final ToRewrite t = itr.previous();
+				final ObjectId[] newParents = new ObjectId[t.oldParents.length];
+				for (int k = 0; k < t.oldParents.length; k++) {
+					final ToRewrite p = toRewrite.get(t.oldParents[k]);
+					if (p != null) {
+						if (p.newId == null) {
+							// Must defer until after the parent is rewritten.
+							queue.add(t);
+							continue REWRITE;
+						} else {
+							newParents[k] = p.newId;
+						}
+					} else {
+						// We have the old parent object. Use it.
+						//
+						newParents[k] = t.oldParents[k];
+					}
+				}
+
+				final Commit newc = new Commit(db);
+				newc.setTreeId(emptyTree);
+				newc.setAuthor(new PersonIdent(me, new Date(t.commitTime)));
+				newc.setCommitter(newc.getAuthor());
+				newc.setParentIds(newParents);
+				newc.setMessage("ORIGINAL " + t.oldId.name() + "\n");
+				t.newId = ow.writeCommit(newc);
+				rewrites.put(t.oldId, t.newId);
+				pm.update(1);
+			}
+		}
+		pm.endTask();
+	}
+
+	private static class ToRewrite {
+		final ObjectId oldId;
+
+		final long commitTime;
+
+		final ObjectId[] oldParents;
+
+		ObjectId newId;
+
+		ToRewrite(final ObjectId o, final long t, final ObjectId[] p) {
+			oldId = o;
+			commitTime = t;
+			oldParents = p;
+		}
+	}
+
+	private void detachHead() throws IOException {
+		final String head = db.getFullBranch();
+		final ObjectId id = db.resolve(Constants.HEAD);
+		if (!ObjectId.isId(head) && id != null) {
+			final LockFile lf;
+			lf = new LockFile(new File(db.getDirectory(), Constants.HEAD));
+			if (!lf.lock())
+				throw new IOException("Cannot lock HEAD");
+			lf.write(id);
+			if (!lf.commit())
+				throw new IOException("Cannot deatch HEAD");
+		}
+	}
+
+	private void deleteAllRefs() throws Exception {
+		final RevWalk rw = new RevWalk(db);
+		for (final Ref r : db.getAllRefs().values()) {
+			if (Constants.HEAD.equals(r.getName()))
+				continue;
+			final RefUpdate u = db.updateRef(r.getName());
+			u.setForceUpdate(true);
+			u.delete(rw);
+		}
+	}
+
+	private void recreateRefs() throws Exception {
+		final Map<String, Ref> refs = computeNewRefs();
+		new RefWriter(refs.values()) {
+			@Override
+			protected void writeFile(final String name, final byte[] content)
+					throws IOException {
+				final File file = new File(db.getDirectory(), name);
+				final LockFile lck = new LockFile(file);
+				if (!lck.lock())
+					throw new ObjectWritingException("Can't write " + file);
+				try {
+					lck.write(content);
+				} catch (IOException ioe) {
+					throw new ObjectWritingException("Can't write " + file);
+				}
+				if (!lck.commit())
+					throw new ObjectWritingException("Can't write " + file);
+			}
+		}.writePackedRefs();
+	}
+
+	private Map<String, Ref> computeNewRefs() throws IOException {
+		final RevWalk rw = new RevWalk(db);
+		final Map<String, Ref> refs = new HashMap<String, Ref>();
+		final BufferedReader br = new BufferedReader(new InputStreamReader(
+				new FileInputStream(refList), Constants.CHARSET));
+		try {
+			String line;
+			while ((line = br.readLine()) != null) {
+				final String[] parts = line.split("[ \t]{1,}");
+				final ObjectId origId = ObjectId.fromString(parts[0]);
+				final String type = parts[1];
+				final String name = parts[2];
+
+				ObjectId id = rewrites.get(origId);
+				if (id == null)
+					id = origId;
+				try {
+					rw.parseAny(id);
+				} catch (MissingObjectException mue) {
+					if (!Constants.TYPE_COMMIT.equals(type)) {
+						System.err.println("skipping " + type + " " + name);
+						continue;
+					}
+					throw new MissingObjectException(id, type);
+				}
+				refs.put(name, new Ref(Ref.Storage.PACKED, name, id));
+			}
+		} finally {
+			br.close();
+		}
+		return refs;
+	}
+}
-- 
1.6.2.1.286.g8173

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-03-17  0:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-13 14:35 [JGIT PATCH] Create a debugging tool "jgit rebuild-commitgraph" Shawn O. Pearce
2009-03-13 14:49 ` Johannes Schindelin
2009-03-13 14:52   ` Shawn O. Pearce
2009-03-15 11:34 ` Robin Rosenberg
2009-03-16 14:44   ` Shawn O. Pearce
2009-03-16 20:09     ` Robin Rosenberg
2009-03-16 20:13       ` Shawn O. Pearce
2009-03-16 23:58         ` [JGIT PATCH v3] Create a debugging tool "jgit debug-rebuild-commitgraph" Shawn O. Pearce

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.