Mercurial > hg4j
comparison src/org/tmatesoft/hg/repo/HgLookup.java @ 423:9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 23 Mar 2012 22:51:18 +0100 |
parents | dfb8405d996f |
children | 48f993aa2f41 |
comparison
equal
deleted
inserted
replaced
422:5d1cc7366d04 | 423:9c9c442b5f2e |
---|---|
20 import java.io.IOException; | 20 import java.io.IOException; |
21 import java.net.MalformedURLException; | 21 import java.net.MalformedURLException; |
22 import java.net.URL; | 22 import java.net.URL; |
23 | 23 |
24 import org.tmatesoft.hg.core.HgBadArgumentException; | 24 import org.tmatesoft.hg.core.HgBadArgumentException; |
25 import org.tmatesoft.hg.core.HgInvalidFileException; | 25 import org.tmatesoft.hg.core.HgRepositoryNotFoundException; |
26 import org.tmatesoft.hg.core.SessionContext; | 26 import org.tmatesoft.hg.core.SessionContext; |
27 import org.tmatesoft.hg.internal.BasicSessionContext; | 27 import org.tmatesoft.hg.internal.BasicSessionContext; |
28 import org.tmatesoft.hg.internal.ConfigFile; | 28 import org.tmatesoft.hg.internal.ConfigFile; |
29 import org.tmatesoft.hg.internal.DataAccessProvider; | 29 import org.tmatesoft.hg.internal.DataAccessProvider; |
30 | 30 |
44 | 44 |
45 public HgLookup(SessionContext ctx) { | 45 public HgLookup(SessionContext ctx) { |
46 sessionContext = ctx; | 46 sessionContext = ctx; |
47 } | 47 } |
48 | 48 |
49 public HgRepository detectFromWorkingDir() throws HgInvalidFileException { | 49 public HgRepository detectFromWorkingDir() throws HgRepositoryNotFoundException { |
50 return detect(System.getProperty("user.dir")); | 50 return detect(System.getProperty("user.dir")); |
51 } | 51 } |
52 | 52 |
53 public HgRepository detect(String location) throws HgInvalidFileException { | 53 public HgRepository detect(String location) throws HgRepositoryNotFoundException { |
54 return detect(new File(location)); | 54 return detect(new File(location)); |
55 } | 55 } |
56 | 56 |
57 // look up in specified location and above | 57 // look up in specified location and above |
58 public HgRepository detect(File location) throws HgInvalidFileException { | 58 public HgRepository detect(File location) throws HgRepositoryNotFoundException { |
59 File dir = location.getAbsoluteFile(); | 59 try { |
60 File repository; | 60 File dir = location.getAbsoluteFile(); |
61 do { | 61 File repository; |
62 repository = new File(dir, ".hg"); | 62 do { |
63 if (repository.exists() && repository.isDirectory()) { | 63 repository = new File(dir, ".hg"); |
64 break; | 64 if (repository.exists() && repository.isDirectory()) { |
65 break; | |
66 } | |
67 repository = null; | |
68 dir = dir.getParentFile(); | |
69 | |
70 } while(dir != null); | |
71 if (repository == null) { | |
72 throw new HgRepositoryNotFoundException(String.format("Can't locate .hg/ directory of Mercurial repository in %s nor in parent dirs", location)).setLocation(location.getPath()); | |
65 } | 73 } |
66 repository = null; | |
67 dir = dir.getParentFile(); | |
68 | |
69 } while(dir != null); | |
70 if (repository == null) { | |
71 // return invalid repository | |
72 return new HgRepository(location.getPath()); | |
73 } | |
74 try { | |
75 String repoPath = repository.getParentFile().getCanonicalPath(); | 74 String repoPath = repository.getParentFile().getCanonicalPath(); |
76 return new HgRepository(getContext(), repoPath, repository); | 75 return new HgRepository(getContext(), repoPath, repository); |
77 } catch (IOException ex) { | 76 } catch (IOException ex) { |
78 throw new HgInvalidFileException(location.toString(), ex, location); | 77 HgRepositoryNotFoundException t = new HgRepositoryNotFoundException("Failed to access repository"); |
78 t.setLocation(location.getPath()).initCause(ex); | |
79 throw t; | |
79 } | 80 } |
80 } | 81 } |
81 | 82 |
82 public HgBundle loadBundle(File location) throws HgInvalidFileException { | 83 public HgBundle loadBundle(File location) throws HgRuntimeException/*FIXME need checked exception for can't find*/ { |
83 if (location == null || !location.canRead()) { | 84 if (location == null || !location.canRead()) { |
84 throw new HgInvalidFileException(String.format("Can't read file %s", location == null ? null : location.getPath()), null, location); | 85 throw new HgInvalidFileException(String.format("Can't read file %s", location == null ? null : location.getPath()), null, location); |
85 } | 86 } |
86 return new HgBundle(getContext(), new DataAccessProvider(getContext()), location).link(); | 87 return new HgBundle(getContext(), new DataAccessProvider(getContext()), location).link(); |
87 } | 88 } |