diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/repo/HgIgnore.java	Mon Jan 24 03:14:45 2011 +0100
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2010-2011 TMate Software Ltd
+ *  
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@svnkit.com
+ */
+package org.tmatesoft.hg.repo;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Set;
+import java.util.TreeSet;
+
+/**
+ *
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class HgIgnore {
+
+	private final HgRepository repo;
+	private Set<String> entries;
+
+	public HgIgnore(HgRepository localRepo) {
+		this.repo = localRepo;
+	}
+
+	private void read() {
+		entries = Collections.emptySet();
+		File hgignoreFile = new File(repo.getRepositoryRoot().getParentFile(), ".hgignore");
+		if (!hgignoreFile.exists()) {
+			return;
+		}
+		entries = new TreeSet<String>();
+		try {
+			BufferedReader fr = new BufferedReader(new FileReader(hgignoreFile));
+			String line;
+			while ((line = fr.readLine()) != null) {
+				// FIXME need to detect syntax:glob and other parameters
+				entries.add(line.trim()); // shall I account for local paths in the file (i.e. back-slashed on windows)?
+			}
+		} catch (IOException ex) {
+			ex.printStackTrace(); // log warn
+		}
+	}
+
+	public void reset() {
+		// FIXME does anyone really need to clear HgIgnore? Perhaps, repo may return new instance each time,
+		// which is used throughout invocation and then discarded?
+		entries = null;
+	}
+
+	public boolean isIgnored(String path) {
+		if (entries == null) {
+			read();
+		}
+		if (entries.contains(path)) {
+			// easy part
+			return true;
+		}
+		// substrings are memory-friendly 
+		int x = 0, i = path.indexOf('/', 0);
+		while (i != -1) {
+			if (entries.contains(path.substring(x, i))) {
+				return true;
+			}
+			// try one with ending slash
+			if (entries.contains(path.substring(x, i+1))) { // even if i is last index, i+1 is safe here
+				return true;
+			}
+			x = i+1;
+			i = path.indexOf('/', x);
+		}
+		return false;
+	}
+}