Mercurial > jhg
comparison cmdline/org/tmatesoft/hg/console/Incoming.java @ 206:63c9fed4369e
Sample command line tools for incoming/outgoing to use commands
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Thu, 28 Apr 2011 02:47:44 +0200 |
| parents | e5407b5a586a |
| children | ef8eba4aa215 |
comparison
equal
deleted
inserted
replaced
| 205:ffc5f6d59f7e | 206:63c9fed4369e |
|---|---|
| 20 import java.util.Collections; | 20 import java.util.Collections; |
| 21 import java.util.Comparator; | 21 import java.util.Comparator; |
| 22 import java.util.HashSet; | 22 import java.util.HashSet; |
| 23 import java.util.Iterator; | 23 import java.util.Iterator; |
| 24 import java.util.LinkedHashMap; | 24 import java.util.LinkedHashMap; |
| 25 import java.util.LinkedHashSet; | |
| 26 import java.util.LinkedList; | 25 import java.util.LinkedList; |
| 27 import java.util.List; | 26 import java.util.List; |
| 28 import java.util.Map.Entry; | 27 import java.util.Map.Entry; |
| 29 | 28 |
| 30 import org.tmatesoft.hg.console.Outgoing.ChangesetFormatter; | 29 import org.tmatesoft.hg.core.HgIncomingCommand; |
| 31 import org.tmatesoft.hg.core.HgBadStateException; | 30 import org.tmatesoft.hg.core.HgRepoFacade; |
| 32 import org.tmatesoft.hg.core.Nodeid; | 31 import org.tmatesoft.hg.core.Nodeid; |
| 33 import org.tmatesoft.hg.internal.RepositoryComparator; | |
| 34 import org.tmatesoft.hg.internal.RepositoryComparator.BranchChain; | |
| 35 import org.tmatesoft.hg.repo.HgBundle; | |
| 36 import org.tmatesoft.hg.repo.HgChangelog; | |
| 37 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; | |
| 38 import org.tmatesoft.hg.repo.HgLookup; | 32 import org.tmatesoft.hg.repo.HgLookup; |
| 39 import org.tmatesoft.hg.repo.HgRemoteRepository; | 33 import org.tmatesoft.hg.repo.HgRemoteRepository; |
| 40 import org.tmatesoft.hg.repo.HgRepository; | |
| 41 | 34 |
| 42 | 35 |
| 43 /** | 36 /** |
| 44 * WORK IN PROGRESS, DO NOT USE | 37 * <em>hg incoming</em> counterpart |
| 45 * hg incoming counterpart | |
| 46 * | 38 * |
| 47 * @author Artem Tikhomirov | 39 * @author Artem Tikhomirov |
| 48 * @author TMate Software Ltd. | 40 * @author TMate Software Ltd. |
| 49 */ | 41 */ |
| 50 public class Incoming { | 42 public class Incoming { |
| 53 if (Boolean.FALSE.booleanValue()) { | 45 if (Boolean.FALSE.booleanValue()) { |
| 54 new SequenceConstructor().test(); | 46 new SequenceConstructor().test(); |
| 55 return; | 47 return; |
| 56 } | 48 } |
| 57 Options cmdLineOpts = Options.parse(args); | 49 Options cmdLineOpts = Options.parse(args); |
| 58 final HgRepository hgRepo = cmdLineOpts.findRepository(); | 50 HgRepoFacade hgRepo = new HgRepoFacade(); |
| 59 if (hgRepo.isInvalid()) { | 51 if (!hgRepo.init(cmdLineOpts.findRepository())) { |
| 60 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); | 52 System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation()); |
| 61 return; | 53 return; |
| 62 } | 54 } |
| 63 HgRemoteRepository hgRemote = new HgLookup().detectRemote(cmdLineOpts.getSingle(""), hgRepo); | 55 HgRemoteRepository hgRemote = new HgLookup().detectRemote(cmdLineOpts.getSingle(""), hgRepo.getRepository()); |
| 64 if (hgRemote.isInvalid()) { | 56 if (hgRemote.isInvalid()) { |
| 65 System.err.printf("Remote repository %s is not valid", hgRemote.getLocation()); | 57 System.err.printf("Remote repository %s is not valid", hgRemote.getLocation()); |
| 66 return; | 58 return; |
| 67 } | 59 } |
| 60 HgIncomingCommand cmd = hgRepo.createIncomingCommand(); | |
| 61 cmd.against(hgRemote); | |
| 68 // | 62 // |
| 69 // in fact, all we need from changelog is set of all nodeids. However, since ParentWalker reuses same Nodeids, it's not too expensive | 63 List<Nodeid> missing = cmd.executeLite(null); |
| 70 // to reuse it here, XXX although later this may need to be refactored | 64 Collections.reverse(missing); // useful to test output, from newer to older |
| 71 final HgChangelog.ParentWalker pw = hgRepo.getChangelog().new ParentWalker(); | 65 Outgoing.dump("Nodes to fetch:", missing); |
| 72 pw.init(); | |
| 73 // | |
| 74 RepositoryComparator repoCompare = new RepositoryComparator(pw, hgRemote); | |
| 75 repoCompare.compare(null); | |
| 76 List<BranchChain> missingBranches = repoCompare.calculateMissingBranches(); | |
| 77 final LinkedHashSet<Nodeid> common = new LinkedHashSet<Nodeid>(); | |
| 78 // XXX common can be obtained from repoCompare, but at the moment it would almost duplicate work of calculateMissingBranches | |
| 79 // once I refactor latter, common shall be taken from repoCompare. | |
| 80 for (BranchChain bc : missingBranches) { | |
| 81 bc.dump(); | |
| 82 common.add(bc.branchRoot); // common known node | |
| 83 List<Nodeid> missing = repoCompare.visitBranches(bc); | |
| 84 assert bc.branchRoot.equals(missing.get(0)); | |
| 85 missing.remove(0); | |
| 86 Collections.reverse(missing); // useful to test output, from newer to older | |
| 87 System.out.println("Nodes to fetch in this branch:"); | |
| 88 for (Nodeid n : missing) { | |
| 89 if (pw.knownNode(n)) { | |
| 90 System.out.println("Erroneous to fetch:" + n); | |
| 91 } else { | |
| 92 System.out.println(n); | |
| 93 } | |
| 94 } | |
| 95 System.out.println("Branch done"); | |
| 96 } | |
| 97 // | 66 // |
| 98 // Complete | 67 // Complete |
| 99 HgBundle changegroup = hgRemote.getChanges(new LinkedList<Nodeid>(common)); | 68 final ChangesetDumpHandler h = new ChangesetDumpHandler(hgRepo.getRepository()); |
| 100 changegroup.changes(hgRepo, new HgChangelog.Inspector() { | 69 h.complete(false); // this option looks up index of parent revision, done via repo.changelog (which doesn't have any of these new revisions) |
| 101 private int localIndex; | 70 // this can be fixed by tracking all nodeid->revision idx inside ChangesetDumpHandler, and refer to repo.changelog only when that mapping didn't work |
| 102 private final ChangesetFormatter formatter = new ChangesetFormatter(); | 71 h.verbose(cmdLineOpts.getBoolean("-v", "--verbose")); |
| 103 | 72 cmd.executeFull(h); |
| 104 public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) { | |
| 105 if (pw.knownNode(nodeid)) { | |
| 106 if (!common.contains(nodeid)) { | |
| 107 throw new HgBadStateException("Bundle shall not report known nodes other than roots we've supplied"); | |
| 108 } | |
| 109 localIndex = hgRepo.getChangelog().getLocalRevision(nodeid); | |
| 110 return; | |
| 111 } | |
| 112 System.out.println(formatter.simple(++localIndex, nodeid, cset)); | |
| 113 } | |
| 114 }); | |
| 115 } | 73 } |
| 116 | 74 |
| 117 | 75 |
| 76 /* | |
| 77 * This is for investigation purposes only | |
| 78 */ | |
| 118 private static class SequenceConstructor { | 79 private static class SequenceConstructor { |
| 119 | 80 |
| 120 private int[] between(int root, int head) { | 81 private int[] between(int root, int head) { |
| 121 if (head <= (root+1)) { | 82 if (head <= (root+1)) { |
| 122 return new int[0]; | 83 return new int[0]; |
