diff src/com/tmate/hgkit/console/Log.java @ 49:26e3eeaa3962

branch and user filtering for log operation
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sat, 15 Jan 2011 01:15:38 +0100
parents e34f90b9ded1
children f1db8610da62
line wrap: on
line diff
--- a/src/com/tmate/hgkit/console/Log.java	Fri Jan 14 23:22:20 2011 +0100
+++ b/src/com/tmate/hgkit/console/Log.java	Sat Jan 15 01:15:38 2011 +0100
@@ -4,14 +4,18 @@
 package com.tmate.hgkit.console;
 
 import java.util.Formatter;
+import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import com.tmate.hgkit.fs.RepositoryLookup;
 import com.tmate.hgkit.ll.Changeset;
 import com.tmate.hgkit.ll.HgDataFile;
 import com.tmate.hgkit.ll.HgRepository;
 import com.tmate.hgkit.ll.Nodeid;
+import com.tmate.hgkit.ll.Revlog;
 
 /**
  * @author artem
@@ -28,13 +32,22 @@
 		}
 		System.out.println(hgRepo.getLocation());
 		final Dump dump = new Dump(hgRepo);
-		dump.complete = false; //cmdLineOpts;
+		dump.complete = true; //cmdLineOpts;
 		dump.reverseOrder = true;
+		dump.branches = cmdLineOpts.branches;
+		if (cmdLineOpts.users != null) {
+			dump.users = new LinkedHashSet<String>();
+			for (String u : cmdLineOpts.users) {
+				dump.users.add(u.toLowerCase());
+			}
+		}
 		if (cmdLineOpts.files.isEmpty()) {
-			// no revisions and no limit
 			if (cmdLineOpts.limit == -1) {
+				// no revisions and no limit
 				hgRepo.getChangelog().all(dump);
 			} else {
+				// in fact, external (to dump inspector) --limit processing yelds incorrect results when other args
+				// e.g. -u or -b are used (i.e. with -u shall give <limit> csets with user, not check last <limit> csets for user 
 				int[] r = new int[] { 0, hgRepo.getChangelog().getRevisionCount() };
 				if (fixRange(r, dump.reverseOrder, cmdLineOpts.limit) == 0) {
 					System.out.println("No changes");
@@ -61,11 +74,7 @@
 			}
 		}
 		//
-//		System.out.println("\n\n=========================");
-//		System.out.println("Range 1-3:");
-//		f1.history(1,3, callback);
-		//
-		//new ChangelogWalker().setFile("hello.c").setRevisionRange(1, 4).accept(new Visitor);
+		// XXX new ChangelogWalker().setFile("hello.c").setRevisionRange(1, 4).accept(new Visitor);
 	}
 	
 	private static int fixRange(int[] start_end, boolean reverse, int limit) {
@@ -83,19 +92,42 @@
 		return rv;
 	}
 
+	// Differences with standard hg log output
+	//   - complete == true (--debug) files are not broke down to modified,+ and -
 	private static final class Dump implements Changeset.Inspector {
 		// params
 		boolean complete = false;
 		boolean reverseOrder = false;
+		Set<String> branches;
+		Set<String> users; // shall be lowercased
 		// own
 		private LinkedList<String> l = new LinkedList<String>();
 		private final HgRepository repo;
+		private Revlog.ParentWalker changelogWalker;
+		private final int tip ; 
 
 		public Dump(HgRepository hgRepo) {
-			this.repo = hgRepo;
+			repo = hgRepo;
+			tip = hgRepo.getChangelog().getRevisionCount() - 1;
 		}
 
 		public void next(int revisionNumber, Nodeid nodeid, Changeset cset) {
+			if (branches != null && !branches.contains(cset.branch())) {
+				return;
+			}
+			if (users != null) {
+				String csetUser = cset.user().toLowerCase();
+				boolean found = false;
+				for (String u : users) {
+					if (csetUser.indexOf(u) != -1) {
+						found = true;
+						break;
+					}
+				}
+				if (!found) {
+					return;
+				}
+			}
 			final String s = print(revisionNumber, nodeid, cset);
 			if (reverseOrder) {
 				l.addFirst(s);
@@ -112,14 +144,27 @@
 				System.out.print(s);
 			}
 			l.clear();
+			changelogWalker = null;
 		}
 
 		private String print(int revNumber, Nodeid csetNodeid, Changeset cset) {
 			StringBuilder sb = new StringBuilder();
 			Formatter f = new Formatter(sb);
 			f.format("changeset:   %d:%s\n", revNumber, complete ? csetNodeid : csetNodeid.shortNotation());
+			if (revNumber == tip) {
+				sb.append("tag:        tip\n");
+			}
 			if (complete) {
-				f.format("parent:      %s\nparent:      %s\nmanifest:    %s\n", "-1", "-1", cset.manifest());
+				if (changelogWalker == null) {
+					changelogWalker = repo.getChangelog().new ParentWalker();
+					changelogWalker.init();
+				}
+				Nodeid p1 = changelogWalker.safeFirstParent(csetNodeid);
+				Nodeid p2 = changelogWalker.safeSecondParent(csetNodeid);
+				int p1x = p1 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevisionNumber(p1);
+				int p2x = p2 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevisionNumber(p2);
+				int mx = repo.getManifest().getLocalRevisionNumber(cset.manifest());
+				f.format("parent:      %d:%s\nparent:      %d:%s\nmanifest:    %d:%s\n", p1x, p1, p2x, p2, mx, cset.manifest());
 			}
 			f.format("user:        %s\ndate:        %s\n", cset.user(), cset.dateString());
 			if (complete) {
@@ -129,13 +174,19 @@
 					sb.append(' ');
 					sb.append(s);
 				}
+				if (cset.extras() != null) {
+					sb.append("\nextra:      ");
+					for (Map.Entry<String, String> e : cset.extras().entrySet()) {
+						sb.append(' ');
+						sb.append(e.getKey());
+						sb.append('=');
+						sb.append(e.getValue());
+					}
+				}
 				f.format("\ndescription:\n%s\n\n", cset.comment());
 			} else {
 				f.format("summary:     %s\n\n", cset.comment());
 			}
-			if (cset.extras() != null) {
-				f.format("extra:    " + cset.extras()); // TODO
-			}
 			return sb.toString();
 		}
 	}