-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckout.java
More file actions
57 lines (49 loc) · 1.83 KB
/
Checkout.java
File metadata and controls
57 lines (49 loc) · 1.83 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
import java.io.File;
import java.io.FileOutputStream;
import java.util.Scanner;
public class Checkout {
public static void checkCommit(String commitKey) throws Exception {
assert KeyValue.checkIfKeyExists(commitKey);
deleteAllFiles();
String stringValueOfCommit = KeyValue.getStringValue(commitKey);
String rootTreeKey = new Scanner(stringValueOfCommit).nextLine();
checkoutTree(rootTreeKey,KeyValue.path);
}
private static void checkoutTree(String treeKey, File dir) throws Exception {
String stringValueOfTree = KeyValue.getStringValue(treeKey);
Scanner scanner = new Scanner(stringValueOfTree);
while (scanner.hasNext()) {
String key = scanner.nextLine();
String name = scanner.nextLine();
File subFile = new File(dir,name);
if(subFile.isDirectory()){
subFile.mkdir();
checkoutTree(key,subFile);
}else{
checkoutBlob(key,subFile);
}
}
}
private static void checkoutBlob(String key, File subFile) throws Exception {
FileOutputStream outputStream = new FileOutputStream(subFile);
outputStream.write(KeyValue.getValue(key));
}
private static void deleteAllFiles () {
for (File file : KeyValue.path.listFiles()) {
if (file.isDirectory() && !file.getName().equals("gitFolder")) {
deleteDirectory(file);
} else {
file.delete();
}
}
}
private static void deleteDirectory (File file){
if (file.listFiles() != null) {
for (File subFile : file.listFiles()) {
if (subFile.isDirectory()) deleteDirectory(subFile);
else subFile.delete();
}
}
file.delete();
}
}