Mercurial > jhg
comparison hg4j-cli/src/main/java/org/tmatesoft/hg/console/Main.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.console; | |
18 | |
19 import static org.tmatesoft.hg.repo.HgRepository.TIP; | |
20 | |
21 import java.util.ArrayList; | |
22 import java.util.Collections; | |
23 import java.util.List; | |
24 import java.util.Map; | |
25 | |
26 import org.tmatesoft.hg.core.HgLogCommand.FileRevision; | |
27 import org.tmatesoft.hg.core.HgManifestCommand; | |
28 import org.tmatesoft.hg.core.Nodeid; | |
29 import org.tmatesoft.hg.internal.ByteArrayChannel; | |
30 import org.tmatesoft.hg.internal.DigestHelper; | |
31 import org.tmatesoft.hg.repo.HgDataFile; | |
32 import org.tmatesoft.hg.repo.HgInternals; | |
33 import org.tmatesoft.hg.repo.HgManifest; | |
34 import org.tmatesoft.hg.repo.HgRepository; | |
35 import org.tmatesoft.hg.repo.HgStatusCollector; | |
36 import org.tmatesoft.hg.repo.HgStatusInspector; | |
37 import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector; | |
38 import org.tmatesoft.hg.util.Path; | |
39 | |
40 /** | |
41 * Various debug dumps. | |
42 * | |
43 * @author Artem Tikhomirov | |
44 * @author TMate Software Ltd. | |
45 */ | |
46 public class Main { | |
47 | |
48 private Options cmdLineOpts; | |
49 private HgRepository hgRepo; | |
50 | |
51 public Main(String[] args) throws Exception { | |
52 cmdLineOpts = Options.parse(args); | |
53 hgRepo = cmdLineOpts.findRepository(); | |
54 if (hgRepo.isInvalid()) { | |
55 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); | |
56 return; | |
57 } | |
58 System.out.println("REPO:" + hgRepo.getLocation()); | |
59 } | |
60 | |
61 public static void main(String[] args) throws Exception { | |
62 Main m = new Main(args); | |
63 m.inflaterLengthException(); | |
64 // m.dumpIgnored(); | |
65 // m.dumpDirstate(); | |
66 // m.testStatusInternals(); | |
67 // m.catCompleteHistory(); | |
68 // m.dumpCompleteManifestLow(); | |
69 // m.dumpCompleteManifestHigh(); | |
70 // m.bunchOfTests(); | |
71 } | |
72 | |
73 private void inflaterLengthException() throws Exception { | |
74 HgDataFile f1 = hgRepo.getFileNode("src/com/tmate/hgkit/console/Bundle.java"); | |
75 HgDataFile f2 = hgRepo.getFileNode("test-repos.jar"); | |
76 System.out.println(f1.isCopy()); | |
77 System.out.println(f2.isCopy()); | |
78 ByteArrayChannel bac = new ByteArrayChannel(); | |
79 f1.content(1, bac); // 0: 1151, 1: 1139 | |
80 System.out.println(bac.toArray().length); | |
81 f2.content(0, bac = new ByteArrayChannel()); // 0: 14269 | |
82 System.out.println(bac.toArray().length); | |
83 } | |
84 | |
85 private void dumpIgnored() { | |
86 HgInternals debug = new HgInternals(hgRepo); | |
87 String[] toCheck = new String[] {"design.txt", "src/com/tmate/hgkit/ll/Changelog.java", "src/Extras.java", "bin/com/tmate/hgkit/ll/Changelog.class"}; | |
88 boolean[] checkResult = debug.checkIgnored(toCheck); | |
89 for (int i = 0; i < toCheck.length; i++) { | |
90 System.out.println("Ignored " + toCheck[i] + ": " + checkResult[i]); | |
91 } | |
92 } | |
93 | |
94 private void dumpDirstate() { | |
95 new HgInternals(hgRepo).dumpDirstate(); | |
96 } | |
97 | |
98 | |
99 private void catCompleteHistory() throws Exception { | |
100 DigestHelper dh = new DigestHelper(); | |
101 for (String fname : cmdLineOpts.getList("")) { | |
102 System.out.println(fname); | |
103 HgDataFile fn = hgRepo.getFileNode(fname); | |
104 if (fn.exists()) { | |
105 int total = fn.getRevisionCount(); | |
106 System.out.printf("Total revisions: %d\n", total); | |
107 for (int i = 0; i < total; i++) { | |
108 ByteArrayChannel sink = new ByteArrayChannel(); | |
109 fn.content(i, sink); | |
110 System.out.println("==========>"); | |
111 byte[] content = sink.toArray(); | |
112 System.out.println(new String(content)); | |
113 int[] parentRevisions = new int[2]; | |
114 byte[] parent1 = new byte[20]; | |
115 byte[] parent2 = new byte[20]; | |
116 fn.parents(i, parentRevisions, parent1, parent2); | |
117 System.out.println(dh.sha1(parent1, parent2, content).asHexString()); | |
118 } | |
119 } else { | |
120 System.out.println(">>>Not found!"); | |
121 } | |
122 } | |
123 } | |
124 | |
125 private void dumpCompleteManifestLow() { | |
126 hgRepo.getManifest().walk(0, TIP, new ManifestDump()); | |
127 } | |
128 | |
129 public static final class ManifestDump implements HgManifest.Inspector { | |
130 public boolean begin(int revision, Nodeid nid) { | |
131 System.out.printf("%d : %s\n", revision, nid); | |
132 return true; | |
133 } | |
134 | |
135 public boolean next(Nodeid nid, String fname, String flags) { | |
136 System.out.println(nid + "\t" + fname + "\t\t" + flags); | |
137 return true; | |
138 } | |
139 | |
140 public boolean end(int revision) { | |
141 System.out.println(); | |
142 return true; | |
143 } | |
144 } | |
145 | |
146 private void dumpCompleteManifestHigh() { | |
147 new HgManifestCommand(hgRepo).dirs(true).execute(new HgManifestCommand.Handler() { | |
148 | |
149 public void begin(Nodeid manifestRevision) { | |
150 System.out.println(">> " + manifestRevision); | |
151 } | |
152 public void dir(Path p) { | |
153 System.out.println(p); | |
154 } | |
155 public void file(FileRevision fileRevision) { | |
156 System.out.print(fileRevision.getRevision());; | |
157 System.out.print(" "); | |
158 System.out.println(fileRevision.getPath()); | |
159 } | |
160 | |
161 public void end(Nodeid manifestRevision) { | |
162 System.out.println(); | |
163 } | |
164 }); | |
165 } | |
166 | |
167 private void bunchOfTests() throws Exception { | |
168 HgInternals debug = new HgInternals(hgRepo); | |
169 debug.dumpDirstate(); | |
170 final StatusDump dump = new StatusDump(); | |
171 dump.showIgnored = false; | |
172 dump.showClean = false; | |
173 HgStatusCollector sc = new HgStatusCollector(hgRepo); | |
174 final int r1 = 0, r2 = 3; | |
175 System.out.printf("Status for changes between revision %d and %d:\n", r1, r2); | |
176 sc.walk(r1, r2, dump); | |
177 // | |
178 System.out.println("\n\nSame, but sorted in the way hg status does:"); | |
179 HgStatusCollector.Record r = sc.status(r1, r2); | |
180 sortAndPrint('M', r.getModified(), null); | |
181 sortAndPrint('A', r.getAdded(), null); | |
182 sortAndPrint('R', r.getRemoved(), null); | |
183 // | |
184 System.out.println("\n\nTry hg status --change <rev>:"); | |
185 sc.change(0, dump); | |
186 System.out.println("\nStatus against working dir:"); | |
187 HgWorkingCopyStatusCollector wcc = new HgWorkingCopyStatusCollector(hgRepo); | |
188 wcc.walk(TIP, dump); | |
189 System.out.println(); | |
190 System.out.printf("Manifest of the revision %d:\n", r2); | |
191 hgRepo.getManifest().walk(r2, r2, new ManifestDump()); | |
192 System.out.println(); | |
193 System.out.printf("\nStatus of working dir against %d:\n", r2); | |
194 r = wcc.status(r2); | |
195 sortAndPrint('M', r.getModified(), null); | |
196 sortAndPrint('A', r.getAdded(), r.getCopied()); | |
197 sortAndPrint('R', r.getRemoved(), null); | |
198 sortAndPrint('?', r.getUnknown(), null); | |
199 sortAndPrint('I', r.getIgnored(), null); | |
200 sortAndPrint('C', r.getClean(), null); | |
201 sortAndPrint('!', r.getMissing(), null); | |
202 } | |
203 | |
204 private void sortAndPrint(char prefix, List<Path> ul, Map<Path, Path> copies) { | |
205 ArrayList<Path> sortList = new ArrayList<Path>(ul); | |
206 Collections.sort(sortList); | |
207 for (Path s : sortList) { | |
208 System.out.print(prefix); | |
209 System.out.print(' '); | |
210 System.out.println(s); | |
211 if (copies != null && copies.containsKey(s)) { | |
212 System.out.println(" " + copies.get(s)); | |
213 } | |
214 } | |
215 } | |
216 | |
217 | |
218 private void testStatusInternals() { | |
219 HgDataFile n = hgRepo.getFileNode(Path.create("design.txt")); | |
220 for (String s : new String[] {"011dfd44417c72bd9e54cf89b82828f661b700ed", "e5529faa06d53e06a816e56d218115b42782f1ba", "c18e7111f1fc89a80a00f6a39d51288289a382fc"}) { | |
221 // expected: 359, 2123, 3079 | |
222 byte[] b = s.getBytes(); | |
223 final Nodeid nid = Nodeid.fromAscii(b, 0, b.length); | |
224 System.out.println(s + " : " + n.length(nid)); | |
225 } | |
226 } | |
227 | |
228 static void force_gc() { | |
229 Runtime.getRuntime().runFinalization(); | |
230 Runtime.getRuntime().gc(); | |
231 Thread.yield(); | |
232 Runtime.getRuntime().runFinalization(); | |
233 Runtime.getRuntime().gc(); | |
234 Thread.yield(); | |
235 } | |
236 | |
237 private static class StatusDump implements HgStatusInspector { | |
238 public boolean hideStatusPrefix = false; // hg status -n option | |
239 public boolean showCopied = true; // -C | |
240 public boolean showIgnored = true; // -i | |
241 public boolean showClean = true; // -c | |
242 | |
243 public void modified(Path fname) { | |
244 print('M', fname); | |
245 } | |
246 | |
247 public void added(Path fname) { | |
248 print('A', fname); | |
249 } | |
250 | |
251 public void copied(Path fnameOrigin, Path fnameAdded) { | |
252 added(fnameAdded); | |
253 if (showCopied) { | |
254 print(' ', fnameOrigin); | |
255 } | |
256 } | |
257 | |
258 public void removed(Path fname) { | |
259 print('R', fname); | |
260 } | |
261 | |
262 public void clean(Path fname) { | |
263 if (showClean) { | |
264 print('C', fname); | |
265 } | |
266 } | |
267 | |
268 public void missing(Path fname) { | |
269 print('!', fname); | |
270 } | |
271 | |
272 public void unknown(Path fname) { | |
273 print('?', fname); | |
274 } | |
275 | |
276 public void ignored(Path fname) { | |
277 if (showIgnored) { | |
278 print('I', fname); | |
279 } | |
280 } | |
281 | |
282 private void print(char status, Path fname) { | |
283 if (!hideStatusPrefix) { | |
284 System.out.print(status); | |
285 System.out.print(' '); | |
286 } | |
287 System.out.println(fname); | |
288 } | |
289 } | |
290 } |