comparison cmdline/org/tmatesoft/hg/console/Log.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/Log.java@e21df6259f83
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 java.util.Formatter;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import org.tmatesoft.hg.core.Cset;
24 import org.tmatesoft.hg.core.LogCommand;
25 import org.tmatesoft.hg.core.LogCommand.FileRevision;
26 import org.tmatesoft.hg.core.Path;
27
28 import com.tmate.hgkit.fs.RepositoryLookup;
29 import com.tmate.hgkit.ll.HgDataFile;
30 import com.tmate.hgkit.ll.HgRepository;
31 import com.tmate.hgkit.ll.Nodeid;
32 import com.tmate.hgkit.ll.Revlog;
33
34 /**
35 * @author Artem Tikhomirov
36 * @author TMate Software Ltd.
37 */
38 public class Log {
39
40 public static void main(String[] args) throws Exception {
41 RepositoryLookup repoLookup = new RepositoryLookup();
42 RepositoryLookup.Options cmdLineOpts = RepositoryLookup.Options.parse(args);
43 HgRepository hgRepo = repoLookup.detect(cmdLineOpts);
44 if (hgRepo.isInvalid()) {
45 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
46 return;
47 }
48 System.out.println(hgRepo.getLocation());
49 final Dump dump = new Dump(hgRepo);
50 dump.complete = true; //cmdLineOpts;
51 dump.verbose = false; //cmdLineOpts;
52 dump.reverseOrder = true;
53 LogCommand cmd = new LogCommand(hgRepo);
54 if (cmdLineOpts.users != null) {
55 for (String u : cmdLineOpts.users) {
56 cmd.user(u);
57 }
58 }
59 if (cmdLineOpts.branches != null) {
60 for (String b : cmdLineOpts.branches) {
61 cmd.branch(b);
62 }
63 }
64 if (cmdLineOpts.limit != -1) {
65 cmd.limit(cmdLineOpts.limit);
66
67 }
68 if (cmdLineOpts.files.isEmpty()) {
69 if (cmdLineOpts.limit == -1) {
70 // no revisions and no limit
71 cmd.execute(dump);
72 } else {
73 // in fact, external (to dump inspector) --limit processing yelds incorrect results when other args
74 // e.g. -u or -b are used (i.e. with -u shall give <limit> csets with user, not check last <limit> csets for user
75 int[] r = new int[] { 0, hgRepo.getChangelog().getRevisionCount() };
76 if (fixRange(r, dump.reverseOrder, cmdLineOpts.limit) == 0) {
77 System.out.println("No changes");
78 return;
79 }
80 cmd.range(r[0], r[1]).execute(dump);
81 }
82 dump.complete();
83 } else {
84 for (String fname : cmdLineOpts.files) {
85 HgDataFile f1 = hgRepo.getFileNode(fname);
86 System.out.println("History of the file: " + f1.getPath());
87 if (cmdLineOpts.limit == -1) {
88 cmd.file(Path.create(fname)).execute(dump);
89 } else {
90 int[] r = new int[] { 0, f1.getRevisionCount() };
91 if (fixRange(r, dump.reverseOrder, cmdLineOpts.limit) == 0) {
92 System.out.println("No changes");
93 continue;
94 }
95 cmd.range(r[0], r[1]).file(Path.create(fname)).execute(dump);
96 }
97 dump.complete();
98 }
99 }
100 //
101 // XXX new ChangelogWalker().setFile("hello.c").setRevisionRange(1, 4).accept(new Visitor);
102 }
103
104 private static int fixRange(int[] start_end, boolean reverse, int limit) {
105 assert start_end.length == 2;
106 if (limit < start_end[1]) {
107 if (reverse) {
108 // adjust left boundary of the range
109 start_end[0] = start_end[1] - limit;
110 } else {
111 start_end[1] = limit; // adjust right boundary
112 }
113 }
114 int rv = start_end[1] - start_end[0];
115 start_end[1]--; // range needs index, not length
116 return rv;
117 }
118
119 private static final class Dump implements LogCommand.Handler {
120 // params
121 boolean complete = false; // roughly --debug
122 boolean reverseOrder = false;
123 boolean verbose = true; // roughly -v
124 // own
125 private LinkedList<String> l = new LinkedList<String>();
126 private final HgRepository repo;
127 private Revlog.ParentWalker changelogWalker;
128 private final int tip ;
129
130 public Dump(HgRepository hgRepo) {
131 repo = hgRepo;
132 tip = hgRepo.getChangelog().getRevisionCount() - 1;
133 }
134
135 public void next(Cset changeset) {
136 final String s = print(changeset);
137 if (reverseOrder) {
138 l.addFirst(s);
139 } else {
140 System.out.print(s);
141 }
142 }
143
144 public void complete() {
145 if (!reverseOrder) {
146 return;
147 }
148 for (String s : l) {
149 System.out.print(s);
150 }
151 l.clear();
152 changelogWalker = null;
153 }
154
155 private String print(Cset cset) {
156 StringBuilder sb = new StringBuilder();
157 Formatter f = new Formatter(sb);
158 final Nodeid csetNodeid = cset.getNodeid();
159 f.format("changeset: %d:%s\n", cset.getRevision(), complete ? csetNodeid : csetNodeid.shortNotation());
160 if (cset.getRevision() == tip || repo.getTags().isTagged(csetNodeid)) {
161
162 sb.append("tag: ");
163 for (String t : repo.getTags().tags(csetNodeid)) {
164 sb.append(t);
165 sb.append(' ');
166 }
167 if (cset.getRevision() == tip) {
168 sb.append("tip");
169 }
170 sb.append('\n');
171 }
172 if (complete) {
173 if (changelogWalker == null) {
174 changelogWalker = repo.getChangelog().new ParentWalker();
175 changelogWalker.init();
176 }
177 Nodeid p1 = changelogWalker.safeFirstParent(csetNodeid);
178 Nodeid p2 = changelogWalker.safeSecondParent(csetNodeid);
179 int p1x = p1 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevisionNumber(p1);
180 int p2x = p2 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevisionNumber(p2);
181 int mx = repo.getManifest().getLocalRevisionNumber(cset.getManifestRevision());
182 f.format("parent: %d:%s\nparent: %d:%s\nmanifest: %d:%s\n", p1x, p1, p2x, p2, mx, cset.getManifestRevision());
183 }
184 f.format("user: %s\ndate: %s\n", cset.getUser(), cset.getDate());
185 if (!complete && verbose) {
186 final List<Path> files = cset.getAffectedFiles();
187 sb.append("files: ");
188 for (Path s : files) {
189 sb.append(' ');
190 sb.append(s);
191 }
192 sb.append('\n');
193 }
194 if (complete) {
195 if (!cset.getModifiedFiles().isEmpty()) {
196 sb.append("files: ");
197 for (FileRevision s : cset.getModifiedFiles()) {
198 sb.append(' ');
199 sb.append(s.getPath());
200 }
201 sb.append('\n');
202 }
203 if (!cset.getAddedFiles().isEmpty()) {
204 sb.append("files+: ");
205 for (FileRevision s : cset.getAddedFiles()) {
206 sb.append(' ');
207 sb.append(s.getPath());
208 }
209 sb.append('\n');
210 }
211 if (!cset.getRemovedFiles().isEmpty()) {
212 sb.append("files-: ");
213 for (Path s : cset.getRemovedFiles()) {
214 sb.append(' ');
215 sb.append(s);
216 }
217 sb.append('\n');
218 }
219 // if (cset.extras() != null) {
220 // sb.append("extra: ");
221 // for (Map.Entry<String, String> e : cset.extras().entrySet()) {
222 // sb.append(' ');
223 // sb.append(e.getKey());
224 // sb.append('=');
225 // sb.append(e.getValue());
226 // }
227 // sb.append('\n');
228 // }
229 }
230 if (complete || verbose) {
231 f.format("description:\n%s\n\n", cset.getComment());
232 } else {
233 f.format("summary: %s\n\n", cset.getComment());
234 }
235 return sb.toString();
236 }
237 }
238 }