forked from hlevy108/GitProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBLOB.java
More file actions
69 lines (62 loc) · 2.33 KB
/
BLOB.java
File metadata and controls
69 lines (62 loc) · 2.33 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
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BLOB {
public static String getFileContents(File file) {
StringBuilder content = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
content.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
public static void copyToBlob(String fileContents, File newFile) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(newFile))) {
writer.write(fileContents);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String addFile(String path) {
File index = new File("git" + File.separator + "index");
updateIndexFile(path, index, "blob");
return createBlob(path);
}
public static String createBlob(String path) {
String fileContents = getFileContents(new File(path));
String key = SHA1.encryptThisString(fileContents);
File BLOB = new File("git" + File.separator + "objects" + File.separator + key);
try {
BLOB.createNewFile();
} catch (IOException e) {
System.out.println(e);
}
copyToBlob(fileContents, BLOB);
return "git" + File.separator + "objects" + File.separator + key;
}
public static void updateIndexFile(String path, File index, String type) {
String fileContents = getFileContents(new File(path));
String hash = SHA1.encryptThisString(fileContents);
try {
if (index.length() == 0) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(index))) {
writer.write(type + " " + hash + " " + path);
}
} else {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(index, true))) {
writer.newLine();
writer.write(type + " " + hash + " " + path);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}