Mercurial > hg4j
comparison cmdline/org/tmatesoft/hg/console/Status.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 | 4a948ec83980 |
children | d5268ca7715b |
comparison
equal
deleted
inserted
replaced
142:37a34044e6bd | 143:b9700740553a |
---|---|
14 * the terms of a license other than GNU General Public License | 14 * the terms of a license other than GNU General Public License |
15 * contact TMate Software at support@hg4j.com | 15 * contact TMate Software at support@hg4j.com |
16 */ | 16 */ |
17 package org.tmatesoft.hg.console; | 17 package org.tmatesoft.hg.console; |
18 | 18 |
19 import static org.tmatesoft.hg.repo.HgRepository.TIP; | 19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; |
20 | 20 |
21 import java.util.ArrayList; | 21 import java.util.ArrayList; |
22 import java.util.Collections; | 22 import java.util.Collections; |
23 import java.util.HashMap; | |
24 import java.util.LinkedList; | |
23 import java.util.List; | 25 import java.util.List; |
24 import java.util.Map; | 26 import java.util.Map; |
27 import java.util.TreeMap; | |
25 | 28 |
26 import org.tmatesoft.hg.core.Nodeid; | 29 import org.tmatesoft.hg.core.HgRepoFacade; |
27 import org.tmatesoft.hg.repo.HgDataFile; | 30 import org.tmatesoft.hg.core.HgStatus; |
28 import org.tmatesoft.hg.repo.HgRepository; | 31 import org.tmatesoft.hg.core.HgStatus.Kind; |
29 import org.tmatesoft.hg.repo.HgStatusInspector; | 32 import org.tmatesoft.hg.core.HgStatusCommand; |
30 import org.tmatesoft.hg.repo.HgInternals; | |
31 import org.tmatesoft.hg.repo.HgStatusCollector; | |
32 import org.tmatesoft.hg.repo.HgStatusCollector.Record; | |
33 import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector; | |
34 import org.tmatesoft.hg.util.Path; | 33 import org.tmatesoft.hg.util.Path; |
35 | 34 |
36 /** | 35 /** |
37 * | 36 * |
38 * @author Artem Tikhomirov | 37 * @author Artem Tikhomirov |
40 */ | 39 */ |
41 public class Status { | 40 public class Status { |
42 | 41 |
43 public static void main(String[] args) throws Exception { | 42 public static void main(String[] args) throws Exception { |
44 Options cmdLineOpts = Options.parse(args); | 43 Options cmdLineOpts = Options.parse(args); |
45 HgRepository hgRepo = cmdLineOpts.findRepository(); | 44 HgRepoFacade hgRepo = new HgRepoFacade(); |
46 if (hgRepo.isInvalid()) { | 45 if (!hgRepo.init(cmdLineOpts.findRepository())) { |
47 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); | 46 System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation()); |
48 return; | 47 return; |
49 } | 48 } |
50 System.out.println(hgRepo.getLocation()); | |
51 // | 49 // |
52 // bunchOfTests(hgRepo); | 50 HgStatusCommand cmd = hgRepo.createStatusCommand(); |
53 // | 51 if (cmdLineOpts.getBoolean("-A", "-all")) { |
54 // new Internals(hgRepo).dumpDirstate(); | 52 cmd.all(); |
55 // | 53 } else { |
56 statusWorkingCopy(hgRepo); | 54 // default: mardu |
57 //statusRevVsWorkingCopy(hgRepo); | 55 cmd.modified(cmdLineOpts.getBoolean(true, "-m", "--modified")); |
58 } | 56 cmd.added(cmdLineOpts.getBoolean(true, "-a", "--added")); |
57 cmd.removed(cmdLineOpts.getBoolean(true, "-r", "--removed")); | |
58 cmd.deleted(cmdLineOpts.getBoolean(true, "-d", "--deleted")); | |
59 cmd.unknown(cmdLineOpts.getBoolean(true, "-u", "--unknonwn")); | |
60 cmd.clean(cmdLineOpts.getBoolean("-c", "--clean")); | |
61 cmd.ignored(cmdLineOpts.getBoolean("-i", "--ignored")); | |
62 } | |
63 // cmd.subrepo(cmdLineOpts.getBoolean("-S", "--subrepos")) | |
64 final boolean noStatusPrefix = cmdLineOpts.getBoolean("-n", "--no-status"); | |
65 final boolean showCopies = cmdLineOpts.getBoolean("-C", "--copies"); | |
66 class StatusHandler implements HgStatusCommand.Handler { | |
67 | |
68 final Map<HgStatus.Kind, List<Path>> data = new TreeMap<HgStatus.Kind, List<Path>>(); | |
69 final Map<Path, Path> copies = showCopies ? new HashMap<Path,Path>() : null; | |
70 | |
71 public void handleStatus(HgStatus s) { | |
72 List<Path> l = data.get(s.getKind()); | |
73 if (l == null) { | |
74 l = new LinkedList<Path>(); | |
75 data.put(s.getKind(), l); | |
76 } | |
77 l.add(s.getPath()); | |
78 if (s.isCopy() && showCopies) { | |
79 copies.put(s.getPath(), s.getOriginalPath()); | |
80 } | |
81 } | |
82 | |
83 public void dump() { | |
84 sortAndPrint('M', data.get(Kind.Modified), null); | |
85 sortAndPrint('A', data.get(Kind.Added), copies); | |
86 sortAndPrint('R', data.get(Kind.Removed), null); | |
87 sortAndPrint('?', data.get(Kind.Unknown), null); | |
88 sortAndPrint('I', data.get(Kind.Ignored), null); | |
89 sortAndPrint('C', data.get(Kind.Clean), null); | |
90 sortAndPrint('!', data.get(Kind.Missing), null); | |
91 } | |
59 | 92 |
60 private static void statusWorkingCopy(HgRepository hgRepo) { | 93 private void sortAndPrint(char prefix, List<Path> ul, Map<Path, Path> copies) { |
61 HgWorkingCopyStatusCollector wcc = new HgWorkingCopyStatusCollector(hgRepo); | 94 if (ul == null) { |
62 HgStatusCollector.Record r = new HgStatusCollector.Record(); | 95 return; |
63 wcc.walk(TIP, r); | 96 } |
64 mardu(r); | 97 ArrayList<Path> sortList = new ArrayList<Path>(ul); |
65 } | 98 Collections.sort(sortList); |
99 for (Path s : sortList) { | |
100 if (!noStatusPrefix) { | |
101 System.out.print(prefix); | |
102 System.out.print(' '); | |
103 } | |
104 System.out.println(s); | |
105 if (copies != null && copies.containsKey(s)) { | |
106 System.out.println(" " + copies.get(s)); | |
107 } | |
108 } | |
109 } | |
110 }; | |
66 | 111 |
67 private static void mardu(Record r) { | 112 StatusHandler statusHandler = new StatusHandler(); |
68 sortAndPrint('M', r.getModified()); | 113 int changeRev = cmdLineOpts.getSingleInt(BAD_REVISION, "--change"); |
69 sortAndPrint('A', r.getAdded(), r.getCopied()); | 114 if (changeRev != BAD_REVISION) { |
70 sortAndPrint('R', r.getRemoved()); | 115 cmd.change(changeRev); |
71 sortAndPrint('?', r.getUnknown()); | 116 } else { |
72 // sortAndPrint('I', r.getIgnored()); | 117 List<String> revisions = cmdLineOpts.getList("--rev"); |
73 // sortAndPrint('C', r.getClean()); | 118 int size = revisions.size(); |
74 sortAndPrint('!', r.getMissing()); | 119 if (size > 1) { |
75 } | 120 cmd.base(Integer.parseInt(revisions.get(size - 2))).revision(Integer.parseInt(revisions.get(size - 1))); |
76 | 121 } else if (size > 0) { |
77 private static void statusRevVsWorkingCopy(HgRepository hgRepo) { | 122 cmd.base(Integer.parseInt(revisions.get(0))); |
78 HgWorkingCopyStatusCollector wcc = new HgWorkingCopyStatusCollector(hgRepo); | |
79 HgStatusCollector.Record r = new HgStatusCollector.Record(); | |
80 wcc.walk(3, r); | |
81 mardu(r); | |
82 } | |
83 | |
84 private static void bunchOfTests(HgRepository hgRepo) throws Exception { | |
85 HgInternals debug = new HgInternals(hgRepo); | |
86 debug.dumpDirstate(); | |
87 final StatusDump dump = new StatusDump(); | |
88 dump.showIgnored = false; | |
89 dump.showClean = false; | |
90 HgStatusCollector sc = new HgStatusCollector(hgRepo); | |
91 final int r1 = 0, r2 = 3; | |
92 System.out.printf("Status for changes between revision %d and %d:\n", r1, r2); | |
93 sc.walk(r1, r2, dump); | |
94 // | |
95 System.out.println("\n\nSame, but sorted in the way hg status does:"); | |
96 HgStatusCollector.Record r = sc.status(r1, r2); | |
97 sortAndPrint('M', r.getModified()); | |
98 sortAndPrint('A', r.getAdded()); | |
99 sortAndPrint('R', r.getRemoved()); | |
100 // | |
101 System.out.println("\n\nTry hg status --change <rev>:"); | |
102 sc.change(0, dump); | |
103 System.out.println("\nStatus against working dir:"); | |
104 HgWorkingCopyStatusCollector wcc = new HgWorkingCopyStatusCollector(hgRepo); | |
105 wcc.walk(TIP, dump); | |
106 System.out.println(); | |
107 System.out.printf("Manifest of the revision %d:\n", r2); | |
108 hgRepo.getManifest().walk(r2, r2, new Manifest.Dump()); | |
109 System.out.println(); | |
110 System.out.printf("\nStatus of working dir against %d:\n", r2); | |
111 r = wcc.status(r2); | |
112 sortAndPrint('M', r.getModified()); | |
113 sortAndPrint('A', r.getAdded(), r.getCopied()); | |
114 sortAndPrint('R', r.getRemoved()); | |
115 sortAndPrint('?', r.getUnknown()); | |
116 sortAndPrint('I', r.getIgnored()); | |
117 sortAndPrint('C', r.getClean()); | |
118 sortAndPrint('!', r.getMissing()); | |
119 } | |
120 | |
121 private static void sortAndPrint(char prefix, List<Path> ul) { | |
122 sortAndPrint(prefix, ul, null); | |
123 } | |
124 private static void sortAndPrint(char prefix, List<Path> ul, Map<Path, Path> copies) { | |
125 ArrayList<Path> sortList = new ArrayList<Path>(ul); | |
126 Collections.sort(sortList); | |
127 for (Path s : sortList) { | |
128 System.out.print(prefix); | |
129 System.out.print(' '); | |
130 System.out.println(s); | |
131 if (copies != null && copies.containsKey(s)) { | |
132 System.out.println(" " + copies.get(s)); | |
133 } | 123 } |
134 } | 124 } |
135 } | 125 cmd.execute(statusHandler); |
136 | 126 statusHandler.dump(); |
137 protected static void testStatusInternals(HgRepository hgRepo) { | |
138 HgDataFile n = hgRepo.getFileNode(Path.create("design.txt")); | |
139 for (String s : new String[] {"011dfd44417c72bd9e54cf89b82828f661b700ed", "e5529faa06d53e06a816e56d218115b42782f1ba", "c18e7111f1fc89a80a00f6a39d51288289a382fc"}) { | |
140 // expected: 359, 2123, 3079 | |
141 byte[] b = s.getBytes(); | |
142 final Nodeid nid = Nodeid.fromAscii(b, 0, b.length); | |
143 System.out.println(s + " : " + n.length(nid)); | |
144 } | |
145 } | |
146 | |
147 private static class StatusDump implements HgStatusInspector { | |
148 public boolean hideStatusPrefix = false; // hg status -n option | |
149 public boolean showCopied = true; // -C | |
150 public boolean showIgnored = true; // -i | |
151 public boolean showClean = true; // -c | |
152 | |
153 public void modified(Path fname) { | |
154 print('M', fname); | |
155 } | |
156 | |
157 public void added(Path fname) { | |
158 print('A', fname); | |
159 } | |
160 | |
161 public void copied(Path fnameOrigin, Path fnameAdded) { | |
162 added(fnameAdded); | |
163 if (showCopied) { | |
164 print(' ', fnameOrigin); | |
165 } | |
166 } | |
167 | |
168 public void removed(Path fname) { | |
169 print('R', fname); | |
170 } | |
171 | |
172 public void clean(Path fname) { | |
173 if (showClean) { | |
174 print('C', fname); | |
175 } | |
176 } | |
177 | |
178 public void missing(Path fname) { | |
179 print('!', fname); | |
180 } | |
181 | |
182 public void unknown(Path fname) { | |
183 print('?', fname); | |
184 } | |
185 | |
186 public void ignored(Path fname) { | |
187 if (showIgnored) { | |
188 print('I', fname); | |
189 } | |
190 } | |
191 | |
192 private void print(char status, Path fname) { | |
193 if (!hideStatusPrefix) { | |
194 System.out.print(status); | |
195 System.out.print(' '); | |
196 } | |
197 System.out.println(fname); | |
198 } | |
199 } | 127 } |
200 } | 128 } |