-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTester.java
More file actions
155 lines (126 loc) · 5.18 KB
/
Tester.java
File metadata and controls
155 lines (126 loc) · 5.18 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
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
public class Tester {
public static void initGitRepoTester() throws IOException {
Git git = new Git();
File gitDir = new File("./git");
File objectsDir = new File("./git/objects");
File indexFile = new File("./git/index");
git.initGitRepo();
boolean repoCreated = gitDir.exists() && objectsDir.exists() && indexFile.exists();
// delete files in directories first
indexFile.delete();
objectsDir.delete();
gitDir.delete();
boolean checkDeletedFiles = !(gitDir.exists() && objectsDir.exists() && indexFile.exists());
if (repoCreated && checkDeletedFiles) {
System.out.println("Created repo and deleted files.");
} else {
if (repoCreated) {
System.out.println("Created repo.");
} else {
System.out.println("Did not create repo.");
}
if (checkDeletedFiles) {
System.out.println("Deleted files.");
} else {
System.out.println("Did not delete files.");
}
}
}
public static void createNewBlobTester(Path path) throws NoSuchAlgorithmException, IOException {
Git git = new Git();
git.createNewBlob(path);
String hash = git.sha1(path);
Path blobPath = Paths.get("./git/objects/" + hash);
if (!Files.exists(blobPath)) {
System.out.println("blob doesn't exist: " + blobPath);
} else {
System.out.println("blob exists: " + blobPath);
}
String hash1 = git.sha1(path);
String hash2 = git.sha1(blobPath);
if (hash1.equals(hash2)) {
System.out.println("the content of file is the same");
}
try (BufferedReader br = new BufferedReader(new FileReader("./git/index"))) {
String line;
boolean indexLineExists = false;
while ((line = br.readLine()) != null) {
if (line.equals(path.getFileName() + " " + hash)) {
indexLineExists = true;
break;
}
}
if (!indexLineExists) {
System.out.println("no index entry: " + path.getFileName());
return;
}
}
Files.deleteIfExists(path);
Files.deleteIfExists(blobPath);
if (!Files.exists(path) && !Files.exists(blobPath)) {
System.out.println("files were deleted");
}
System.out.println("everything was created (blob + index entry)");
}
public static void SHATester() throws IOException, NoSuchAlgorithmException {
Git git = new Git();
//sha1 tester
File f = new File("./git/testFile.txt");
if (!f.exists()) {
Path testFile = Paths.get("./git/testFile.txt");
testFile.toFile().createNewFile();
}
// it exists, so write to it
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(("testFile.txt")));
writer.write("hello world");
writer.close();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
System.out.println("hash: " + git.sha1(Paths.get("./git/testFile.txt")));
System.out.println("expected hash: " + "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed");
}
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
Git git = new Git();
git.resetGit();
git.initGitRepo();
File testFolder = new File ("testTreeFolder");
File sample1 = new File ("./testTreeFolder/sample1.txt");
if(testFolder.exists())
git.deleteDir(testFolder);
testFolder.mkdir();
sample1.createNewFile();
BufferedWriter bWriter = new BufferedWriter(new FileWriter(sample1));
bWriter.write("just some sample text");
bWriter.close();
git.createNewBlob("testTreeFolder");
git.commit("Jacob Massey", "testing my first commit");
File test = new File("testFile.txt");
if(test.exists())
test.delete(); //RESETS it if needed
FileWriter fWriter = new FileWriter(test,true);
BufferedWriter bufferWritter = new BufferedWriter(fWriter);
bufferWritter.write("this is a test");
bufferWritter.close();
git.createNewBlob("testFile.txt");
git.commit("Jacob Massey", "testing my SECOND commit!");
// File test2 = new File("testFile2.txt");
// if(test2.exists())
// test2.delete(); //RESETS it if needed
// FileWriter fileWritter2 = new FileWriter(test2,true);
// BufferedWriter bufferWritter2 = new BufferedWriter(fileWritter2);
// bufferWritter2.write("this is a second test for secret reasons", 0, 40);
// bufferWritter2.close();
// testing blob
// Paths.get("./git/testFile.txt")
//git.createNewBlob("testFile.txt");
//createNewBlobTester(testFile);
// git.createNewBlob();
}
}