Mercurial > hg4j
comparison src/org/tmatesoft/hg/repo/HgRepository.java @ 142:37a34044e6bd
More reasonable use of path normalizer and path.source
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Thu, 17 Feb 2011 05:06:07 +0100 |
| parents | 8248aae33f7d |
| children | acc6151b1b7a |
comparison
equal
deleted
inserted
replaced
| 141:8248aae33f7d | 142:37a34044e6bd |
|---|---|
| 55 } | 55 } |
| 56 | 56 |
| 57 private final File repoDir; // .hg folder | 57 private final File repoDir; // .hg folder |
| 58 private final String repoLocation; | 58 private final String repoLocation; |
| 59 private final DataAccessProvider dataAccess; | 59 private final DataAccessProvider dataAccess; |
| 60 private final PathRewrite normalizePath = new PathRewrite() { | 60 private final PathRewrite normalizePath; |
| 61 | |
| 62 public String rewrite(String path) { | |
| 63 // TODO handle . and .. (although unlikely to face them from GUI client) | |
| 64 path = path.replace('\\', '/').replace("//", "/"); | |
| 65 if (path.startsWith("/")) { | |
| 66 path = path.substring(1); | |
| 67 } | |
| 68 return path; | |
| 69 } | |
| 70 }; | |
| 71 private final PathRewrite dataPathHelper; | 61 private final PathRewrite dataPathHelper; |
| 72 private final PathRewrite repoPathHelper; | 62 private final PathRewrite repoPathHelper; |
| 73 | 63 |
| 74 private HgChangelog changelog; | 64 private HgChangelog changelog; |
| 75 private HgManifest manifest; | 65 private HgManifest manifest; |
| 84 HgRepository(String repositoryPath) { | 74 HgRepository(String repositoryPath) { |
| 85 repoDir = null; | 75 repoDir = null; |
| 86 repoLocation = repositoryPath; | 76 repoLocation = repositoryPath; |
| 87 dataAccess = null; | 77 dataAccess = null; |
| 88 dataPathHelper = repoPathHelper = null; | 78 dataPathHelper = repoPathHelper = null; |
| 79 normalizePath = null; | |
| 89 } | 80 } |
| 90 | 81 |
| 91 HgRepository(File repositoryRoot) throws IOException { | 82 HgRepository(File repositoryRoot) throws IOException { |
| 92 assert ".hg".equals(repositoryRoot.getName()) && repositoryRoot.isDirectory(); | 83 assert ".hg".equals(repositoryRoot.getName()) && repositoryRoot.isDirectory(); |
| 93 repoDir = repositoryRoot; | 84 repoDir = repositoryRoot; |
| 94 repoLocation = repositoryRoot.getParentFile().getCanonicalPath(); | 85 repoLocation = repositoryRoot.getParentFile().getCanonicalPath(); |
| 95 dataAccess = new DataAccessProvider(); | 86 dataAccess = new DataAccessProvider(); |
| 87 final boolean runningOnWindows = System.getProperty("os.name").indexOf("Windows") != -1; | |
| 88 if (runningOnWindows) { | |
| 89 normalizePath = new PathRewrite() { | |
| 90 | |
| 91 public String rewrite(String path) { | |
| 92 // TODO handle . and .. (although unlikely to face them from GUI client) | |
| 93 path = path.replace('\\', '/').replace("//", "/"); | |
| 94 if (path.startsWith("/")) { | |
| 95 path = path.substring(1); | |
| 96 } | |
| 97 return path; | |
| 98 } | |
| 99 }; | |
| 100 } else { | |
| 101 normalizePath = new PathRewrite.Empty(); // or strip leading slash, perhaps? | |
| 102 } | |
| 96 parseRequires(); | 103 parseRequires(); |
| 97 dataPathHelper = impl.buildDataFilesHelper(); | 104 dataPathHelper = impl.buildDataFilesHelper(); |
| 98 repoPathHelper = impl.buildRepositoryFilesHelper(); | 105 repoPathHelper = impl.buildRepositoryFilesHelper(); |
| 99 } | 106 } |
| 100 | 107 |
| 156 return new HgDataFile(this, path); | 163 return new HgDataFile(this, path); |
| 157 } | 164 } |
| 158 return new HgDataFile(this, path, content); | 165 return new HgDataFile(this, path, content); |
| 159 } | 166 } |
| 160 | 167 |
| 161 public PathRewrite getPathHelper() { // Really need to be public? | 168 /* clients need to rewrite path from their FS to a repository-friendly paths, and, perhaps, vice versa*/ |
| 169 public PathRewrite getToRepoPathHelper() { | |
| 162 return normalizePath; | 170 return normalizePath; |
| 163 } | 171 } |
| 164 | 172 |
| 165 // local to hide use of io.File. | 173 // local to hide use of io.File. |
| 166 /*package-local*/ File getRepositoryRoot() { | 174 /*package-local*/ File getRepositoryRoot() { |
| 192 } | 200 } |
| 193 | 201 |
| 194 // FIXME not sure repository shall create walkers | 202 // FIXME not sure repository shall create walkers |
| 195 /*package-local*/ FileIterator createWorkingDirWalker() { | 203 /*package-local*/ FileIterator createWorkingDirWalker() { |
| 196 File repoRoot = repoDir.getParentFile(); | 204 File repoRoot = repoDir.getParentFile(); |
| 197 Path.Source pathSrc = new Path.SimpleSource(new PathRewrite.Composite(new RelativePathRewrite(repoRoot), getPathHelper())); | 205 Path.Source pathSrc = new Path.SimpleSource(new PathRewrite.Composite(new RelativePathRewrite(repoRoot), getToRepoPathHelper())); |
| 198 // Impl note: simple source is enough as files in the working dir are all unique | 206 // Impl note: simple source is enough as files in the working dir are all unique |
| 199 // even if they might get reused (i.e. after FileIterator#reset() and walking once again), | 207 // even if they might get reused (i.e. after FileIterator#reset() and walking once again), |
| 200 // path caching is better to be done in the code which knows that path are being reused | 208 // path caching is better to be done in the code which knows that path are being reused |
| 201 return new FileWalker(repoRoot, pathSrc); | 209 return new FileWalker(repoRoot, pathSrc); |
| 202 } | 210 } |
