comparison cmdline/org/tmatesoft/hg/console/Status.java @ 72:9a03a80a0f2f

Command-line frontend moved to separate source root with new package statement
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sun, 23 Jan 2011 03:46:59 +0100
parents src/com/tmate/hgkit/console/Status.java@b771e94a4f7c
children 6f1b88693d48
comparison
equal deleted inserted replaced
71:ce6d23673f2d 72:9a03a80a0f2f
1 /*
2 * Copyright (c) 2010-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@svnkit.com
16 */
17 package org.tmatesoft.hg.console;
18
19 import static com.tmate.hgkit.ll.HgRepository.TIP;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25 import com.tmate.hgkit.fs.RepositoryLookup;
26 import com.tmate.hgkit.ll.HgDataFile;
27 import com.tmate.hgkit.ll.HgRepository;
28 import com.tmate.hgkit.ll.Internals;
29 import com.tmate.hgkit.ll.LocalHgRepo;
30 import com.tmate.hgkit.ll.Nodeid;
31 import com.tmate.hgkit.ll.StatusCollector;
32 import com.tmate.hgkit.ll.WorkingCopyStatusCollector;
33
34 /**
35 *
36 * @author Artem Tikhomirov
37 * @author TMate Software Ltd.
38 */
39 public class Status {
40
41 public static void main(String[] args) throws Exception {
42 RepositoryLookup repoLookup = new RepositoryLookup();
43 RepositoryLookup.Options cmdLineOpts = RepositoryLookup.Options.parse(args);
44 HgRepository hgRepo = repoLookup.detect(cmdLineOpts);
45 if (hgRepo.isInvalid()) {
46 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
47 return;
48 }
49 System.out.println(hgRepo.getLocation());
50 Internals debug = new Internals(hgRepo);
51 debug.dumpDirstate();
52 final StatusDump dump = new StatusDump();
53 dump.showIgnored = false;
54 dump.showClean = false;
55 StatusCollector sc = new StatusCollector(hgRepo);
56 final int r1 = 0, r2 = 3;
57 System.out.printf("Status for changes between revision %d and %d:\n", r1, r2);
58 sc.walk(r1, r2, dump);
59 //
60 System.out.println("\n\nSame, but sorted in the way hg status does:");
61 StatusCollector.Record r = sc.status(r1, r2);
62 sortAndPrint('M', r.getModified());
63 sortAndPrint('A', r.getAdded());
64 sortAndPrint('R', r.getRemoved());
65 //
66 System.out.println("\n\nTry hg status --change <rev>:");
67 sc.change(0, dump);
68 System.out.println("\nStatus against working dir:");
69 WorkingCopyStatusCollector wcc = new WorkingCopyStatusCollector(hgRepo, ((LocalHgRepo) hgRepo).createWorkingDirWalker());
70 wcc.walk(TIP, dump);
71 System.out.println();
72 System.out.printf("Manifest of the revision %d:\n", r2);
73 hgRepo.getManifest().walk(r2, r2, new Manifest.Dump());
74 System.out.println();
75 System.out.printf("\nStatus of working dir against %d:\n", r2);
76 r = wcc.status(r2);
77 sortAndPrint('M', r.getModified());
78 sortAndPrint('A', r.getAdded());
79 sortAndPrint('R', r.getRemoved());
80 sortAndPrint('?', r.getUnknown());
81 sortAndPrint('I', r.getIgnored());
82 sortAndPrint('C', r.getClean());
83 sortAndPrint('!', r.getMissing());
84 }
85
86 private static void sortAndPrint(char prefix, List<String> ul) {
87 ArrayList<String> sortList = new ArrayList<String>(ul);
88 Collections.sort(sortList);
89 for (String s : sortList) {
90 System.out.print(prefix);
91 System.out.print(' ');
92 System.out.println(s);
93 }
94 }
95
96 protected static void testStatusInternals(HgRepository hgRepo) {
97 HgDataFile n = hgRepo.getFileNode("design.txt");
98 for (String s : new String[] {"011dfd44417c72bd9e54cf89b82828f661b700ed", "e5529faa06d53e06a816e56d218115b42782f1ba", "c18e7111f1fc89a80a00f6a39d51288289a382fc"}) {
99 // expected: 359, 2123, 3079
100 byte[] b = s.getBytes();
101 final Nodeid nid = Nodeid.fromAscii(b, 0, b.length);
102 System.out.println(s + " : " + n.length(nid));
103 }
104 }
105
106 private static class StatusDump implements StatusCollector.Inspector {
107 public boolean hideStatusPrefix = false; // hg status -n option
108 public boolean showCopied = true; // -C
109 public boolean showIgnored = true; // -i
110 public boolean showClean = true; // -c
111
112 public void modified(String fname) {
113 print('M', fname);
114 }
115
116 public void added(String fname) {
117 print('A', fname);
118 }
119
120 public void copied(String fnameOrigin, String fnameAdded) {
121 added(fnameAdded);
122 if (showCopied) {
123 print(' ', fnameOrigin);
124 }
125 }
126
127 public void removed(String fname) {
128 print('R', fname);
129 }
130
131 public void clean(String fname) {
132 if (showClean) {
133 print('C', fname);
134 }
135 }
136
137 public void missing(String fname) {
138 print('!', fname);
139 }
140
141 public void unknown(String fname) {
142 print('?', fname);
143 }
144
145 public void ignored(String fname) {
146 if (showIgnored) {
147 print('I', fname);
148 }
149 }
150
151 private void print(char status, String fname) {
152 if (!hideStatusPrefix) {
153 System.out.print(status);
154 System.out.print(' ');
155 }
156 System.out.println(fname);
157 }
158 }
159 }