comparison src/com/tmate/hgkit/ll/LocalHgRepo.java @ 22:603806cd2dc6

Status of local working dir against non-tip base revision
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 06 Jan 2011 03:30:20 +0100
parents e929cecae4e1
children d4fdd1845b3f
comparison
equal deleted inserted replaced
21:e929cecae4e1 22:603806cd2dc6
1 /* 1 /*
2 * Copyright (c) 2010, 2011 Artem Tikhomirov 2 * Copyright (c) 2010, 2011 Artem Tikhomirov
3 */ 3 */
4 package com.tmate.hgkit.ll; 4 package com.tmate.hgkit.ll;
5 5
6 import java.io.BufferedInputStream;
6 import java.io.BufferedReader; 7 import java.io.BufferedReader;
7 import java.io.File; 8 import java.io.File;
8 import java.io.FileInputStream; 9 import java.io.FileInputStream;
9 import java.io.IOException; 10 import java.io.IOException;
10 import java.io.InputStreamReader; 11 import java.io.InputStreamReader;
89 final File rootDir = repoDir.getParentFile(); 90 final File rootDir = repoDir.getParentFile();
90 folders.add(rootDir); 91 folders.add(rootDir);
91 final HgDirstate dirstate = loadDirstate(); 92 final HgDirstate dirstate = loadDirstate();
92 final HgIgnore hgignore = loadIgnore(); 93 final HgIgnore hgignore = loadIgnore();
93 TreeSet<String> knownEntries = dirstate.all(); 94 TreeSet<String> knownEntries = dirstate.all();
94 final boolean isTipBase = baseRevision == TIP || baseRevision == getManifest().revisionCount(); 95 final boolean isTipBase = baseRevision == TIP || baseRevision == getManifest().getRevisionCount();
95 final ManifestRevisionCollector collect = isTipBase ? null : new ManifestRevisionCollector(); 96 final ManifestRevisionCollector collect = isTipBase ? null : new ManifestRevisionCollector();
96 if (!isTipBase) { 97 if (!isTipBase) {
97 getManifest().walk(baseRevision, baseRevision, collect); 98 getManifest().walk(baseRevision, baseRevision, collect);
98 } 99 }
99 do { 100 do {
102 if (f.isDirectory()) { 103 if (f.isDirectory()) {
103 if (!".hg".equals(f.getName())) { 104 if (!".hg".equals(f.getName())) {
104 folders.addLast(f); 105 folders.addLast(f);
105 } 106 }
106 } else { 107 } else {
107 // FIXME path relative to rootDir 108 // FIXME path relative to rootDir - need more robust approach
108 String fname = normalize(f.getPath().substring(rootDir.getPath().length() + 1)); 109 String fname = normalize(f.getPath().substring(rootDir.getPath().length() + 1));
109 if (hgignore.isIgnored(fname)) { 110 if (hgignore.isIgnored(fname)) {
110 inspector.ignored(fname); 111 inspector.ignored(fname);
111 } else { 112 } else {
112 if (knownEntries.remove(fname)) { 113 if (knownEntries.remove(fname)) {
113 // modified, added, removed, clean 114 // modified, added, removed, clean
114 HgDirstate.Record r; 115 if (collect != null) { // need to check against base revision, not FS file
115 if ((r = dirstate.checkNormal(fname)) != null) { 116 checkLocalStatusAgainstBaseRevision(collect, fname, f, dirstate, inspector);
116 // either clean or modified 117 } else {
117 if (f.lastModified() / 1000 == r.time && r.size == f.length()) { 118 checkLocalStatusAgainstFile(fname, f, dirstate, inspector);
118 inspector.clean(fname);
119 } else {
120 // FIXME check actual content to avoid false modified files
121 inspector.modified(fname);
122 }
123 } else if ((r = dirstate.checkAdded(fname)) != null) {
124 if (r.name2 == null) {
125 inspector.added(fname);
126 } else {
127 inspector.copied(fname, r.name2);
128 }
129 } else if ((r = dirstate.checkRemoved(fname)) != null) {
130 inspector.removed(fname);
131 } else if ((r = dirstate.checkMerged(fname)) != null) {
132 inspector.modified(fname);
133 } 119 }
134 } else { 120 } else {
135 inspector.unknown(fname); 121 inspector.unknown(fname);
136 } 122 }
137 } 123 }
138 } 124 }
139 } 125 }
140 } while (!folders.isEmpty()); 126 } while (!folders.isEmpty());
127 if (collect != null) {
128 for (String r : collect.idsMap.keySet()) {
129 inspector.removed(r);
130 }
131 }
141 for (String m : knownEntries) { 132 for (String m : knownEntries) {
142 inspector.missing(m); 133 // removed from the repository and missing from working dir shall not be reported as 'deleted'
143 } 134 if (dirstate.checkRemoved(m) == null) {
144 } 135 inspector.missing(m);
145 136 }
137 }
138 }
139
140 private static void checkLocalStatusAgainstFile(String fname, File f, HgDirstate dirstate, StatusInspector inspector) {
141 HgDirstate.Record r;
142 if ((r = dirstate.checkNormal(fname)) != null) {
143 // either clean or modified
144 if (f.lastModified() / 1000 == r.time && r.size == f.length()) {
145 inspector.clean(fname);
146 } else {
147 // FIXME check actual content to avoid false modified files
148 inspector.modified(fname);
149 }
150 } else if ((r = dirstate.checkAdded(fname)) != null) {
151 if (r.name2 == null) {
152 inspector.added(fname);
153 } else {
154 inspector.copied(fname, r.name2);
155 }
156 } else if ((r = dirstate.checkRemoved(fname)) != null) {
157 inspector.removed(fname);
158 } else if ((r = dirstate.checkMerged(fname)) != null) {
159 inspector.modified(fname);
160 }
161 }
162
163 // XXX refactor checkLocalStatus methods in more OO way
164 private void checkLocalStatusAgainstBaseRevision(ManifestRevisionCollector collect, String fname, File f, HgDirstate dirstate, StatusInspector inspector) {
165 // fname is in the dirstate, either Normal, Added, Removed or Merged
166 Nodeid nid1 = collect.idsMap.remove(fname);
167 String flags = collect.flagsMap.remove(fname);
168 HgDirstate.Record r;
169 if (nid1 == null) {
170 // normal: added?
171 // added: not known at the time of baseRevision, shall report
172 // merged: was not known, report as added?
173 if ((r = dirstate.checkAdded(fname)) != null) {
174 if (r.name2 != null && collect.idsMap.containsKey(r.name2)) {
175 collect.idsMap.remove(r.name2);
176 collect.idsMap.remove(r.name2);
177 inspector.copied(r.name2, fname);
178 return;
179 }
180 // fall-through, report as added
181 } else if (dirstate.checkRemoved(fname) != null) {
182 // removed: removed file was not known at the time of baseRevision, and we should not report it as removed
183 return;
184 }
185 inspector.added(fname);
186 } else {
187 // was known; check whether clean or modified
188 // when added - seems to be the case of a file added once again, hence need to check if content is different
189 if ((r = dirstate.checkNormal(fname)) != null || (r = dirstate.checkMerged(fname)) != null || (r = dirstate.checkAdded(fname)) != null) {
190 // either clean or modified
191 HgDataFile fileNode = getFileNode(fname);
192 final int lengthAtRevision = fileNode.length(nid1);
193 if (r.size /* XXX File.length() ?! */ != lengthAtRevision || flags != todoGenerateFlags(fname /*java.io.File*/)) {
194 inspector.modified(fname);
195 } else {
196 // check actual content to see actual changes
197 // XXX consider adding HgDataDile.compare(File/byte[]/whatever) operation to optimize comparison
198 if (areTheSame(f, fileNode.content(nid1))) {
199 inspector.clean(fname);
200 } else {
201 inspector.modified(fname);
202 }
203 }
204 }
205 // only those left in idsMap after processing are reported as removed
206 }
207
208 // TODO think over if content comparison may be done more effectively by e.g. calculating nodeid for a local file and comparing it with nodeid from manifest
209 // we don't need to tell exact difference, hash should be enough to detect difference, and it doesn't involve reading historical file content, and it's relatively
210 // cheap to calc hash on a file (no need to keep it completely in memory). OTOH, if I'm right that the next approach is used for nodeids:
211 // changeset nodeid + hash(actual content) => entry (Nodeid) in the next Manifest
212 // then it's sufficient to check parents from dirstate, and if they do not match parents from file's baseRevision (non matching parents means different nodeids).
213 // The question is whether original Hg treats this case (same content, different parents and hence nodeids) as 'modified' or 'clean'
214 }
215
216 private static String todoGenerateFlags(String fname) {
217 // FIXME implement
218 return null;
219 }
220 private static boolean areTheSame(File f, byte[] data) {
221 try {
222 BufferedInputStream is = new BufferedInputStream(new FileInputStream(f));
223 int i = 0;
224 while (i < data.length && data[i] == is.read()) {
225 i++; // increment only for successful match, otherwise won't tell last byte in data was the same as read from the stream
226 }
227 return i == data.length && is.read() == -1; // although data length is expected to be the same (see caller), check that we reached EOF, no more data left.
228 } catch (IOException ex) {
229 ex.printStackTrace(); // log warn
230 }
231 return false;
232 }
233
146 // XXX package-local, unless there are cases when required from outside (guess, working dir/revision walkers may hide dirstate access and no public visibility needed) 234 // XXX package-local, unless there are cases when required from outside (guess, working dir/revision walkers may hide dirstate access and no public visibility needed)
147 public final HgDirstate loadDirstate() { 235 public final HgDirstate loadDirstate() {
148 // XXX may cache in SoftReference if creation is expensive 236 // XXX may cache in SoftReference if creation is expensive
149 return new HgDirstate(this, new File(repoDir, "dirstate")); 237 return new HgDirstate(this, new File(repoDir, "dirstate"));
150 } 238 }
322 path = path.substring(1); 410 path = path.substring(1);
323 } 411 }
324 return path; 412 return path;
325 } 413 }
326 414
327 private final class ManifestRevisionCollector implements HgManifest.Inspector { 415 // XXX idsMap is being modified from outside. It's better to let outer (modifying) code to create these maps instead
416 private static final class ManifestRevisionCollector implements HgManifest.Inspector {
328 final HashMap<String, Nodeid> idsMap = new HashMap<String, Nodeid>(); 417 final HashMap<String, Nodeid> idsMap = new HashMap<String, Nodeid>();
329 final HashMap<String, String> flagsMap = new HashMap<String, String>(); 418 final HashMap<String, String> flagsMap = new HashMap<String, String>();
330 419
331 public boolean next(Nodeid nid, String fname, String flags) { 420 public boolean next(Nodeid nid, String fname, String flags) {
332 idsMap.put(fname, nid); 421 idsMap.put(fname, nid);