comparison hg4j-cli/src/main/java/org/tmatesoft/hg/console/ChangesetDumpHandler.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
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 java.util.Formatter;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import org.tmatesoft.hg.core.HgChangeset;
24 import org.tmatesoft.hg.core.HgChangesetHandler;
25 import org.tmatesoft.hg.core.Nodeid;
26 import org.tmatesoft.hg.core.HgLogCommand.FileRevision;
27 import org.tmatesoft.hg.repo.HgRepository;
28 import org.tmatesoft.hg.util.Path;
29
30 /**
31 *
32 * @author Artem Tikhomirov
33 * @author TMate Software Ltd.
34 */
35 public class ChangesetDumpHandler implements HgChangesetHandler {
36 // params
37 private boolean complete = false; // roughly --debug
38 private boolean reverseOrder = false;
39 private boolean verbose = false; // roughly -v
40 // own
41 private LinkedList<String> l = new LinkedList<String>();
42 private final HgRepository repo;
43 private final int tip;
44
45 public ChangesetDumpHandler(HgRepository hgRepo) {
46 repo = hgRepo;
47 tip = hgRepo.getChangelog().getLastRevision();
48 }
49
50 public ChangesetDumpHandler complete(boolean b) {
51 complete = b;
52 return this;
53 }
54
55 public ChangesetDumpHandler reversed(boolean b) {
56 reverseOrder = b;
57 return this;
58 }
59
60 public ChangesetDumpHandler verbose(boolean b) {
61 verbose = b;
62 return this;
63 }
64
65 public void next(HgChangeset changeset) {
66 final String s = print(changeset);
67 if (reverseOrder) {
68 // XXX in fact, need to insert s into l according to changeset.getRevision()
69 // because when file history is being followed, revisions of the original file (with smaller revNumber)
70 // are reported *after* revisions of present file and with addFirst appear above them
71 l.addFirst(s);
72 } else {
73 System.out.print(s);
74 }
75 }
76
77 public void done() {
78 if (!reverseOrder) {
79 return;
80 }
81 for (String s : l) {
82 System.out.print(s);
83 }
84 l.clear();
85 }
86
87 private String print(HgChangeset cset) {
88 StringBuilder sb = new StringBuilder();
89 Formatter f = new Formatter(sb);
90 final Nodeid csetNodeid = cset.getNodeid();
91 f.format("changeset: %d:%s\n", cset.getRevision(), complete ? csetNodeid : csetNodeid.shortNotation());
92 if (cset.getRevision() == tip || repo.getTags().isTagged(csetNodeid)) {
93
94 sb.append("tag: ");
95 for (String t : repo.getTags().tags(csetNodeid)) {
96 sb.append(t);
97 sb.append(' ');
98 }
99 if (cset.getRevision() == tip) {
100 sb.append("tip");
101 }
102 sb.append('\n');
103 }
104 if (complete) {
105 Nodeid p1 = cset.getFirstParentRevision();
106 Nodeid p2 = cset.getSecondParentRevision();
107 int p1x = p1 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevision(p1);
108 int p2x = p2 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevision(p2);
109 int mx = repo.getManifest().getLocalRevision(cset.getManifestRevision());
110 f.format("parent: %d:%s\nparent: %d:%s\nmanifest: %d:%s\n", p1x, p1, p2x, p2, mx, cset.getManifestRevision());
111 }
112 f.format("user: %s\ndate: %s\n", cset.getUser(), cset.getDate().toString());
113 if (!complete && verbose) {
114 final List<Path> files = cset.getAffectedFiles();
115 sb.append("files: ");
116 for (Path s : files) {
117 sb.append(' ');
118 sb.append(s);
119 }
120 sb.append('\n');
121 }
122 if (complete) {
123 if (!cset.getModifiedFiles().isEmpty()) {
124 sb.append("files: ");
125 for (FileRevision s : cset.getModifiedFiles()) {
126 sb.append(' ');
127 sb.append(s.getPath());
128 }
129 sb.append('\n');
130 }
131 if (!cset.getAddedFiles().isEmpty()) {
132 sb.append("files+: ");
133 for (FileRevision s : cset.getAddedFiles()) {
134 sb.append(' ');
135 sb.append(s.getPath());
136 }
137 sb.append('\n');
138 }
139 if (!cset.getRemovedFiles().isEmpty()) {
140 sb.append("files-: ");
141 for (Path s : cset.getRemovedFiles()) {
142 sb.append(' ');
143 sb.append(s);
144 }
145 sb.append('\n');
146 }
147 // if (cset.extras() != null) {
148 // sb.append("extra: ");
149 // for (Map.Entry<String, String> e : cset.extras().entrySet()) {
150 // sb.append(' ');
151 // sb.append(e.getKey());
152 // sb.append('=');
153 // sb.append(e.getValue());
154 // }
155 // sb.append('\n');
156 // }
157 }
158 if (complete || verbose) {
159 f.format("description:\n%s\n\n", cset.getComment());
160 } else {
161 f.format("summary: %s\n\n", cset.getComment());
162 }
163 return sb.toString();
164 }
165 }