tikhomirov@143: /* tikhomirov@143: * Copyright (c) 2011 TMate Software Ltd tikhomirov@143: * tikhomirov@143: * This program is free software; you can redistribute it and/or modify tikhomirov@143: * it under the terms of the GNU General Public License as published by tikhomirov@143: * the Free Software Foundation; version 2 of the License. tikhomirov@143: * tikhomirov@143: * This program is distributed in the hope that it will be useful, tikhomirov@143: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@143: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@143: * GNU General Public License for more details. tikhomirov@143: * tikhomirov@143: * For information on how to redistribute this software under tikhomirov@143: * the terms of a license other than GNU General Public License tikhomirov@143: * contact TMate Software at support@hg4j.com tikhomirov@143: */ tikhomirov@143: package org.tmatesoft.hg.console; tikhomirov@143: tikhomirov@143: import static org.tmatesoft.hg.repo.HgRepository.TIP; tikhomirov@143: tikhomirov@143: import java.util.ArrayList; tikhomirov@143: import java.util.Collections; tikhomirov@143: import java.util.List; tikhomirov@143: import java.util.Map; tikhomirov@143: tikhomirov@143: import org.tmatesoft.hg.core.HgLogCommand.FileRevision; tikhomirov@143: import org.tmatesoft.hg.core.HgManifestCommand; tikhomirov@143: import org.tmatesoft.hg.core.Nodeid; tikhomirov@157: import org.tmatesoft.hg.internal.ByteArrayChannel; tikhomirov@143: import org.tmatesoft.hg.internal.DigestHelper; tikhomirov@229: import org.tmatesoft.hg.internal.PathGlobMatcher; tikhomirov@220: import org.tmatesoft.hg.repo.HgBranches; tikhomirov@143: import org.tmatesoft.hg.repo.HgDataFile; tikhomirov@143: import org.tmatesoft.hg.repo.HgInternals; tikhomirov@143: import org.tmatesoft.hg.repo.HgManifest; tikhomirov@143: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@143: import org.tmatesoft.hg.repo.HgStatusCollector; tikhomirov@143: import org.tmatesoft.hg.repo.HgStatusInspector; tikhomirov@143: import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector; tikhomirov@143: import org.tmatesoft.hg.util.Path; tikhomirov@143: tikhomirov@143: /** tikhomirov@143: * Various debug dumps. tikhomirov@143: * tikhomirov@143: * @author Artem Tikhomirov tikhomirov@143: * @author TMate Software Ltd. tikhomirov@143: */ tikhomirov@143: public class Main { tikhomirov@143: tikhomirov@143: private Options cmdLineOpts; tikhomirov@143: private HgRepository hgRepo; tikhomirov@143: tikhomirov@143: public Main(String[] args) throws Exception { tikhomirov@143: cmdLineOpts = Options.parse(args); tikhomirov@143: hgRepo = cmdLineOpts.findRepository(); tikhomirov@143: if (hgRepo.isInvalid()) { tikhomirov@143: System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); tikhomirov@143: return; tikhomirov@143: } tikhomirov@143: System.out.println("REPO:" + hgRepo.getLocation()); tikhomirov@143: } tikhomirov@143: tikhomirov@143: public static void main(String[] args) throws Exception { tikhomirov@143: Main m = new Main(args); tikhomirov@226: m.testFileStatus(); tikhomirov@226: // m.dumpBranches(); tikhomirov@220: // m.inflaterLengthException(); tikhomirov@157: // m.dumpIgnored(); tikhomirov@157: // m.dumpDirstate(); tikhomirov@157: // m.testStatusInternals(); tikhomirov@157: // m.catCompleteHistory(); tikhomirov@157: // m.dumpCompleteManifestLow(); tikhomirov@157: // m.dumpCompleteManifestHigh(); tikhomirov@157: // m.bunchOfTests(); tikhomirov@157: } tikhomirov@157: tikhomirov@226: private void testFileStatus() { tikhomirov@226: // final Path path = Path.create("src/org/tmatesoft/hg/util/"); tikhomirov@229: // final Path path = Path.create("src/org/tmatesoft/hg/internal/Experimental.java"); tikhomirov@229: // final Path path = Path.create("dir/file3"); tikhomirov@229: // HgWorkingCopyStatusCollector wcsc = HgWorkingCopyStatusCollector.create(hgRepo, path); tikhomirov@229: HgWorkingCopyStatusCollector wcsc = HgWorkingCopyStatusCollector.create(hgRepo, new PathGlobMatcher("*")); tikhomirov@226: wcsc.walk(TIP, new StatusDump()); tikhomirov@229: new HgManifestCommand(hgRepo).dirs(true).revision(TIP).execute(new HgManifestCommand.Handler() { tikhomirov@229: tikhomirov@229: public void file(FileRevision fileRevision) { tikhomirov@229: } tikhomirov@229: tikhomirov@229: public void end(Nodeid manifestRevision) { tikhomirov@229: } tikhomirov@229: tikhomirov@229: public void dir(Path p) { tikhomirov@229: System.out.println(p); tikhomirov@229: } tikhomirov@229: tikhomirov@229: public void begin(Nodeid manifestRevision) { tikhomirov@229: } tikhomirov@229: }); tikhomirov@226: } tikhomirov@226: tikhomirov@220: private void dumpBranches() { tikhomirov@220: HgBranches b = hgRepo.getBranches(); tikhomirov@220: for (HgBranches.BranchInfo bi : b.getAllBranches()) { tikhomirov@220: System.out.print(bi.getName()); tikhomirov@220: if (bi.isClosed()) { tikhomirov@220: System.out.print("!"); tikhomirov@220: } tikhomirov@220: System.out.print(" "); tikhomirov@220: System.out.print(bi.getStart()); tikhomirov@220: System.out.print(" "); tikhomirov@220: System.out.println(bi.getHeads()); tikhomirov@220: } tikhomirov@220: } tikhomirov@220: tikhomirov@157: private void inflaterLengthException() throws Exception { tikhomirov@157: HgDataFile f1 = hgRepo.getFileNode("src/com/tmate/hgkit/console/Bundle.java"); tikhomirov@157: HgDataFile f2 = hgRepo.getFileNode("test-repos.jar"); tikhomirov@157: System.out.println(f1.isCopy()); tikhomirov@157: System.out.println(f2.isCopy()); tikhomirov@157: ByteArrayChannel bac = new ByteArrayChannel(); tikhomirov@158: f1.content(1, bac); // 0: 1151, 1: 1139 tikhomirov@158: System.out.println(bac.toArray().length); tikhomirov@158: f2.content(0, bac = new ByteArrayChannel()); // 0: 14269 tikhomirov@157: System.out.println(bac.toArray().length); tikhomirov@143: } tikhomirov@143: tikhomirov@143: private void dumpIgnored() { tikhomirov@143: HgInternals debug = new HgInternals(hgRepo); tikhomirov@143: String[] toCheck = new String[] {"design.txt", "src/com/tmate/hgkit/ll/Changelog.java", "src/Extras.java", "bin/com/tmate/hgkit/ll/Changelog.class"}; tikhomirov@143: boolean[] checkResult = debug.checkIgnored(toCheck); tikhomirov@143: for (int i = 0; i < toCheck.length; i++) { tikhomirov@143: System.out.println("Ignored " + toCheck[i] + ": " + checkResult[i]); tikhomirov@143: } tikhomirov@143: } tikhomirov@143: tikhomirov@143: private void dumpDirstate() { tikhomirov@143: new HgInternals(hgRepo).dumpDirstate(); tikhomirov@143: } tikhomirov@143: tikhomirov@143: tikhomirov@157: private void catCompleteHistory() throws Exception { tikhomirov@143: DigestHelper dh = new DigestHelper(); tikhomirov@143: for (String fname : cmdLineOpts.getList("")) { tikhomirov@143: System.out.println(fname); tikhomirov@143: HgDataFile fn = hgRepo.getFileNode(fname); tikhomirov@143: if (fn.exists()) { tikhomirov@143: int total = fn.getRevisionCount(); tikhomirov@143: System.out.printf("Total revisions: %d\n", total); tikhomirov@143: for (int i = 0; i < total; i++) { tikhomirov@157: ByteArrayChannel sink = new ByteArrayChannel(); tikhomirov@157: fn.content(i, sink); tikhomirov@143: System.out.println("==========>"); tikhomirov@157: byte[] content = sink.toArray(); tikhomirov@143: System.out.println(new String(content)); tikhomirov@143: int[] parentRevisions = new int[2]; tikhomirov@143: byte[] parent1 = new byte[20]; tikhomirov@143: byte[] parent2 = new byte[20]; tikhomirov@143: fn.parents(i, parentRevisions, parent1, parent2); tikhomirov@143: System.out.println(dh.sha1(parent1, parent2, content).asHexString()); tikhomirov@143: } tikhomirov@143: } else { tikhomirov@143: System.out.println(">>>Not found!"); tikhomirov@143: } tikhomirov@143: } tikhomirov@143: } tikhomirov@143: tikhomirov@143: private void dumpCompleteManifestLow() { tikhomirov@143: hgRepo.getManifest().walk(0, TIP, new ManifestDump()); tikhomirov@143: } tikhomirov@143: tikhomirov@143: public static final class ManifestDump implements HgManifest.Inspector { tikhomirov@222: public boolean begin(int manifestRevision, Nodeid nid, int changelogRevision) { tikhomirov@222: System.out.printf("%d : %s\n", manifestRevision, nid); tikhomirov@143: return true; tikhomirov@143: } tikhomirov@143: tikhomirov@143: public boolean next(Nodeid nid, String fname, String flags) { tikhomirov@143: System.out.println(nid + "\t" + fname + "\t\t" + flags); tikhomirov@143: return true; tikhomirov@143: } tikhomirov@143: tikhomirov@143: public boolean end(int revision) { tikhomirov@143: System.out.println(); tikhomirov@143: return true; tikhomirov@143: } tikhomirov@143: } tikhomirov@143: tikhomirov@143: private void dumpCompleteManifestHigh() { tikhomirov@143: new HgManifestCommand(hgRepo).dirs(true).execute(new HgManifestCommand.Handler() { tikhomirov@143: tikhomirov@143: public void begin(Nodeid manifestRevision) { tikhomirov@143: System.out.println(">> " + manifestRevision); tikhomirov@143: } tikhomirov@143: public void dir(Path p) { tikhomirov@143: System.out.println(p); tikhomirov@143: } tikhomirov@143: public void file(FileRevision fileRevision) { tikhomirov@143: System.out.print(fileRevision.getRevision());; tikhomirov@143: System.out.print(" "); tikhomirov@143: System.out.println(fileRevision.getPath()); tikhomirov@143: } tikhomirov@143: tikhomirov@143: public void end(Nodeid manifestRevision) { tikhomirov@143: System.out.println(); tikhomirov@143: } tikhomirov@143: }); tikhomirov@143: } tikhomirov@143: tikhomirov@143: private void bunchOfTests() throws Exception { tikhomirov@143: HgInternals debug = new HgInternals(hgRepo); tikhomirov@143: debug.dumpDirstate(); tikhomirov@143: final StatusDump dump = new StatusDump(); tikhomirov@143: dump.showIgnored = false; tikhomirov@143: dump.showClean = false; tikhomirov@143: HgStatusCollector sc = new HgStatusCollector(hgRepo); tikhomirov@143: final int r1 = 0, r2 = 3; tikhomirov@143: System.out.printf("Status for changes between revision %d and %d:\n", r1, r2); tikhomirov@143: sc.walk(r1, r2, dump); tikhomirov@143: // tikhomirov@143: System.out.println("\n\nSame, but sorted in the way hg status does:"); tikhomirov@143: HgStatusCollector.Record r = sc.status(r1, r2); tikhomirov@143: sortAndPrint('M', r.getModified(), null); tikhomirov@143: sortAndPrint('A', r.getAdded(), null); tikhomirov@143: sortAndPrint('R', r.getRemoved(), null); tikhomirov@143: // tikhomirov@143: System.out.println("\n\nTry hg status --change :"); tikhomirov@143: sc.change(0, dump); tikhomirov@143: System.out.println("\nStatus against working dir:"); tikhomirov@143: HgWorkingCopyStatusCollector wcc = new HgWorkingCopyStatusCollector(hgRepo); tikhomirov@143: wcc.walk(TIP, dump); tikhomirov@143: System.out.println(); tikhomirov@143: System.out.printf("Manifest of the revision %d:\n", r2); tikhomirov@143: hgRepo.getManifest().walk(r2, r2, new ManifestDump()); tikhomirov@143: System.out.println(); tikhomirov@143: System.out.printf("\nStatus of working dir against %d:\n", r2); tikhomirov@143: r = wcc.status(r2); tikhomirov@143: sortAndPrint('M', r.getModified(), null); tikhomirov@143: sortAndPrint('A', r.getAdded(), r.getCopied()); tikhomirov@143: sortAndPrint('R', r.getRemoved(), null); tikhomirov@143: sortAndPrint('?', r.getUnknown(), null); tikhomirov@143: sortAndPrint('I', r.getIgnored(), null); tikhomirov@143: sortAndPrint('C', r.getClean(), null); tikhomirov@143: sortAndPrint('!', r.getMissing(), null); tikhomirov@143: } tikhomirov@143: tikhomirov@143: private void sortAndPrint(char prefix, List ul, Map copies) { tikhomirov@143: ArrayList sortList = new ArrayList(ul); tikhomirov@143: Collections.sort(sortList); tikhomirov@143: for (Path s : sortList) { tikhomirov@143: System.out.print(prefix); tikhomirov@143: System.out.print(' '); tikhomirov@143: System.out.println(s); tikhomirov@143: if (copies != null && copies.containsKey(s)) { tikhomirov@143: System.out.println(" " + copies.get(s)); tikhomirov@143: } tikhomirov@143: } tikhomirov@143: } tikhomirov@143: tikhomirov@143: tikhomirov@143: private void testStatusInternals() { tikhomirov@143: HgDataFile n = hgRepo.getFileNode(Path.create("design.txt")); tikhomirov@143: for (String s : new String[] {"011dfd44417c72bd9e54cf89b82828f661b700ed", "e5529faa06d53e06a816e56d218115b42782f1ba", "c18e7111f1fc89a80a00f6a39d51288289a382fc"}) { tikhomirov@143: // expected: 359, 2123, 3079 tikhomirov@143: byte[] b = s.getBytes(); tikhomirov@143: final Nodeid nid = Nodeid.fromAscii(b, 0, b.length); tikhomirov@143: System.out.println(s + " : " + n.length(nid)); tikhomirov@143: } tikhomirov@143: } tikhomirov@143: tikhomirov@197: static void force_gc() { tikhomirov@197: Runtime.getRuntime().runFinalization(); tikhomirov@197: Runtime.getRuntime().gc(); tikhomirov@197: Thread.yield(); tikhomirov@197: Runtime.getRuntime().runFinalization(); tikhomirov@197: Runtime.getRuntime().gc(); tikhomirov@197: Thread.yield(); tikhomirov@197: } tikhomirov@197: tikhomirov@143: private static class StatusDump implements HgStatusInspector { tikhomirov@143: public boolean hideStatusPrefix = false; // hg status -n option tikhomirov@143: public boolean showCopied = true; // -C tikhomirov@143: public boolean showIgnored = true; // -i tikhomirov@143: public boolean showClean = true; // -c tikhomirov@143: tikhomirov@143: public void modified(Path fname) { tikhomirov@143: print('M', fname); tikhomirov@143: } tikhomirov@143: tikhomirov@143: public void added(Path fname) { tikhomirov@143: print('A', fname); tikhomirov@143: } tikhomirov@143: tikhomirov@143: public void copied(Path fnameOrigin, Path fnameAdded) { tikhomirov@143: added(fnameAdded); tikhomirov@143: if (showCopied) { tikhomirov@143: print(' ', fnameOrigin); tikhomirov@143: } tikhomirov@143: } tikhomirov@143: tikhomirov@143: public void removed(Path fname) { tikhomirov@143: print('R', fname); tikhomirov@143: } tikhomirov@143: tikhomirov@143: public void clean(Path fname) { tikhomirov@143: if (showClean) { tikhomirov@143: print('C', fname); tikhomirov@143: } tikhomirov@143: } tikhomirov@143: tikhomirov@143: public void missing(Path fname) { tikhomirov@143: print('!', fname); tikhomirov@143: } tikhomirov@143: tikhomirov@143: public void unknown(Path fname) { tikhomirov@143: print('?', fname); tikhomirov@143: } tikhomirov@143: tikhomirov@143: public void ignored(Path fname) { tikhomirov@143: if (showIgnored) { tikhomirov@143: print('I', fname); tikhomirov@143: } tikhomirov@143: } tikhomirov@143: tikhomirov@143: private void print(char status, Path fname) { tikhomirov@143: if (!hideStatusPrefix) { tikhomirov@143: System.out.print(status); tikhomirov@143: System.out.print(' '); tikhomirov@143: } tikhomirov@143: System.out.println(fname); tikhomirov@143: } tikhomirov@143: } tikhomirov@143: }