-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommit.java
More file actions
279 lines (241 loc) · 9.73 KB
/
Commit.java
File metadata and controls
279 lines (241 loc) · 9.73 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
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Formatter;
import java.util.List;
import java.util.Scanner;
public class Commit {
String hashOfTree;
String prevHash;
String nextHash;
String author;
String date;
String summary;
public Commit(String sha1, String author, String summary) throws Exception {
this.prevHash = sha1;
this.hashOfTree = createTree();
this.nextHash = "";
this.author = author;
this.date = getDate();
this.summary = summary;
String hash = getCommitSHA1();
File file = new File("./objects/" + hash);
file.createNewFile();
FileWriter writer = new FileWriter(file);
PrintWriter out = new PrintWriter(writer);
out.print(hashOfTree + "\n" + prevHash + "\n" + nextHash + "\n" + author + "\n" + date + "\n" + summary);
writer.close();
out.close();
//updating previous commit to link to this one
File head = new File ("./HEAD");
//read HEAD
if (head.exists ())//i.e. if there is a previous commit
{
Scanner scanner = new Scanner(new File("./HEAD"));
String prevCommit = "./objects/" + scanner.useDelimiter("\\A").next();
scanner.close();
//replacing line 3
Path path = Paths.get(prevCommit);
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
lines.set(2, getCommitSHA1 ());
Files.write(path, lines, StandardCharsets.UTF_8);
//trimming new line from end
File commitToTrim = new File (prevCommit);
Scanner commitScanner = new Scanner(commitToTrim);
String untrimmed = commitScanner.useDelimiter("\\A").next();
String trimmed = untrimmed.substring (0, untrimmed.length () - 1);
commitScanner.close();
FileWriter prevWriter = new FileWriter(prevCommit,false);
PrintWriter prevPrint = new PrintWriter(prevWriter);
prevPrint.print (trimmed);
prevWriter.close ();
prevPrint.close ();
}
updateHEAD ();
}
public Commit(String author, String summary) throws Exception {
this("", author, summary);
updateHEAD ();
}
public void setNextCommit(String nextCommitHash) throws Exception {
if (this.nextHash == "") {
this.nextHash = nextCommitHash;
}
String hash = getCommitSHA1();
File file = new File("./objects/" + hash);
FileWriter writer = new FileWriter(file);
PrintWriter out = new PrintWriter(writer);
out.print(hashOfTree + "\n" + prevHash + "\n" + nextHash + "\n" + author + "\n" + date + "\n" + summary);
writer.close();
out.close();
}
public String getCommitSHA1() throws Exception {
String content = hashOfTree + "\n" + prevHash + "\n" + "" + "\n" + author + "\n" + date + "\n" + summary;
return getSHA1fromString(content);
}
public String createTree() throws Exception {
Tree tree = new Tree();
Boolean editingOrDeleting = false;
//adding each file in index
File index = new File ("index");
if (index.exists () && index.length () != 0)
{
FileInputStream fstream = new FileInputStream("index");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
if (strLine.charAt (0) == 'b' || strLine.charAt (0) == 't')
{
tree.add (strLine);
}
else
{
tree.addEditedOrDeleted (strLine, prevHash);
editingOrDeleting = true;
}
}
//Close the input stream
in.close();
}
//clearing index
if (index.exists ())
{
index.delete ();
}
index.createNewFile ();
//adding previous tree
if (!prevHash.equals ("") && !editingOrDeleting)
{
tree.add ("tree : " + getTreeSHA1FromCommit(prevHash));
}
return tree.writeToFile();
}
public void checkout (String commitSHA1) throws IOException
{
Scanner scanner = new Scanner(new File ("./objects/" + commitSHA1));
String commitFirstLine = scanner.useDelimiter("\\n").next();
scanner.close();
File checkoutFolder = new File ("./checkoutFolder");
checkoutFolder.mkdir();
addEverythingFromTree (commitFirstLine, "");
}
public void addEverythingFromTree (String treeSHAString, String prefix) throws IOException
{
File tree = new File ("./objects/" + treeSHAString);
if (tree.length () != 0)
{
FileInputStream fstream = new FileInputStream("./objects/" + treeSHAString);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Do epic stuff
//restore file
if (strLine.charAt (0) == 't' && strLine.substring (6).indexOf (":") != -1)//making new folder
{
File makeSureThisFolderExists = new File ("./checkoutFolder/" + prefix + strLine.substring (50));//directory addition
if (!makeSureThisFolderExists.exists ())
{
makeSureThisFolderExists.mkdir ();
}
addEverythingFromTree (strLine.substring (7, 47), prefix + strLine.substring (50) + "/");
}
}
in.close ();
FileInputStream fstream2 = new FileInputStream("./objects/" + treeSHAString);
// Get the object of DataInputStream
DataInputStream in2 = new DataInputStream(fstream2);
BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
String strLine2;
//Read File Line By Line
String fileContents = "";
while ((strLine2 = br2.readLine()) != null) {
// Do epic stuff
//restore file
fileContents = "";
if (strLine2.charAt (0) == 'b')
{
File blobFile = new File ("./objects/" + strLine2.substring (7,47));
Scanner scanner = new Scanner(blobFile);
if (blobFile.length () != 0)
{
fileContents = scanner.useDelimiter("\\A").next();
scanner.close();
}
File newFile = new File ("./checkoutFolder/" + prefix + strLine2.substring (50));
newFile.createNewFile ();
FileWriter writer = new FileWriter("./checkoutFolder/" + prefix + strLine2.substring (50),false);
PrintWriter out = new PrintWriter(writer);
out.print (fileContents);
writer.close ();
out.close ();
}
else if (strLine2.substring (6).indexOf (":") == -1)//is past tree, not directory
{
addEverythingFromTree (strLine2.substring (7), "./");
}
}
in2.close ();
}
}
public String getDate() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM d, uuuu");
LocalDateTime now = LocalDateTime.now();
return now.format(dtf);
}
public String getSHA1fromString(String myString) throws Exception {
// hashes file with SHA1 hash code into String called SHA1
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(myString.getBytes("UTF-8"));
Formatter formatter = new Formatter();
for (byte b : crypt.digest()) {
formatter.format("%02x", b);
}
String SHA1 = formatter.toString();
formatter.close();
return SHA1;
}
public void updateHEAD () throws Exception
{
File head = new File ("./HEAD");
head.createNewFile();
FileWriter writer = new FileWriter("./HEAD",false);
PrintWriter out = new PrintWriter(writer);
out.print (getCommitSHA1 ());
writer.close ();
out.close ();
}
//Method to get Commit's Tree based on a Commit's SHA1
//This method will open the SHA1 of the Commit and return the hash of the Tree (it's first line)
public static String getTreeSHA1FromCommit (String commitSHA) throws Exception
{
File commitFile = new File ("./objects/" + commitSHA);
if (!commitFile.exists ())
{
throw new Exception ("No commit exists with the given SHA.");
}
Scanner scanner = new Scanner(commitFile);
String commitContents = scanner.useDelimiter("\\n").next();
scanner.close();
return commitContents;
}
}