comparison src/org/tmatesoft/hg/internal/RevlogDump.java @ 533:e6f72c9829a6

Generate patches using diff algorithm
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 30 Jan 2013 15:48:36 +0100
parents 6c22bdc0bdfd
children 47dfa0ec7e35
comparison
equal deleted inserted replaced
532:688c1ab113bb 533:e6f72c9829a6
1 /* 1 /*
2 * Copyright (c) 2010-2012 TMate Software Ltd 2 * Copyright (c) 2010-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 *
25 import java.io.IOException; 25 import java.io.IOException;
26 import java.io.UnsupportedEncodingException; 26 import java.io.UnsupportedEncodingException;
27 import java.math.BigInteger; 27 import java.math.BigInteger;
28 import java.nio.ByteBuffer; 28 import java.nio.ByteBuffer;
29 import java.nio.channels.FileChannel; 29 import java.nio.channels.FileChannel;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
30 import java.util.zip.Inflater; 32 import java.util.zip.Inflater;
31 33
32 /** 34 /**
33 * Utility to test/debug/troubleshoot 35 * Utility to test/debug/troubleshoot
34 * 36 *
46 public static void main(String[] args) throws Exception { 48 public static void main(String[] args) throws Exception {
47 String repo = "/temp/hg/hello/.hg/"; 49 String repo = "/temp/hg/hello/.hg/";
48 String filename = "store/00changelog.i"; 50 String filename = "store/00changelog.i";
49 // String filename = "store/data/hello.c.i"; 51 // String filename = "store/data/hello.c.i";
50 // String filename = "store/data/docs/readme.i"; 52 // String filename = "store/data/docs/readme.i";
53 System.out.println(escape("abc\0def\nzxc\tmnb"));
51 boolean dumpDataFull = true; 54 boolean dumpDataFull = true;
52 boolean dumpDataStats = false; 55 boolean dumpDataStats = false;
53 if (args.length > 1) { 56 if (args.length > 1) {
54 repo = args[0]; 57 repo = args[0];
55 filename = args[1]; 58 filename = args[1];
138 sb.append(String.format("%d..%d, %d", s, e, l)); 141 sb.append(String.format("%d..%d, %d", s, e, l));
139 if (completeDataDump) { 142 if (completeDataDump) {
140 byte[] src = new byte[l]; 143 byte[] src = new byte[l];
141 dis.read(src, 0, l); 144 dis.read(src, 0, l);
142 sb.append(":"); 145 sb.append(":");
143 sb.append(new String(src, 0, l, "UTF-8")); 146 sb.append(escape(new String(src, 0, l, "UTF-8")));
144 } else { 147 } else {
145 dis.skipBytes(l); 148 dis.skipBytes(l);
146 } 149 }
147 sb.append('\n'); 150 sb.append('\n');
148 } 151 }
149 return sb.toString(); 152 return sb.toString();
150 } else { 153 } else {
151 if (completeDataDump) { 154 if (completeDataDump) {
152 return new String(data, offset, len, "UTF-8"); 155 return escape(new String(data, offset, len, "UTF-8"));
153 } 156 }
154 return String.format("<DATA>:%d bytes", len-offset); 157 return String.format("<DATA>:%d bytes", len-offset);
155 } 158 }
156 } 159 }
160
161 private static Pattern controlCharPattern = Pattern.compile("\\p{Cntrl}");
162 // \p{Cntrl} A control character: [\x00-\x1F\x7F]
163 private static String[] replacements = new String[33];
164 static {
165 for (int i = 0; i < 32; i++) {
166 // no idea why need FOUR backslashes to get only one in printout
167 replacements[i] = String.format("\\\\%X", i);
168 }
169 replacements[32] = String.format("\\\\%X", 127);
170 }
171 // handy to get newline-separated data printed on newlines.
172 // set to false for non-printable data (e.g. binaries, where \n doesn't make sense)
173 private static boolean leaveNewlineInData = true;
174
175 private static String escape(CharSequence possiblyWithBinary) {
176 Matcher m = controlCharPattern.matcher(possiblyWithBinary);
177 StringBuffer rv = new StringBuffer();
178 while (m.find()) {
179 char c = m.group().charAt(0);
180 if (leaveNewlineInData && c == '\n') {
181 continue;
182 }
183 int x = (int) c;
184 m.appendReplacement(rv, replacements[x == 127 ? 32 : x]);
185 }
186 m.appendTail(rv);
187 return rv.toString();
188 }
157 } 189 }