diff cmdline/org/tmatesoft/hg/console/ChangesetDumpHandler.java @ 206:63c9fed4369e

Sample command line tools for incoming/outgoing to use commands
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 28 Apr 2011 02:47:44 +0200
parents
children ef8eba4aa215
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmdline/org/tmatesoft/hg/console/ChangesetDumpHandler.java	Thu Apr 28 02:47:44 2011 +0200
@@ -0,0 +1,165 @@
+/*
+ * Copyright (c) 2011 TMate Software Ltd
+ *  
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@hg4j.com
+ */
+package org.tmatesoft.hg.console;
+
+import java.util.Formatter;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.tmatesoft.hg.core.HgChangeset;
+import org.tmatesoft.hg.core.HgChangesetHandler;
+import org.tmatesoft.hg.core.Nodeid;
+import org.tmatesoft.hg.core.HgLogCommand.FileRevision;
+import org.tmatesoft.hg.repo.HgRepository;
+import org.tmatesoft.hg.util.Path;
+
+/**
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class ChangesetDumpHandler implements HgChangesetHandler {
+	// params
+	private boolean complete = false; // roughly --debug
+	private boolean reverseOrder = false;
+	private boolean verbose = true; // roughly -v
+	// own
+	private LinkedList<String> l = new LinkedList<String>();
+	private final HgRepository repo;
+	private final int tip;
+
+	public ChangesetDumpHandler(HgRepository hgRepo) {
+		repo = hgRepo;
+		tip = hgRepo.getChangelog().getLastRevision();
+	}
+
+	public ChangesetDumpHandler complete(boolean b) {
+		complete = b;
+		return this;
+	}
+
+	public ChangesetDumpHandler reversed(boolean b) {
+		reverseOrder = b;
+		return this;
+	}
+
+	public ChangesetDumpHandler verbose(boolean b) {
+		verbose = b;
+		return this;
+	}
+
+	public void next(HgChangeset changeset) {
+		final String s = print(changeset);
+		if (reverseOrder) {
+			// XXX in fact, need to insert s into l according to changeset.getRevision()
+			// because when file history is being followed, revisions of the original file (with smaller revNumber)
+			// are reported *after* revisions of present file and with addFirst appear above them
+			l.addFirst(s);
+		} else {
+			System.out.print(s);
+		}
+	}
+
+	public void done() {
+		if (!reverseOrder) {
+			return;
+		}
+		for (String s : l) {
+			System.out.print(s);
+		}
+		l.clear();
+	}
+
+	private String print(HgChangeset cset) {
+		StringBuilder sb = new StringBuilder();
+		Formatter f = new Formatter(sb);
+		final Nodeid csetNodeid = cset.getNodeid();
+		f.format("changeset:   %d:%s\n", cset.getRevision(), complete ? csetNodeid : csetNodeid.shortNotation());
+		if (cset.getRevision() == tip || repo.getTags().isTagged(csetNodeid)) {
+
+			sb.append("tag:         ");
+			for (String t : repo.getTags().tags(csetNodeid)) {
+				sb.append(t);
+				sb.append(' ');
+			}
+			if (cset.getRevision() == tip) {
+				sb.append("tip");
+			}
+			sb.append('\n');
+		}
+		if (complete) {
+			Nodeid p1 = cset.getFirstParentRevision();
+			Nodeid p2 = cset.getSecondParentRevision();
+			int p1x = p1 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevision(p1);
+			int p2x = p2 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevision(p2);
+			int mx = repo.getManifest().getLocalRevision(cset.getManifestRevision());
+			f.format("parent:      %d:%s\nparent:      %d:%s\nmanifest:    %d:%s\n", p1x, p1, p2x, p2, mx, cset.getManifestRevision());
+		}
+		f.format("user:        %s\ndate:        %s\n", cset.getUser(), cset.getDate());
+		if (!complete && verbose) {
+			final List<Path> files = cset.getAffectedFiles();
+			sb.append("files:      ");
+			for (Path s : files) {
+				sb.append(' ');
+				sb.append(s);
+			}
+			sb.append('\n');
+		}
+		if (complete) {
+			if (!cset.getModifiedFiles().isEmpty()) {
+				sb.append("files:      ");
+				for (FileRevision s : cset.getModifiedFiles()) {
+					sb.append(' ');
+					sb.append(s.getPath());
+				}
+				sb.append('\n');
+			}
+			if (!cset.getAddedFiles().isEmpty()) {
+				sb.append("files+:     ");
+				for (FileRevision s : cset.getAddedFiles()) {
+					sb.append(' ');
+					sb.append(s.getPath());
+				}
+				sb.append('\n');
+			}
+			if (!cset.getRemovedFiles().isEmpty()) {
+				sb.append("files-:     ");
+				for (Path s : cset.getRemovedFiles()) {
+					sb.append(' ');
+					sb.append(s);
+				}
+				sb.append('\n');
+			}
+			// if (cset.extras() != null) {
+			// sb.append("extra:      ");
+			// for (Map.Entry<String, String> e : cset.extras().entrySet()) {
+			// sb.append(' ');
+			// sb.append(e.getKey());
+			// sb.append('=');
+			// sb.append(e.getValue());
+			// }
+			// sb.append('\n');
+			// }
+		}
+		if (complete || verbose) {
+			f.format("description:\n%s\n\n", cset.getComment());
+		} else {
+			f.format("summary:     %s\n\n", cset.getComment());
+		}
+		return sb.toString();
+	}
+}