-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkingListTester.java
More file actions
61 lines (52 loc) · 1.94 KB
/
WorkingListTester.java
File metadata and controls
61 lines (52 loc) · 1.94 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
import java.io.File;
import java.io.FileWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
public class WorkingListTester {
public static void main(String[] args) {
try {
// make directories
new File("git").mkdirs();
new File("git/objects").mkdirs();
new File("proj/docs").mkdirs();
// make some files
File f1 = new File("proj/README.md");
File f2 = new File("proj/docs/Hello.txt");
File f3 = new File("proj/docs/World.txt");
write(f1, "readme\n");
write(f2, "hello\n");
write(f3, "world\n");
// blob them
String h1 = Blob.createBlobFromPath(f1.getPath());
String h2 = Blob.createBlobFromPath(f2.getPath());
String h3 = Blob.createBlobFromPath(f3.getPath());
// write index
File idx = new File("git/index");
FileWriter w = new FileWriter(idx, false);
w.write(h1 + " " + f1.getPath() + "\n");
w.write(h2 + " " + f2.getPath() + "\n");
w.write(h3 + " " + f3.getPath());
w.close();
// build tree
WorkingList wl = new WorkingList();
String root = wl.build();
// print results
System.out.println("Root tree: " + root);
System.out.println("Contents:");
System.out.println(readObj(root));
} catch (Exception e) {
e.printStackTrace();
}
}
private static void write(File f, String s) throws Exception {
f.getParentFile().mkdirs();
FileWriter w = new FileWriter(f, false);
w.write(s);
w.close();
}
private static String readObj(String sha) throws Exception {
File obj = new File("git/objects", sha);
byte[] b = Files.readAllBytes(obj.toPath());
return new String(b, StandardCharsets.UTF_8);
}
}