comparison src/org/tmatesoft/hg/internal/NewlineFilter.java @ 114:46291ec605a0

Filters to read and initialize according to configuration files
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 03 Feb 2011 22:13:55 +0100
parents 67ae317408c9
children 68ba22a2133a
comparison
equal deleted inserted replaced
113:67ae317408c9 114:46291ec605a0
22 22
23 import java.io.File; 23 import java.io.File;
24 import java.io.FileInputStream; 24 import java.io.FileInputStream;
25 import java.io.FileOutputStream; 25 import java.io.FileOutputStream;
26 import java.nio.ByteBuffer; 26 import java.nio.ByteBuffer;
27 import java.util.ArrayList;
28 import java.util.Map;
27 29
28 import org.tmatesoft.hg.core.Path; 30 import org.tmatesoft.hg.core.Path;
31 import org.tmatesoft.hg.repo.HgInternals;
29 import org.tmatesoft.hg.repo.HgRepository; 32 import org.tmatesoft.hg.repo.HgRepository;
30 33
31 /** 34 /**
32 * 35 *
33 * @author Artem Tikhomirov 36 * @author Artem Tikhomirov
152 } 155 }
153 return -1; 156 return -1;
154 } 157 }
155 158
156 public static class Factory implements Filter.Factory { 159 public static class Factory implements Filter.Factory {
157 private final boolean localIsWin = File.separatorChar == '\\'; // FIXME 160 private boolean failIfInconsistent = true;
158 private final boolean failIfInconsistent = true; 161 private Path.Matcher lfMatcher;
159 162 private Path.Matcher crlfMatcher;
160 public Filter create(HgRepository hgRepo, Path path, Options opts) { 163 private Path.Matcher binMatcher;
161 if (opts.getDirection() == FromRepo) { 164 private Path.Matcher nativeMatcher;
162 } else if (opts.getDirection() == ToRepo) { 165 private String nativeRepoFormat;
163 } 166 private String nativeOSFormat;
164 return new NewlineFilter(failIfInconsistent, 1); 167
168 public void initialize(HgRepository hgRepo, ConfigFile cfg) {
169 failIfInconsistent = cfg.getBoolean("eol", "only-consistent", true);
170 File cfgFile = new File(new HgInternals(hgRepo).getRepositoryDir(), ".hgeol");
171 if (!cfgFile.canRead()) {
172 return;
173 }
174 // XXX if .hgeol is not checked out, we may get it from repository
175 // HgDataFile cfgFileNode = hgRepo.getFileNode(".hgeol");
176 // if (!cfgFileNode.exists()) {
177 // return;
178 // }
179 // XXX perhaps, add HgDataFile.hasWorkingCopy and workingCopyContent()?
180 ConfigFile hgeol = new ConfigFile();
181 hgeol.addLocation(cfgFile);
182 nativeRepoFormat = hgeol.getSection("repository").get("native");
183 if (nativeRepoFormat == null) {
184 nativeRepoFormat = "LF";
185 }
186 final String os = System.getProperty("os.name"); // XXX need centralized set of properties
187 nativeOSFormat = os.indexOf("Windows") != -1 ? "CRLF" : "LF";
188 // I assume pattern ordering in .hgeol is not important
189 ArrayList<String> lfPatterns = new ArrayList<String>();
190 ArrayList<String> crlfPatterns = new ArrayList<String>();
191 ArrayList<String> nativePatterns = new ArrayList<String>();
192 ArrayList<String> binPatterns = new ArrayList<String>();
193 for (Map.Entry<String,String> e : hgeol.getSection("patterns").entrySet()) {
194 if ("CRLF".equals(e.getValue())) {
195 crlfPatterns.add(e.getKey());
196 } else if ("LF".equals(e.getValue())) {
197 lfPatterns.add(e.getKey());
198 } else if ("native".equals(e.getValue())) {
199 nativePatterns.add(e.getKey());
200 } else if ("BIN".equals(e.getValue())) {
201 binPatterns.add(e.getKey());
202 } else {
203 System.out.printf("Can't recognize .hgeol entry: %s for %s", e.getValue(), e.getKey()); // FIXME log warning
204 }
205 }
206 if (!crlfPatterns.isEmpty()) {
207 crlfMatcher = new PathGlobMatcher(crlfPatterns.toArray(new String[crlfPatterns.size()]));
208 }
209 if (!lfPatterns.isEmpty()) {
210 lfMatcher = new PathGlobMatcher(lfPatterns.toArray(new String[lfPatterns.size()]));
211 }
212 if (!binPatterns.isEmpty()) {
213 binMatcher = new PathGlobMatcher(binPatterns.toArray(new String[binPatterns.size()]));
214 }
215 if (!nativePatterns.isEmpty()) {
216 nativeMatcher = new PathGlobMatcher(nativePatterns.toArray(new String[nativePatterns.size()]));
217 }
218 }
219
220 public Filter create(Path path, Options opts) {
221 if (binMatcher == null && crlfMatcher == null && lfMatcher == null && nativeMatcher == null) {
222 // not initialized - perhaps, no .hgeol found
223 return null;
224 }
225 if (binMatcher != null && binMatcher.accept(path)) {
226 return null;
227 }
228 if (crlfMatcher != null && crlfMatcher.accept(path)) {
229 return new NewlineFilter(failIfInconsistent, 1);
230 } else if (lfMatcher != null && lfMatcher.accept(path)) {
231 return new NewlineFilter(failIfInconsistent, 0);
232 } else if (nativeMatcher != null && nativeMatcher.accept(path)) {
233 if (nativeOSFormat.equals(nativeRepoFormat)) {
234 return null;
235 }
236 if (opts.getDirection() == FromRepo) {
237 int transform = "CRLF".equals(nativeOSFormat) ? 1 : 0;
238 return new NewlineFilter(failIfInconsistent, transform);
239 } else if (opts.getDirection() == ToRepo) {
240 int transform = "CRLF".equals(nativeOSFormat) ? 0 : 1;
241 return new NewlineFilter(failIfInconsistent, transform);
242 }
243 return null;
244 }
245 return null;
165 } 246 }
166 } 247 }
167 248
168 public static void main(String[] args) throws Exception { 249 public static void main(String[] args) throws Exception {
169 FileInputStream fis = new FileInputStream(new File("/temp/design.lf.txt")); 250 FileInputStream fis = new FileInputStream(new File("/temp/design.lf.txt"));