-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGit.java
More file actions
197 lines (184 loc) · 7.4 KB
/
Git.java
File metadata and controls
197 lines (184 loc) · 7.4 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
import java.security.MessageDigest;
import java.io.*;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.NoSuchAlgorithmException;
public abstract class Git {
// initalizes a Git Repo with an "objects" folder and "index" file
public static void initalizeGitRepo() {
File gitFolder = new File("./git"); // git folder
File objectsFolder = new File("./git/objects"); // objects folder
File file = new File("./git", "index"); // index file
if (objectsFolder.exists() && file.exists()) {
System.out.println("Git Repository already exists");
}
// creates the git folder
if (!gitFolder.exists()) {
gitFolder.mkdirs();
}
// creates the objects folder
if (!objectsFolder.exists()) {
objectsFolder.mkdirs();
}
// creates the file index
String filePath = file.getPath();
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath))) {
bufferedWriter.write("");
} catch (IOException e) {
e.printStackTrace();
}
}
// hashes the content of a file using SHA1
public static String SHA1Hashing(File file) throws IOException {
String content;
if(file.isDirectory())
content = fileContent(getDirFile(file));
else
content = fileContent(file); // getting the content
try {
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.update(content.getBytes("UTF-8"));
BigInteger temp = new BigInteger(1, crypt.digest());
return temp.toString(16);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// this is here bc otherwise there is an error message. well can anyone actually
// bypass the "try"?
return "error";
}
// creating tree file for a directory
private static File getDirFile(File file) throws IOException {
try{
File tempFile = File.createTempFile("tempFile", null);
FileWriter fw = new FileWriter(tempFile);
File [] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++){
String hash = SHA1Hashing(fileList[i]);
if (i < fileList.length - 1){
if (fileList[i].isDirectory())
fw.write("tree " + hash + " " + fileList[i].getName() + "\n"); // when calling sha1 hash dirFile gets reset to having content so when writing again it goes in the middle
else
fw.write("blob " + hash + " " + fileList[i].getName() + "\n");
}
else{
if (fileList[i].isDirectory())
fw.write("tree " + hash + " " + fileList[i].getName());
else
fw.write("blob " + hash + " " + fileList[i].getName());
}
}
fw.close();
return tempFile;
}
catch (IOException e){
e.printStackTrace();
return File.createTempFile("tempFile", null);
}
}
// converts data in the file into a single string
private static String fileContent(File file) {
StringBuilder sb = new StringBuilder();
try (BufferedReader bf = new BufferedReader(new FileReader(file));) {
while (bf.ready()) {
sb.append(bf.readLine());
if (bf.ready()) {
// if has next line, then add a "\n"
sb.append("\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public static void blobCreation(File file) throws IOException {
if(!file.exists()){
throw new FileNotFoundException();
}
String name;
if (file.isDirectory()){
File dirFile = getDirFile(file);
name = SHA1Hashing(dirFile); //getting hashed name
File blobFile = new File("./git/objects", name); // the blob file
// creates the blob file
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(blobFile.getPath())); BufferedReader br = new BufferedReader(new FileReader (dirFile))) {
while (br.ready())
bufferedWriter.write(br.read());
br.close();
bufferedWriter.close();
}
catch (IOException e) {
e.printStackTrace();
}
// creating blob files for all subfiles/subdirectories and their respective files/subdirectories etc.
try{
File [] files = file.listFiles();
for (File f : files){
blobCreation(f);
}
}
catch (IOException e){
e.printStackTrace();
}
}
else{
name = SHA1Hashing(file); // getting hashed name
File blobFile = new File("./git/objects/" + name); // the blob file
// creates the blob file
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(blobFile.getPath())); BufferedReader br = new BufferedReader(new FileReader (file))) {
while (br.ready())
bufferedWriter.write(br.read());
bufferedWriter.close();
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
// adds a new line into the index file
try {
FileWriter fw = new FileWriter("./git/index", true);
BufferedWriter bufferedWriter = new BufferedWriter(fw);
// writes the content into index by first checking to see if the index file has been written into or not, (to determine if a /n is needed)
// and then if the file is a directory or not, (to determine if tree or blob) and then if the file is in a subdir or if it is in the home dir
// (to determine whether to write the parent dir or not)
FileReader checks = new FileReader("./git/index");
if (!checks.ready()) {
if (file.isDirectory()){
if(isInHomeDir(file))
bufferedWriter.write("tree " + name + " " + file.getName());
else
bufferedWriter.write("tree " + name + " " + file.getParentFile().getName() + "/" + file.getName());
}else{
if (isInHomeDir(file))
bufferedWriter.write("blob " + name + " " + file.getName());
else
bufferedWriter.write("blob " + name + " " + file.getParentFile().getName() + "/" + file.getName());
}
} else {
if (file.isDirectory()){
if (isInHomeDir(file))
bufferedWriter.write("\ntree " + name + " " + file.getName());
else
bufferedWriter.write("\ntree " + name + " " + file.getParentFile().getName() + "/" + file.getName());
} else {
if(isInHomeDir(file))
bufferedWriter.write("\nblob " + name + " " + file.getName());
else
bufferedWriter.write("\nblob " + name + " " + file.getParentFile().getName() + "/" + file.getName());
}
}
// bufferedWriter.write(name + " " + file.getName() + "\n");
bufferedWriter.close();
checks.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean isInHomeDir (File file){
return file.getParentFile().getPath().equals(".");
}
}