-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileHelper.java
373 lines (313 loc) · 15 KB
/
FileHelper.java
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
import java.security.MessageDigest;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
//import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.BasicFileAttributes;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
public class FileHelper {
public static final String initialCommit = "0000000000000000000000000000000000000000000000000000000000000000.commit";
public static String getHash256(String filename) throws NoSuchAlgorithmException, IOException {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
FileInputStream fis = new FileInputStream(filename);
byte[] data = new byte[1024];
int read = 0;
while ((read = fis.read(data)) != -1) {
sha256.update(data, 0, read);
};
byte[] hashBytes = sha256.digest();
StringBuffer sb = new StringBuffer();
//char[] conversionArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
for (int i = 0; i < hashBytes.length; i++) {
sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1));
//sb.append(Integer.toString(conversionArray[hashBytes[i]]));
}
fis.close();
String fileHash = sb.toString();
return fileHash;
}
public static String treeToObject (String srcDir, String destDir) throws FileNotFoundException, IOException, NoSuchAlgorithmException {
String srcPath, hashFile, destPath, hashTree, dir;
Stack<String> dirs = new Stack<String>();
dirs.push("");
File tempFile = File.createTempFile("tree", ".tmp");
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tempFile.toString(), false)));
while (!dirs.empty()) {
dir = dirs.pop();
File folder = new File(srcDir + "/" + dir);
File[] subFiles = folder.listFiles();
for (int i = 0; i < subFiles.length; i++) {
if (subFiles[i].isFile()) {
srcPath = subFiles[i].toString();
hashFile = getHash256(srcPath) + ".file";
destPath = destDir + "/.gitlet/files/" + hashFile;
copyFile(srcPath, destPath);
out.println(hashFile + " " + dir + "/" + subFiles[i].getName());
} else { //subFiles[i] = directory
String path = subFiles[i].toString();
dirs.push(path.substring(srcDir.length(), path.length()).replace("\\", "/"));
}
}
}
out.close();
hashTree = getHash256(tempFile.toString()) + ".tree";
destPath = destDir + "/.gitlet/trees/" + hashTree;
copyFile(tempFile.toString(), destPath);
tempFile.delete();
return hashTree;
}
public static void objectToTree(String srcDir, String firstTreeName, String destDir) throws IOException, FileNotFoundException {
BufferedReader in = new BufferedReader(new FileReader(srcDir + "/.gitlet/trees/" + firstTreeName));
String line, hashName, hashExtension, fileName, srcPath, destPath;
File newFolder;
while ((line = in.readLine()) != null) {
hashName = line.substring(0, 69); //as12d2.file
hashExtension = line.substring(65, 69); //.tree
fileName = line.substring(70, line.length()); //dir2
srcPath = srcDir + "/.gitlet/files/" + hashName;
destPath = destDir + "/" + fileName;
copyFile(srcPath, destPath);
}
in.close();
}
public static void copyFile(String srcPath, String destPath) throws IOException, FileNotFoundException {
File destFile = new File(destPath);
if (!destFile.exists()) {
(Paths.get(destPath)).getParent().toFile().mkdirs();
}
InputStream in = new FileInputStream(srcPath);
OutputStream out = new FileOutputStream(destPath);
byte[] nextSector = new byte[4096];
int length;
while((length = in.read(nextSector)) > 0) {
out.write(nextSector, 0, length);
}
in.close();
out.close();
}
public static String createTree(String prevTreeName, ArrayList<String> addFileNames, ArrayList<String> delFileNames) throws FileNotFoundException, IOException, NoSuchAlgorithmException {
String line, hashTree, hashName, fileName, destPath, hashFile, srcPath;
File tempFile = File.createTempFile("tree", ".tmp");
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tempFile.toString(), false)));
if(!prevTreeName.equals("")) {
String prevTreePath = State.srcDir + "/.gitlet/trees/" + prevTreeName;
BufferedReader in = new BufferedReader(new FileReader(prevTreePath));
while ((line = in.readLine()) != null) {
hashName = line.substring(0, 69); //xxxxx.file
fileName = line.substring(70, line.length()); //dir2
if (delFileNames.contains(fileName) || addFileNames.contains(fileName)) {
continue;
} else {
out.println(line);
}
}
in.close();
}
for(String s : addFileNames) {
srcPath = State.srcDir + "/" + s;
hashFile = getHash256(srcPath) + ".file";
destPath = State.srcDir + "/.gitlet/files/" + hashFile;
copyFile(srcPath, destPath);
out.println(hashFile + " " + s);
}
out.close();
hashTree = getHash256(tempFile.toString()) + ".tree";
destPath = State.srcDir + "/.gitlet/trees/" + hashTree;
copyFile(tempFile.toString(), destPath);
tempFile.delete();
return hashTree;
}
public static String createTree(HashMap<String, String> files) throws FileNotFoundException, IOException, NoSuchAlgorithmException {
String hashTree, destPath, hashFile;
File tempFile = File.createTempFile("tree", ".tmp");
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tempFile.toString(), false)));
for(String file : files.keySet()) {
hashFile = files.get(file);
out.println(hashFile + " " + file);
}
out.close();
hashTree = getHash256(tempFile.toString()) + ".tree";
destPath = State.srcDir + "/.gitlet/trees/" + hashTree;
copyFile(tempFile.toString(), destPath);
tempFile.delete();
return hashTree;
}
public static String commit(String message) throws IOException, FileNotFoundException, NoSuchAlgorithmException {
String hashTree, hashCommit, destPath, parentCommit, parentTree;
File tempFile = File.createTempFile("tree", ".tmp");
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tempFile.toString(), false)));
File checkInitial = new File(State.srcDir + "/.gitlet/commits/" + initialCommit);
if (checkInitial.exists()) {
parentCommit = getCurrCommit();
if (parentCommit.equals(initialCommit)) {
hashTree = createTree("", State.addFiles, State.delFiles);
} else {
parentTree = getTree(parentCommit);
hashTree = createTree(parentTree, State.addFiles, State.delFiles);
}
out.println(parentCommit);
out.println(hashTree);
out.println(message);
out.close();
hashCommit = getHash256(tempFile.toString()) + ".commit";
} else {
out.println("");
out.println("");
out.println(message);
out.close();
hashCommit = initialCommit;
}
destPath = State.srcDir + "/.gitlet/commits/" + hashCommit;
copyFile(tempFile.toString(), destPath);
tempFile.delete();
return hashCommit;
}
public static String commitFromTree(String parentCommit, String treeName, String message) throws IOException, FileNotFoundException, NoSuchAlgorithmException {
String hashCommit, destPath;
File tempFile = File.createTempFile("tree", ".tmp");
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tempFile.toString(), false)));
out.println(parentCommit);
out.println(treeName);
out.println(message);
out.close();
hashCommit = getHash256(tempFile.toString()) + ".commit";
destPath = State.srcDir + "/.gitlet/commits/" + hashCommit;
copyFile(tempFile.toString(), destPath);
tempFile.delete();
return hashCommit;
}
public static void updateBranch(String branchName, String commitName) throws IOException {
BufferedWriter out = new BufferedWriter(new FileWriter(State.srcDir + "/.gitlet/branches/" + branchName + ".branch"));
out.write(commitName, 0, commitName.length());
out.close();
}
public static String getCommitLine(String commitName, int lineNumber) throws FileNotFoundException, IOException {
String commitPath = State.srcDir + "/.gitlet/commits/" + commitName;
BufferedReader in = new BufferedReader(new FileReader(commitPath));
for(int i = 1; i < lineNumber; i++) {
in.readLine();
}
String commitLine = in.readLine(); //returns xxxxx.commit
in.close();
return commitLine;
}
public static String getCommit(String branchName) throws IOException, FileNotFoundException {
String branchPath = State.srcDir + "/.gitlet/branches/" + branchName + ".branch";
BufferedReader in = new BufferedReader(new FileReader(branchPath));
String commit = in.readLine();
in.close();
return commit;
} //returns commitName
public static String getCurrCommit() throws IOException, FileNotFoundException {
return getCommit(State.head);
} //returns commitName
public static String getPrevCommit(String commitName) throws IOException, FileNotFoundException {
return getCommitLine(commitName, 1);
} //returns commitName
public static String getTree(String commitName) throws IOException, FileNotFoundException { //commitName includes ".commit"
return getCommitLine(commitName, 2);
}
public static String getCommitMessage(String commitName) throws IOException, FileNotFoundException {
return getCommitLine(commitName, 3);
}
public static void printCommit(String commitName) throws IOException {
System.out.println("Commit " + commitName.substring(0, 64) + ".");
Path commit = Paths.get(State.srcDir + "/.gitlet/commits/" + commitName);
String creationTime = Files.readAttributes(commit, BasicFileAttributes.class).creationTime().toString();
String printTime = creationTime.substring(0, 10) + " " + creationTime.substring(11, 19);
System.out.println(printTime);
System.out.println(FileHelper.getCommitMessage(commitName));
System.out.println("");
}
public static ArrayList<String> getCommitTree(String startCommit, String endCommit) throws IOException {
ArrayList<String> commitTree = new ArrayList<String>();
String prevCommit = startCommit;
if (!startCommit.equals(endCommit)) {
do {
commitTree.add(prevCommit);
prevCommit = getPrevCommit(prevCommit);
} while (!prevCommit.equals(endCommit));
}
commitTree.add(endCommit);
return commitTree;
}
public static boolean isFileInCommit(String fileName) throws IOException, NoSuchAlgorithmException, FileNotFoundException {
String currCommit = getCurrCommit();
if(currCommit.equals(initialCommit)) {
return false;
}
String treePath = State.srcDir + "/.gitlet/trees/" + getTree(currCommit);
BufferedReader in = new BufferedReader(new FileReader(treePath));
String line;
String filePath = State.srcDir + "/" + fileName;
String fileHash = getHash256(filePath);
while ((line = in.readLine()) != null) {
if(fileHash.equals(line.substring(0, 64)) && fileName.equals(line.substring(70, line.length()))) {
return true;
}
}
return false;
}
public static String getCommonAncestor(String firstCommit, String secondCommit) throws IOException {
ArrayList<String> firstCommitTree = getCommitTree(firstCommit, initialCommit);
ArrayList<String> secondCommitTree = getCommitTree(secondCommit, initialCommit);
firstCommitTree.retainAll(secondCommitTree);
return firstCommitTree.get(0);
}
public static HashMap<String, String> listFilesInCommit(String commitName) throws FileNotFoundException, IOException {
HashMap<String, String> files = new HashMap<String, String>();
if(!commitName.equals(initialCommit)) {
String treePath = State.srcDir + "/.gitlet/trees/" + getTree(commitName);
BufferedReader in = new BufferedReader(new FileReader(treePath));
String line, hashName, fileName;
while ((line = in.readLine()) != null) {
hashName = line.substring(0, 69); //with .tree
fileName = line.substring(70, line.length());
files.put(fileName, hashName);
}
}
return files;
}
public static void test() throws IOException, NoSuchAlgorithmException {
String workingDir = System.getProperty("user.dir");
System.out.println(workingDir);
//String dirPath = workingDir + "/dir";
String objectsPath = workingDir + "/objects";
String testPath = workingDir + "/test";
String firstTree = "7bef7b4f2b820c1ccd327f15c1d88dda4b143998cd4fce1f3164c313dc216189.tree";
//treeToObject("C:/temp/dir", "C:/temp/proj");
//objectToTree("C:/temp/proj", firstTree, "C:/temp/dir2");
ArrayList<String> addedFiles = new ArrayList<String>();
addedFiles.add("/wug.txt");
ArrayList<String> delFiles = new ArrayList<String>();
//delFiles.add("/hug.txt");
//createTree("C:/temp/dir", "C:/temp/proj", firstTree, addedFiles, delFiles);
String firstCommit = commit("initial commit");
addedFiles.clear();
addedFiles.add("/dir2/tug.txt");
delFiles.add("/wug.txt");
commit("second commit");
}
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}