comparison src/com/tmate/hgkit/ll/HgIgnore.java @ 15:865bf07f381f

Basic hgignore handling
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 27 Dec 2010 00:50:02 +0100
parents
children
comparison
equal deleted inserted replaced
14:442dc6ee647b 15:865bf07f381f
1 /*
2 * Copyright (c) 2010 Artem Tikhomirov
3 */
4 package com.tmate.hgkit.ll;
5
6 import java.io.BufferedReader;
7 import java.io.File;
8 import java.io.FileReader;
9 import java.io.IOException;
10 import java.util.Collections;
11 import java.util.Set;
12 import java.util.TreeSet;
13
14 /**
15 *
16 * @author artem
17 */
18 public class HgIgnore {
19
20 private final LocalHgRepo repo;
21 private Set<String> entries;
22
23 public HgIgnore(LocalHgRepo localRepo) {
24 this.repo = localRepo;
25 }
26
27 private void read() {
28 entries = Collections.emptySet();
29 File hgignoreFile = new File(repo.getRepositoryRoot().getParentFile(), ".hgignore");
30 if (!hgignoreFile.exists()) {
31 return;
32 }
33 entries = new TreeSet<String>();
34 try {
35 BufferedReader fr = new BufferedReader(new FileReader(hgignoreFile));
36 String line;
37 while ((line = fr.readLine()) != null) {
38 // FIXME need to detect syntax:glob and other parameters
39 entries.add(line.trim()); // shall I account for local paths in the file (i.e. back-slashed on windows)?
40 }
41 } catch (IOException ex) {
42 ex.printStackTrace(); // log warn
43 }
44 }
45
46 public void reset() {
47 // FIXME does anyone really need to clear HgIgnore? Perhaps, repo may return new instance each time,
48 // which is used throughout invocation and then discarded?
49 entries = null;
50 }
51
52 public boolean isIgnored(String path) {
53 if (entries == null) {
54 read();
55 }
56 if (entries.contains(path)) {
57 // easy part
58 return true;
59 }
60 // substrings are memory-friendly
61 int x = 0, i = path.indexOf('/', 0);
62 while (i != -1) {
63 if (entries.contains(path.substring(x, i))) {
64 return true;
65 }
66 // try one with ending slash
67 if (entries.contains(path.substring(x, i+1))) { // even if i is last index, i+1 is safe here
68 return true;
69 }
70 x = i+1;
71 i = path.indexOf('/', x);
72 }
73 return false;
74 }
75 }