-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGit.java
More file actions
386 lines (348 loc) · 14.3 KB
/
Git.java
File metadata and controls
386 lines (348 loc) · 14.3 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Stack;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Comparator;
import java.util.HashSet;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
//LOOK AT TOP OF README FOR AN IMPORTANT EXPLANATION!
public class Git {
public static final boolean COMPRESSING = !true;
public static String getPath(String filePath, String folderName){
return (filePath == null ? "" : filePath + "/") + folderName;
}
public static boolean directoryExists(String filePath, String folderName){
String path = getPath(filePath, folderName);
File newDir = new File(path);
return newDir.exists() && newDir.isDirectory();
}
public static boolean fileExists(String filePath, String fileName){
String path = getPath(filePath, fileName);
File f = new File(path);
return f.exists() && f.isFile();
}
public static void makeDirectory(String filePath, String folderName){
if (directoryExists(filePath, folderName)){
return;
}
String path = getPath(filePath, folderName);
File newDir = new File(path);
newDir.mkdir();
}
private static void deleteDirectory(File file)
{
for (File subfile : file.listFiles()) {
if (subfile.isDirectory()) {
deleteDirectory(subfile);
}
subfile.delete();
}
file.delete();
}
public static void deleteDirectory(String filePath, String folderName){
if (!directoryExists(filePath, folderName)){
return;
}
String path = getPath(filePath, folderName);
File dir = new File(path);
deleteDirectory(dir);
}
public static void makeFile(String filePath, String fileName){
if (fileExists(filePath, fileName)){
return;
}
String path = getPath(filePath, fileName);
File f = new File(path);
try {
f.createNewFile();
} catch (Exception e){
e.printStackTrace();
}
}
public static void deleteFile(String filePath, String fileName){
if (fileExists(filePath, fileName)){
String path = getPath(filePath, fileName);
File f = new File(path);
f.delete();
}
}
public static void cleanGit(){
deleteDirectory(null, "git");
intializeRepo();
}
public static String readFile(String filePath, String fileName){
String path = getPath(filePath, fileName);
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
if (sb.length() > 1){
sb.deleteCharAt(sb.length() - 1); //remove terminator
}
return sb.toString();
}
public static String readFileCompressed(String filePath, String fileName){
return null;
}
public static void writeToFile(String filePath, String fileName, String content){
String path = getPath(filePath, fileName);
try (FileWriter writer = new FileWriter(path)) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeToFileCompressed(String filePath, String fileName, String content){
}
public static void appendToFile(String filePath, String fileName, String content){
String path = getPath(filePath, fileName);
try (FileWriter fw = new FileWriter(path, true);
BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String hash(String content){
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] hashBytes = digest.digest(content.getBytes(StandardCharsets.UTF_8)); //these two lines googled from library
StringBuilder hex = new StringBuilder();
for (byte b : hashBytes) {
hex.append(String.format("%02x", b));
}
return hex.toString();
} catch (Exception e) {
}
return null;
}
public static String hashFile(String filePath, String fileName){
return hash(readFile(filePath, fileName));
}
public static void intializeRepo(){
if (fileExists("git/objects", "index") && fileExists("git/objects", "HEAD")
&& directoryExists("git", "objects") && directoryExists(null, "git")){
System.out.println("Git Repository Already Exists");
return;
}
deleteFile(null, "git"); //in case "cloggs" options
makeDirectory(null, "git");
deleteFile("git", "objects"); //in case "cloggs" options
makeDirectory("git", "objects");
deleteDirectory("git", "index"); //in case "cloggs" options
makeFile("git/objects", "index");
deleteDirectory("git", "HEAD"); //in case "cloggs" options
makeFile("git/objects", "HEAD");
}
public static void blobify(String filePath, String fileName){
String content = readFile(filePath, fileName);
String hash = hash(content);
makeFile("git/objects", hash);
if (COMPRESSING){
content = compress(content);
}
writeToFile("git/objects", hash, content);
}
public static boolean alreadyIndexed(String str){
return readFile("git/objects", "index").contains(str);
}
public static boolean isIndexEmpty(){
return readFile("git/objects", "index").equals("");
}
public static void index(String filePath, String fileName){ //assumes not already there!
String path = getPath(filePath, fileName);
String entry = hashFile(filePath, fileName) + " " + path;
if (alreadyIndexed(entry)){
//already entirely in there; do nothing
} else if (alreadyIndexed(path)){
//updating file
String indexFile = readFile("git/objects", "index");
int pathIndex = indexFile.indexOf(path);
int hashIndex = indexFile.substring(0, pathIndex).lastIndexOf("\n") + 1;
String newFile = indexFile.substring(0, hashIndex) + entry + indexFile.substring(pathIndex + path.length());
writeToFile("git/objects", "index", newFile);
} else {
if (!isIndexEmpty()){
appendToFile("git/objects", "index", "\n");
}
appendToFile("git/objects", "index", entry);
}
}
public static String compress(String input) {
byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
Deflater deflater = new Deflater();
deflater.setInput(inputBytes);
deflater.finish();
byte[] buffer = new byte[1024];
int compressedLength = deflater.deflate(buffer);
deflater.end();
return Base64.getEncoder().encodeToString(java.util.Arrays.copyOf(buffer, compressedLength));
}
public static String decompress(String compressedBase64) {
byte[] compressedBytes = Base64.getDecoder().decode(compressedBase64);
Inflater inflater = new Inflater();
inflater.setInput(compressedBytes);
byte[] buffer = new byte[1024];
try {
int decompressedLength = inflater.inflate(buffer);
inflater.end();
return new String(buffer, 0, decompressedLength, StandardCharsets.UTF_8);
} catch (Exception e){
e.printStackTrace();
return null;
}
}
public static String makeTree(String filePath, String fileName){ //returns hash value
String path = getPath(filePath, fileName);
File dir = new File(path);
if (!dir.exists() || !dir.isDirectory()){
throw new IllegalArgumentException();
}
StringBuilder sb = new StringBuilder();
for (File subfile : dir.listFiles()) {
String subpath = subfile.getParent();
String subname = subfile.getName();
String newEntry = new String();
if (subfile.isFile()){
blobify(subpath, subname);
newEntry = "blob " + hashFile(subpath, subname) + " " + getPath(subpath, subname);
} else if (subfile.isDirectory()){
String subtreeHash = makeTree(subpath, subname);
newEntry = "tree " + subtreeHash + " " + getPath(subpath, subname);
}
sb.append(newEntry);
sb.append("\n");
}
sb.deleteCharAt(sb.length() - 1); //removes terminal new line
String treeContents = sb.toString();
String treeHash = hash(treeContents);
makeFile("git/objects", treeHash);
writeToFile("git/objects", treeHash, treeContents);
return treeHash;
}
//IGNORE: Incorrect programming attempt
// public static void makeIndexTree(){
// String indexFile = readFile("git/objects", "index");
// String[] entriesArr = indexFile.split("\n");
// Stack<String> entries = new Stack<String>();
// for (int i = 0; i < entriesArr.length; i++){
// entries.add("blob " + entriesArr[i]);
// }
// entries.sort(Comparator.comparing((String s) -> s.split(" ")[2]).reversed());
// while (entries.size() > 0){
// //above sorts by depth (ie. number of /s) to make sure subfolders get done first.
// String topPath = entries.peek().split(" ")[2];
// if (topPath.lastIndexOf("/") == -1){
// //no subdirectories; make root file for it
// //MAKE ROOT FILE
// String rootEntry = entries.pop();
// String blobName = hash(rootEntry);
// makeFile(null, blobName);
// writeToFile(null, blobName, rootEntry);
// }
// String newDir = topPath.substring(0, topPath.lastIndexOf("/"));
// StringBuilder treeFile = new StringBuilder();
// while (entries.size() > 0){
// String nextPath = entries.peek().split(" ")[2];
// if (!nextPath.contains(newDir)){
// break;
// }
// treeFile.append(entries.pop());
// treeFile.append("\n");
// }
// if (treeFile.length() > 0){
// treeFile.deleteCharAt(treeFile.length() - 1);
// }
// String treeContent = treeFile.toString();
// String blobName = hash(treeContent);
// makeFile("git/objects", blobName);
// writeToFile("git/objects", blobName, treeContent);
// entries.push("tree " + blobName + " " + newDir);
// }
// }
//NOTE: Does not automatically BLOB everything inside of it!
//That should have been done when indexed anyway.
public static void makeIndexTree(){
StringBuilder rootTreeContents = new StringBuilder();
String indexFile = readFile("git/objects", "index");
String[] entriesArr = indexFile.split("\n");
ArrayList<String> entries = new ArrayList<String>();
HashSet<String> directories = new HashSet<String>();
for (int i = 0; i < entriesArr.length; i++){
String path = entriesArr[i].split(" ")[1]; //no tree/blob prefix yet
if (path.contains("/")){
String directory = path.substring(0, path.indexOf("/"));
directories.add(directory);
entries.add("blob " + entriesArr[i]);
} else { //is a file
rootTreeContents.append("blob " + entriesArr[i]);
rootTreeContents.append("\n");
}
}
for (String directory : directories){
String treeHash = makeIndexTreeHelper(entries, directory);
rootTreeContents.append("tree " + treeHash + " " + directory);
rootTreeContents.append("\n");
}
if (rootTreeContents.length() > 0){
rootTreeContents.deleteCharAt(rootTreeContents.length() - 1);
}
String contents = rootTreeContents.toString();
String hash = hash(contents);
makeFile(null, hash);
writeToFile(null, hash, contents);
}
//returns tree hash
public static String makeIndexTreeHelper(ArrayList<String> entries, String directoryPrefix){
ArrayList<String> subentries = new ArrayList<String>();
for (String entry : entries){
String path = entry.split(" ")[2];
if (path.contains(directoryPrefix)){
subentries.add(entry);
}
}
HashSet<String> treeEntryRows = new HashSet<String>(); //get only unqiue adds
for (String subentry : subentries){
String subpath = subentry.split(" ")[2].substring(directoryPrefix.length() + 1);
if (subpath.contains("/")){ //a directory
String firstFolder = subpath.substring(0, subpath.indexOf("/"));
String subTreeHash = makeIndexTreeHelper(subentries, directoryPrefix + "/" + firstFolder);
treeEntryRows.add("tree " + subTreeHash + " " + directoryPrefix + "/" + firstFolder);
} else { //a file
treeEntryRows.add(subentry); //already formatted nicely
}
}
StringBuilder entryContentSB = new StringBuilder();
for (String s : treeEntryRows){
entryContentSB.append(s);
entryContentSB.append("\n");
}
if (entryContentSB.length() > 0){
entryContentSB.deleteCharAt(entryContentSB.length() - 1);
}
String entryContent = entryContentSB.toString();
String treeHash = hash(entryContent);
makeFile("git/objects", treeHash);
writeToFile("git/objects", treeHash, entryContent);
return treeHash;
}
public static void main(String[] args){
cleanGit();
intializeRepo();
index("A/B/D", "f3");
index("A/B", "f2");
index("A/C", "f4");
index("A", "f1");
index(null, ".gitignore");
makeIndexTree();
}
}