changeset 335:3d41dc148d14

Do not fail with exception on syntax errors in .hgignore
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 10 Nov 2011 05:44:12 +0100
parents 15e1961719f2
children f74e36b7344b
files src/org/tmatesoft/hg/core/HgInvalidControlFileException.java src/org/tmatesoft/hg/repo/HgIgnore.java src/org/tmatesoft/hg/repo/HgRepository.java
diffstat 3 files changed, 26 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/core/HgInvalidControlFileException.java	Wed Nov 09 05:22:26 2011 +0100
+++ b/src/org/tmatesoft/hg/core/HgInvalidControlFileException.java	Thu Nov 10 05:44:12 2011 +0100
@@ -23,7 +23,8 @@
 /**
  * WORK IN PROGRESS
  * 
- * Subclass of {@link HgInvalidFileException} to indicate failure to deal with one of <b>Mercurial</b> control files (those under .hg/)
+ * Subclass of {@link HgInvalidFileException} to indicate failure to deal with one of <b>Mercurial</b> control files 
+ * (most likely those under .hg/, but also those residing in the repository, with special meaning to the Mercurial, like .hgtags or .hgignore)
  * @author Artem Tikhomirov
  * @author TMate Software Ltd.
  */
--- a/src/org/tmatesoft/hg/repo/HgIgnore.java	Wed Nov 09 05:22:26 2011 +0100
+++ b/src/org/tmatesoft/hg/repo/HgIgnore.java	Thu Nov 10 05:44:12 2011 +0100
@@ -24,6 +24,7 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
 
 import org.tmatesoft.hg.util.Path;
 
@@ -41,19 +42,20 @@
 		entries = Collections.emptyList();
 	}
 
-	/* package-local */void read(File hgignoreFile) throws IOException {
+	/* package-local */List<String> read(File hgignoreFile) throws IOException {
 		if (!hgignoreFile.exists()) {
-			return;
+			return null;
 		}
 		BufferedReader fr = new BufferedReader(new FileReader(hgignoreFile));
 		try {
-			read(fr);
+			return read(fr);
 		} finally {
 			fr.close();
 		}
 	}
 
-	/* package-local */void read(BufferedReader content) throws IOException {
+	/* package-local */List<String> read(BufferedReader content) throws IOException {
+		ArrayList<String> errors = new ArrayList<String>();
 		ArrayList<Pattern> result = new ArrayList<Pattern>(entries); // start with existing
 		String syntax = "regexp"; // or "glob"
 		String line;
@@ -62,7 +64,9 @@
 			if (line.startsWith("syntax:")) {
 				syntax = line.substring("syntax:".length()).trim();
 				if (!"regexp".equals(syntax) && !"glob".equals(syntax)) {
-					throw new IllegalStateException(line);
+					errors.add(line);
+					continue;
+					//throw new IllegalStateException(line);
 				}
 			} else if (line.length() > 0) {
 				// shall I account for local paths in the file (i.e.
@@ -87,11 +91,16 @@
 					// however, own attempts make me think '\' on Windows are not treated as escapes
 					line = glob2regex(line);
 				}
-				result.add(Pattern.compile(line)); // case-sensitive
+				try {
+					result.add(Pattern.compile(line)); // case-sensitive
+				} catch (PatternSyntaxException ex) {
+					errors.add(line + "@" + ex.getMessage());
+				}
 			}
 		}
 		result.trimToSize();
 		entries = result;
+		return errors.isEmpty() ? null : errors;
 	}
 
 	// note, #isIgnored(), even if queried for directories and returned positive reply, may still get
--- a/src/org/tmatesoft/hg/repo/HgRepository.java	Wed Nov 09 05:22:26 2011 +0100
+++ b/src/org/tmatesoft/hg/repo/HgRepository.java	Thu Nov 10 05:44:12 2011 +0100
@@ -323,15 +323,20 @@
 	 * Access to configured set of ignored files.
 	 * @see HgIgnore#isIgnored(Path)
 	 */
-	public HgIgnore getIgnore() {
+	public HgIgnore getIgnore() /*throws HgInvalidControlFileException */{
 		// TODO read config for additional locations
 		if (ignore == null) {
 			ignore = new HgIgnore();
+			File ignoreFile = new File(getWorkingDir(), ".hgignore");
 			try {
-				File ignoreFile = new File(getWorkingDir(), ".hgignore");
-				ignore.read(ignoreFile);
+				final List<String> errors = ignore.read(ignoreFile);
+				if (errors != null) {
+					getContext().getLog().warn(getClass(), "Syntax errors parsing .hgignore:\n%s", errors);
+				}
 			} catch (IOException ex) {
-				getContext().getLog().warn(getClass(), ex, null);
+				final String m = "Error reading .hgignore file";
+				getContext().getLog().warn(getClass(), ex, m);
+//				throw new HgInvalidControlFileException(m, ex, ignoreFile);
 			}
 		}
 		return ignore;