comparison src/org/tmatesoft/hg/repo/HgIgnore.java @ 74:6f1b88693d48

Complete refactoring to org.tmatesoft
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 24 Jan 2011 03:14:45 +0100
parents src/com/tmate/hgkit/ll/HgIgnore.java@865bf07f381f
children c2ce1cfaeb9e
comparison
equal deleted inserted replaced
73:0d279bcc4442 74:6f1b88693d48
1 /*
2 * Copyright (c) 2010-2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@svnkit.com
16 */
17 package org.tmatesoft.hg.repo;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.util.Collections;
24 import java.util.Set;
25 import java.util.TreeSet;
26
27 /**
28 *
29 * @author Artem Tikhomirov
30 * @author TMate Software Ltd.
31 */
32 public class HgIgnore {
33
34 private final HgRepository repo;
35 private Set<String> entries;
36
37 public HgIgnore(HgRepository localRepo) {
38 this.repo = localRepo;
39 }
40
41 private void read() {
42 entries = Collections.emptySet();
43 File hgignoreFile = new File(repo.getRepositoryRoot().getParentFile(), ".hgignore");
44 if (!hgignoreFile.exists()) {
45 return;
46 }
47 entries = new TreeSet<String>();
48 try {
49 BufferedReader fr = new BufferedReader(new FileReader(hgignoreFile));
50 String line;
51 while ((line = fr.readLine()) != null) {
52 // FIXME need to detect syntax:glob and other parameters
53 entries.add(line.trim()); // shall I account for local paths in the file (i.e. back-slashed on windows)?
54 }
55 } catch (IOException ex) {
56 ex.printStackTrace(); // log warn
57 }
58 }
59
60 public void reset() {
61 // FIXME does anyone really need to clear HgIgnore? Perhaps, repo may return new instance each time,
62 // which is used throughout invocation and then discarded?
63 entries = null;
64 }
65
66 public boolean isIgnored(String path) {
67 if (entries == null) {
68 read();
69 }
70 if (entries.contains(path)) {
71 // easy part
72 return true;
73 }
74 // substrings are memory-friendly
75 int x = 0, i = path.indexOf('/', 0);
76 while (i != -1) {
77 if (entries.contains(path.substring(x, i))) {
78 return true;
79 }
80 // try one with ending slash
81 if (entries.contains(path.substring(x, i+1))) { // even if i is last index, i+1 is safe here
82 return true;
83 }
84 x = i+1;
85 i = path.indexOf('/', x);
86 }
87 return false;
88 }
89 }