forked from dyltsai/git-project-Dylan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit.java
More file actions
168 lines (129 loc) · 5.01 KB
/
Git.java
File metadata and controls
168 lines (129 loc) · 5.01 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
168
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Git {
public static void initGitRepo() throws IOException {
File gitDir = new File("./git");
File objectsDir = new File("./git/objects");
File indexFile = new File("./git/index");
if (gitDir.exists() && objectsDir.exists() && indexFile.exists()) {
System.out.println("Git Repository already exists");
return;
}
if (!gitDir.exists()) {
gitDir.mkdir();
}
if (!objectsDir.exists()) {
objectsDir.mkdir();
}
if (!indexFile.exists()) {
indexFile.createNewFile();
}
}
public static void initGitRepoTester() throws IOException {
File gitDir = new File("./git");
File objectsDir = new File("./git/objects");
File indexFile = new File("./git/index");
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 String sha1(Path file) throws NoSuchAlgorithmException, IOException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] byteArr = md.digest(Files.readAllBytes(file));
BigInteger n = new BigInteger(1, byteArr);
String hash = n.toString(16);
while (hash.length() < 40) {
hash = "0" + hash;
}
return hash;
}
public static void createNewBlob(Path path) throws IOException, NoSuchAlgorithmException {
Path hash = Paths.get(".git/objects/" + sha1(path));
if (Files.exists(hash)) {
Files.delete(hash);
}
Files.copy(path, hash);
BufferedReader br = new BufferedReader(new FileReader((".git/index")));
try (BufferedWriter writer = new BufferedWriter(new FileWriter(".git/index", true))) {
writer.write(path.getFileName() + " " + sha1(path));
writer.newLine();
}
}
public static void createNewBlobTester(Path path) throws NoSuchAlgorithmException, IOException {
createNewBlob(path);
String hash = 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 = sha1(path);
String hash2 = 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 main(String[] args) throws IOException, NoSuchAlgorithmException {
// initGitRepo();
// initGitRepoTester();
// sha1 tester
Path testFile = Paths.get(".git/testFile.txt");
testFile.toFile().createNewFile();
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: " + sha1(testFile));
System.out.println("expected hash: " + "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed");
// testing blob
createNewBlob(testFile);
createNewBlobTester(testFile);
}
}