comparison src/org/tmatesoft/hg/repo/HgLookup.java @ 618:7c0d2ce340b8

Refactor approach how content finds it way down to a commit revision
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 16 May 2013 19:46:13 +0200
parents f97e81d13190
children 4e6179bde4fc
comparison
equal deleted inserted replaced
617:65c01508f002 618:7c0d2ce340b8
36 * Utility methods to find Mercurial repository at a given location 36 * Utility methods to find Mercurial repository at a given location
37 * 37 *
38 * @author Artem Tikhomirov 38 * @author Artem Tikhomirov
39 * @author TMate Software Ltd. 39 * @author TMate Software Ltd.
40 */ 40 */
41 public class HgLookup { 41 public class HgLookup implements SessionContext.Source {
42 42
43 private ConfigFile globalCfg; 43 private ConfigFile globalCfg;
44 private SessionContext sessionContext; 44 private SessionContext sessionContext;
45 45
46 public HgLookup() { 46 public HgLookup() {
79 } while(dir != null); 79 } while(dir != null);
80 if (repository == null) { 80 if (repository == null) {
81 throw new HgRepositoryNotFoundException(String.format("Can't locate .hg/ directory of Mercurial repository in %s nor in parent dirs", location)).setLocation(location.getPath()); 81 throw new HgRepositoryNotFoundException(String.format("Can't locate .hg/ directory of Mercurial repository in %s nor in parent dirs", location)).setLocation(location.getPath());
82 } 82 }
83 String repoPath = repository.getParentFile().getAbsolutePath(); 83 String repoPath = repository.getParentFile().getAbsolutePath();
84 HgRepository rv = new HgRepository(getContext(), repoPath, repository); 84 HgRepository rv = new HgRepository(getSessionContext(), repoPath, repository);
85 int requiresFlags = rv.getImplHelper().getRequiresFlags(); 85 int requiresFlags = rv.getImplHelper().getRequiresFlags();
86 if ((requiresFlags & RequiresFile.REVLOGV1) == 0) { 86 if ((requiresFlags & RequiresFile.REVLOGV1) == 0) {
87 throw new HgRepositoryNotFoundException(String.format("%s: repository version is not supported (Mercurial <0.9?)", repoPath)).setLocation(location.getPath()); 87 throw new HgRepositoryNotFoundException(String.format("%s: repository version is not supported (Mercurial <0.9?)", repoPath)).setLocation(location.getPath());
88 } 88 }
89 return rv; 89 return rv;
91 91
92 public HgBundle loadBundle(File location) throws HgRepositoryNotFoundException { 92 public HgBundle loadBundle(File location) throws HgRepositoryNotFoundException {
93 if (location == null || !location.canRead()) { 93 if (location == null || !location.canRead()) {
94 throw new HgRepositoryNotFoundException(String.format("Can't read file %s", location)).setLocation(String.valueOf(location)); 94 throw new HgRepositoryNotFoundException(String.format("Can't read file %s", location)).setLocation(String.valueOf(location));
95 } 95 }
96 return new HgBundle(getContext(), new DataAccessProvider(getContext()), location).link(); 96 return new HgBundle(getSessionContext(), new DataAccessProvider(getSessionContext()), location).link();
97 } 97 }
98 98
99 /** 99 /**
100 * Try to instantiate remote server using an immediate url or an url from configuration files 100 * Try to instantiate remote server using an immediate url or an url from configuration files
101 * 101 *
132 url = new URL(server); 132 url = new URL(server);
133 } catch (MalformedURLException ex) { 133 } catch (MalformedURLException ex) {
134 throw new HgBadArgumentException(String.format("Found %s server spec in the config, but failed to initialize with it", key), ex); 134 throw new HgBadArgumentException(String.format("Found %s server spec in the config, but failed to initialize with it", key), ex);
135 } 135 }
136 } 136 }
137 return new HgRemoteRepository(getContext(), url); 137 return new HgRemoteRepository(getSessionContext(), url);
138 } 138 }
139 139
140 public HgRemoteRepository detect(URL url) throws HgBadArgumentException { 140 public HgRemoteRepository detect(URL url) throws HgBadArgumentException {
141 if (url == null) { 141 if (url == null) {
142 throw new IllegalArgumentException(); 142 throw new IllegalArgumentException();
143 } 143 }
144 if (Boolean.FALSE.booleanValue()) { 144 if (Boolean.FALSE.booleanValue()) {
145 throw Internals.notImplemented(); 145 throw Internals.notImplemented();
146 } 146 }
147 return new HgRemoteRepository(getContext(), url); 147 return new HgRemoteRepository(getSessionContext(), url);
148 } 148 }
149 149
150 private ConfigFile getGlobalConfig() { 150 private ConfigFile getGlobalConfig() {
151 if (globalCfg == null) { 151 if (globalCfg == null) {
152 globalCfg = new ConfigFile(getContext()); 152 globalCfg = new ConfigFile(getSessionContext());
153 try { 153 try {
154 globalCfg.addLocation(new File(System.getProperty("user.home"), ".hgrc")); 154 globalCfg.addLocation(new File(System.getProperty("user.home"), ".hgrc"));
155 } catch (HgInvalidFileException ex) { 155 } catch (HgInvalidFileException ex) {
156 // XXX perhaps, makes sense to let caller/client know that we've failed to read global config? 156 // XXX perhaps, makes sense to let caller/client know that we've failed to read global config?
157 getContext().getLog().dump(getClass(), Warn, ex, null); 157 getSessionContext().getLog().dump(getClass(), Warn, ex, null);
158 } 158 }
159 } 159 }
160 return globalCfg; 160 return globalCfg;
161 } 161 }
162 162
163 private SessionContext getContext() { 163 public SessionContext getSessionContext() {
164 if (sessionContext == null) { 164 if (sessionContext == null) {
165 sessionContext = new BasicSessionContext(null); 165 sessionContext = new BasicSessionContext(null);
166 } 166 }
167 return sessionContext; 167 return sessionContext;
168 } 168 }