forked from hlevy108/GitProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTREE.java
More file actions
85 lines (82 loc) · 2.79 KB
/
TREE.java
File metadata and controls
85 lines (82 loc) · 2.79 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
import java.io.File;
import java.io.IOException;
public class TREE {
public static void createROOT() {
String fileContents = "";
for (File f : new File(".").listFiles()) {
String key = createTREE(f.getName());
if (key.equals("dns")) {
continue;
}
if (!fileContents.isBlank()) {
fileContents += "\n";
}
String type = "";
if (f.isFile()) {
type = "blob";
} else {
type = "tree";
}
fileContents += type + " " + key + " " + f.getName();
}
String key = SHA1.encryptThisString(fileContents);
if (fileContents.isBlank()) {
return;
}
File TREE = new File("git" + File.separator + "objects" + File.separator + key);
try {
TREE.createNewFile();
} catch (IOException e) {
System.out.println(e);
}
BLOB.copyToBlob(fileContents, TREE);
}
public static String createTREE(String dirPath) {
File file = new File(dirPath);
String fileContents = "";
if (file.isFile()) { // for recursion, if its a file just end the loop here
String key = SHA1.encryptThisString(BLOB.getFileContents(file));
if (!verifyIndexUpdate(dirPath, new File(key), new File("git" + File.separator + "index"))) {
return "dns";
}
return key;
}
if (file.listFiles() == null) {
return "dns";
}
for (File f : file.listFiles()) { // go through each file in the directory and make the tree file
if (createTREE(dirPath + File.separator + f.getName()).equals("dns")) {
continue;
}
if (!fileContents.isBlank()) {
fileContents += "\n";
}
String type = "";
if (f.isFile()) {
type = "blob";
} else {
type = "tree";
}
fileContents += type + " " + createTREE(f.getPath()) + " " + f.getName();
}
String key = SHA1.encryptThisString(fileContents);
if (fileContents.isBlank()) {
return "dns";
}
File TREE = new File("git" + File.separator + "objects" + File.separator + key);
try {
TREE.createNewFile();
} catch (IOException e) {
System.out.println(e);
}
BLOB.copyToBlob(fileContents, TREE);
return key;
}
public static boolean verifyIndexUpdate(String path, File blob, File index) {
String contents = BLOB.getFileContents(index);
if (!contents.contains(blob.getName() + " " + path)) {
return false;
}
return true;
}
}