changeset 182:f26ffe04ced0

Refactor HgBundle to dispatch changes found through callback
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 12 Apr 2011 19:36:18 +0200
parents cd3371670f0b
children 9807bf8f3a9c
files cmdline/org/tmatesoft/hg/console/Bundle.java cmdline/org/tmatesoft/hg/console/Incoming.java src/org/tmatesoft/hg/repo/HgBundle.java src/org/tmatesoft/hg/repo/HgChangelog.java
diffstat 4 files changed, 74 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/cmdline/org/tmatesoft/hg/console/Bundle.java	Tue Apr 12 19:10:38 2011 +0200
+++ b/cmdline/org/tmatesoft/hg/console/Bundle.java	Tue Apr 12 19:36:18 2011 +0200
@@ -18,9 +18,12 @@
 
 import java.io.File;
 
+import org.tmatesoft.hg.core.Nodeid;
 import org.tmatesoft.hg.repo.HgBundle;
+import org.tmatesoft.hg.repo.HgChangelog;
 import org.tmatesoft.hg.repo.HgLookup;
 import org.tmatesoft.hg.repo.HgRepository;
+import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
 
 
 /**
@@ -32,12 +35,12 @@
 public class Bundle {
 	public static void main(String[] args) throws Exception {
 		Options cmdLineOpts = Options.parse(args);
-		HgRepository hgRepo = cmdLineOpts.findRepository();
+		final HgRepository hgRepo = cmdLineOpts.findRepository();
 		if (hgRepo.isInvalid()) {
 			System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
 			return;
 		}
-		File bundleFile = new File("/temp/hg/hg-bundle-000000000000-gz.tmp");
+		File bundleFile = new File("/temp/hg/hg-bundle-a78c980749e3.tmp");
 		HgBundle hgBundle = new HgLookup().loadBundle(bundleFile);
 //		hgBundle.dump();
 		/* pass -R <path-to-repo-with-less-revisions-than-bundle>, e.g. for bundle with tip=168 and -R \temp\hg4j-50 with tip:159
@@ -46,7 +49,18 @@
 		-Changeset {User: ..., Comment: Correct project name...}
 		-Changeset {User: ..., Comment: Record possible...}
 		*/
-		hgBundle.changes(hgRepo);
+		hgBundle.changes(hgRepo, new HgChangelog.Inspector() {
+			private final HgChangelog changelog = hgRepo.getChangelog();
+			
+			public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) {
+				if (changelog.isKnown(nodeid)) {
+					System.out.print("+");
+				} else {
+					System.out.print("-");
+				}
+				System.out.printf("%d:%s\n%s\n", revisionNumber, nodeid.shortNotation(), cset.toString());
+			}
+		});
 	}
 
 /*
--- a/cmdline/org/tmatesoft/hg/console/Incoming.java	Tue Apr 12 19:10:38 2011 +0200
+++ b/cmdline/org/tmatesoft/hg/console/Incoming.java	Tue Apr 12 19:36:18 2011 +0200
@@ -16,8 +16,6 @@
  */
 package org.tmatesoft.hg.console;
 
-import java.io.File;
-import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -32,8 +30,6 @@
 
 import org.tmatesoft.hg.core.HgException;
 import org.tmatesoft.hg.core.Nodeid;
-import org.tmatesoft.hg.internal.ConfigFile;
-import org.tmatesoft.hg.internal.Internals;
 import org.tmatesoft.hg.internal.RepositoryComparator;
 import org.tmatesoft.hg.internal.RepositoryComparator.BranchChain;
 import org.tmatesoft.hg.repo.HgChangelog;
--- a/src/org/tmatesoft/hg/repo/HgBundle.java	Tue Apr 12 19:10:38 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgBundle.java	Tue Apr 12 19:36:18 2011 +0200
@@ -75,16 +75,21 @@
 		return da;
 	}
 
-	// shows changes recorded in the bundle that are missing from the supplied repository 
-	public void changes(final HgRepository hgRepo) throws HgException, IOException {
-		Inspector insp = new Inspector() {
+	/**
+	 * Get changes recorded in the bundle that are missing from the supplied repository.
+	 * @param hgRepo repository that shall possess base revision for this bundle
+	 * @param inspector callback to get each changeset found 
+	 */
+	public void changes(final HgRepository hgRepo, final HgChangelog.Inspector inspector) throws HgException, IOException {
+		Inspector bundleInsp = new Inspector() {
 			DigestHelper dh = new DigestHelper();
 			boolean emptyChangelog = true;
 			private DataAccess prevRevContent;
+			private int revisionIndex;
 
 			public void changelogStart() {
 				emptyChangelog = true;
-				
+				revisionIndex = 0;
 			}
 
 			public void changelogEnd() {
@@ -141,13 +146,8 @@
 						throw new IllegalStateException("Integrity check failed on " + bundleFile + ", node:" + ge.node());
 					}
 					ByteArrayDataAccess csetDataAccess = new ByteArrayDataAccess(csetContent);
-					if (changelog.isKnown(ge.node())) {
-						System.out.print("+");
-					} else {
-						System.out.print("-");
-					}
 					RawChangeset cs = RawChangeset.parse(csetDataAccess);
-					System.out.println(cs.toString());
+					inspector.next(revisionIndex++, ge.node(), cs);
 					prevRevContent.done();
 					prevRevContent = csetDataAccess.reset();
 				} catch (CancelledException ex) {
@@ -164,7 +164,7 @@
 			public void fileEnd(String name) {}
 
 		};
-		inspectChangelog(insp);
+		inspectChangelog(bundleInsp);
 	}
 
 	public void dump() throws IOException {
@@ -239,12 +239,7 @@
 		}
 		DataAccess da = getDataStream();
 		try {
-			if (da.isEmpty()) {
-				return;
-			}
-			inspector.changelogStart();
-			readGroup(da, inspector);
-			inspector.changelogEnd();
+			internalInspectChangelog(da, inspector);
 		} finally {
 			da.done();
 		}
@@ -260,11 +255,7 @@
 				return;
 			}
 			skipGroup(da); // changelog
-			if (!da.isEmpty()) {
-				inspector.manifestStart();
-				readGroup(da, inspector);
-				inspector.manifestEnd();
-			}
+			internalInspectManifest(da, inspector);
 		} finally {
 			da.done();
 		}
@@ -276,24 +267,15 @@
 		}
 		DataAccess da = getDataStream();
 		try {
-			if (!da.isEmpty()) {
-				skipGroup(da); // changelog
-			}
-			if (!da.isEmpty()) {
-				skipGroup(da); // manifest
+			if (da.isEmpty()) {
+				return;
 			}
-			while (!da.isEmpty()) {
-				int fnameLen = da.readInt();
-				if (fnameLen <= 4) {
-					break; // null chunk, the last one.
-				}
-				byte[] nameBuf = new byte[fnameLen - 4];
-				da.readBytes(nameBuf, 0, nameBuf.length);
-				String fname = new String(nameBuf);
-				inspector.fileStart(fname);
-				readGroup(da, inspector);
-				inspector.fileEnd(fname);
+			skipGroup(da); // changelog
+			if (da.isEmpty()) {
+				return;
 			}
+			skipGroup(da); // manifest
+			internalInspectFiles(da, inspector);
 		} finally {
 			da.done();
 		}
@@ -305,37 +287,47 @@
 		}
 		DataAccess da = getDataStream();
 		try {
-			if (da.isEmpty()) {
-				return;
-			}
-			inspector.changelogStart();
-			readGroup(da, inspector);
-			inspector.changelogEnd();
-			//
-			if (da.isEmpty()) {
-				return;
-			}
-			inspector.manifestStart();
-			readGroup(da, inspector);
-			inspector.manifestEnd();
-			//
-			while (!da.isEmpty()) {
-				int fnameLen = da.readInt();
-				if (fnameLen <= 4) {
-					break; // null chunk, the last one.
-				}
-				byte[] fnameBuf = new byte[fnameLen - 4];
-				da.readBytes(fnameBuf, 0, fnameBuf.length);
-				String name = new String(fnameBuf);
-				inspector.fileStart(name);
-				readGroup(da, inspector);
-				inspector.fileEnd(name);
-			}
+			internalInspectChangelog(da, inspector);
+			internalInspectManifest(da, inspector);
+			internalInspectFiles(da, inspector);
 		} finally {
 			da.done();
 		}
 	}
 
+	private void internalInspectChangelog(DataAccess da, Inspector inspector) throws IOException {
+		if (da.isEmpty()) {
+			return;
+		}
+		inspector.changelogStart();
+		readGroup(da, inspector);
+		inspector.changelogEnd();
+	}
+
+	private void internalInspectManifest(DataAccess da, Inspector inspector) throws IOException {
+		if (da.isEmpty()) {
+			return;
+		}
+		inspector.manifestStart();
+		readGroup(da, inspector);
+		inspector.manifestEnd();
+	}
+
+	private void internalInspectFiles(DataAccess da, Inspector inspector) throws IOException {
+		while (!da.isEmpty()) {
+			int fnameLen = da.readInt();
+			if (fnameLen <= 4) {
+				break; // null chunk, the last one.
+			}
+			byte[] fnameBuf = new byte[fnameLen - 4];
+			da.readBytes(fnameBuf, 0, fnameBuf.length);
+			String name = new String(fnameBuf);
+			inspector.fileStart(name);
+			readGroup(da, inspector);
+			inspector.fileEnd(name);
+		}
+	}
+
 	private static void readGroup(DataAccess da, Inspector inspector) throws IOException {
 		int len = da.readInt();
 		boolean good2go = true;
--- a/src/org/tmatesoft/hg/repo/HgChangelog.java	Tue Apr 12 19:10:38 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgChangelog.java	Tue Apr 12 19:36:18 2011 +0200
@@ -94,6 +94,7 @@
 
 	public interface Inspector {
 		// TODO describe whether cset is new instance each time
+		// describe what revisionNumber is when Inspector is used with HgBundle (BAD_REVISION or bundle's local order?) 
 		void next(int revisionNumber, Nodeid nodeid, RawChangeset cset);
 	}