diff cmdline/org/tmatesoft/hg/console/Status.java @ 143:b9700740553a

Command line tools parse and respect most of command-line arguments
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 17 Feb 2011 22:16:25 +0100
parents 4a948ec83980
children d5268ca7715b
line wrap: on
line diff
--- a/cmdline/org/tmatesoft/hg/console/Status.java	Thu Feb 17 05:06:07 2011 +0100
+++ b/cmdline/org/tmatesoft/hg/console/Status.java	Thu Feb 17 22:16:25 2011 +0100
@@ -16,21 +16,20 @@
  */
 package org.tmatesoft.hg.console;
 
-import static org.tmatesoft.hg.repo.HgRepository.TIP;
+import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.TreeMap;
 
-import org.tmatesoft.hg.core.Nodeid;
-import org.tmatesoft.hg.repo.HgDataFile;
-import org.tmatesoft.hg.repo.HgRepository;
-import org.tmatesoft.hg.repo.HgStatusInspector;
-import org.tmatesoft.hg.repo.HgInternals;
-import org.tmatesoft.hg.repo.HgStatusCollector;
-import org.tmatesoft.hg.repo.HgStatusCollector.Record;
-import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector;
+import org.tmatesoft.hg.core.HgRepoFacade;
+import org.tmatesoft.hg.core.HgStatus;
+import org.tmatesoft.hg.core.HgStatus.Kind;
+import org.tmatesoft.hg.core.HgStatusCommand;
 import org.tmatesoft.hg.util.Path;
 
 /**
@@ -42,159 +41,88 @@
 
 	public static void main(String[] args) throws Exception {
 		Options cmdLineOpts = Options.parse(args);
-		HgRepository hgRepo = cmdLineOpts.findRepository();
-		if (hgRepo.isInvalid()) {
-			System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
+		HgRepoFacade hgRepo = new HgRepoFacade();
+		if (!hgRepo.init(cmdLineOpts.findRepository())) {
+			System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation());
 			return;
 		}
-		System.out.println(hgRepo.getLocation());
-		//
-//		bunchOfTests(hgRepo);
-		//
-//		new Internals(hgRepo).dumpDirstate();
 		//
-		statusWorkingCopy(hgRepo);
-		//statusRevVsWorkingCopy(hgRepo);
-	}
-
-	private static void statusWorkingCopy(HgRepository hgRepo) {
-		HgWorkingCopyStatusCollector wcc = new HgWorkingCopyStatusCollector(hgRepo);
-		HgStatusCollector.Record r = new HgStatusCollector.Record();
-		wcc.walk(TIP, r);
-		mardu(r);
-	}
-
-	private static void mardu(Record r) {
-		sortAndPrint('M', r.getModified());
-		sortAndPrint('A', r.getAdded(), r.getCopied());
-		sortAndPrint('R', r.getRemoved());
-		sortAndPrint('?', r.getUnknown());
-//		sortAndPrint('I', r.getIgnored());
-//		sortAndPrint('C', r.getClean());
-		sortAndPrint('!', r.getMissing());
-	}
-	
-	private static void statusRevVsWorkingCopy(HgRepository hgRepo) {
-		HgWorkingCopyStatusCollector wcc = new HgWorkingCopyStatusCollector(hgRepo);
-		HgStatusCollector.Record r = new HgStatusCollector.Record();
-		wcc.walk(3, r);
-		mardu(r);
-	}
+		HgStatusCommand cmd = hgRepo.createStatusCommand();
+		if (cmdLineOpts.getBoolean("-A", "-all")) {
+			cmd.all();
+		} else {
+			// default: mardu
+			cmd.modified(cmdLineOpts.getBoolean(true, "-m", "--modified"));
+			cmd.added(cmdLineOpts.getBoolean(true, "-a", "--added"));
+			cmd.removed(cmdLineOpts.getBoolean(true, "-r", "--removed"));
+			cmd.deleted(cmdLineOpts.getBoolean(true, "-d", "--deleted"));
+			cmd.unknown(cmdLineOpts.getBoolean(true, "-u", "--unknonwn"));
+			cmd.clean(cmdLineOpts.getBoolean("-c", "--clean"));
+			cmd.ignored(cmdLineOpts.getBoolean("-i", "--ignored"));
+		}
+//		cmd.subrepo(cmdLineOpts.getBoolean("-S", "--subrepos"))
+		final boolean noStatusPrefix = cmdLineOpts.getBoolean("-n", "--no-status");
+		final boolean showCopies = cmdLineOpts.getBoolean("-C", "--copies");
+		class StatusHandler implements HgStatusCommand.Handler {
+			
+			final Map<HgStatus.Kind, List<Path>> data = new TreeMap<HgStatus.Kind, List<Path>>();
+			final Map<Path, Path> copies = showCopies ? new HashMap<Path,Path>() : null;
+			
+			public void handleStatus(HgStatus s) {
+				List<Path> l = data.get(s.getKind());
+				if (l == null) {
+					l = new LinkedList<Path>();
+					data.put(s.getKind(), l);
+				}
+				l.add(s.getPath());
+				if (s.isCopy() && showCopies) {
+					copies.put(s.getPath(), s.getOriginalPath());
+				}
+			}
+			
+			public void dump() {
+				sortAndPrint('M', data.get(Kind.Modified), null);
+				sortAndPrint('A', data.get(Kind.Added), copies);
+				sortAndPrint('R', data.get(Kind.Removed), null);
+				sortAndPrint('?', data.get(Kind.Unknown), null);
+				sortAndPrint('I', data.get(Kind.Ignored), null);
+				sortAndPrint('C', data.get(Kind.Clean), null);
+				sortAndPrint('!', data.get(Kind.Missing), null);
+			}
 
-	private static void bunchOfTests(HgRepository hgRepo) throws Exception {
-		HgInternals debug = new HgInternals(hgRepo);
-		debug.dumpDirstate();
-		final StatusDump dump = new StatusDump();
-		dump.showIgnored = false;
-		dump.showClean = false;
-		HgStatusCollector sc = new HgStatusCollector(hgRepo);
-		final int r1 = 0, r2 = 3;
-		System.out.printf("Status for changes between revision %d and %d:\n", r1, r2);
-		sc.walk(r1, r2, dump);
-		// 
-		System.out.println("\n\nSame, but sorted in the way hg status does:");
-		HgStatusCollector.Record r = sc.status(r1, r2);
-		sortAndPrint('M', r.getModified());
-		sortAndPrint('A', r.getAdded());
-		sortAndPrint('R', r.getRemoved());
-		//
-		System.out.println("\n\nTry hg status --change <rev>:");
-		sc.change(0, dump);
-		System.out.println("\nStatus against working dir:");
-		HgWorkingCopyStatusCollector wcc = new HgWorkingCopyStatusCollector(hgRepo);
-		wcc.walk(TIP, dump);
-		System.out.println();
-		System.out.printf("Manifest of the revision %d:\n", r2);
-		hgRepo.getManifest().walk(r2, r2, new Manifest.Dump());
-		System.out.println();
-		System.out.printf("\nStatus of working dir against %d:\n", r2);
-		r = wcc.status(r2);
-		sortAndPrint('M', r.getModified());
-		sortAndPrint('A', r.getAdded(), r.getCopied());
-		sortAndPrint('R', r.getRemoved());
-		sortAndPrint('?', r.getUnknown());
-		sortAndPrint('I', r.getIgnored());
-		sortAndPrint('C', r.getClean());
-		sortAndPrint('!', r.getMissing());
-	}
-	
-	private static void sortAndPrint(char prefix, List<Path> ul) {
-		sortAndPrint(prefix, ul, null);
-	}
-	private static void sortAndPrint(char prefix, List<Path> ul, Map<Path, Path> copies) {
-		ArrayList<Path> sortList = new ArrayList<Path>(ul);
-		Collections.sort(sortList);
-		for (Path s : sortList)  {
-			System.out.print(prefix);
-			System.out.print(' ');
-			System.out.println(s);
-			if (copies != null && copies.containsKey(s)) {
-				System.out.println("  " + copies.get(s));
+			private void sortAndPrint(char prefix, List<Path> ul, Map<Path, Path> copies) {
+				if (ul == null) {
+					return;
+				}
+				ArrayList<Path> sortList = new ArrayList<Path>(ul);
+				Collections.sort(sortList);
+				for (Path s : sortList)  {
+					if (!noStatusPrefix) {
+						System.out.print(prefix);
+						System.out.print(' ');
+					}
+					System.out.println(s);
+					if (copies != null && copies.containsKey(s)) {
+						System.out.println("  " + copies.get(s));
+					}
+				}
+			}
+		};
+
+		StatusHandler statusHandler = new StatusHandler(); 
+		int changeRev = cmdLineOpts.getSingleInt(BAD_REVISION, "--change");
+		if (changeRev != BAD_REVISION) {
+			cmd.change(changeRev);
+		} else {
+			List<String> revisions = cmdLineOpts.getList("--rev");
+			int size = revisions.size();
+			if (size > 1) {
+				cmd.base(Integer.parseInt(revisions.get(size - 2))).revision(Integer.parseInt(revisions.get(size - 1)));
+			} else if (size > 0) {
+				cmd.base(Integer.parseInt(revisions.get(0)));
 			}
 		}
-	}
-	
-	protected static void testStatusInternals(HgRepository hgRepo) {
-		HgDataFile n = hgRepo.getFileNode(Path.create("design.txt"));
-		for (String s : new String[] {"011dfd44417c72bd9e54cf89b82828f661b700ed", "e5529faa06d53e06a816e56d218115b42782f1ba", "c18e7111f1fc89a80a00f6a39d51288289a382fc"}) {
-			// expected: 359, 2123, 3079
-			byte[] b = s.getBytes();
-			final Nodeid nid = Nodeid.fromAscii(b, 0, b.length);
-			System.out.println(s + " : " + n.length(nid));
-		}
-	}
-
-	private static class StatusDump implements HgStatusInspector {
-		public boolean hideStatusPrefix = false; // hg status -n option
-		public boolean showCopied = true; // -C
-		public boolean showIgnored = true; // -i
-		public boolean showClean = true; // -c
-
-		public void modified(Path fname) {
-			print('M', fname);
-		}
-
-		public void added(Path fname) {
-			print('A', fname);
-		}
-
-		public void copied(Path fnameOrigin, Path fnameAdded) {
-			added(fnameAdded);
-			if (showCopied) {
-				print(' ', fnameOrigin);
-			}
-		}
-
-		public void removed(Path fname) {
-			print('R', fname);
-		}
-
-		public void clean(Path fname) {
-			if (showClean) {
-				print('C', fname);
-			}
-		}
-
-		public void missing(Path fname) {
-			print('!', fname);
-		}
-
-		public void unknown(Path fname) {
-			print('?', fname);
-		}
-
-		public void ignored(Path fname) {
-			if (showIgnored) {
-				print('I', fname);
-			}
-		}
-		
-		private void print(char status, Path fname) {
-			if (!hideStatusPrefix) {
-				System.out.print(status);
-				System.out.print(' ');
-			}
-			System.out.println(fname);
-		}
+		cmd.execute(statusHandler);
+		statusHandler.dump();
 	}
 }