-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkingList.java
More file actions
320 lines (293 loc) · 9.04 KB
/
WorkingList.java
File metadata and controls
320 lines (293 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class WorkingList {
private static class Item {
String type; // "blob" or "tree"
String sha;
String path; // forward slashes
Item(String t, String s, String p) {
this.type = t;
this.sha = s;
this.path = p;
}
}
public String build() throws Exception {
ensureObjects();
ArrayList<Item> list = read();
sort(list);
while (true) {
if (list.size() == 1) {
Item only = list.get(0);
if (only.type.equals("tree")) {
return only.sha;
}
}
String dir = leafDir(list);
if (dir == null) {
String rootSha = makeTree("", list);
ArrayList<Item> one = new ArrayList<Item>();
one.add(new Item("tree", rootSha, ""));
list = one;
return rootSha;
}
String dirSha = makeTree(dir, list);
list = collapse(list, dir, dirSha);
}
}
private ArrayList<Item> read() throws Exception {
ArrayList<Item> list = new ArrayList<Item>();
File idx = new File("git" + File.separator + "index");
if (!idx.exists()) {
return list;
}
BufferedReader br = new BufferedReader(new FileReader(idx));
String line = br.readLine();
while (line != null) {
String s = line.trim();
if (s.length() > 0) {
int sp = s.indexOf(' ');
if (sp != -1) {
String sha = s.substring(0, sp);
String p = s.substring(sp + 1).replace('\\', '/');
list.add(new Item("blob", sha, p));
}
}
line = br.readLine();
}
br.close();
return list;
}
private void sort(ArrayList<Item> list) {
Collections.sort(list, new Comparator<Item>() {
public int compare(Item a, Item b) {
return a.path.compareTo(b.path);
}
});
}
private String leafDir(ArrayList<Item> list) {
String best = null;
int bestDepth = -1;
int i = 0;
while (i < list.size()) {
Item it = list.get(i);
String parent = findParentDir(it.path);
if (parent.length() > 0) {
boolean deeper = needsChildBuild(list, parent);
if (deeper == false) {
int d = depth(parent);
if (d > bestDepth) {
bestDepth = d;
best = parent;
}
}
}
i = i + 1;
}
return best;
}
private boolean needsChildBuild(ArrayList<Item> list, String dir) {
String prefix = dir + "/";
int i = 0;
while (i < list.size()) {
Item it = list.get(i);
if (it.path.startsWith(prefix)) {
String rest = it.path.substring(prefix.length());
int slash = rest.indexOf('/');
if (slash != -1) {
String childDir = dir + "/" + rest.substring(0, slash);
boolean childDone = hasTree(list, childDir);
if (childDone == false) {
return true;
}
}
}
i = i + 1;
}
return false;
}
private boolean hasTree(ArrayList<Item> list, String dir) {
int i = 0;
while (i < list.size()) {
Item it = list.get(i);
if (it.type.equals("tree")) {
if (it.path.equals(dir)) {
return true;
}
}
i = i + 1;
}
return false;
}
private String makeTree(String dir, ArrayList<Item> list) throws Exception {
ArrayList<String> lines = new ArrayList<String>();
int i = 0;
while (i < list.size()) {
Item it = list.get(i);
String parent = findParentDir(it.path);
if (parent.equals(dir)) {
String name = base(it.path);
if (it.type.equals("blob")) {
if (name.length() > 0) {
if (name.indexOf('/') == -1) {
lines.add("blob " + it.sha + " " + name);
}
}
} else {
lines.add("tree " + it.sha + " " + name);
}
}
i = i + 1;
}
Collections.sort(lines);
String data = join(lines);
String sha = sha1(data);
writeObj(sha, data);
return sha;
}
private ArrayList<Item> collapse(ArrayList<Item> list, String dir, String sha) {
ArrayList<Item> out = new ArrayList<Item>();
String prefix;
if (dir.length() == 0) {
prefix = "";
} else {
prefix = dir + "/";
}
int i = 0;
while (i < list.size()) {
Item it = list.get(i);
boolean drop = false;
if (dir.length() == 0) {
String parent = findParentDir(it.path);
if (parent.equals("")) {
drop = true;
}
} else {
if (it.path.startsWith(prefix)) {
String rest = it.path.substring(prefix.length());
int slash = rest.indexOf('/');
if (slash == -1) {
drop = true;
} else {
String maybeChildDir = dir + "/" + rest.substring(0, slash);
if (it.type.equals("tree")) {
if (it.path.equals(maybeChildDir)) {
drop = true;
}
}
}
}
}
if (drop == false) {
out.add(it);
}
i = i + 1;
}
out.add(new Item("tree", sha, dir));
sort(out);
return out;
}
private String findParentDir(String path) {
if (path == null) {
return "";
}
String s = path.replace('\\', '/');
int i = s.lastIndexOf('/');
if (i == -1) {
return "";
} else {
return s.substring(0, i);
}
}
private String base(String path) {
if (path == null) {
return "";
}
String s = path.replace('\\', '/');
int i = s.lastIndexOf('/');
if (i == -1) {
return s;
} else {
return s.substring(i + 1);
}
}
private int depth(String path) {
if (path == null) {
return 0;
}
if (path.length() == 0) {
return 0;
}
int c = 1;
int i = 0;
while (i < path.length()) {
if (path.charAt(i) == '/') {
c = c + 1;
}
i = i + 1;
}
return c;
}
private String join(ArrayList<String> lines) {
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < lines.size()) {
sb.append(lines.get(i));
if (i < lines.size() - 1) {
sb.append("\n");
}
i = i + 1;
}
return sb.toString();
}
private void ensureObjects() {
File git = new File("git");
if (!git.exists()) {
git.mkdir();
}
File objects = new File(git, "objects");
if (!objects.exists()) {
objects.mkdir();
}
}
private void writeObj(String sha, String data) throws Exception {
File out = new File("git" + File.separator + "objects", sha);
if (!out.exists()) {
FileWriter w = new FileWriter(out, false);
w.write(data);
w.flush();
w.close();
}
}
private String sha1(String s) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-1 not available");
}
byte[] dig = md.digest(s.getBytes(StandardCharsets.UTF_8));
return toHex(dig);
}
private String toHex(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
int i = 0;
while (i < b.length) {
int v = b[i] & 0xff;
String h = Integer.toHexString(v);
if (h.length() == 1) {
sb.append('0');
}
sb.append(h);
i = i + 1;
}
return sb.toString();
}
}