-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTree.java
More file actions
206 lines (182 loc) · 5.99 KB
/
Tree.java
File metadata and controls
206 lines (182 loc) · 5.99 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
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.HashSet;
public class Tree {
private ArrayList<String> blobTree;
private ArrayList<Tree> childTrees;
private HashSet<String> fileNameList;
private ArrayList<String> treeList;
private HashSet<String> sha1List;
private String sha1;
private String fileName;
public Tree() {
blobTree = new ArrayList<String>();
childTrees = new ArrayList<Tree>();
sha1 = "";
treeList = new ArrayList<String>();
fileNameList = new HashSet<String>();
sha1List = new HashSet<String>();
}
public Tree (String fileName)
{
blobTree = new ArrayList<String>();
childTrees = new ArrayList<Tree>();
sha1 = "";
treeList = new ArrayList<String>();
this.fileName = fileName;
fileNameList = new HashSet<String>();
sha1List = new HashSet<String>();
}
public void add(String indexLine) {
if (!(blobTree.contains(indexLine))) {
blobTree.add(indexLine);
}
}
public void remove(String indexLine) {
for (int i = blobTree.size() - 1; i >= 0; i--) {
if (blobTree.get(i).contains(indexLine)) {
blobTree.remove(i);
treeList.remove (indexLine);
}
}
}
public static String hashFromString(String input) {
StringBuilder hexString = new StringBuilder();
try
{
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] hashBytes = md.digest(input.getBytes());
hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xFF & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return hexString.toString();
}
public String addDirectory(String directoryPath) throws IOException {
File directory = new File(directoryPath);
if (!directory.exists() || !directory.isDirectory()) {
throw new IllegalArgumentException("Invalid directory path: " + directoryPath);
}
Tree tree = new Tree();
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
Tree childTree = new Tree();
tree.addTreeEntry("tree", childTree.addDirectory(file.getPath()), file.getName());
} else if (file.isDirectory()) {
Tree childTree = new Tree();
childTree.addDirectory(file.getAbsolutePath());
childTrees.add(childTree);
}
}
sha1 = calculateTreeSHA1();
save();
return tree.getSHA1();
}
public String getFileName ()
{
return fileName;
}
public void handleDeletedFiles(ArrayList<String> deletedFiles)
{
blobTree.removeIf(entry -> {
String fileName = getFileName();
return deletedFiles.contains(fileName);
});
}
public void handleEditedFiles (ArrayList<String> editedFiles)
{
for (String entry: blobTree)
{
String fileName = getFileName();
if (editedFiles.contains(fileName))
{
entry = entry.replaceFirst(getSHA1(), hashFromString (entry));
}
}
}
public String getSHA1()
{
return sha1;
}
public ArrayList<Tree> getChildTrees ()
{
return childTrees;
}
public String calculateTreeSHA1() {
StringBuilder treeContent = new StringBuilder();
for (String line : blobTree) {
treeContent.append(line).append("\n");
}
for (Tree child : childTrees) {
treeContent.append("tree : ").append(child.getSHA1()).append(" : child\n");
}
return hashFromString(treeContent.toString());
}
public static String byteToHex(final byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash) {
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
public void addTreeEntry(String fileType, String sha1, String fileName) {
if (fileType.equals("tree")) {
if (!treeList.contains(fileType + " : " + sha1)) {
if (fileName.isEmpty()) {
treeList.add(fileType + " : " + sha1);
} else {
blobTree.add(fileType + " : " + sha1 + " : " + fileName);
fileNameList.add(fileName);
}
sha1List.add(sha1);
}
}
else if (fileType.equals("blob")) {
if (!treeList.contains(fileType + " : " + sha1 + " : " + fileName)) {
treeList.add(fileType + " : " + sha1 + " : " + fileName);
fileNameList.add(fileName);
sha1List.add(sha1);
}
}
}
public void save() throws IOException {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < blobTree.size(); i++) {
sb.append(blobTree.get(i));
if (i < blobTree.size() - 1) {
sb.append("\n");
}
}
String hash = hashFromString(sb.toString());
File tempFile = new File("./objects/" + hash);
tempFile.createNewFile();
FileWriter fw = new FileWriter(tempFile);
fw.write(sb.toString());
fw.flush();
fw.close();
}
public ArrayList<String> getBlobTree() {
return blobTree;
}
public ArrayList<String> getTreeList()
{
return treeList;
}
}