comparison src/org/tmatesoft/hg/repo/HgDirstate.java @ 74:6f1b88693d48

Complete refactoring to org.tmatesoft
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 24 Jan 2011 03:14:45 +0100
parents src/com/tmate/hgkit/ll/HgDirstate.java@b771e94a4f7c
children d55d4eedfc57
comparison
equal deleted inserted replaced
73:0d279bcc4442 74:6f1b88693d48
1 /*
2 * Copyright (c) 2010-2011 TMate Software Ltd
3 *
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
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@svnkit.com
16 */
17 package org.tmatesoft.hg.repo;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.Collections;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24 import java.util.TreeSet;
25
26 import org.tmatesoft.hg.internal.DataAccess;
27 import org.tmatesoft.hg.internal.DataAccessProvider;
28
29
30 /**
31 * @see http://mercurial.selenic.com/wiki/DirState
32 * @see http://mercurial.selenic.com/wiki/FileFormats#dirstate
33 *
34 * @author Artem Tikhomirov
35 * @author TMate Software Ltd.
36 */
37 public class HgDirstate {
38
39 private final DataAccessProvider accessProvider;
40 private final File dirstateFile;
41 private Map<String, Record> normal;
42 private Map<String, Record> added;
43 private Map<String, Record> removed;
44 private Map<String, Record> merged;
45
46 /*package-local*/ HgDirstate() {
47 // empty instance
48 accessProvider = null;
49 dirstateFile = null;
50 }
51
52 public HgDirstate(DataAccessProvider dap, File dirstate) {
53 accessProvider = dap;
54 dirstateFile = dirstate;
55 }
56
57 private void read() {
58 normal = added = removed = merged = Collections.<String, Record>emptyMap();
59 if (dirstateFile == null || !dirstateFile.exists()) {
60 return;
61 }
62 DataAccess da = accessProvider.create(dirstateFile);
63 if (da.isEmpty()) {
64 return;
65 }
66 // not sure linked is really needed here, just for ease of debug
67 normal = new LinkedHashMap<String, Record>();
68 added = new LinkedHashMap<String, Record>();
69 removed = new LinkedHashMap<String, Record>();
70 merged = new LinkedHashMap<String, Record>();
71 try {
72 // XXX skip(40) if we don't need these?
73 byte[] parents = new byte[40];
74 da.readBytes(parents, 0, 40);
75 parents = null;
76 do {
77 final byte state = da.readByte();
78 final int fmode = da.readInt();
79 final int size = da.readInt();
80 final int time = da.readInt();
81 final int nameLen = da.readInt();
82 String fn1 = null, fn2 = null;
83 byte[] name = new byte[nameLen];
84 da.readBytes(name, 0, nameLen);
85 for (int i = 0; i < nameLen; i++) {
86 if (name[i] == 0) {
87 fn1 = new String(name, 0, i, "UTF-8"); // XXX unclear from documentation what encoding is used there
88 fn2 = new String(name, i+1, nameLen - i - 1, "UTF-8"); // need to check with different system codepages
89 break;
90 }
91 }
92 if (fn1 == null) {
93 fn1 = new String(name);
94 }
95 Record r = new Record(fmode, size, time, fn1, fn2);
96 if (state == 'n') {
97 normal.put(r.name1, r);
98 } else if (state == 'a') {
99 added.put(r.name1, r);
100 } else if (state == 'r') {
101 removed.put(r.name1, r);
102 } else if (state == 'm') {
103 merged.put(r.name1, r);
104 } else {
105 // FIXME log error?
106 }
107 } while (!da.isEmpty());
108 } catch (IOException ex) {
109 ex.printStackTrace(); // FIXME log error, clean dirstate?
110 } finally {
111 da.done();
112 }
113 }
114
115 // new, modifiable collection
116 /*package-local*/ TreeSet<String> all() {
117 read();
118 TreeSet<String> rv = new TreeSet<String>();
119 @SuppressWarnings("unchecked")
120 Map<String, Record>[] all = new Map[] { normal, added, removed, merged };
121 for (int i = 0; i < all.length; i++) {
122 for (Record r : all[i].values()) {
123 rv.add(r.name1);
124 }
125 }
126 return rv;
127 }
128
129 /*package-local*/ Record checkNormal(String fname) {
130 return normal.get(fname);
131 }
132
133 /*package-local*/ Record checkAdded(String fname) {
134 return added.get(fname);
135 }
136 /*package-local*/ Record checkRemoved(String fname) {
137 return removed.get(fname);
138 }
139 /*package-local*/ Record checkMerged(String fname) {
140 return merged.get(fname);
141 }
142
143
144
145
146 public void dump() {
147 read();
148 @SuppressWarnings("unchecked")
149 Map<String, Record>[] all = new Map[] { normal, added, removed, merged };
150 char[] x = new char[] {'n', 'a', 'r', 'm' };
151 for (int i = 0; i < all.length; i++) {
152 for (Record r : all[i].values()) {
153 System.out.printf("%c %3o%6d %30tc\t\t%s", x[i], r.mode, r.size, (long) r.time * 1000, r.name1);
154 if (r.name2 != null) {
155 System.out.printf(" --> %s", r.name2);
156 }
157 System.out.println();
158 }
159 System.out.println();
160 }
161 }
162
163 /*package-local*/ static class Record {
164 final int mode;
165 final int size;
166 final int time;
167 final String name1;
168 final String name2;
169
170 public Record(int fmode, int fsize, int ftime, String name1, String name2) {
171 mode = fmode;
172 size = fsize;
173 time = ftime;
174 this.name1 = name1;
175 this.name2 = name2;
176
177 }
178 }
179 }