Mercurial > jhg
comparison src/org/tmatesoft/hg/repo/HgRepository.java @ 295:981f9f50bb6c
Issue 11: Error log facility. SessionContext to share common facilities
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 16 Sep 2011 05:35:32 +0200 |
parents | a415fe296a50 |
children | c3d2233ba842 |
comparison
equal
deleted
inserted
replaced
294:32890bab7209 | 295:981f9f50bb6c |
---|---|
25 import java.util.HashMap; | 25 import java.util.HashMap; |
26 import java.util.List; | 26 import java.util.List; |
27 | 27 |
28 import org.tmatesoft.hg.core.HgDataStreamException; | 28 import org.tmatesoft.hg.core.HgDataStreamException; |
29 import org.tmatesoft.hg.core.Nodeid; | 29 import org.tmatesoft.hg.core.Nodeid; |
30 import org.tmatesoft.hg.core.SessionContext; | |
30 import org.tmatesoft.hg.internal.ByteArrayChannel; | 31 import org.tmatesoft.hg.internal.ByteArrayChannel; |
31 import org.tmatesoft.hg.internal.ConfigFile; | 32 import org.tmatesoft.hg.internal.ConfigFile; |
32 import org.tmatesoft.hg.internal.DataAccessProvider; | 33 import org.tmatesoft.hg.internal.DataAccessProvider; |
33 import org.tmatesoft.hg.internal.Experimental; | 34 import org.tmatesoft.hg.internal.Experimental; |
34 import org.tmatesoft.hg.internal.Filter; | 35 import org.tmatesoft.hg.internal.Filter; |
35 import org.tmatesoft.hg.internal.Internals; | 36 import org.tmatesoft.hg.internal.Internals; |
36 import org.tmatesoft.hg.internal.RequiresFile; | |
37 import org.tmatesoft.hg.internal.RevlogStream; | 37 import org.tmatesoft.hg.internal.RevlogStream; |
38 import org.tmatesoft.hg.internal.SubrepoManager; | 38 import org.tmatesoft.hg.internal.SubrepoManager; |
39 import org.tmatesoft.hg.util.CancelledException; | 39 import org.tmatesoft.hg.util.CancelledException; |
40 import org.tmatesoft.hg.util.Pair; | 40 import org.tmatesoft.hg.util.Pair; |
41 import org.tmatesoft.hg.util.Path; | 41 import org.tmatesoft.hg.util.Path; |
71 private final DataAccessProvider dataAccess; | 71 private final DataAccessProvider dataAccess; |
72 private final PathRewrite normalizePath; | 72 private final PathRewrite normalizePath; |
73 private final PathRewrite dataPathHelper; | 73 private final PathRewrite dataPathHelper; |
74 private final PathRewrite repoPathHelper; | 74 private final PathRewrite repoPathHelper; |
75 private final boolean isCaseSensitiveFileSystem; | 75 private final boolean isCaseSensitiveFileSystem; |
76 private final SessionContext sessionContext; | |
76 | 77 |
77 private HgChangelog changelog; | 78 private HgChangelog changelog; |
78 private HgManifest manifest; | 79 private HgManifest manifest; |
79 private HgTags tags; | 80 private HgTags tags; |
80 private HgBranches branches; | 81 private HgBranches branches; |
93 workingDir = null; | 94 workingDir = null; |
94 repoLocation = repositoryPath; | 95 repoLocation = repositoryPath; |
95 dataAccess = null; | 96 dataAccess = null; |
96 dataPathHelper = repoPathHelper = null; | 97 dataPathHelper = repoPathHelper = null; |
97 normalizePath = null; | 98 normalizePath = null; |
99 sessionContext = null; | |
98 isCaseSensitiveFileSystem = !Internals.runningOnWindows(); | 100 isCaseSensitiveFileSystem = !Internals.runningOnWindows(); |
99 } | 101 } |
100 | 102 |
101 HgRepository(String repositoryPath, File repositoryRoot) { | 103 HgRepository(SessionContext ctx, String repositoryPath, File repositoryRoot) { |
102 assert ".hg".equals(repositoryRoot.getName()) && repositoryRoot.isDirectory(); | 104 assert ".hg".equals(repositoryRoot.getName()) && repositoryRoot.isDirectory(); |
103 assert repositoryPath != null; | 105 assert repositoryPath != null; |
104 assert repositoryRoot != null; | 106 assert repositoryRoot != null; |
107 assert ctx != null; | |
105 repoDir = repositoryRoot; | 108 repoDir = repositoryRoot; |
106 workingDir = repoDir.getParentFile(); | 109 workingDir = repoDir.getParentFile(); |
107 if (workingDir == null) { | 110 if (workingDir == null) { |
108 throw new IllegalArgumentException(repoDir.toString()); | 111 throw new IllegalArgumentException(repoDir.toString()); |
109 } | 112 } |
110 repoLocation = repositoryPath; | 113 repoLocation = repositoryPath; |
111 dataAccess = new DataAccessProvider(); | 114 sessionContext = ctx; |
115 dataAccess = new DataAccessProvider(ctx); | |
112 final boolean runningOnWindows = Internals.runningOnWindows(); | 116 final boolean runningOnWindows = Internals.runningOnWindows(); |
113 isCaseSensitiveFileSystem = !runningOnWindows; | 117 isCaseSensitiveFileSystem = !runningOnWindows; |
114 if (runningOnWindows) { | 118 if (runningOnWindows) { |
115 normalizePath = new PathRewrite() { | 119 normalizePath = new PathRewrite() { |
116 | 120 |
125 } | 129 } |
126 }; | 130 }; |
127 } else { | 131 } else { |
128 normalizePath = new PathRewrite.Empty(); // or strip leading slash, perhaps? | 132 normalizePath = new PathRewrite.Empty(); // or strip leading slash, perhaps? |
129 } | 133 } |
130 parseRequires(); | 134 impl.parseRequires(this, new File(repoDir, "requires")); |
131 dataPathHelper = impl.buildDataFilesHelper(); | 135 dataPathHelper = impl.buildDataFilesHelper(); |
132 repoPathHelper = impl.buildRepositoryFilesHelper(); | 136 repoPathHelper = impl.buildRepositoryFilesHelper(); |
133 } | 137 } |
134 | 138 |
135 @Override | 139 @Override |
174 ByteArrayChannel sink = new ByteArrayChannel(); | 178 ByteArrayChannel sink = new ByteArrayChannel(); |
175 hgTags.content(i, sink); | 179 hgTags.content(i, sink); |
176 final String content = new String(sink.toArray(), "UTF8"); | 180 final String content = new String(sink.toArray(), "UTF8"); |
177 tags.readGlobal(new StringReader(content)); | 181 tags.readGlobal(new StringReader(content)); |
178 } catch (CancelledException ex) { | 182 } catch (CancelledException ex) { |
179 ex.printStackTrace(); // IGNORE, can't happen, we did not configure cancellation | 183 // IGNORE, can't happen, we did not configure cancellation |
184 getContext().getLog().debug(getClass(), ex, null); | |
180 } catch (HgDataStreamException ex) { | 185 } catch (HgDataStreamException ex) { |
181 ex.printStackTrace(); // FIXME need to react | 186 getContext().getLog().error(getClass(), ex, null); |
187 // FIXME need to react | |
182 } catch (IOException ex) { | 188 } catch (IOException ex) { |
183 // UnsupportedEncodingException can't happen (UTF8) | 189 // UnsupportedEncodingException can't happen (UTF8) |
184 // only from readGlobal. Need to reconsider exceptions thrown from there | 190 // only from readGlobal. Need to reconsider exceptions thrown from there |
185 ex.printStackTrace(); // XXX need to decide what to do this. failure to read single revision shall not break complete cycle | 191 getContext().getLog().error(getClass(), ex, null); |
192 // XXX need to decide what to do this. failure to read single revision shall not break complete cycle | |
186 } | 193 } |
187 } | 194 } |
188 } | 195 } |
189 tags.readGlobal(new File(getWorkingDir(), ".hgtags")); // XXX replace with HgDataFile.workingCopy | 196 tags.readGlobal(new File(getWorkingDir(), ".hgtags")); // XXX replace with HgDataFile.workingCopy |
190 tags.readLocal(new File(repoDir, "localtags")); | 197 tags.readLocal(new File(repoDir, "localtags")); |
191 } catch (IOException ex) { | 198 } catch (IOException ex) { |
192 ex.printStackTrace(); // FIXME log or othewise report | 199 getContext().getLog().error(getClass(), ex, null); |
193 } | 200 } |
194 } | 201 } |
195 return tags; | 202 return tags; |
196 } | 203 } |
197 | 204 |
300 ignore = new HgIgnore(); | 307 ignore = new HgIgnore(); |
301 try { | 308 try { |
302 File ignoreFile = new File(getWorkingDir(), ".hgignore"); | 309 File ignoreFile = new File(getWorkingDir(), ".hgignore"); |
303 ignore.read(ignoreFile); | 310 ignore.read(ignoreFile); |
304 } catch (IOException ex) { | 311 } catch (IOException ex) { |
305 ex.printStackTrace(); // log warn | 312 getContext().getLog().warn(getClass(), ex, null); |
306 } | 313 } |
307 } | 314 } |
308 return ignore; | 315 return ignore; |
309 } | 316 } |
310 | 317 |
332 try { | 339 try { |
333 File fake = File.createTempFile(f.getName(), null); | 340 File fake = File.createTempFile(f.getName(), null); |
334 fake.deleteOnExit(); | 341 fake.deleteOnExit(); |
335 return new RevlogStream(dataAccess, fake); | 342 return new RevlogStream(dataAccess, fake); |
336 } catch (IOException ex) { | 343 } catch (IOException ex) { |
337 ex.printStackTrace(); // FIXME report in debug | 344 getContext().getLog().info(getClass(), ex, null); |
338 } | 345 } |
339 } | 346 } |
340 } | 347 } |
341 return null; // XXX empty stream instead? | 348 return null; // XXX empty stream instead? |
342 } | 349 } |
343 | 350 |
344 // can't expose internal class, otherwise seems reasonable to have it in API | 351 // can't expose internal class, otherwise seems reasonable to have it in API |
345 /*package-local*/ ConfigFile getConfigFile() { | 352 /*package-local*/ ConfigFile getConfigFile() { |
346 if (configFile == null) { | 353 if (configFile == null) { |
347 configFile = impl.newConfigFile(); | 354 configFile = new ConfigFile(); |
348 configFile.addLocation(new File(System.getProperty("user.home"), ".hgrc")); | 355 try { |
349 // last one, overrides anything else | 356 configFile.addLocation(new File(System.getProperty("user.home"), ".hgrc")); |
350 // <repo>/.hg/hgrc | 357 // last one, overrides anything else |
351 configFile.addLocation(new File(getRepositoryRoot(), "hgrc")); | 358 // <repo>/.hg/hgrc |
359 configFile.addLocation(new File(getRepositoryRoot(), "hgrc")); | |
360 } catch (IOException ex) { | |
361 getContext().getLog().warn(getClass(), ex, "Errors while reading user configuration file"); | |
362 } | |
352 } | 363 } |
353 return configFile; | 364 return configFile; |
354 } | 365 } |
355 | 366 |
356 /*package-local*/ List<Filter> getFiltersFromRepoToWorkingDir(Path p) { | 367 /*package-local*/ List<Filter> getFiltersFromRepoToWorkingDir(Path p) { |
361 return instantiateFilters(p, new Filter.Options(Filter.Direction.ToRepo)); | 372 return instantiateFilters(p, new Filter.Options(Filter.Direction.ToRepo)); |
362 } | 373 } |
363 | 374 |
364 /*package-local*/ File getFile(HgDataFile dataFile) { | 375 /*package-local*/ File getFile(HgDataFile dataFile) { |
365 return new File(getWorkingDir(), dataFile.getPath().toString()); | 376 return new File(getWorkingDir(), dataFile.getPath().toString()); |
377 } | |
378 | |
379 /*package-local*/ SessionContext getContext() { | |
380 return sessionContext; | |
366 } | 381 } |
367 | 382 |
368 private List<Filter> instantiateFilters(Path p, Filter.Options opts) { | 383 private List<Filter> instantiateFilters(Path p, Filter.Options opts) { |
369 List<Filter.Factory> factories = impl.getFilters(this, getConfigFile()); | 384 List<Filter.Factory> factories = impl.getFilters(this, getConfigFile()); |
370 if (factories.isEmpty()) { | 385 if (factories.isEmpty()) { |
377 rv.add(f); | 392 rv.add(f); |
378 } | 393 } |
379 } | 394 } |
380 return rv; | 395 return rv; |
381 } | 396 } |
382 | |
383 private void parseRequires() { | |
384 new RequiresFile().parse(impl, new File(repoDir, "requires")); | |
385 } | |
386 | |
387 } | 397 } |