Mercurial > jhg
comparison src/org/tmatesoft/hg/repo/HgIgnore.java @ 289:086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> | 
|---|---|
| date | Mon, 12 Sep 2011 14:11:45 +0200 | 
| parents | c8baeb813d74 | 
| children | 3d41dc148d14 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 288:b11f6a08f748 | 289:086a326f181f | 
|---|---|
| 31 * Handling of ignored paths according to .hgignore configuration | 31 * Handling of ignored paths according to .hgignore configuration | 
| 32 * | 32 * | 
| 33 * @author Artem Tikhomirov | 33 * @author Artem Tikhomirov | 
| 34 * @author TMate Software Ltd. | 34 * @author TMate Software Ltd. | 
| 35 */ | 35 */ | 
| 36 public class HgIgnore { | 36 public class HgIgnore implements Path.Matcher { | 
| 37 | 37 | 
| 38 private List<Pattern> entries; | 38 private List<Pattern> entries; | 
| 39 | 39 | 
| 40 HgIgnore() { | 40 HgIgnore() { | 
| 41 entries = Collections.emptyList(); | 41 entries = Collections.emptyList(); | 
| 102 // troublesome when one walks not over io.File, but Eclipse's IResource or any other custom VFS. | 102 // troublesome when one walks not over io.File, but Eclipse's IResource or any other custom VFS. | 
| 103 // | 103 // | 
| 104 // | 104 // | 
| 105 // might be interesting, although looks like of no direct use in my case | 105 // might be interesting, although looks like of no direct use in my case | 
| 106 // @see http://stackoverflow.com/questions/1247772/is-there-an-equivalent-of-java-util-regex-for-glob-type-patterns | 106 // @see http://stackoverflow.com/questions/1247772/is-there-an-equivalent-of-java-util-regex-for-glob-type-patterns | 
| 107 private String glob2regex(String line) { | 107 // | 
| 108 // TODO consider refactoring to reuse in PathGlobMatcher#glob2regexp | |
| 109 private static String glob2regex(String line) { | |
| 108 assert line.length() > 0; | 110 assert line.length() > 0; | 
| 109 StringBuilder sb = new StringBuilder(line.length() + 10); | 111 StringBuilder sb = new StringBuilder(line.length() + 10); | 
| 110 if (line.charAt(0) != '*') { | 112 if (line.charAt(0) != '*') { | 
| 111 sb.append('^'); // help avoid matcher.find() to match 'bin' pattern in the middle of the filename | 113 sb.append('^'); // help avoid matcher.find() to match 'bin' pattern in the middle of the filename | 
| 112 } | 114 } | 
| 152 sb.append(ch); | 154 sb.append(ch); | 
| 153 } | 155 } | 
| 154 return sb.toString(); | 156 return sb.toString(); | 
| 155 } | 157 } | 
| 156 | 158 | 
| 157 // TODO use PathGlobMatcher | 159 /** | 
| 160 * @param path file or directory name in question | |
| 161 * @return <code>true</code> if matches repository configuration of ignored files. | |
| 162 */ | |
| 158 public boolean isIgnored(Path path) { | 163 public boolean isIgnored(Path path) { | 
| 159 for (Pattern p : entries) { | 164 for (Pattern p : entries) { | 
| 160 if (p.matcher(path).find()) { | 165 if (p.matcher(path).find()) { | 
| 161 return true; | 166 return true; | 
| 162 } | 167 } | 
| 163 } | 168 } | 
| 164 return false; | 169 return false; | 
| 165 } | 170 } | 
| 171 | |
| 172 /** | |
| 173 * A handy wrap of {@link #isIgnored(Path)} into {@link Path.Matcher}. Yields same result as {@link #isIgnored(Path)}. | |
| 174 * @return <code>true</code> if file is deemed ignored. | |
| 175 */ | |
| 176 public boolean accept(Path path) { | |
| 177 return isIgnored(path); | |
| 178 } | |
| 166 } | 179 } | 
