comparison src/com/tmate/hgkit/console/Log.java @ 49:26e3eeaa3962

branch and user filtering for log operation
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sat, 15 Jan 2011 01:15:38 +0100
parents e34f90b9ded1
children f1db8610da62
comparison
equal deleted inserted replaced
48:e34f90b9ded1 49:26e3eeaa3962
2 * Copyright (c) 2010, 2011 Artem Tikhomirov 2 * Copyright (c) 2010, 2011 Artem Tikhomirov
3 */ 3 */
4 package com.tmate.hgkit.console; 4 package com.tmate.hgkit.console;
5 5
6 import java.util.Formatter; 6 import java.util.Formatter;
7 import java.util.LinkedHashSet;
7 import java.util.LinkedList; 8 import java.util.LinkedList;
8 import java.util.List; 9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
9 12
10 import com.tmate.hgkit.fs.RepositoryLookup; 13 import com.tmate.hgkit.fs.RepositoryLookup;
11 import com.tmate.hgkit.ll.Changeset; 14 import com.tmate.hgkit.ll.Changeset;
12 import com.tmate.hgkit.ll.HgDataFile; 15 import com.tmate.hgkit.ll.HgDataFile;
13 import com.tmate.hgkit.ll.HgRepository; 16 import com.tmate.hgkit.ll.HgRepository;
14 import com.tmate.hgkit.ll.Nodeid; 17 import com.tmate.hgkit.ll.Nodeid;
18 import com.tmate.hgkit.ll.Revlog;
15 19
16 /** 20 /**
17 * @author artem 21 * @author artem
18 */ 22 */
19 public class Log { 23 public class Log {
26 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); 30 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
27 return; 31 return;
28 } 32 }
29 System.out.println(hgRepo.getLocation()); 33 System.out.println(hgRepo.getLocation());
30 final Dump dump = new Dump(hgRepo); 34 final Dump dump = new Dump(hgRepo);
31 dump.complete = false; //cmdLineOpts; 35 dump.complete = true; //cmdLineOpts;
32 dump.reverseOrder = true; 36 dump.reverseOrder = true;
37 dump.branches = cmdLineOpts.branches;
38 if (cmdLineOpts.users != null) {
39 dump.users = new LinkedHashSet<String>();
40 for (String u : cmdLineOpts.users) {
41 dump.users.add(u.toLowerCase());
42 }
43 }
33 if (cmdLineOpts.files.isEmpty()) { 44 if (cmdLineOpts.files.isEmpty()) {
34 // no revisions and no limit
35 if (cmdLineOpts.limit == -1) { 45 if (cmdLineOpts.limit == -1) {
46 // no revisions and no limit
36 hgRepo.getChangelog().all(dump); 47 hgRepo.getChangelog().all(dump);
37 } else { 48 } else {
49 // in fact, external (to dump inspector) --limit processing yelds incorrect results when other args
50 // e.g. -u or -b are used (i.e. with -u shall give <limit> csets with user, not check last <limit> csets for user
38 int[] r = new int[] { 0, hgRepo.getChangelog().getRevisionCount() }; 51 int[] r = new int[] { 0, hgRepo.getChangelog().getRevisionCount() };
39 if (fixRange(r, dump.reverseOrder, cmdLineOpts.limit) == 0) { 52 if (fixRange(r, dump.reverseOrder, cmdLineOpts.limit) == 0) {
40 System.out.println("No changes"); 53 System.out.println("No changes");
41 return; 54 return;
42 } 55 }
59 } 72 }
60 dump.complete(); 73 dump.complete();
61 } 74 }
62 } 75 }
63 // 76 //
64 // System.out.println("\n\n========================="); 77 // XXX new ChangelogWalker().setFile("hello.c").setRevisionRange(1, 4).accept(new Visitor);
65 // System.out.println("Range 1-3:");
66 // f1.history(1,3, callback);
67 //
68 //new ChangelogWalker().setFile("hello.c").setRevisionRange(1, 4).accept(new Visitor);
69 } 78 }
70 79
71 private static int fixRange(int[] start_end, boolean reverse, int limit) { 80 private static int fixRange(int[] start_end, boolean reverse, int limit) {
72 assert start_end.length == 2; 81 assert start_end.length == 2;
73 if (limit < start_end[1]) { 82 if (limit < start_end[1]) {
81 int rv = start_end[1] - start_end[0]; 90 int rv = start_end[1] - start_end[0];
82 start_end[1]--; // range needs index, not length 91 start_end[1]--; // range needs index, not length
83 return rv; 92 return rv;
84 } 93 }
85 94
95 // Differences with standard hg log output
96 // - complete == true (--debug) files are not broke down to modified,+ and -
86 private static final class Dump implements Changeset.Inspector { 97 private static final class Dump implements Changeset.Inspector {
87 // params 98 // params
88 boolean complete = false; 99 boolean complete = false;
89 boolean reverseOrder = false; 100 boolean reverseOrder = false;
101 Set<String> branches;
102 Set<String> users; // shall be lowercased
90 // own 103 // own
91 private LinkedList<String> l = new LinkedList<String>(); 104 private LinkedList<String> l = new LinkedList<String>();
92 private final HgRepository repo; 105 private final HgRepository repo;
106 private Revlog.ParentWalker changelogWalker;
107 private final int tip ;
93 108
94 public Dump(HgRepository hgRepo) { 109 public Dump(HgRepository hgRepo) {
95 this.repo = hgRepo; 110 repo = hgRepo;
111 tip = hgRepo.getChangelog().getRevisionCount() - 1;
96 } 112 }
97 113
98 public void next(int revisionNumber, Nodeid nodeid, Changeset cset) { 114 public void next(int revisionNumber, Nodeid nodeid, Changeset cset) {
115 if (branches != null && !branches.contains(cset.branch())) {
116 return;
117 }
118 if (users != null) {
119 String csetUser = cset.user().toLowerCase();
120 boolean found = false;
121 for (String u : users) {
122 if (csetUser.indexOf(u) != -1) {
123 found = true;
124 break;
125 }
126 }
127 if (!found) {
128 return;
129 }
130 }
99 final String s = print(revisionNumber, nodeid, cset); 131 final String s = print(revisionNumber, nodeid, cset);
100 if (reverseOrder) { 132 if (reverseOrder) {
101 l.addFirst(s); 133 l.addFirst(s);
102 } else { 134 } else {
103 System.out.print(s); 135 System.out.print(s);
110 } 142 }
111 for (String s : l) { 143 for (String s : l) {
112 System.out.print(s); 144 System.out.print(s);
113 } 145 }
114 l.clear(); 146 l.clear();
147 changelogWalker = null;
115 } 148 }
116 149
117 private String print(int revNumber, Nodeid csetNodeid, Changeset cset) { 150 private String print(int revNumber, Nodeid csetNodeid, Changeset cset) {
118 StringBuilder sb = new StringBuilder(); 151 StringBuilder sb = new StringBuilder();
119 Formatter f = new Formatter(sb); 152 Formatter f = new Formatter(sb);
120 f.format("changeset: %d:%s\n", revNumber, complete ? csetNodeid : csetNodeid.shortNotation()); 153 f.format("changeset: %d:%s\n", revNumber, complete ? csetNodeid : csetNodeid.shortNotation());
154 if (revNumber == tip) {
155 sb.append("tag: tip\n");
156 }
121 if (complete) { 157 if (complete) {
122 f.format("parent: %s\nparent: %s\nmanifest: %s\n", "-1", "-1", cset.manifest()); 158 if (changelogWalker == null) {
159 changelogWalker = repo.getChangelog().new ParentWalker();
160 changelogWalker.init();
161 }
162 Nodeid p1 = changelogWalker.safeFirstParent(csetNodeid);
163 Nodeid p2 = changelogWalker.safeSecondParent(csetNodeid);
164 int p1x = p1 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevisionNumber(p1);
165 int p2x = p2 == Nodeid.NULL ? -1 : repo.getChangelog().getLocalRevisionNumber(p2);
166 int mx = repo.getManifest().getLocalRevisionNumber(cset.manifest());
167 f.format("parent: %d:%s\nparent: %d:%s\nmanifest: %d:%s\n", p1x, p1, p2x, p2, mx, cset.manifest());
123 } 168 }
124 f.format("user: %s\ndate: %s\n", cset.user(), cset.dateString()); 169 f.format("user: %s\ndate: %s\n", cset.user(), cset.dateString());
125 if (complete) { 170 if (complete) {
126 final List<String> files = cset.files(); 171 final List<String> files = cset.files();
127 sb.append("files: "); 172 sb.append("files: ");
128 for (String s : files) { 173 for (String s : files) {
129 sb.append(' '); 174 sb.append(' ');
130 sb.append(s); 175 sb.append(s);
131 } 176 }
177 if (cset.extras() != null) {
178 sb.append("\nextra: ");
179 for (Map.Entry<String, String> e : cset.extras().entrySet()) {
180 sb.append(' ');
181 sb.append(e.getKey());
182 sb.append('=');
183 sb.append(e.getValue());
184 }
185 }
132 f.format("\ndescription:\n%s\n\n", cset.comment()); 186 f.format("\ndescription:\n%s\n\n", cset.comment());
133 } else { 187 } else {
134 f.format("summary: %s\n\n", cset.comment()); 188 f.format("summary: %s\n\n", cset.comment());
135 }
136 if (cset.extras() != null) {
137 f.format("extra: " + cset.extras()); // TODO
138 } 189 }
139 return sb.toString(); 190 return sb.toString();
140 } 191 }
141 } 192 }
142 } 193 }