comparison src/com/tmate/hgkit/ll/WorkingCopyStatusCollector.java @ 59:b771e94a4f7c

Introduce Internals to keep LocalHgRepo casts and alike in a single place. WCSC optionally to reuse SC data
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 18 Jan 2011 00:08:15 +0100
parents 4cfc47bc14cc
children a47530a2ea12
comparison
equal deleted inserted replaced
58:4cfc47bc14cc 59:b771e94a4f7c
21 */ 21 */
22 public class WorkingCopyStatusCollector { 22 public class WorkingCopyStatusCollector {
23 23
24 private final HgRepository repo; 24 private final HgRepository repo;
25 private final FileWalker repoWalker; 25 private final FileWalker repoWalker;
26 private HgDirstate dirstate;
27 private StatusCollector baseRevisionCollector;
26 28
27 public WorkingCopyStatusCollector(HgRepository hgRepo, FileWalker hgRepoWalker) { 29 public WorkingCopyStatusCollector(HgRepository hgRepo, FileWalker hgRepoWalker) {
28 this.repo = hgRepo; 30 this.repo = hgRepo;
29 this.repoWalker = hgRepoWalker; 31 this.repoWalker = hgRepoWalker;
30 } 32 }
31 33
34 /**
35 * Optionally, supply a collector instance that may cache (or have already cached) base revision
36 * @param sc may be null
37 */
38 public void setBseRevisionCollector(StatusCollector sc) {
39 baseRevisionCollector = sc;
40 }
41
42 private HgDirstate getDirstate() {
43 if (dirstate == null) {
44 if (repo instanceof LocalHgRepo) {
45 dirstate = ((LocalHgRepo) repo).loadDirstate();
46 } else {
47 dirstate = new HgDirstate();
48 }
49 }
50 return dirstate;
51 }
52
53 // may be invoked few times
32 public void walk(int baseRevision, StatusCollector.Inspector inspector) { 54 public void walk(int baseRevision, StatusCollector.Inspector inspector) {
33 final HgIgnore hgIgnore = ((LocalHgRepo) repo).loadIgnore(); // FIXME hack 55 final HgIgnore hgIgnore = ((LocalHgRepo) repo).loadIgnore(); // FIXME hack
34 final HgDirstate dirstate = ((LocalHgRepo) repo).loadDirstate(); // FIXME hack 56 TreeSet<String> knownEntries = getDirstate().all();
35 TreeSet<String> knownEntries = dirstate.all();
36 final boolean isTipBase = baseRevision == TIP || baseRevision == repo.getManifest().getRevisionCount(); 57 final boolean isTipBase = baseRevision == TIP || baseRevision == repo.getManifest().getRevisionCount();
37 StatusCollector.ManifestRevisionInspector collect = null; 58 StatusCollector.ManifestRevisionInspector collect = null;
38 Set<String> baseRevFiles = Collections.emptySet(); 59 Set<String> baseRevFiles = Collections.emptySet();
39 if (!isTipBase) { 60 if (!isTipBase) {
40 collect = new StatusCollector.ManifestRevisionInspector(baseRevision, baseRevision); 61 if (baseRevisionCollector != null) {
41 repo.getManifest().walk(baseRevision, baseRevision, collect); 62 collect = baseRevisionCollector.raw(baseRevision);
63 } else {
64 collect = new StatusCollector.ManifestRevisionInspector(baseRevision, baseRevision);
65 repo.getManifest().walk(baseRevision, baseRevision, collect);
66 }
42 baseRevFiles = new TreeSet<String>(collect.files(baseRevision)); 67 baseRevFiles = new TreeSet<String>(collect.files(baseRevision));
43 } 68 }
44 repoWalker.reset(); 69 repoWalker.reset();
45 while (repoWalker.hasNext()) { 70 while (repoWalker.hasNext()) {
46 repoWalker.next(); 71 repoWalker.next();
51 } else if (knownEntries.remove(fname)) { 76 } else if (knownEntries.remove(fname)) {
52 // modified, added, removed, clean 77 // modified, added, removed, clean
53 if (collect != null) { // need to check against base revision, not FS file 78 if (collect != null) { // need to check against base revision, not FS file
54 Nodeid nid1 = collect.nodeid(baseRevision, fname); 79 Nodeid nid1 = collect.nodeid(baseRevision, fname);
55 String flags = collect.flags(baseRevision, fname); 80 String flags = collect.flags(baseRevision, fname);
56 checkLocalStatusAgainstBaseRevision(baseRevFiles, nid1, flags, fname, f, dirstate, inspector); 81 checkLocalStatusAgainstBaseRevision(baseRevFiles, nid1, flags, fname, f, inspector);
57 baseRevFiles.remove(fname); 82 baseRevFiles.remove(fname);
58 } else { 83 } else {
59 checkLocalStatusAgainstFile(fname, f, dirstate, inspector); 84 checkLocalStatusAgainstFile(fname, f, inspector);
60 } 85 }
61 } else { 86 } else {
62 inspector.unknown(fname); 87 inspector.unknown(fname);
63 } 88 }
64 } 89 }
67 inspector.removed(r); 92 inspector.removed(r);
68 } 93 }
69 } 94 }
70 for (String m : knownEntries) { 95 for (String m : knownEntries) {
71 // removed from the repository and missing from working dir shall not be reported as 'deleted' 96 // removed from the repository and missing from working dir shall not be reported as 'deleted'
72 if (dirstate.checkRemoved(m) == null) { 97 if (getDirstate().checkRemoved(m) == null) {
73 inspector.missing(m); 98 inspector.missing(m);
74 } 99 }
75 } 100 }
76 } 101 }
77 102
82 } 107 }
83 108
84 //******************************************** 109 //********************************************
85 110
86 111
87 private static void checkLocalStatusAgainstFile(String fname, File f, HgDirstate dirstate, StatusCollector.Inspector inspector) { 112 private void checkLocalStatusAgainstFile(String fname, File f, StatusCollector.Inspector inspector) {
88 HgDirstate.Record r; 113 HgDirstate.Record r;
89 if ((r = dirstate.checkNormal(fname)) != null) { 114 if ((r = getDirstate().checkNormal(fname)) != null) {
90 // either clean or modified 115 // either clean or modified
91 if (f.lastModified() / 1000 == r.time && r.size == f.length()) { 116 if (f.lastModified() / 1000 == r.time && r.size == f.length()) {
92 inspector.clean(fname); 117 inspector.clean(fname);
93 } else { 118 } else {
94 // FIXME check actual content to avoid false modified files 119 // FIXME check actual content to avoid false modified files
95 inspector.modified(fname); 120 inspector.modified(fname);
96 } 121 }
97 } else if ((r = dirstate.checkAdded(fname)) != null) { 122 } else if ((r = getDirstate().checkAdded(fname)) != null) {
98 if (r.name2 == null) { 123 if (r.name2 == null) {
99 inspector.added(fname); 124 inspector.added(fname);
100 } else { 125 } else {
101 inspector.copied(fname, r.name2); 126 inspector.copied(fname, r.name2);
102 } 127 }
103 } else if ((r = dirstate.checkRemoved(fname)) != null) { 128 } else if ((r = getDirstate().checkRemoved(fname)) != null) {
104 inspector.removed(fname); 129 inspector.removed(fname);
105 } else if ((r = dirstate.checkMerged(fname)) != null) { 130 } else if ((r = getDirstate().checkMerged(fname)) != null) {
106 inspector.modified(fname); 131 inspector.modified(fname);
107 } 132 }
108 } 133 }
109 134
110 // XXX refactor checkLocalStatus methods in more OO way 135 // XXX refactor checkLocalStatus methods in more OO way
111 private void checkLocalStatusAgainstBaseRevision(Set<String> baseRevNames, Nodeid nid1, String flags, String fname, File f, HgDirstate dirstate, StatusCollector.Inspector inspector) { 136 private void checkLocalStatusAgainstBaseRevision(Set<String> baseRevNames, Nodeid nid1, String flags, String fname, File f, StatusCollector.Inspector inspector) {
112 // fname is in the dirstate, either Normal, Added, Removed or Merged 137 // fname is in the dirstate, either Normal, Added, Removed or Merged
113 HgDirstate.Record r; 138 HgDirstate.Record r;
114 if (nid1 == null) { 139 if (nid1 == null) {
115 // normal: added? 140 // normal: added?
116 // added: not known at the time of baseRevision, shall report 141 // added: not known at the time of baseRevision, shall report
117 // merged: was not known, report as added? 142 // merged: was not known, report as added?
118 if ((r = dirstate.checkAdded(fname)) != null) { 143 if ((r = getDirstate().checkAdded(fname)) != null) {
119 if (r.name2 != null && baseRevNames.contains(r.name2)) { 144 if (r.name2 != null && baseRevNames.contains(r.name2)) {
120 baseRevNames.remove(r.name2); 145 baseRevNames.remove(r.name2);
121 inspector.copied(r.name2, fname); 146 inspector.copied(r.name2, fname);
122 return; 147 return;
123 } 148 }
124 // fall-through, report as added 149 // fall-through, report as added
125 } else if (dirstate.checkRemoved(fname) != null) { 150 } else if (getDirstate().checkRemoved(fname) != null) {
126 // removed: removed file was not known at the time of baseRevision, and we should not report it as removed 151 // removed: removed file was not known at the time of baseRevision, and we should not report it as removed
127 return; 152 return;
128 } 153 }
129 inspector.added(fname); 154 inspector.added(fname);
130 } else { 155 } else {
131 // was known; check whether clean or modified 156 // was known; check whether clean or modified
132 // when added - seems to be the case of a file added once again, hence need to check if content is different 157 // when added - seems to be the case of a file added once again, hence need to check if content is different
133 if ((r = dirstate.checkNormal(fname)) != null || (r = dirstate.checkMerged(fname)) != null || (r = dirstate.checkAdded(fname)) != null) { 158 if ((r = getDirstate().checkNormal(fname)) != null || (r = getDirstate().checkMerged(fname)) != null || (r = getDirstate().checkAdded(fname)) != null) {
134 // either clean or modified 159 // either clean or modified
135 HgDataFile fileNode = repo.getFileNode(fname); 160 HgDataFile fileNode = repo.getFileNode(fname);
136 final int lengthAtRevision = fileNode.length(nid1); 161 final int lengthAtRevision = fileNode.length(nid1);
137 if (r.size /* XXX File.length() ?! */ != lengthAtRevision || flags != todoGenerateFlags(fname /*java.io.File*/)) { 162 if (r.size /* XXX File.length() ?! */ != lengthAtRevision || flags != todoGenerateFlags(fname /*java.io.File*/)) {
138 inspector.modified(fname); 163 inspector.modified(fname);