Mercurial > hg4j
comparison hg4j/src/main/java/org/tmatesoft/hg/core/HgIncomingCommand.java @ 213:6ec4af642ba8 gradle
Project uses Gradle for build - actual changes
author | Alexander Kitaev <kitaev@gmail.com> |
---|---|
date | Tue, 10 May 2011 10:52:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
212:edb2e2829352 | 213:6ec4af642ba8 |
---|---|
1 /* | |
2 * Copyright (c) 2011 TMate Software Ltd | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.core; | |
18 | |
19 import java.io.IOException; | |
20 import java.util.ArrayList; | |
21 import java.util.HashSet; | |
22 import java.util.Iterator; | |
23 import java.util.LinkedHashSet; | |
24 import java.util.LinkedList; | |
25 import java.util.List; | |
26 import java.util.Set; | |
27 import java.util.TreeSet; | |
28 | |
29 import org.tmatesoft.hg.internal.RepositoryComparator; | |
30 import org.tmatesoft.hg.internal.RepositoryComparator.BranchChain; | |
31 import org.tmatesoft.hg.repo.HgBundle; | |
32 import org.tmatesoft.hg.repo.HgChangelog; | |
33 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; | |
34 import org.tmatesoft.hg.repo.HgRemoteRepository; | |
35 import org.tmatesoft.hg.repo.HgRepository; | |
36 import org.tmatesoft.hg.util.CancelledException; | |
37 | |
38 /** | |
39 * Command to find out changes available in a remote repository, missing locally. | |
40 * | |
41 * @author Artem Tikhomirov | |
42 * @author TMate Software Ltd. | |
43 */ | |
44 public class HgIncomingCommand { | |
45 | |
46 private final HgRepository localRepo; | |
47 private HgRemoteRepository remoteRepo; | |
48 @SuppressWarnings("unused") | |
49 private boolean includeSubrepo; | |
50 private RepositoryComparator comparator; | |
51 private List<BranchChain> missingBranches; | |
52 private HgChangelog.ParentWalker parentHelper; | |
53 private Set<String> branches; | |
54 | |
55 public HgIncomingCommand(HgRepository hgRepo) { | |
56 localRepo = hgRepo; | |
57 } | |
58 | |
59 public HgIncomingCommand against(HgRemoteRepository hgRemote) { | |
60 remoteRepo = hgRemote; | |
61 comparator = null; | |
62 missingBranches = null; | |
63 return this; | |
64 } | |
65 | |
66 /** | |
67 * Select specific branch to push. | |
68 * Multiple branch specification possible (changeset from any of these would be included in result). | |
69 * Note, {@link #executeLite(Object)} does not respect this setting. | |
70 * | |
71 * @param branch - branch name, case-sensitive, non-null. | |
72 * @return <code>this</code> for convenience | |
73 * @throws IllegalArgumentException when branch argument is null | |
74 */ | |
75 public HgIncomingCommand branch(String branch) { | |
76 if (branch == null) { | |
77 throw new IllegalArgumentException(); | |
78 } | |
79 if (branches == null) { | |
80 branches = new TreeSet<String>(); | |
81 } | |
82 branches.add(branch); | |
83 return this; | |
84 } | |
85 | |
86 /** | |
87 * PLACEHOLDER, NOT IMPLEMENTED YET. | |
88 * | |
89 * Whether to include sub-repositories when collecting changes, default is <code>true</code> XXX or false? | |
90 * @return <code>this</code> for convenience | |
91 */ | |
92 public HgIncomingCommand subrepo(boolean include) { | |
93 includeSubrepo = include; | |
94 throw HgRepository.notImplemented(); | |
95 } | |
96 | |
97 /** | |
98 * Lightweight check for incoming changes, gives only list of revisions to pull. | |
99 * Reported changes are from any branch (limits set by {@link #branch(String)} are not taken into account. | |
100 * | |
101 * @param context anything hg4j can use to get progress and/or cancel support | |
102 * @return list of nodes present at remote and missing locally | |
103 * @throws HgException | |
104 * @throws CancelledException | |
105 */ | |
106 public List<Nodeid> executeLite(Object context) throws HgException, CancelledException { | |
107 LinkedHashSet<Nodeid> result = new LinkedHashSet<Nodeid>(); | |
108 RepositoryComparator repoCompare = getComparator(context); | |
109 for (BranchChain bc : getMissingBranches(context)) { | |
110 List<Nodeid> missing = repoCompare.visitBranches(bc); | |
111 HashSet<Nodeid> common = new HashSet<Nodeid>(); // ordering is irrelevant | |
112 repoCompare.collectKnownRoots(bc, common); | |
113 // missing could only start with common elements. Once non-common, rest is just distinct branch revision trails. | |
114 for (Iterator<Nodeid> it = missing.iterator(); it.hasNext() && common.contains(it.next()); it.remove()) ; | |
115 result.addAll(missing); | |
116 } | |
117 ArrayList<Nodeid> rv = new ArrayList<Nodeid>(result); | |
118 return rv; | |
119 } | |
120 | |
121 /** | |
122 * Full information about incoming changes | |
123 * | |
124 * @throws HgException | |
125 * @throws CancelledException | |
126 */ | |
127 public void executeFull(final HgChangesetHandler handler) throws HgException, CancelledException { | |
128 if (handler == null) { | |
129 throw new IllegalArgumentException("Delegate can't be null"); | |
130 } | |
131 final List<Nodeid> common = getCommon(handler); | |
132 HgBundle changegroup = remoteRepo.getChanges(common); | |
133 try { | |
134 changegroup.changes(localRepo, new HgChangelog.Inspector() { | |
135 private int localIndex; | |
136 private final HgChangelog.ParentWalker parentHelper; | |
137 private final ChangesetTransformer transformer; | |
138 | |
139 { | |
140 transformer = new ChangesetTransformer(localRepo, handler, getParentHelper()); | |
141 transformer.limitBranches(branches); | |
142 parentHelper = getParentHelper(); | |
143 // new revisions, if any, would be added after all existing, and would get numbered started with last+1 | |
144 localIndex = localRepo.getChangelog().getRevisionCount(); | |
145 } | |
146 | |
147 public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) { | |
148 if (parentHelper.knownNode(nodeid)) { | |
149 if (!common.contains(nodeid)) { | |
150 throw new HgBadStateException("Bundle shall not report known nodes other than roots we've supplied"); | |
151 } | |
152 return; | |
153 } | |
154 transformer.next(localIndex++, nodeid, cset); | |
155 } | |
156 }); | |
157 } catch (IOException ex) { | |
158 throw new HgException(ex); | |
159 } | |
160 } | |
161 | |
162 private RepositoryComparator getComparator(Object context) throws HgException, CancelledException { | |
163 if (remoteRepo == null) { | |
164 throw new HgBadArgumentException("Shall specify remote repository to compare against", null); | |
165 } | |
166 if (comparator == null) { | |
167 comparator = new RepositoryComparator(getParentHelper(), remoteRepo); | |
168 // comparator.compare(context); // XXX meanwhile we use distinct path to calculate common | |
169 } | |
170 return comparator; | |
171 } | |
172 | |
173 private HgChangelog.ParentWalker getParentHelper() { | |
174 if (parentHelper == null) { | |
175 parentHelper = localRepo.getChangelog().new ParentWalker(); | |
176 parentHelper.init(); | |
177 } | |
178 return parentHelper; | |
179 } | |
180 | |
181 private List<BranchChain> getMissingBranches(Object context) throws HgException, CancelledException { | |
182 if (missingBranches == null) { | |
183 missingBranches = getComparator(context).calculateMissingBranches(); | |
184 } | |
185 return missingBranches; | |
186 } | |
187 | |
188 private List<Nodeid> getCommon(Object context) throws HgException, CancelledException { | |
189 // return getComparator(context).getCommon(); | |
190 final LinkedHashSet<Nodeid> common = new LinkedHashSet<Nodeid>(); | |
191 // XXX common can be obtained from repoCompare, but at the moment it would almost duplicate work of calculateMissingBranches | |
192 // once I refactor latter, common shall be taken from repoCompare. | |
193 RepositoryComparator repoCompare = getComparator(context); | |
194 for (BranchChain bc : getMissingBranches(context)) { | |
195 repoCompare.collectKnownRoots(bc, common); | |
196 } | |
197 return new LinkedList<Nodeid>(common); | |
198 } | |
199 } |