tikhomirov@10: /* tikhomirov@20: * Copyright (c) 2010, 2011 Artem Tikhomirov tikhomirov@10: */ tikhomirov@10: package com.tmate.hgkit.console; tikhomirov@10: tikhomirov@18: import static com.tmate.hgkit.ll.HgRepository.TIP; tikhomirov@18: tikhomirov@55: import java.util.ArrayList; tikhomirov@55: import java.util.Collections; tikhomirov@55: import java.util.List; tikhomirov@55: tikhomirov@10: import com.tmate.hgkit.fs.RepositoryLookup; tikhomirov@22: import com.tmate.hgkit.ll.HgDataFile; tikhomirov@10: import com.tmate.hgkit.ll.HgRepository; tikhomirov@10: import com.tmate.hgkit.ll.LocalHgRepo; tikhomirov@22: import com.tmate.hgkit.ll.Nodeid; tikhomirov@55: import com.tmate.hgkit.ll.StatusCollector; tikhomirov@58: import com.tmate.hgkit.ll.WorkingCopyStatusCollector; tikhomirov@10: tikhomirov@10: /** tikhomirov@10: * tikhomirov@10: * @author artem tikhomirov@10: */ tikhomirov@10: public class Status { tikhomirov@10: tikhomirov@10: public static void main(String[] args) throws Exception { tikhomirov@10: RepositoryLookup repoLookup = new RepositoryLookup(); tikhomirov@10: RepositoryLookup.Options cmdLineOpts = RepositoryLookup.Options.parse(args); tikhomirov@10: HgRepository hgRepo = repoLookup.detect(cmdLineOpts); tikhomirov@10: if (hgRepo.isInvalid()) { tikhomirov@10: System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); tikhomirov@10: return; tikhomirov@10: } tikhomirov@10: System.out.println(hgRepo.getLocation()); tikhomirov@10: ((LocalHgRepo) hgRepo).loadDirstate().dump(); tikhomirov@18: final StatusDump dump = new StatusDump(); tikhomirov@18: dump.showIgnored = false; tikhomirov@18: dump.showClean = false; tikhomirov@55: StatusCollector sc = new StatusCollector(hgRepo); tikhomirov@55: final int r1 = 0, r2 = 3; tikhomirov@20: System.out.printf("Status for changes between revision %d and %d:\n", r1, r2); tikhomirov@55: sc.walk(r1, r2, dump); tikhomirov@55: // tikhomirov@55: System.out.println("\n\nSame, but sorted in the way hg status does:"); tikhomirov@55: StatusCollector.Record r = sc.status(r1, r2); tikhomirov@55: sortAndPrint('M', r.getModified()); tikhomirov@55: sortAndPrint('A', r.getAdded()); tikhomirov@55: sortAndPrint('R', r.getRemoved()); tikhomirov@56: // tikhomirov@56: System.out.println("\n\nTry hg status --change :"); tikhomirov@56: sc.change(0, dump); tikhomirov@57: System.out.println("\nStatus against working dir:"); tikhomirov@58: WorkingCopyStatusCollector wcc = new WorkingCopyStatusCollector(hgRepo, ((LocalHgRepo) hgRepo).createWorkingDirWalker()); tikhomirov@58: wcc.walk(TIP, dump); tikhomirov@57: System.out.println(); tikhomirov@57: System.out.printf("Manifest of the revision %d:\n", r2); tikhomirov@57: hgRepo.getManifest().walk(r2, r2, new Manifest.Dump()); tikhomirov@57: System.out.println(); tikhomirov@57: System.out.printf("\nStatus of working dir against %d:\n", r2); tikhomirov@58: r = wcc.status(r2); tikhomirov@58: sortAndPrint('M', r.getModified()); tikhomirov@58: sortAndPrint('A', r.getAdded()); tikhomirov@58: sortAndPrint('R', r.getRemoved()); tikhomirov@58: sortAndPrint('?', r.getUnknown()); tikhomirov@58: sortAndPrint('I', r.getIgnored()); tikhomirov@58: sortAndPrint('C', r.getClean()); tikhomirov@58: sortAndPrint('!', r.getMissing()); tikhomirov@55: } tikhomirov@55: tikhomirov@55: private static void sortAndPrint(char prefix, List ul) { tikhomirov@55: ArrayList sortList = new ArrayList(ul); tikhomirov@55: Collections.sort(sortList); tikhomirov@55: for (String s : sortList) { tikhomirov@55: System.out.print(prefix); tikhomirov@55: System.out.print(' '); tikhomirov@55: System.out.println(s); tikhomirov@55: } tikhomirov@22: } tikhomirov@22: tikhomirov@22: protected static void testStatusInternals(HgRepository hgRepo) { tikhomirov@22: HgDataFile n = hgRepo.getFileNode("design.txt"); tikhomirov@22: for (String s : new String[] {"011dfd44417c72bd9e54cf89b82828f661b700ed", "e5529faa06d53e06a816e56d218115b42782f1ba", "c18e7111f1fc89a80a00f6a39d51288289a382fc"}) { tikhomirov@22: // expected: 359, 2123, 3079 tikhomirov@22: byte[] b = s.getBytes(); tikhomirov@22: final Nodeid nid = Nodeid.fromAscii(b, 0, b.length); tikhomirov@22: System.out.println(s + " : " + n.length(nid)); tikhomirov@22: } tikhomirov@18: } tikhomirov@18: tikhomirov@55: private static class StatusDump implements StatusCollector.Inspector { tikhomirov@18: public boolean hideStatusPrefix = false; // hg status -n option tikhomirov@18: public boolean showCopied = true; // -C tikhomirov@18: public boolean showIgnored = true; // -i tikhomirov@18: public boolean showClean = true; // -c tikhomirov@18: tikhomirov@18: public void modified(String fname) { tikhomirov@18: print('M', fname); tikhomirov@18: } tikhomirov@18: tikhomirov@18: public void added(String fname) { tikhomirov@18: print('A', fname); tikhomirov@18: } tikhomirov@18: tikhomirov@18: public void copied(String fnameOrigin, String fnameAdded) { tikhomirov@18: added(fnameAdded); tikhomirov@18: if (showCopied) { tikhomirov@18: print(' ', fnameOrigin); tikhomirov@18: } tikhomirov@18: } tikhomirov@18: tikhomirov@18: public void removed(String fname) { tikhomirov@18: print('R', fname); tikhomirov@18: } tikhomirov@18: tikhomirov@18: public void clean(String fname) { tikhomirov@18: if (showClean) { tikhomirov@18: print('C', fname); tikhomirov@18: } tikhomirov@18: } tikhomirov@18: tikhomirov@18: public void missing(String fname) { tikhomirov@18: print('!', fname); tikhomirov@18: } tikhomirov@18: tikhomirov@18: public void unknown(String fname) { tikhomirov@18: print('?', fname); tikhomirov@18: } tikhomirov@18: tikhomirov@18: public void ignored(String fname) { tikhomirov@18: if (showIgnored) { tikhomirov@18: print('I', fname); tikhomirov@18: } tikhomirov@18: } tikhomirov@18: tikhomirov@18: private void print(char status, String fname) { tikhomirov@18: if (!hideStatusPrefix) { tikhomirov@18: System.out.print(status); tikhomirov@18: System.out.print(' '); tikhomirov@18: } tikhomirov@18: System.out.println(fname); tikhomirov@18: } tikhomirov@10: } tikhomirov@10: }