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@233: import org.tmatesoft.hg.core.HgLogCommand; tikhomirov@143: import org.tmatesoft.hg.core.HgLogCommand.FileRevision; tikhomirov@232: import org.tmatesoft.hg.core.HgCatCommand; tikhomirov@231: import org.tmatesoft.hg.core.HgFileRevision; 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@233: import org.tmatesoft.hg.repo.HgChangelog; 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@231: import org.tmatesoft.hg.repo.HgMergeState; 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@239: import org.tmatesoft.hg.repo.HgSubrepoLocation; tikhomirov@239: import org.tmatesoft.hg.repo.HgSubrepoLocation.Kind; tikhomirov@143: import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector; tikhomirov@233: import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; tikhomirov@235: import org.tmatesoft.hg.util.Pair; tikhomirov@143: import org.tmatesoft.hg.util.Path; tikhomirov@236: import org.tmatesoft.hg.util.ProgressSupport; 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@239: m.testSubrepos(); tikhomirov@239: // m.testReadWorkingCopy(); tikhomirov@236: // m.testParents(); tikhomirov@235: // m.testEffectiveFileLog(); tikhomirov@233: // m.testCatAtCsetRevision(); tikhomirov@232: // m.testMergeState(); tikhomirov@231: // m.testFileStatus(); tikhomirov@237: // 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@239: tikhomirov@239: private void testSubrepos() throws Exception { tikhomirov@239: for (HgSubrepoLocation l : hgRepo.getSubrepositories()) { tikhomirov@239: System.out.println(l.getLocation()); tikhomirov@239: System.out.println(l.getSource()); tikhomirov@239: System.out.println(l.getType()); tikhomirov@239: System.out.println(l.isCommitted() ? l.getRevision() : "not yet committed"); tikhomirov@239: if (l.getType() == Kind.Hg) { tikhomirov@239: HgRepository r = l.getRepo(); tikhomirov@239: System.out.printf("%s has %d revisions\n", l.getLocation(), r.getChangelog().getLastRevision() + 1); tikhomirov@239: if (r.getChangelog().getLastRevision() >= 0) { tikhomirov@239: final RawChangeset c = r.getChangelog().range(TIP, TIP).get(0); tikhomirov@239: System.out.printf("TIP: %s %s %s\n", c.user(), c.dateString(), c.comment()); tikhomirov@239: } tikhomirov@239: } tikhomirov@239: } tikhomirov@239: } tikhomirov@239: tikhomirov@237: private void testReadWorkingCopy() throws Exception { tikhomirov@237: for (String fname : cmdLineOpts.getList("")) { tikhomirov@237: HgDataFile fn = hgRepo.getFileNode(fname); tikhomirov@237: ByteArrayChannel sink = new ByteArrayChannel(); tikhomirov@237: fn.workingCopy(sink); tikhomirov@237: System.out.printf("%s: read %d bytes of working copy", fname, sink.toArray().length); tikhomirov@237: } tikhomirov@237: } tikhomirov@237: tikhomirov@235: private void testParents() throws Exception { tikhomirov@235: // hg parents cmd tikhomirov@235: final Pair wcParents = hgRepo.getWorkingCopyParents(); tikhomirov@235: ChangesetDumpHandler dump = new ChangesetDumpHandler(hgRepo); tikhomirov@235: final HgChangelog clog = hgRepo.getChangelog(); tikhomirov@235: HgLogCommand cmd = new HgLogCommand(hgRepo); tikhomirov@235: if (wcParents.hasFirst()) { tikhomirov@235: int x = clog.getLocalRevision(wcParents.first()); tikhomirov@235: cmd.range(x, x).execute(dump); // FIXME HgLogCommand shall support Nodeid as revisions tikhomirov@235: } tikhomirov@235: if (wcParents.hasSecond()) { tikhomirov@235: int x = clog.getLocalRevision(wcParents.second()); tikhomirov@235: cmd.range(x, x).execute(dump); tikhomirov@235: } tikhomirov@235: } tikhomirov@235: tikhomirov@235: // -R \temp\hg\hg4j-50 src/org/tmatesoft/hg/internal/RevlogStream.java tikhomirov@233: private void testEffectiveFileLog() { tikhomirov@233: for (String fname : cmdLineOpts.getList("")) { tikhomirov@233: System.out.println(fname); tikhomirov@233: HgDataFile fn = hgRepo.getFileNode(fname); tikhomirov@233: if (fn.exists()) { tikhomirov@233: fn.history(new HgChangelog.Inspector() { tikhomirov@233: public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) { tikhomirov@233: System.out.printf("%d:%s\n", revisionNumber, nodeid); tikhomirov@233: } tikhomirov@233: }); tikhomirov@233: } tikhomirov@233: } tikhomirov@233: } tikhomirov@233: tikhomirov@232: // TODO as test in TestCat tikhomirov@232: private void testCatAtCsetRevision() throws Exception { tikhomirov@232: HgCatCommand cmd = new HgCatCommand(hgRepo); tikhomirov@232: cmd.file(Path.create("src/org/tmatesoft/hg/internal/RevlogStream.java")); tikhomirov@232: cmd.changeset(Nodeid.fromAscii("08db726a0fb7914ac9d27ba26dc8bbf6385a0554")); tikhomirov@232: final ByteArrayChannel sink = new ByteArrayChannel(); tikhomirov@232: cmd.execute(sink); tikhomirov@232: System.out.println(sink.toArray().length); tikhomirov@232: } tikhomirov@232: tikhomirov@231: private void testMergeState() throws Exception { tikhomirov@231: final HgMergeState mergeState = hgRepo.getMergeState(); tikhomirov@231: mergeState.refresh(); tikhomirov@231: for (HgMergeState.Entry e : mergeState.getConflicts()) { tikhomirov@231: System.out.println(e.getState() + " " + e.getActualFile()); tikhomirov@231: System.out.println("p1: " + formatFileRevision(e.getFirstParent())); tikhomirov@231: System.out.println("p2: " + formatFileRevision(e.getSecondParent())); tikhomirov@231: System.out.println("ancestor: " + formatFileRevision(e.getCommonAncestor())); tikhomirov@231: System.out.println(); tikhomirov@231: } tikhomirov@231: } tikhomirov@231: tikhomirov@231: private static String formatFileRevision(HgFileRevision r) throws Exception { tikhomirov@231: final ByteArrayChannel sink = new ByteArrayChannel(); tikhomirov@231: r.putContentTo(sink); tikhomirov@231: return String.format("%s %s (%d bytes)", r.getPath(), r.getRevision(), sink.toArray().length); tikhomirov@231: } tikhomirov@231: 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@231: // final Path path = Path.create("missing-dir/"); tikhomirov@229: // HgWorkingCopyStatusCollector wcsc = HgWorkingCopyStatusCollector.create(hgRepo, path); tikhomirov@233: HgWorkingCopyStatusCollector wcsc = HgWorkingCopyStatusCollector.create(hgRepo, new PathGlobMatcher("mi**")); tikhomirov@226: wcsc.walk(TIP, new StatusDump()); tikhomirov@226: } tikhomirov@226: tikhomirov@236: /* tikhomirov@236: * Straightforward approach to collect branches, no use of branchheads.cache tikhomirov@236: * First, single run - 18 563 tikhomirov@236: * 10 runs (after 1 warm up) of HgBranches.collect took 167391 ms, ~17 seconds per run. tikhomirov@236: */ tikhomirov@220: private void dumpBranches() { tikhomirov@236: final long start0 = System.currentTimeMillis(); tikhomirov@220: HgBranches b = hgRepo.getBranches(); tikhomirov@236: System.out.println("1:" + (System.currentTimeMillis() - start0)); tikhomirov@220: for (HgBranches.BranchInfo bi : b.getAllBranches()) { tikhomirov@220: System.out.print(bi.getName()); tikhomirov@236: // if (bi.isClosed()) { tikhomirov@236: // System.out.print("!"); tikhomirov@236: // } tikhomirov@236: // System.out.print(" "); tikhomirov@236: // System.out.print(bi.getStart()); tikhomirov@220: System.out.print(" "); tikhomirov@220: System.out.println(bi.getHeads()); tikhomirov@220: } tikhomirov@236: // final long start = System.currentTimeMillis(); tikhomirov@236: // for (int i = 0; i < 10; i++) { tikhomirov@236: // b.collect(ProgressSupport.Factory.get(null)); tikhomirov@236: // } tikhomirov@236: // System.out.println("10:" + (System.currentTimeMillis() - start)); 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: }