comparison src/org/tmatesoft/hg/repo/HgDirstate.java @ 412:63c5a9d7ca3f smartgit3

Follow-up for Issue 29: unify path translation for manifest and dirstate
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 21 Mar 2012 14:54:02 +0100
parents b015f3918120
children 528b6780a8bd
comparison
equal deleted inserted replaced
411:464b4404e75d 412:63c5a9d7ca3f
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 *
21 import java.io.BufferedReader; 21 import java.io.BufferedReader;
22 import java.io.File; 22 import java.io.File;
23 import java.io.FileNotFoundException; 23 import java.io.FileNotFoundException;
24 import java.io.FileReader; 24 import java.io.FileReader;
25 import java.io.IOException; 25 import java.io.IOException;
26 import java.nio.charset.CharacterCodingException;
26 import java.util.Collections; 27 import java.util.Collections;
27 import java.util.HashMap; 28 import java.util.HashMap;
28 import java.util.LinkedHashMap; 29 import java.util.LinkedHashMap;
29 import java.util.Map; 30 import java.util.Map;
30 import java.util.TreeSet; 31 import java.util.TreeSet;
31 32
32 import org.tmatesoft.hg.core.HgInvalidControlFileException; 33 import org.tmatesoft.hg.core.HgInvalidControlFileException;
33 import org.tmatesoft.hg.core.Nodeid; 34 import org.tmatesoft.hg.core.Nodeid;
34 import org.tmatesoft.hg.internal.DataAccess; 35 import org.tmatesoft.hg.internal.DataAccess;
36 import org.tmatesoft.hg.internal.EncodingHelper;
35 import org.tmatesoft.hg.util.Pair; 37 import org.tmatesoft.hg.util.Pair;
36 import org.tmatesoft.hg.util.Path; 38 import org.tmatesoft.hg.util.Path;
37 import org.tmatesoft.hg.util.PathPool; 39 import org.tmatesoft.hg.util.PathPool;
38 import org.tmatesoft.hg.util.PathRewrite; 40 import org.tmatesoft.hg.util.PathRewrite;
39 41
72 dirstateFile = dirstate; // XXX decide whether file names shall be kept local to reader (see #branches()) or passed from outside 74 dirstateFile = dirstate; // XXX decide whether file names shall be kept local to reader (see #branches()) or passed from outside
73 this.pathPool = pathPool; 75 this.pathPool = pathPool;
74 canonicalPathRewrite = canonicalPath; 76 canonicalPathRewrite = canonicalPath;
75 } 77 }
76 78
77 /*package-local*/ void read() throws HgInvalidControlFileException { 79 /*package-local*/ void read(EncodingHelper encodingHelper) throws HgInvalidControlFileException {
78 normal = added = removed = merged = Collections.<Path, Record>emptyMap(); 80 normal = added = removed = merged = Collections.<Path, Record>emptyMap();
79 parents = new Pair<Nodeid,Nodeid>(Nodeid.NULL, Nodeid.NULL); 81 parents = new Pair<Nodeid,Nodeid>(Nodeid.NULL, Nodeid.NULL);
80 if (canonicalPathRewrite != null) { 82 if (canonicalPathRewrite != null) {
81 canonical2dirstateName = new HashMap<Path,Path>(); 83 canonical2dirstateName = new HashMap<Path,Path>();
82 } else { 84 } else {
106 String fn1 = null, fn2 = null; 108 String fn1 = null, fn2 = null;
107 byte[] name = new byte[nameLen]; 109 byte[] name = new byte[nameLen];
108 da.readBytes(name, 0, nameLen); 110 da.readBytes(name, 0, nameLen);
109 for (int i = 0; i < nameLen; i++) { 111 for (int i = 0; i < nameLen; i++) {
110 if (name[i] == 0) { 112 if (name[i] == 0) {
111 fn1 = new String(name, 0, i, "UTF-8"); // XXX unclear from documentation what encoding is used there 113 fn1 = encodingHelper.fromDirstate(name, 0, i);
112 fn2 = new String(name, i+1, nameLen - i - 1, "UTF-8"); // need to check with different system codepages 114 fn2 = encodingHelper.fromDirstate(name, i+1, nameLen - i - 1);
113 break; 115 break;
114 } 116 }
115 } 117 }
116 if (fn1 == null) { 118 if (fn1 == null) {
117 fn1 = new String(name); 119 fn1 = encodingHelper.fromDirstate(name, 0, nameLen);
118 } 120 }
119 Record r = new Record(fmode, size, time, pathPool.path(fn1), fn2 == null ? null : pathPool.path(fn2)); 121 Record r = new Record(fmode, size, time, pathPool.path(fn1), fn2 == null ? null : pathPool.path(fn2));
120 if (canonicalPathRewrite != null) { 122 if (canonicalPathRewrite != null) {
121 Path canonicalPath = pathPool.path(canonicalPathRewrite.rewrite(fn1).toString()); 123 Path canonicalPath = pathPool.path(canonicalPathRewrite.rewrite(fn1).toString());
122 if (canonicalPath != r.name()) { // == as they come from the same pool 124 if (canonicalPath != r.name()) { // == as they come from the same pool
143 merged.put(r.name1, r); 145 merged.put(r.name1, r);
144 } else { 146 } else {
145 repo.getContext().getLog().warn(getClass(), "Dirstate record for file %s (size: %d, tstamp:%d) has unknown state '%c'", r.name1, r.size(), r.time, state); 147 repo.getContext().getLog().warn(getClass(), "Dirstate record for file %s (size: %d, tstamp:%d) has unknown state '%c'", r.name1, r.size(), r.time, state);
146 } 148 }
147 } 149 }
150 } catch (CharacterCodingException ex) {
151 throw new HgInvalidControlFileException(String.format("Failed reading file names from dirstate using encoding %s", encodingHelper.charset().name()), ex, dirstateFile);
148 } catch (IOException ex) { 152 } catch (IOException ex) {
149 throw new HgInvalidControlFileException("Dirstate read failed", ex, dirstateFile); 153 throw new HgInvalidControlFileException("Dirstate read failed", ex, dirstateFile);
150 } finally { 154 } finally {
151 da.done(); 155 da.done();
152 } 156 }