-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGitTester.java
More file actions
167 lines (154 loc) · 4.84 KB
/
GitTester.java
File metadata and controls
167 lines (154 loc) · 4.84 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
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
public class GitTester {
public static void main(String[] args) {
File repo = new File("git");
if (repo.exists()) {
deleteRecursively(repo);
}
// Initialize repository
Git g = new Git();
g.initializeRepository();
boolean repoOk = verifyRepo();
if (repoOk == true) {
System.out.println("Repo initialized: PASS");
} else {
System.out.println("Repo initialized: FAIL");
return;
}
// Make file
boolean made = makeFile("tester_file.txt", "hello\nline2\n");
if (made == true) {
System.out.println("File created: PASS");
} else {
System.out.println("File created: FAIL");
return;
}
// Compute SHA-1
String expected;
try {
expected = Blob.sha1FromFile(new File("tester_file.txt"));
} catch (Exception e) {
System.out.println("SHA1 compute: FAIL");
return;
}
System.out.println("SHA1 computed: " + expected);
// Make blob and compare
String created;
try {
created = Blob.createBlobFromPath("tester_file.txt");
} catch (Exception e) {
System.out.println("Blob creation: FAIL");
return;
}
if (created.equals(expected)) {
System.out.println("Blob creation: PASS");
} else {
System.out.println("Blob creation: FAIL");
return;
}
// Add to index
try {
Index idx = new Index();
idx.add("tester_file.txt");
} catch (Exception e) {
System.out.println("Add to index: FAIL");
return;
}
boolean inIndex = indexHasEntry("tester_file.txt", created);
if (inIndex == true) {
System.out.println("Index updated: PASS");
} else {
System.out.println("Index updated: FAIL");
return;
}
// Delete repo
if (repo.exists()) {
deleteRecursively(repo);
}
if (!repo.exists()) {
System.out.println("Repo cleanup: PASS");
} else {
System.out.println("Repo cleanup: FAIL");
return;
}
// Delete program files
File f = new File("tester_file.txt");
if (f.exists()) {
f.delete();
}
if (!f.exists()) {
System.out.println("File cleanup: PASS");
} else {
System.out.println("File cleanup: FAIL");
}
}
private static boolean verifyRepo() {
File repo = new File("git");
File objects = new File(repo, "objects");
File index = new File(repo, "index");
File head = new File(repo, "HEAD");
if (repo.exists() && repo.isDirectory()) {
if (objects.exists() && objects.isDirectory()) {
if (index.exists() && index.isFile()) {
if (head.exists() && head.isFile()) {
return true;
}
}
}
}
return false;
}
private static void deleteRecursively(File f) {
if (f.isDirectory()) {
File[] kids = f.listFiles();
if (kids != null) {
int i = 0;
while (i < kids.length) {
deleteRecursively(kids[i]);
i = i + 1;
}
}
}
f.delete();
}
private static boolean makeFile(String name, String content) {
try {
FileWriter w = new FileWriter(new File(name), false);
w.write(content);
w.flush();
w.close();
return true;
} catch (Exception e) {
return false;
}
}
private static boolean indexHasEntry(String fileName, String hash) {
try {
File idx = new File("git" + File.separator + "index");
if (!idx.exists()) {
return false;
}
BufferedReader br = new BufferedReader(new FileReader(idx));
String line = br.readLine();
while (line != null) {
int space = line.indexOf(' ');
if (space != -1) {
String left = line.substring(0, space);
String right = line.substring(space + 1);
if (left.equals(hash) && right.equals(fileName)) {
br.close();
return true;
}
}
line = br.readLine();
}
br.close();
return false;
} catch (Exception e) {
return false;
}
}
}