tikhomirov@143: /* tikhomirov@415: * Copyright (c) 2011-2012 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@429: import static org.tmatesoft.hg.repo.HgRepository.WORKING_COPY; tikhomirov@456: import static org.tmatesoft.hg.util.LogFacility.Severity.*; tikhomirov@143: tikhomirov@254: import java.io.File; tikhomirov@143: import java.util.ArrayList; tikhomirov@143: import java.util.Collections; tikhomirov@328: import java.util.HashSet; tikhomirov@143: import java.util.List; tikhomirov@143: import java.util.Map; tikhomirov@143: tikhomirov@328: import org.tmatesoft.hg.core.HgChangeset; tikhomirov@328: import org.tmatesoft.hg.core.HgChangesetTreeHandler; tikhomirov@354: import org.tmatesoft.hg.core.HgException; tikhomirov@231: import org.tmatesoft.hg.core.HgFileRevision; tikhomirov@295: import org.tmatesoft.hg.core.HgLogCommand; tikhomirov@143: import org.tmatesoft.hg.core.HgManifestCommand; tikhomirov@506: import org.tmatesoft.hg.core.HgManifestHandler; tikhomirov@143: import org.tmatesoft.hg.core.Nodeid; tikhomirov@425: import org.tmatesoft.hg.internal.BasicSessionContext; tikhomirov@157: import org.tmatesoft.hg.internal.ByteArrayChannel; tikhomirov@483: import org.tmatesoft.hg.internal.ConfigFile; tikhomirov@143: import org.tmatesoft.hg.internal.DigestHelper; tikhomirov@229: import org.tmatesoft.hg.internal.PathGlobMatcher; tikhomirov@254: import org.tmatesoft.hg.internal.RelativePathRewrite; tikhomirov@295: import org.tmatesoft.hg.internal.StreamLogFacility; tikhomirov@484: import org.tmatesoft.hg.repo.HgBookmarks; tikhomirov@220: import org.tmatesoft.hg.repo.HgBranches; tikhomirov@233: import org.tmatesoft.hg.repo.HgChangelog; tikhomirov@295: import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; tikhomirov@143: import org.tmatesoft.hg.repo.HgDataFile; tikhomirov@290: import org.tmatesoft.hg.repo.HgDirstate; tikhomirov@295: import org.tmatesoft.hg.repo.HgDirstate.EntryKind; tikhomirov@295: import org.tmatesoft.hg.repo.HgDirstate.Record; tikhomirov@339: import org.tmatesoft.hg.repo.HgIgnore; tikhomirov@143: import org.tmatesoft.hg.repo.HgInternals; tikhomirov@143: import org.tmatesoft.hg.repo.HgManifest; tikhomirov@295: import org.tmatesoft.hg.repo.HgManifest.Flags; tikhomirov@231: import org.tmatesoft.hg.repo.HgMergeState; tikhomirov@143: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@506: import org.tmatesoft.hg.repo.HgRevisionMap; tikhomirov@423: import org.tmatesoft.hg.repo.HgRuntimeException; 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@506: import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector; tikhomirov@501: import org.tmatesoft.hg.repo.ext.HgExtensionsManager; tikhomirov@501: import org.tmatesoft.hg.repo.ext.HgExtensionsManager.HgExt; tikhomirov@501: import org.tmatesoft.hg.repo.ext.Rebase; tikhomirov@254: import org.tmatesoft.hg.util.FileWalker; tikhomirov@295: import org.tmatesoft.hg.util.LogFacility; tikhomirov@235: import org.tmatesoft.hg.util.Pair; tikhomirov@143: import org.tmatesoft.hg.util.Path; tikhomirov@254: import org.tmatesoft.hg.util.PathRewrite; 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@295: @SuppressWarnings("unused") 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@299: cmdLineOpts = Options.parse(args, Collections.emptySet()); 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@507: // m.tryExtensions(); tikhomirov@501: // m.dumpBookmarks(); tikhomirov@484: // m.readConfigFile(); tikhomirov@483: // m.dumpCommitLastMessage(); tikhomirov@507: m.buildFileLog(); tikhomirov@305: // m.testConsoleLog(); tikhomirov@270: // m.testTreeTraversal(); tikhomirov@244: // m.testRevisionMap(); tikhomirov@472: // m.testSubrepos(); tikhomirov@239: // m.testReadWorkingCopy(); tikhomirov@254: // m.testParents(); tikhomirov@243: // m.testEffectiveFileLog(); tikhomirov@345: // m.testMergeState(); tikhomirov@231: // m.testFileStatus(); tikhomirov@341: // m.dumpBranches(); tikhomirov@220: // m.inflaterLengthException(); tikhomirov@157: // m.dumpIgnored(); tikhomirov@295: // m.dumpDirstate(); tikhomirov@157: // m.testStatusInternals(); tikhomirov@157: // m.catCompleteHistory(); tikhomirov@157: // m.dumpCompleteManifestLow(); tikhomirov@445: // m.dumpCompleteManifestHigh(); tikhomirov@157: // m.bunchOfTests(); tikhomirov@157: } tikhomirov@484: tikhomirov@501: private void tryExtensions() throws Exception { tikhomirov@501: HgExtensionsManager em = hgRepo.getExtensions(); tikhomirov@501: if (!em.isEnabled(HgExt.Rebase)) { tikhomirov@501: System.out.println("Rebase is not enabled"); tikhomirov@501: return; tikhomirov@501: } tikhomirov@501: Rebase re = em.getRebaseExtension(); tikhomirov@501: if (!re.refresh().isRebaseInProgress()) { tikhomirov@501: System.out.println("No rebase is in progress"); tikhomirov@501: return; tikhomirov@501: } tikhomirov@501: System.out.printf("%s %s %s\n", re.getWorkingDirParent().shortNotation(), re.getTarget().shortNotation(), re.getExternalParent().shortNotation()); tikhomirov@501: System.out.printf("collapse:%b, keep:%b, keepbranches:%b\n", re.isCollapse(), re.isKeepOriginalRevisions(), re.isKeepBranchNames()); tikhomirov@501: } tikhomirov@501: tikhomirov@484: // TODO as test tikhomirov@484: private void dumpBookmarks() throws Exception { tikhomirov@484: HgBookmarks bm = hgRepo.getBookmarks(); tikhomirov@484: String active = bm.getActiveBookmarkName(); tikhomirov@506: ArrayList all = new ArrayList(bm.getAllBookmarks()); tikhomirov@484: Collections.sort(all); tikhomirov@484: for (String bmname : all) { tikhomirov@484: if (bmname.equals(active)) { tikhomirov@484: System.out.print(" * "); tikhomirov@484: } else { tikhomirov@484: System.out.print(" "); tikhomirov@484: } tikhomirov@484: System.out.printf("%-26s (%s)\n", bmname, bm.getRevision(bmname).shortNotation()); tikhomirov@484: } tikhomirov@484: } tikhomirov@483: tikhomirov@483: // TODO as test tikhomirov@483: private void readConfigFile() throws Exception { tikhomirov@490: ConfigFile configFile = new ConfigFile(hgRepo.getSessionContext()); tikhomirov@483: configFile.addLocation(new File(System.getProperty("user.home"), "test-cfg/aaa/config1")); tikhomirov@483: for (String s : configFile.getSectionNames()) { tikhomirov@483: System.out.printf("[%s]\n", s); tikhomirov@483: for (Map.Entry e : configFile.getSection(s).entrySet()) { tikhomirov@483: System.out.printf("%s = %s\n", e.getKey(), e.getValue()); tikhomirov@483: } tikhomirov@483: } tikhomirov@483: tikhomirov@483: } tikhomirov@483: tikhomirov@481: private void dumpCommitLastMessage() throws Exception { tikhomirov@481: System.out.println(hgRepo.getCommitLastMessage()); tikhomirov@481: } tikhomirov@464: tikhomirov@328: private void buildFileLog() throws Exception { tikhomirov@450: final long start = System.nanoTime(); tikhomirov@328: HgLogCommand cmd = new HgLogCommand(hgRepo); tikhomirov@511: cmd.file("file1b.txt", true); tikhomirov@511: final int[] count = new int[] { 0 }; tikhomirov@328: cmd.execute(new HgChangesetTreeHandler() { tikhomirov@427: public void treeElement(HgChangesetTreeHandler.TreeElement entry) { tikhomirov@423: StringBuilder sb = new StringBuilder(); tikhomirov@423: HashSet test = new HashSet(entry.childRevisions()); tikhomirov@423: for (HgChangeset cc : entry.children()) { tikhomirov@423: sb.append(cc.getRevisionIndex()); tikhomirov@423: sb.append(':'); tikhomirov@423: sb.append(cc.getNodeid().shortNotation()); tikhomirov@423: sb.append(", "); tikhomirov@423: } tikhomirov@423: final Pair parents = entry.parentRevisions(); tikhomirov@423: final boolean isJoin = !parents.first().isNull() && !parents.second().isNull(); tikhomirov@423: final boolean isFork = entry.children().size() > 1; tikhomirov@423: final HgChangeset cset = entry.changeset(); tikhomirov@471: System.out.printf("%d:%s - %s (%s)\n", cset.getRevisionIndex(), cset.getNodeid().shortNotation(), cset.getComment(), cset.getPhase()); tikhomirov@423: if (!isJoin && !isFork && !entry.children().isEmpty()) { tikhomirov@423: System.out.printf("\t=> %s\n", sb); tikhomirov@423: } tikhomirov@423: if (isJoin) { tikhomirov@423: HgChangeset p1 = entry.parents().first(); tikhomirov@423: HgChangeset p2 = entry.parents().second(); tikhomirov@423: System.out.printf("\tjoin <= (%d:%s, %d:%s)", p1.getRevisionIndex(), p1.getNodeid().shortNotation(), p2.getRevisionIndex(), p2.getNodeid().shortNotation()); tikhomirov@423: if (isFork) { tikhomirov@423: System.out.print(", "); tikhomirov@366: } tikhomirov@423: } tikhomirov@423: if (isFork) { tikhomirov@423: if (!isJoin) { tikhomirov@423: System.out.print('\t'); tikhomirov@366: } tikhomirov@423: System.out.printf("fork => [%s]", sb); tikhomirov@423: } tikhomirov@423: if (isJoin || isFork) { tikhomirov@423: System.out.println(); tikhomirov@317: } tikhomirov@511: count[0]++; tikhomirov@305: } tikhomirov@328: }); tikhomirov@511: System.out.println(count[0]); tikhomirov@450: final long end = System.nanoTime(); tikhomirov@450: System.out.printf("buildFileLog: %,d ms\n", (end-start)/1000); tikhomirov@305: } tikhomirov@305: tikhomirov@366: private void buildFileLogOld() throws Exception { tikhomirov@305: final HgDataFile fn = hgRepo.getFileNode("file1"); tikhomirov@305: final int[] fileChangesetRevisions = new int[fn.getRevisionCount()]; tikhomirov@305: fn.history(new HgChangelog.Inspector() { tikhomirov@305: private int fileLocalRevisions = 0; tikhomirov@305: private int[] parentRevisions = new int[2]; tikhomirov@305: tikhomirov@305: public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) { tikhomirov@366: try { tikhomirov@366: fileChangesetRevisions[fileLocalRevisions] = revisionNumber; tikhomirov@366: fn.parents(fileLocalRevisions, parentRevisions, null, null); tikhomirov@366: boolean join = parentRevisions[0] != -1 && parentRevisions[1] != -1; tikhomirov@366: if (join) { tikhomirov@366: System.out.print("join["); tikhomirov@366: } tikhomirov@366: if (parentRevisions[0] != -1) { tikhomirov@366: System.out.printf("%2d->%2d, ", fileChangesetRevisions[parentRevisions[0]], revisionNumber); tikhomirov@366: } tikhomirov@366: if (parentRevisions[1] != -1) { tikhomirov@366: System.out.printf("%2d->%2d, ", fileChangesetRevisions[parentRevisions[1]], revisionNumber); tikhomirov@366: } tikhomirov@366: if (join) { tikhomirov@366: System.out.print("]"); tikhomirov@366: } tikhomirov@366: fileLocalRevisions++; tikhomirov@423: } catch (HgRuntimeException ex) { tikhomirov@366: ex.printStackTrace(); tikhomirov@305: } tikhomirov@305: } tikhomirov@305: }); tikhomirov@305: System.out.println(); tikhomirov@305: } tikhomirov@243: tikhomirov@295: private void testConsoleLog() { tikhomirov@456: LogFacility fc = new StreamLogFacility(Debug, true, System.out); tikhomirov@456: System.out.printf("isDebug: %s, isInfo:%s\n", fc.isDebug(), fc.getLevel() == Info); tikhomirov@456: fc.dump(getClass(), Debug, "%d", 1); tikhomirov@456: fc.dump(getClass(), Info, "%d\n", 2); tikhomirov@456: fc.dump(getClass(), Warn, "%d\n", 3); tikhomirov@456: fc.dump(getClass(), Error, "%d", 4); tikhomirov@295: Exception ex = new Exception(); tikhomirov@456: fc.dump(getClass(), Debug, ex, "message"); tikhomirov@456: fc.dump(getClass(), Info, ex, null); tikhomirov@456: fc.dump(getClass(), Warn, ex, null); tikhomirov@456: fc.dump(getClass(), Error, ex, "message"); tikhomirov@295: } tikhomirov@295: tikhomirov@254: private void testTreeTraversal() throws Exception { tikhomirov@254: File repoRoot = hgRepo.getWorkingDir(); tikhomirov@254: Path.Source pathSrc = new Path.SimpleSource(new PathRewrite.Composite(new RelativePathRewrite(repoRoot), hgRepo.getToRepoPathHelper())); tikhomirov@431: FileWalker w = new FileWalker(new BasicSessionContext(null), repoRoot, pathSrc); tikhomirov@254: int count = 0; tikhomirov@254: final long start = System.currentTimeMillis(); tikhomirov@254: while (w.hasNext()) { tikhomirov@254: count++; tikhomirov@254: w.next(); tikhomirov@254: } tikhomirov@254: System.out.printf("Traversal of %d files took %d ms", count, System.currentTimeMillis() - start); tikhomirov@254: } tikhomirov@254: tikhomirov@243: /* tikhomirov@243: * cpython repo with 70715 revisions. tikhomirov@243: 3 revisions - 80 ms vs 250 ms (250ms init) tikhomirov@243: 4 revisions - 110 ms vs 265 ms (265 ms init) tikhomirov@243: 5 revisions - 94 vs 266. tikhomirov@367: complete iteration in changelog.getRevisionIndex(tipNodeid) takes 47 ms tikhomirov@243: compared to complete iteration inside RevisionMap.init() of 171 ms. tikhomirov@243: The only difference is latter instantiates Nodeids, while former compares binary content as is. tikhomirov@243: Hence, with 20-30 ms per regular getLocalRevision, it pays off to use RevisionMap with at least 15-20 tikhomirov@243: queries tikhomirov@243: */ tikhomirov@243: private void testRevisionMap() throws Exception { tikhomirov@243: HgChangelog changelog = hgRepo.getChangelog(); tikhomirov@433: HgRevisionMap rmap = new HgRevisionMap(changelog).init(); // warm-up, ensure complete file read tikhomirov@243: int tip = changelog.getLastRevision(); tikhomirov@243: // take 5 arbitrary revisions at 0, 1/4, 2/4, 3/4 and 4/4 tikhomirov@243: final Nodeid[] revs = new Nodeid[5]; tikhomirov@243: revs[4] = changelog.getRevision(0); tikhomirov@243: revs[3] = changelog.getRevision(tip / 4); tikhomirov@243: revs[2] = changelog.getRevision(tip / 2); tikhomirov@243: revs[1] = changelog.getRevision(tip / 4 + tip / 2); tikhomirov@243: revs[0] = changelog.getRevision(tip); tikhomirov@243: long start = System.currentTimeMillis(); tikhomirov@243: for (int i = 0; i < revs.length; i++) { tikhomirov@367: final int localRev = changelog.getRevisionIndex(revs[i]); tikhomirov@243: System.out.printf("%d:%s\n", localRev, revs[i]); tikhomirov@243: } tikhomirov@243: System.out.println(System.currentTimeMillis() - start); tikhomirov@243: System.out.println(); tikhomirov@243: // tikhomirov@243: start = System.currentTimeMillis(); tikhomirov@433: rmap = new HgRevisionMap(changelog).init(); tikhomirov@243: long s2 = System.currentTimeMillis(); tikhomirov@243: for (int i = 0; i < revs.length; i++) { tikhomirov@367: final int localRev = rmap.revisionIndex(revs[i]); tikhomirov@243: System.out.printf("%d:%s\n", localRev, revs[i]); tikhomirov@243: } tikhomirov@243: System.out.println(System.currentTimeMillis() - start); tikhomirov@243: System.out.printf("\t from that, init took %d ms\n", s2 - start); tikhomirov@243: tikhomirov@243: } tikhomirov@239: tikhomirov@442: // any repository with subrepositories tikhomirov@239: private void testSubrepos() throws Exception { tikhomirov@442: // @see TestSubrepo#testAccessAPI 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@442: System.out.printf("%s (%s) has %d revisions\n", l.getLocation(), r.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@442: 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: HgLogCommand cmd = new HgLogCommand(hgRepo); tikhomirov@235: if (wcParents.hasFirst()) { tikhomirov@253: cmd.changeset(wcParents.first()).execute(dump); tikhomirov@235: } tikhomirov@235: if (wcParents.hasSecond()) { tikhomirov@253: cmd.changeset(wcParents.second()).execute(dump); tikhomirov@235: } tikhomirov@252: System.out.println("Branch:" + hgRepo.getWorkingCopyBranchName()); tikhomirov@235: } tikhomirov@235: tikhomirov@242: /* tikhomirov@242: * -R \temp\hg\hg4j-50 src/org/tmatesoft/hg/internal/RevlogStream.java tikhomirov@242: * tikhomirov@242: * -R \temp\hg\cpython Lib/doctest.py, range 15907..68588, total 251 revision tikhomirov@242: * no improvement (collect linkRev, hgchangelog.range([])) 10890 ms tikhomirov@242: * improved history logic in HgDataFile (minimize reads of close revisions): tikhomirov@242: * with no sort (defect for tool-created repos) took 10500 ms tikhomirov@242: * with sort (to order revisions from linkRev before use) 610 ms tikhomirov@242: * HgChangelog.range() - 92 calls tikhomirov@242: * RevlogStream with separate iterate(int[] sortedRevisions,...) tikhomirov@242: * RevlogStream.ReaderN1.range(): 185 380 ms tikhomirov@242: */ tikhomirov@366: private void testEffectiveFileLog() throws Exception { tikhomirov@233: for (String fname : cmdLineOpts.getList("")) { tikhomirov@233: System.out.println(fname); tikhomirov@242: final long start = System.currentTimeMillis(); 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@242: System.out.printf("Done: %d\n", System.currentTimeMillis() - start); tikhomirov@233: } tikhomirov@233: } tikhomirov@233: tikhomirov@231: private void testMergeState() throws Exception { tikhomirov@231: final HgMergeState mergeState = hgRepo.getMergeState(); tikhomirov@231: mergeState.refresh(); tikhomirov@270: System.out.printf("isMerging: %s, isStale: %s.\n", mergeState.isMerging(), mergeState.isStale()); tikhomirov@270: System.out.printf("P1:%s\nP2:%s\nState parent:%s\n",mergeState.getFirstParent().shortNotation(), mergeState.getSecondParent().shortNotation(), mergeState.getStateParent().shortNotation()); 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@423: private void testFileStatus() throws Exception { 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@429: wcsc.walk(WORKING_COPY, 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@366: private void dumpBranches() throws Exception { 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: // System.out.print(" "); tikhomirov@236: // System.out.print(bi.getStart()); tikhomirov@220: System.out.print(" "); tikhomirov@308: System.out.print(bi.getHeads()); tikhomirov@308: if (bi.isClosed()) { tikhomirov@308: System.out.print(" x "); tikhomirov@308: } tikhomirov@308: System.out.println(); tikhomirov@220: } tikhomirov@308: // b.writeCache(); 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: String[] toCheck = new String[] {"design.txt", "src/com/tmate/hgkit/ll/Changelog.java", "src/Extras.java", "bin/com/tmate/hgkit/ll/Changelog.class"}; tikhomirov@339: HgIgnore ignore = hgRepo.getIgnore(); tikhomirov@143: for (int i = 0; i < toCheck.length; i++) { tikhomirov@339: System.out.println("Ignored " + toCheck[i] + ": " + ignore.isIgnored(Path.create(toCheck[i]))); tikhomirov@143: } tikhomirov@143: } tikhomirov@348: tikhomirov@348: static class DirstateDump implements HgDirstate.Inspector { tikhomirov@348: private final char[] x = new char[] {'n', 'a', 'r', 'm' }; tikhomirov@348: tikhomirov@348: public boolean next(EntryKind kind, Record entry) { tikhomirov@348: System.out.printf("%c %3o%6d %30tc\t\t%s", x[kind.ordinal()], entry.mode(), entry.size(), (long) entry.modificationTime() * 1000, entry.name()); tikhomirov@348: if (entry.copySource() != null) { tikhomirov@348: System.out.printf(" --> %s", entry.copySource()); tikhomirov@348: } tikhomirov@348: System.out.println(); tikhomirov@348: return true; tikhomirov@348: } tikhomirov@348: } tikhomirov@143: tikhomirov@348: private void dumpDirstate() throws Exception { tikhomirov@348: new HgInternals(hgRepo).getDirstate().walk(new DirstateDump()); tikhomirov@290: HgWorkingCopyStatusCollector wcc = HgWorkingCopyStatusCollector.create(hgRepo, new Path.Matcher.Any()); tikhomirov@290: wcc.getDirstate().walk(new HgDirstate.Inspector() { tikhomirov@290: tikhomirov@290: public boolean next(EntryKind kind, Record entry) { tikhomirov@290: System.out.printf("%s %s\n", kind, entry.name()); tikhomirov@290: return true; tikhomirov@290: } tikhomirov@290: }); 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@366: private void dumpCompleteManifestLow() throws Exception { tikhomirov@143: hgRepo.getManifest().walk(0, TIP, new ManifestDump()); tikhomirov@143: } tikhomirov@143: tikhomirov@424: 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@285: public boolean next(Nodeid nid, Path fname, Flags 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@366: private void dumpCompleteManifestHigh() throws Exception { tikhomirov@427: new HgManifestCommand(hgRepo).dirs(true).execute(new HgManifestHandler() { 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@249: public void file(HgFileRevision fileRevision) { tikhomirov@423: System.out.print(fileRevision.getRevision());; tikhomirov@423: System.out.print(" "); tikhomirov@423: System.out.printf("%s %s", fileRevision.getParents().first().shortNotation(), fileRevision.getParents().second().shortNotation()); tikhomirov@423: System.out.print(" "); tikhomirov@423: 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: 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@429: wcc.walk(WORKING_COPY, 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@354: private void testStatusInternals() throws HgException { 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@416: System.out.println(s + " : " + n.getLength(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@360: public void invalid(Path fname, Exception ex) { tikhomirov@360: System.out.printf("FAILURE: %s\n", fname); tikhomirov@360: ex.printStackTrace(System.out); tikhomirov@360: } tikhomirov@360: 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: }