comparison cmdline/org/tmatesoft/hg/console/Main.java @ 143:b9700740553a

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