comparison src/org/tmatesoft/hg/core/HgStatusCommand.java @ 131:aa1629f36482

Renamed .core classes to start with Hg prefix
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 16 Feb 2011 20:47:56 +0100
parents src/org/tmatesoft/hg/core/StatusCommand.java@44b97930570c
children 4a948ec83980
comparison
equal deleted inserted replaced
130:7567f4a42fe5 131:aa1629f36482
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 static org.tmatesoft.hg.core.HgStatus.Kind.*;
20 import static org.tmatesoft.hg.repo.HgRepository.*;
21
22 import java.util.ConcurrentModificationException;
23
24 import org.tmatesoft.hg.core.Path.Matcher;
25 import org.tmatesoft.hg.internal.ChangelogHelper;
26 import org.tmatesoft.hg.repo.HgRepository;
27 import org.tmatesoft.hg.repo.HgStatusCollector;
28 import org.tmatesoft.hg.repo.HgStatusInspector;
29 import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector;
30
31 /**
32 * Command to obtain file status information, 'hg status' counterpart.
33 *
34 * @author Artem Tikhomirov
35 * @author TMate Software Ltd.
36 */
37 public class HgStatusCommand {
38 private final HgRepository repo;
39
40 private int startRevision = TIP;
41 private int endRevision = WORKING_COPY;
42 private boolean visitSubRepo = true;
43
44 private final Mediator mediator = new Mediator();
45
46 public HgStatusCommand(HgRepository hgRepo) {
47 repo = hgRepo;
48 defaults();
49 }
50
51 public HgStatusCommand defaults() {
52 final Mediator m = mediator;
53 m.needModified = m.needAdded = m.needRemoved = m.needUnknown = m.needMissing = true;
54 m.needCopies = m.needClean = m.needIgnored = false;
55 return this;
56 }
57 public HgStatusCommand all() {
58 final Mediator m = mediator;
59 m.needModified = m.needAdded = m.needRemoved = m.needUnknown = m.needMissing = true;
60 m.needCopies = m.needClean = m.needIgnored = true;
61 return this;
62 }
63
64
65 public HgStatusCommand modified(boolean include) {
66 mediator.needModified = include;
67 return this;
68 }
69 public HgStatusCommand added(boolean include) {
70 mediator.needAdded = include;
71 return this;
72 }
73 public HgStatusCommand removed(boolean include) {
74 mediator.needRemoved = include;
75 return this;
76 }
77 public HgStatusCommand deleted(boolean include) {
78 mediator.needMissing = include;
79 return this;
80 }
81 public HgStatusCommand unknown(boolean include) {
82 mediator.needUnknown = include;
83 return this;
84 }
85 public HgStatusCommand clean(boolean include) {
86 mediator.needClean = include;
87 return this;
88 }
89 public HgStatusCommand ignored(boolean include) {
90 mediator.needIgnored = include;
91 return this;
92 }
93
94 /**
95 * if set, either base:revision or base:workingdir
96 * to unset, pass {@link HgRepository#TIP} or {@link HgRepository#BAD_REVISION}
97 * @param revision
98 * @return
99 */
100
101 public HgStatusCommand base(int revision) {
102 if (revision == WORKING_COPY) {
103 throw new IllegalArgumentException();
104 }
105 if (revision == BAD_REVISION) {
106 revision = TIP;
107 }
108 startRevision = revision;
109 return this;
110 }
111
112 /**
113 * Revision without base == --change
114 * Pass {@link HgRepository#WORKING_COPY} or {@link HgRepository#BAD_REVISION} to reset
115 * @param revision
116 * @return
117 */
118 public HgStatusCommand revision(int revision) {
119 if (revision == BAD_REVISION) {
120 revision = WORKING_COPY;
121 }
122 // XXX negative values, except for predefined constants, shall throw IAE.
123 endRevision = revision;
124 return this;
125 }
126
127 // pass null to reset
128 public HgStatusCommand match(Path.Matcher pathMatcher) {
129 mediator.matcher = pathMatcher;
130 return this;
131 }
132
133 public HgStatusCommand subrepo(boolean visit) {
134 visitSubRepo = visit;
135 throw HgRepository.notImplemented();
136 }
137
138 /**
139 * Perform status operation according to parameters set.
140 *
141 * @param handler callback to get status information
142 * @throws IllegalArgumentException if handler is <code>null</code>
143 * @throws ConcurrentModificationException if this command already runs (i.e. being used from another thread)
144 */
145 public void execute(Handler statusHandler) {
146 if (statusHandler == null) {
147 throw new IllegalArgumentException();
148 }
149 if (mediator.busy()) {
150 throw new ConcurrentModificationException();
151 }
152 HgStatusCollector sc = new HgStatusCollector(repo); // TODO from CommandContext
153 // PathPool pathHelper = new PathPool(repo.getPathHelper()); // TODO from CommandContext
154 try {
155 // XXX if I need a rough estimation (for ProgressMonitor) of number of work units,
156 // I may use number of files in either rev1 or rev2 manifest edition
157 mediator.start(statusHandler, new ChangelogHelper(repo, startRevision));
158 if (endRevision == WORKING_COPY) {
159 HgWorkingCopyStatusCollector wcsc = new HgWorkingCopyStatusCollector(repo);
160 wcsc.setBaseRevisionCollector(sc);
161 wcsc.walk(startRevision, mediator);
162 } else {
163 if (startRevision == TIP) {
164 sc.change(endRevision, mediator);
165 } else {
166 sc.walk(startRevision, endRevision, mediator);
167 }
168 }
169 } finally {
170 mediator.done();
171 }
172 }
173
174 public interface Handler {
175 void handleStatus(HgStatus s);
176 }
177
178 private class Mediator implements HgStatusInspector {
179 boolean needModified;
180 boolean needAdded;
181 boolean needRemoved;
182 boolean needUnknown;
183 boolean needMissing;
184 boolean needClean;
185 boolean needIgnored;
186 boolean needCopies;
187 Matcher matcher;
188 Handler handler;
189 private ChangelogHelper logHelper;
190
191 Mediator() {
192 }
193
194 public void start(Handler h, ChangelogHelper changelogHelper) {
195 handler = h;
196 logHelper = changelogHelper;
197 }
198
199 public void done() {
200 handler = null;
201 logHelper = null;
202 }
203
204 public boolean busy() {
205 return handler != null;
206 }
207
208 public void modified(Path fname) {
209 if (needModified) {
210 if (matcher == null || matcher.accept(fname)) {
211 handler.handleStatus(new HgStatus(Modified, fname, logHelper));
212 }
213 }
214 }
215 public void added(Path fname) {
216 if (needAdded) {
217 if (matcher == null || matcher.accept(fname)) {
218 handler.handleStatus(new HgStatus(Added, fname, logHelper));
219 }
220 }
221 }
222 public void removed(Path fname) {
223 if (needRemoved) {
224 if (matcher == null || matcher.accept(fname)) {
225 handler.handleStatus(new HgStatus(Removed, fname, logHelper));
226 }
227 }
228 }
229 public void copied(Path fnameOrigin, Path fnameAdded) {
230 if (needCopies) {
231 if (matcher == null || matcher.accept(fnameAdded)) {
232 handler.handleStatus(new HgStatus(Added, fnameAdded, fnameOrigin, logHelper));
233 }
234 }
235 }
236 public void missing(Path fname) {
237 if (needMissing) {
238 if (matcher == null || matcher.accept(fname)) {
239 handler.handleStatus(new HgStatus(Missing, fname, logHelper));
240 }
241 }
242 }
243 public void unknown(Path fname) {
244 if (needUnknown) {
245 if (matcher == null || matcher.accept(fname)) {
246 handler.handleStatus(new HgStatus(Unknown, fname, logHelper));
247 }
248 }
249 }
250 public void clean(Path fname) {
251 if (needClean) {
252 if (matcher == null || matcher.accept(fname)) {
253 handler.handleStatus(new HgStatus(Clean, fname, logHelper));
254 }
255 }
256 }
257 public void ignored(Path fname) {
258 if (needIgnored) {
259 if (matcher == null || matcher.accept(fname)) {
260 handler.handleStatus(new HgStatus(Ignored, fname, logHelper));
261 }
262 }
263 }
264 }
265 }