-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtree.java
More file actions
227 lines (205 loc) · 8.06 KB
/
tree.java
File metadata and controls
227 lines (205 loc) · 8.06 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
import java.util.*;
import javafx.scene.Parent;
import java.io.*;
public class Tree {
private StringBuilder sData;
private Map<String, String> hMap;
private List<String> tree;
private static final String deleted = "*deleted*";
private static final String edited = "*edited*";
public Tree() {
sData = new StringBuilder();
hMap = new HashMap<>();
tree = new ArrayList<>();
}
public void writeToFile() throws Exception {
String sFile = Blob.hashStringToSHA1(sData.toString());
PrintWriter w_git = new PrintWriter(new BufferedWriter(new FileWriter("./objects/" + sFile)));
w_git.print(sData.toString());
w_git.close();
}
public void addIndex(String parentTree) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader("index"))) {
String line;
while ((line = br.readLine()) != null) {
if (line.contains(deleted)) {
String filetoDelete = line.substring(10);
// deleteOrEdit(line, parentTree);
writeNewTreeDelete(parentTree, filetoDelete);
}
String[] parts = line.split(" : ");
if (parts.length >= 3) {
String type = parts[0];
String hash = parts[1];
String name = parts[2];
if (type.equals("blob")) {
sData.append("blob").append(hash).append(" : ").append(name).append("\n");
} else if (type.equals("tree")) {
sData.append("tree : ").append(hash).append("\n");
} else {
deleteOrEdit(line, parentTree);
}
} else {
sData.append(line).append("\n");
}
}
}
}
public void writeNewTreeDelete(String parentTree, String fileToDelete) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader("./objects/" + parentTree))) {
String line;
while ((line = br.readLine()) != null) {
if (!line.contains(fileToDelete)) {
sData.append(line);
}
}
}
}
public void replaceEditedFileEntry(String fileName, String newSha) {
String[] lines = sData.toString().split("\\n");
StringBuilder newContent = new StringBuilder();
for (String line : lines) {
if (line.contains(fileName)) {
line = "blob : " + newSha + " : " + fileName;
}
newContent.append(line).append("\n");
}
sData = new StringBuilder(newContent.toString().trim());
}
public void markEdited(String sFile) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("index", true))) {
bw.write(edited + " " + sFile + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
public void add(String sLine) {
String strData = sData.toString();
if (strData.contains(sLine))
return;
else {
sData.append(sLine + "\n");
}
}
public void remove(String sFile) {
String sContent = sData.toString();
if (!sContent.contains(sFile))
return;
StringBuilder newContent = new StringBuilder();
Scanner sc = new Scanner(sContent);
while (sc.hasNextLine()) {
String curLine = sc.nextLine();
if (curLine.contains(sFile))
newContent.append("\n");
else
newContent.append(curLine).append("\n");
}
sc.close();
sData = newContent;
}
public void getContents() throws IOException {
for (Map.Entry<String, String> k : hMap.entrySet()) {
String line = "blob : " + k.getKey() + " : " + k.getValue();
sData.append(line).append("\n");
}
for (int i = 0; i < tree.size(); i++) {
String line = "tree : " + tree.get(i);
sData.append(line).append("\n");
}
File fileToDelete = new File("tree");
fileToDelete.delete();
sData = new StringBuilder(sData.toString().trim());
}
public String getString() {
return sData.toString().trim();
}
public String getSHA1() {
return Blob.hashStringToSHA1(sData.toString());
}
public void writeToObjects() throws IOException {
File shaName = new File("objects/" + Blob.hashStringToSHA1(sData.toString()));
if (!shaName.exists()) {
shaName.createNewFile();
}
if (shaName.exists()) {
FileWriter fw = new FileWriter(shaName);
sData.toString().trim();
fw.write(sData.toString());
fw.close();
}
}
public String addDirectory(String directoryPath) throws IOException {
File directory = new File(directoryPath);
if (!directory.exists() || !directory.isDirectory()) {
throw new IOException("Blud this isn't a directory bud");
}
File[] files = directory.listFiles();
Tree t = new Tree();
for (File file : files) {
if (file.isDirectory()) {
Tree another = new Tree();
t.add("tree : " + another.getSHA1());
} else {
Blob b = new Blob(file.getAbsolutePath());
t.add("blob : " + b.getSha() + " : " + file.getName());
hMap.put(b.getSha(), file.getName());
}
}
t.writeToObjects();
return t.getSHA1();
}
private String readFileContent(String fileName) throws IOException {
StringBuilder content = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
content.append(line).append("\n");
}
}
return content.toString();
}
public void deleteOrEdit(String content, String parentCommit) throws IOException {
if (content.contains(deleted)) {
findFile(content.substring(10), parentCommit);
} else if (content.contains(edited)) {
String orginalFile = content.substring(9);
findFile(orginalFile, Commit.getTreeFromSha(parentCommit));
Scanner scan = new Scanner(new File(content.substring(9)));
String edit = scan.useDelimiter("\\A").next();
scan.close();
String newSha = Blob.hashStringToSHA1(edit);
add("blob : " + newSha + " : " + orginalFile);
}
}
public void findFile(String sha, String parentSha) throws IOException {
File file = new File("./objects/" + parentSha);
if (file.length() > 0) {
Scanner scan = new Scanner(file);
String content = scan.useDelimiter("\\A").next();
scan.close();
if (content.contains(sha)) {
FileInputStream fileInput = new FileInputStream("./objects" + parentSha);
DataInputStream data = new DataInputStream(fileInput);
BufferedReader br = new BufferedReader(new InputStreamReader(data));
String line;
while ((line = br.readLine()) != null) {
if (!line.contains(sha))
add(line);
}
data.close();
} else {
FileInputStream fileInput = new FileInputStream("./objects" + parentSha);
DataInputStream data = new DataInputStream(fileInput);
BufferedReader br = new BufferedReader(new InputStreamReader(data));
String line;
while ((line = br.readLine()) != null) {
if (line.substring(6).indexOf(":") == -1 && line.contains("tree"))
findFile(sha, line.substring(7, 47));
else
add(line);
}
br.close();
}
}
}
}