comparison src/org/tmatesoft/hg/repo/HgIgnore.java @ 408:e732521a9eb4 smartgit3

Issue 28: support hgignore entries with syntax prefix
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 16 Mar 2012 15:06:44 +0100
parents 58016b1b8554
children 0f5696623512
comparison
equal deleted inserted replaced
407:30922c728341 408:e732521a9eb4
1 /* 1 /*
2 * Copyright (c) 2010-2011 TMate Software Ltd 2 * Copyright (c) 2010-2012 TMate Software Ltd
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 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 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License. 6 * the Free Software Foundation; version 2 of the License.
7 * 7 *
53 fr.close(); 53 fr.close();
54 } 54 }
55 } 55 }
56 56
57 /* package-local */List<String> read(BufferedReader content) throws IOException { 57 /* package-local */List<String> read(BufferedReader content) throws IOException {
58 final String REGEXP = "regexp", GLOB = "glob";
59 final String REGEXP_PREFIX = REGEXP + ":", GLOB_PREFIX = GLOB + ":";
58 ArrayList<String> errors = new ArrayList<String>(); 60 ArrayList<String> errors = new ArrayList<String>();
59 ArrayList<Pattern> result = new ArrayList<Pattern>(entries); // start with existing 61 ArrayList<Pattern> result = new ArrayList<Pattern>(entries); // start with existing
60 String syntax = "regexp"; // or "glob" 62 String syntax = REGEXP;
61 String line; 63 String line;
62 while ((line = content.readLine()) != null) { 64 while ((line = content.readLine()) != null) {
63 line = line.trim(); 65 line = line.trim();
64 if (line.startsWith("syntax:")) { 66 if (line.startsWith("syntax:")) {
65 syntax = line.substring("syntax:".length()).trim(); 67 syntax = line.substring("syntax:".length()).trim();
66 if (!"regexp".equals(syntax) && !"glob".equals(syntax)) { 68 if (!REGEXP.equals(syntax) && !GLOB.equals(syntax)) {
67 errors.add(line); 69 errors.add(line);
68 continue; 70 continue;
69 //throw new IllegalStateException(line); 71 //throw new IllegalStateException(line);
70 } 72 }
71 } else if (line.length() > 0) { 73 } else if (line.length() > 0) {
79 s = x; // with exclusion of char at [x], s now points to what used to be at [x+1] 81 s = x; // with exclusion of char at [x], s now points to what used to be at [x+1]
80 } else { 82 } else {
81 line = line.substring(0, x).trim(); 83 line = line.substring(0, x).trim();
82 } 84 }
83 } 85 }
86 // due to the nature of Mercurial implementation, lines prefixed with syntax kind
87 // are processed correctly (despite the fact hgignore(5) suggest "syntax:<kind>" as the
88 // only way to specify it). lineSyntax below leaves a chance for the line to switch
89 // syntax in use without affecting default kind.
90 String lineSyntax;
91 if (line.startsWith(GLOB_PREFIX)) {
92 line = line.substring(GLOB_PREFIX.length()).trim();
93 lineSyntax = GLOB;
94 } else if (line.startsWith(REGEXP_PREFIX)) {
95 line = line.substring(REGEXP_PREFIX.length()).trim();
96 lineSyntax = REGEXP;
97 } else {
98 lineSyntax = syntax;
99 }
84 if (line.length() == 0) { 100 if (line.length() == 0) {
85 continue; 101 continue;
86 } 102 }
87 if ("glob".equals(syntax)) { 103 if (GLOB.equals(lineSyntax)) {
88 // hgignore(5) 104 // hgignore(5)
89 // (http://www.selenic.com/mercurial/hgignore.5.html) says slashes '\' are escape characters, 105 // (http://www.selenic.com/mercurial/hgignore.5.html) says slashes '\' are escape characters,
90 // hence no special treatment of Windows path 106 // hence no special treatment of Windows path
91 // however, own attempts make me think '\' on Windows are not treated as escapes 107 // however, own attempts make me think '\' on Windows are not treated as escapes
92 line = glob2regex(line); 108 line = glob2regex(line);
93 } else { 109 } else {
94 assert "regexp".equals(syntax); 110 assert REGEXP.equals(lineSyntax);
95 // regular expression patterns need not match start of the line unless demanded explicitly 111 // regular expression patterns need not match start of the line unless demanded explicitly
96 line = line.charAt(0) == '^' ? line : ".*" + line; 112 line = line.charAt(0) == '^' ? line : ".*" + line;
97 } 113 }
98 try { 114 try {
99 result.add(Pattern.compile(line)); // case-sensitive 115 result.add(Pattern.compile(line)); // case-sensitive