comparison src/org/tmatesoft/hg/internal/StoragePathHelper.java @ 616:5e0313485eef

encode directories as demanded by fncache format
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 14 May 2013 17:31:35 +0200
parents 48f993aa2f41
children
comparison
equal deleted inserted replaced
615:84104448a0bf 616:5e0313485eef
1 /* 1 /*
2 * Copyright (c) 2011-2012 TMate Software Ltd 2 * Copyright (c) 2011-2013 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 *
20 import java.nio.CharBuffer; 20 import java.nio.CharBuffer;
21 import java.nio.charset.Charset; 21 import java.nio.charset.Charset;
22 import java.nio.charset.CharsetEncoder; 22 import java.nio.charset.CharsetEncoder;
23 import java.util.Arrays; 23 import java.util.Arrays;
24 import java.util.TreeSet; 24 import java.util.TreeSet;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 25
28 import org.tmatesoft.hg.util.PathRewrite; 26 import org.tmatesoft.hg.util.PathRewrite;
29 27
30 /** 28 /**
31 * @see http://mercurial.selenic.com/wiki/CaseFoldingPlan 29 * @see http://mercurial.selenic.com/wiki/CaseFoldingPlan
34 * 32 *
35 * @author Artem Tikhomirov 33 * @author Artem Tikhomirov
36 * @author TMate Software Ltd. 34 * @author TMate Software Ltd.
37 */ 35 */
38 class StoragePathHelper implements PathRewrite { 36 class StoragePathHelper implements PathRewrite {
39 37
38 static final String STR_STORE = "store/";
39 static final String STR_DATA = "data/";
40 static final String STR_DH = "dh/";
41
40 private final boolean store; 42 private final boolean store;
41 private final boolean fncache; 43 private final boolean fncache;
42 private final boolean dotencode; 44 private final boolean dotencode;
43 private final Pattern suffix2replace; 45 private final EncodeDirPathHelper dirPathRewrite;
44 private final CharsetEncoder csEncoder; 46 private final CharsetEncoder csEncoder;
45 private final char[] hexEncodedByte = new char[] {'~', '0', '0'}; 47 private final char[] hexEncodedByte = new char[] {'~', '0', '0'};
46 private final ByteBuffer byteEncodingBuf; 48 private final ByteBuffer byteEncodingBuf;
47 private final CharBuffer charEncodingBuf; 49 private final CharBuffer charEncodingBuf;
48 50
53 public StoragePathHelper(boolean isStore, boolean isFncache, boolean isDotencode, Charset fsEncoding) { 55 public StoragePathHelper(boolean isStore, boolean isFncache, boolean isDotencode, Charset fsEncoding) {
54 assert fsEncoding != null; 56 assert fsEncoding != null;
55 store = isStore; 57 store = isStore;
56 fncache = isFncache; 58 fncache = isFncache;
57 dotencode = isDotencode; 59 dotencode = isDotencode;
58 suffix2replace = Pattern.compile("\\.([id]|hg)/"); 60 dirPathRewrite = new EncodeDirPathHelper();
59 csEncoder = fsEncoding.newEncoder(); 61 csEncoder = fsEncoding.newEncoder();
60 byteEncodingBuf = ByteBuffer.allocate(Math.round(csEncoder.maxBytesPerChar()) + 1/*in fact, need ceil, hence +1*/); 62 byteEncodingBuf = ByteBuffer.allocate(Math.round(csEncoder.maxBytesPerChar()) + 1/*in fact, need ceil, hence +1*/);
61 charEncodingBuf = CharBuffer.allocate(1); 63 charEncodingBuf = CharBuffer.allocate(1);
62 } 64 }
63 65
64 /** 66 /**
65 * path argument is repository-relative name of the user's file. 67 * path argument is repository-relative name of the user's file.
66 * It has to be normalized (slashes) and shall not include extension .i or .d. 68 * It has to be normalized (slashes) and shall not include extension .i or .d.
67 */ 69 */
68 public CharSequence rewrite(CharSequence p) { 70 public CharSequence rewrite(CharSequence p) {
69 final String STR_STORE = "store/";
70 final String STR_DATA = "data/";
71 final String STR_DH = "dh/";
72 final String reservedChars = "\\:*?\"<>|"; 71 final String reservedChars = "\\:*?\"<>|";
73 72
74 Matcher suffixMatcher = suffix2replace.matcher(p); 73 CharSequence path = dirPathRewrite.rewrite(p);
75 CharSequence path;
76 // Matcher.replaceAll, but without extra toString
77 boolean found = suffixMatcher.find();
78 if (found) {
79 StringBuffer sb = new StringBuffer(p.length() + 20);
80 do {
81 suffixMatcher.appendReplacement(sb, ".$1.hg/");
82 } while (found = suffixMatcher.find());
83 suffixMatcher.appendTail(sb);
84 path = sb;
85 } else {
86 path = p;
87 }
88 74
89 StringBuilder sb = new StringBuilder(path.length() << 1); 75 StringBuilder sb = new StringBuilder(path.length() << 1);
90 if (store || fncache) { 76 if (store || fncache) {
91 for (int i = 0; i < path.length(); i++) { 77 for (int i = 0; i < path.length(); i++) {
92 final char ch = path.charAt(i); 78 final char ch = path.charAt(i);