comparison cmdline/org/tmatesoft/hg/console/Log.java @ 206:63c9fed4369e

Sample command line tools for incoming/outgoing to use commands
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 28 Apr 2011 02:47:44 +0200
parents 33a7d76f067b
children 41a778e3fd31
comparison
equal deleted inserted replaced
205:ffc5f6d59f7e 206:63c9fed4369e
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 java.util.Formatter;
20 import java.util.LinkedList;
21 import java.util.List; 19 import java.util.List;
22 20
23 import org.tmatesoft.hg.core.HgChangeset;
24 import org.tmatesoft.hg.core.HgLogCommand; 21 import org.tmatesoft.hg.core.HgLogCommand;
25 import org.tmatesoft.hg.core.HgLogCommand.FileRevision; 22 import org.tmatesoft.hg.core.HgLogCommand.FileRevision;
26 import org.tmatesoft.hg.core.Nodeid;
27 import org.tmatesoft.hg.repo.HgDataFile; 23 import org.tmatesoft.hg.repo.HgDataFile;
28 import org.tmatesoft.hg.repo.HgRepository; 24 import org.tmatesoft.hg.repo.HgRepository;
29 import org.tmatesoft.hg.util.Path;
30 25
31 26
32 /** 27 /**
33 * @author Artem Tikhomirov 28 * @author Artem Tikhomirov
34 * @author TMate Software Ltd. 29 * @author TMate Software Ltd.
43 if (hgRepo.isInvalid()) { 38 if (hgRepo.isInvalid()) {
44 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); 39 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
45 return; 40 return;
46 } 41 }
47 final Dump dump = new Dump(hgRepo); 42 final Dump dump = new Dump(hgRepo);
48 dump.complete = cmdLineOpts.getBoolean("--debug"); 43 dump.complete(cmdLineOpts.getBoolean("--debug"));
49 dump.verbose = cmdLineOpts.getBoolean("-v", "--verbose"); 44 dump.verbose(cmdLineOpts.getBoolean("-v", "--verbose"));
50 dump.reverseOrder = true; 45 final boolean reverseOrder = true;
46 dump.reversed(reverseOrder);
51 HgLogCommand cmd = new HgLogCommand(hgRepo); 47 HgLogCommand cmd = new HgLogCommand(hgRepo);
52 for (String u : cmdLineOpts.getList("-u", "--user")) { 48 for (String u : cmdLineOpts.getList("-u", "--user")) {
53 cmd.user(u); 49 cmd.user(u);
54 } 50 }
55 for (String b : cmdLineOpts.getList("-b", "--branches")) { 51 for (String b : cmdLineOpts.getList("-b", "--branches")) {
67 cmd.execute(dump); 63 cmd.execute(dump);
68 } else { 64 } else {
69 // in fact, external (to dump inspector) --limit processing yelds incorrect results when other args 65 // in fact, external (to dump inspector) --limit processing yelds incorrect results when other args
70 // e.g. -u or -b are used (i.e. with -u shall give <limit> csets with user, not check last <limit> csets for user 66 // e.g. -u or -b are used (i.e. with -u shall give <limit> csets with user, not check last <limit> csets for user
71 int[] r = new int[] { 0, hgRepo.getChangelog().getRevisionCount() }; 67 int[] r = new int[] { 0, hgRepo.getChangelog().getRevisionCount() };
72 if (fixRange(r, dump.reverseOrder, limit) == 0) { 68 if (fixRange(r, reverseOrder, limit) == 0) {
73 System.out.println("No changes"); 69 System.out.println("No changes");
74 return; 70 return;
75 } 71 }
76 cmd.range(r[0], r[1]).execute(dump); 72 cmd.range(r[0], r[1]).execute(dump);
77 } 73 }
78 dump.complete(); 74 dump.done();
79 } else { 75 } else {
80 for (String fname : files) { 76 for (String fname : files) {
81 HgDataFile f1 = hgRepo.getFileNode(fname); 77 HgDataFile f1 = hgRepo.getFileNode(fname);
82 System.out.println("History of the file: " + f1.getPath()); 78 System.out.println("History of the file: " + f1.getPath());
83 if (limit == -1) { 79 if (limit == -1) {
84 cmd.file(f1.getPath(), true).execute(dump); 80 cmd.file(f1.getPath(), true).execute(dump);
85 } else { 81 } else {
86 int[] r = new int[] { 0, f1.getRevisionCount() }; 82 int[] r = new int[] { 0, f1.getRevisionCount() };
87 if (fixRange(r, dump.reverseOrder, limit) == 0) { 83 if (fixRange(r, reverseOrder, limit) == 0) {
88 System.out.println("No changes"); 84 System.out.println("No changes");
89 continue; 85 continue;
90 } 86 }
91 cmd.range(r[0], r[1]).file(f1.getPath(), true).execute(dump); 87 cmd.range(r[0], r[1]).file(f1.getPath(), true).execute(dump);
92 } 88 }
93 dump.complete(); 89 dump.done();
94 } 90 }
95 } 91 }
96 // cmd = null; 92 // cmd = null;
97 System.out.println("Total time:" + (System.currentTimeMillis() - start)); 93 System.out.println("Total time:" + (System.currentTimeMillis() - start));
98 // Main.force_gc(); 94 // Main.force_gc();
111 int rv = start_end[1] - start_end[0]; 107 int rv = start_end[1] - start_end[0];
112 start_end[1]--; // range needs index, not length 108 start_end[1]--; // range needs index, not length
113 return rv; 109 return rv;
114 } 110 }
115 111
116 private static final class Dump implements HgLogCommand.FileHistoryHandler { 112 private static final class Dump extends ChangesetDumpHandler implements HgLogCommand.FileHistoryHandler {
117 // params
118 boolean complete = false; // roughly --debug
119 boolean reverseOrder = false;
120 boolean verbose = true; // roughly -v
121 // own
122 private LinkedList<String> l = new LinkedList<String>();
123 private final HgRepository repo;
124 // private HgChangelog.ParentWalker changelogWalker;
125 private final int tip ;
126 113
127 public Dump(HgRepository hgRepo) { 114 public Dump(HgRepository hgRepo) {
128 repo = hgRepo; 115 super(hgRepo);
129 tip = hgRepo.getChangelog().getLastRevision();
130 } 116 }
131 117
132 public void copy(FileRevision from, FileRevision to) { 118 public void copy(FileRevision from, FileRevision to) {
133 System.out.printf("Got notified that %s(%s) was originally known as %s(%s)\n", to.getPath(), to.getRevision(), from.getPath(), from.getRevision()); 119 System.out.printf("Got notified that %s(%s) was originally known as %s(%s)\n", to.getPath(), to.getRevision(), from.getPath(), from.getRevision());
134 } 120 }
135
136 public void next(HgChangeset changeset) {
137 final String s = print(changeset);
138 if (reverseOrder) {
139 // XXX in fact, need to insert s into l according to changeset.getRevision()
140 // because when file history is being followed, revisions of the original file (with smaller revNumber)
141 // are reported *after* revisions of present file and with addFirst appear above them
142 l.addFirst(s);
143 } else {
144 System.out.print(s);
145 }
146 }
147
148 public void complete() {
149 if (!reverseOrder) {
150 return;
151 }
152 for (String s : l) {
153 System.out.print(s);
154 }
155 l.clear();
156 // changelogWalker = null;
157 }
158
159 private String print(HgChangeset cset) {
160 StringBuilder sb = new StringBuilder();
161 Formatter f = new Formatter(sb);
162 final Nodeid csetNodeid = cset.getNodeid();
163 f.format("changeset: %d:%s\n", cset.getRevision(), complete ? csetNodeid : csetNodeid.shortNotation());
164 if (cset.getRevision() == tip || repo.getTags().isTagged(csetNodeid)) {
165
166 sb.append("tag: ");
167 for (String t : repo.getTags().tags(csetNodeid)) {
168 sb.append(t);
169 sb.append(' ');
170 }
171 if (cset.getRevision() == tip) {
172 sb.append("tip");
173 }
174 sb.append('\n');
175 }
176 if (complete) {
177 // if (changelogWalker == null) {
178 // changelogWalker = repo.getChangelog().new ParentWalker();
179 // changelogWalker.init();
180 // }
181 // Nodeid p1 = changelogWalker.safeFirstParent(csetNodeid);
182 // Nodeid p2 = changelogWalker.safeSecondParent(csetNodeid);
183 Nodeid p1 = cset.getFirstParentRevision();
184 Nodeid p2 = cset.getSecondParentRevision();
185 int p1x = p1 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevision(p1);
186 int p2x = p2 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevision(p2);
187 int mx = repo.getManifest().getLocalRevision(cset.getManifestRevision());
188 f.format("parent: %d:%s\nparent: %d:%s\nmanifest: %d:%s\n", p1x, p1, p2x, p2, mx, cset.getManifestRevision());
189 }
190 f.format("user: %s\ndate: %s\n", cset.getUser(), cset.getDate());
191 if (!complete && verbose) {
192 final List<Path> files = cset.getAffectedFiles();
193 sb.append("files: ");
194 for (Path s : files) {
195 sb.append(' ');
196 sb.append(s);
197 }
198 sb.append('\n');
199 }
200 if (complete) {
201 if (!cset.getModifiedFiles().isEmpty()) {
202 sb.append("files: ");
203 for (FileRevision s : cset.getModifiedFiles()) {
204 sb.append(' ');
205 sb.append(s.getPath());
206 }
207 sb.append('\n');
208 }
209 if (!cset.getAddedFiles().isEmpty()) {
210 sb.append("files+: ");
211 for (FileRevision s : cset.getAddedFiles()) {
212 sb.append(' ');
213 sb.append(s.getPath());
214 }
215 sb.append('\n');
216 }
217 if (!cset.getRemovedFiles().isEmpty()) {
218 sb.append("files-: ");
219 for (Path s : cset.getRemovedFiles()) {
220 sb.append(' ');
221 sb.append(s);
222 }
223 sb.append('\n');
224 }
225 // if (cset.extras() != null) {
226 // sb.append("extra: ");
227 // for (Map.Entry<String, String> e : cset.extras().entrySet()) {
228 // sb.append(' ');
229 // sb.append(e.getKey());
230 // sb.append('=');
231 // sb.append(e.getValue());
232 // }
233 // sb.append('\n');
234 // }
235 }
236 if (complete || verbose) {
237 f.format("description:\n%s\n\n", cset.getComment());
238 } else {
239 f.format("summary: %s\n\n", cset.getComment());
240 }
241 return sb.toString();
242 }
243 } 121 }
244 } 122 }