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