comparison hg4j/src/main/java/org/tmatesoft/hg/repo/HgDirstate.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
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@hg4j.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 import org.tmatesoft.hg.util.Path;
29
30
31 /**
32 * @see http://mercurial.selenic.com/wiki/DirState
33 * @see http://mercurial.selenic.com/wiki/FileFormats#dirstate
34 *
35 * @author Artem Tikhomirov
36 * @author TMate Software Ltd.
37 */
38 class HgDirstate {
39
40 private final DataAccessProvider accessProvider;
41 private final File dirstateFile;
42 // deliberate String, not Path as it seems useless to keep Path here
43 private Map<String, Record> normal;
44 private Map<String, Record> added;
45 private Map<String, Record> removed;
46 private Map<String, Record> merged;
47
48 /*package-local*/ HgDirstate() {
49 // empty instance
50 accessProvider = null;
51 dirstateFile = null;
52 }
53
54 public HgDirstate(DataAccessProvider dap, File dirstate) {
55 accessProvider = dap;
56 dirstateFile = dirstate;
57 }
58
59 private void read() {
60 normal = added = removed = merged = Collections.<String, Record>emptyMap();
61 if (dirstateFile == null || !dirstateFile.exists()) {
62 return;
63 }
64 DataAccess da = accessProvider.create(dirstateFile);
65 if (da.isEmpty()) {
66 return;
67 }
68 // not sure linked is really needed here, just for ease of debug
69 normal = new LinkedHashMap<String, Record>();
70 added = new LinkedHashMap<String, Record>();
71 removed = new LinkedHashMap<String, Record>();
72 merged = new LinkedHashMap<String, Record>();
73 try {
74 // XXX skip(40) if we don't need these?
75 byte[] parents = new byte[40];
76 da.readBytes(parents, 0, 40);
77 parents = null;
78 do {
79 final byte state = da.readByte();
80 final int fmode = da.readInt();
81 final int size = da.readInt();
82 final int time = da.readInt();
83 final int nameLen = da.readInt();
84 String fn1 = null, fn2 = null;
85 byte[] name = new byte[nameLen];
86 da.readBytes(name, 0, nameLen);
87 for (int i = 0; i < nameLen; i++) {
88 if (name[i] == 0) {
89 fn1 = new String(name, 0, i, "UTF-8"); // XXX unclear from documentation what encoding is used there
90 fn2 = new String(name, i+1, nameLen - i - 1, "UTF-8"); // need to check with different system codepages
91 break;
92 }
93 }
94 if (fn1 == null) {
95 fn1 = new String(name);
96 }
97 Record r = new Record(fmode, size, time, fn1, fn2);
98 if (state == 'n') {
99 normal.put(r.name1, r);
100 } else if (state == 'a') {
101 added.put(r.name1, r);
102 } else if (state == 'r') {
103 removed.put(r.name1, r);
104 } else if (state == 'm') {
105 merged.put(r.name1, r);
106 } else {
107 // FIXME log error?
108 }
109 } while (!da.isEmpty());
110 } catch (IOException ex) {
111 ex.printStackTrace(); // FIXME log error, clean dirstate?
112 } finally {
113 da.done();
114 }
115 }
116
117 // new, modifiable collection
118 /*package-local*/ TreeSet<String> all() {
119 read();
120 TreeSet<String> rv = new TreeSet<String>();
121 @SuppressWarnings("unchecked")
122 Map<String, Record>[] all = new Map[] { normal, added, removed, merged };
123 for (int i = 0; i < all.length; i++) {
124 for (Record r : all[i].values()) {
125 rv.add(r.name1);
126 }
127 }
128 return rv;
129 }
130
131 /*package-local*/ Record checkNormal(Path fname) {
132 return normal.get(fname.toString());
133 }
134
135 /*package-local*/ Record checkAdded(Path fname) {
136 return added.get(fname.toString());
137 }
138 /*package-local*/ Record checkRemoved(Path fname) {
139 return removed.get(fname.toString());
140 }
141 /*package-local*/ Record checkRemoved(String fname) {
142 return removed.get(fname);
143 }
144 /*package-local*/ Record checkMerged(Path fname) {
145 return merged.get(fname.toString());
146 }
147
148
149
150
151 /*package-local*/ void dump() {
152 read();
153 @SuppressWarnings("unchecked")
154 Map<String, Record>[] all = new Map[] { normal, added, removed, merged };
155 char[] x = new char[] {'n', 'a', 'r', 'm' };
156 for (int i = 0; i < all.length; i++) {
157 for (Record r : all[i].values()) {
158 System.out.printf("%c %3o%6d %30tc\t\t%s", x[i], r.mode, r.size, (long) r.time * 1000, r.name1);
159 if (r.name2 != null) {
160 System.out.printf(" --> %s", r.name2);
161 }
162 System.out.println();
163 }
164 System.out.println();
165 }
166 }
167
168 /*package-local*/ static class Record {
169 final int mode;
170 final int size;
171 final int time;
172 final String name1;
173 final String name2;
174
175 public Record(int fmode, int fsize, int ftime, String name1, String name2) {
176 mode = fmode;
177 size = fsize;
178 time = ftime;
179 this.name1 = name1;
180 this.name2 = name2;
181
182 }
183 }
184 }