-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTree.java
More file actions
416 lines (305 loc) · 12.7 KB
/
Tree.java
File metadata and controls
416 lines (305 loc) · 12.7 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
public class Tree {
public ArrayList<String> trees;
public ArrayList<String> blobs;
private int numberOfCommits;
private File pointedAtFolder;
private String startPath = "/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/";
File tree;
public Tree() throws IOException {
File objects = new File("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/");
if (!objects.exists()) {
objects.mkdir();
}
tree = new File(startPath + "Objects/Tree");
if (!tree.exists()) {
tree.createNewFile();
}
File file2 = new File("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/");
if (!file2.exists()) {
file2.mkdir();
}
File index = new File("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/Index");
if (!index.exists()) {
index.createNewFile();
}
File treeIndex = new File("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/Tree");
if (!treeIndex.exists()) {
treeIndex.createNewFile();
}
this.numberOfCommits = 0;
this.blobs = new ArrayList<String>();
this.trees = new ArrayList<String>();
}
public Tree(String name) throws IOException {
File file = new File("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/" + name);
if (!file.exists()) {
file.createNewFile();
}
File objects = new File("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/");
if (!objects.exists()) {
objects.mkdir();
}
File index = new File("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/Index");
if (!index.exists()) {
index.createNewFile();
}
File treeIndex = new File("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/Tree");
if (!treeIndex.exists()) {
treeIndex.createNewFile();
}
this.numberOfCommits = 0;
this.blobs = new ArrayList<String>();
this.trees = new ArrayList<String>();
}
public void pointToFile(String folderName) {
File folder = new File("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/CommitTester.java");
if (folder.isDirectory()) {
}
}
// if type does not have "tree : " or "blob : " it must not be a valid string
// input
// if type contains "tree : " add it to the array list of trees and vice versa
public boolean add(String fileName, String inputString) throws IOException, NoSuchAlgorithmException {
String type = fileName;
File fileToAdd = new File(inputString + fileName);
if (!fileToAdd.exists()) {
String isTree = fileName.substring(0, 6);
if (!isTree.equalsIgnoreCase("tree :")) {
throw new FileNotFoundException("Invalid file to add");
}
}
if (fileToAdd.exists() && fileToAdd.isFile()) {
String fileContents = Helper.fileContents(fileToAdd);
String hashOfFile = Blob.getSha1(fileContents);
String newEntryForTree = "Blob : " + hashOfFile + " : " + fileName;
blobs.add(newEntryForTree);
Blob b = new Blob(fileName);
b.makeFile();
} else if (fileToAdd.isDirectory()) {
String folderContents = Helper.fileContents(fileToAdd);
String newEntryForTree = "Tree : " + Helper.getSHA1(folderContents) + " : " + fileName;
System.out.println("Added " + fileName);
addDirectory(inputString + fileName + "/");
} else {
String newEntryForTree = fileName;
trees.add(newEntryForTree);
}
// entries.add(fileToAdd)
// the method accepts a filename, OR a tree string
// tree : HASH : folderName
writeTree();
if (type.contains("tree : ") || type.contains("blob : ")) {
if (type.contains("tree : ") && !trees.contains(type)) {
trees.add(type);
return true;
} else if (type.contains("blob : ") && !blobs.contains(type)) {
blobs.add(type);
return true;
}
}
return false;
}
// if the string contains ".txt" then you must be tyring to remove via file name
// loop through the blobs
// loop through the trees
// note that if type is a sha1 file, you must loop through both the trees and
// the blob array list
public boolean remove(String name) throws IOException {
// removes a blob with its name
for (int i = blobs.size()-1; i >= 0; i--)
{
if (blobs.get(i).indexOf (name) != -1)
{
blobs.remove(i);
}
}
for (int i = trees.size()-1; i >= 0; i--)
{
if (trees.get(i).indexOf (name) != -1)
{
trees.remove(i);
}
}
// for (String s : blobs) {
// if (s.contains(name)) {
// blobs.remove(s);
// }
// }
// for (String s : trees) {
// if (s.contains(name)) {
// trees.remove(s);
// }
// }
writeTree();
return true;
}
public String generateBlob () throws NoSuchAlgorithmException, IOException
{
File tree = new File (startPath + "Objects/Tree");
Blob blob = new Blob (startPath + "Objects/Tree");
blob.makeFile();
return Blob.getSha1(Helper.fileContents(tree));
}
public void save() throws Exception {
if (numberOfCommits != calculateNumberOfCommits()) {
numberOfCommits = calculateNumberOfCommits();
String SHA1 = generateSHA1();
Path objectFilePath = Paths.get("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects", SHA1);
String originalString = returnStringOfCommits();
byte[] originalSByte = originalString.getBytes();
Files.write(objectFilePath, originalSByte);
} else {
throw new Exception("Cannot save when you have not added a new tree or blob");
}
}
// return the number of string objects in both the blobs and trees array list
public int calculateNumberOfCommits() {
return trees.size() + blobs.size();
}
// generate the sha1 hash
public String generateSHA1() throws NoSuchAlgorithmException {
StringBuilder sb = new StringBuilder("");
for (String s : trees) {
sb.append(s);
}
for (String s : blobs) {
sb.append(s);
}
String input = sb.toString();
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(input.getBytes());
StringBuffer sbuffer = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sbuffer.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return sbuffer.toString();
}
// returns a giant string of all the commits withing blobs array and trees array
public String returnStringOfCommits() {
StringBuilder sb = new StringBuilder("");
int numBlobs = blobs.size();
int numTrees = trees.size();
for (int i = 0; i < numBlobs; i++) {
if (i != numBlobs - 1) {
sb.append(blobs.get(i) + "\n");
} else if (numTrees >= 1) {
sb.append(blobs.get(i) + "\n");
} else {
sb.append(blobs.get(i));
}
}
for (int i = 0; i < numTrees; i++) {
if (i != numTrees - 1) {
sb.append(trees.get(i) + "\n");
} else {
sb.append(trees.get(i));
}
}
return sb.toString();
}
public String allContents() throws IOException { // got from prev shared code
StringBuilder resultStringBuilder = new StringBuilder();
StringBuilder record = new StringBuilder("");
BufferedReader br = new BufferedReader(
new FileReader("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/Tree"));
while (br.ready()) {
record.append((char) br.read());
}
br.close();
String s = record.toString() + trees.toString() + blobs.toString();
return s;
}
public String allBlobs() {
String out = "";
for (String s : blobs) {
out += s;
out += "\n";
}
return out;
}
public String allTrees() {
String out = "";
for (String s : trees) {
out += s;
out += "\n";
}
return out;
}
public void writeTree() throws IOException {
String toWrite = allTrees() + allBlobs();
Helper.writeToFile(toWrite, "Tree", "/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/");
}
public String addDirectory(String directoryPath) throws NoSuchAlgorithmException, IOException {
File directory = new File(directoryPath);
if (directory.isDirectory()) {
ArrayList <String> blobsInternal = new ArrayList<>();
ArrayList <String> treesInternal = new ArrayList<>();
int len = "/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/".length();
String folderName = directoryPath.substring(len, directoryPath.length() - 1);
System.out.println(folderName);
String contents[] = directory.list(); // gotten from internet -->
// https://www.tutorialspoint.com/how-to-get-list-of-all-files-folders-from-a-folder-in-java#:~:text=The%20List()%20method,of%20the%20files%20and%20directories.
String output = "";
for (String s : contents) {
File temp = new File(directoryPath + s);
// char sLast = s.charAt(s.length() - 1);
// Path p = Paths.get(s);
if (!temp.isDirectory()) {
Blob b1 = new Blob(directoryPath + s);
b1.makeFile();
output += "Blob : " + b1.getSha1(b1.fileContents()) + " : " + s + " \n";
blobsInternal.add("Blob : " + b1.getSha1(b1.fileContents()) + " : " + s);
System.out.println("Blob Sha : " + b1.getSha1(b1.fileContents()));
} else {
Tree childTree = new Tree();
Blob b2 = new Blob(directoryPath + s);
String childSHA = childTree.addDirectory(directoryPath + s + "/");
System.out.println("Subtree Sha: " + childSHA);
output += "Tree : " + childSHA + " : " + s + "\n";
blobsInternal.add("Tree : " + childSHA + " : " + s);
}
}
String outputSHA = Blob.getSha1(output);
StringBuilder sb = new StringBuilder("");
for (String s : treesInternal) {
sb.append(s);
}
for (String s : blobsInternal) {
sb.append(s);
}
String input = sb.toString();
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(input.getBytes());
StringBuffer sbuffer = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sbuffer.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
outputSHA = sbuffer.toString();
outputSHA = Blob.getSha1(output);
PrintWriter pw = new PrintWriter(
"/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/" + outputSHA);
pw.print(output);
pw.close();
trees.add("Tree : " + outputSHA + " : " + folderName);
System.out.println("Done " + Blob.getSha1("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/" + outputSHA));
String oldOutput = Blob.getSha1("/Users/lilbarbar/Desktop/Honors Topics/Programming-Git/Objects/" + outputSHA);
return outputSHA;
}
return null;
}
}