-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommit.java
More file actions
464 lines (417 loc) · 14.8 KB
/
Commit.java
File metadata and controls
464 lines (417 loc) · 14.8 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Formatter;
import java.util.HashMap;
import javax.lang.model.util.ElementScanner14;
public class Commit {
private File commit;
private String prevSha = "";
private String nextSha = "";
private String author;
private String shaOfThisCommit;
private String date;
private String summary;
private Tree mainTree;
private String contentsOfFile;
private String sha1 ="";
private Date date1;
private String shaOfPrevTree;
public Commit(String prevCommitSha, String author, String summary) throws Exception {
prevSha = prevCommitSha;
File objects = new File("./objects");
if (!objects.exists())
objects.mkdirs();
Tree tree = new Tree();
if (prevCommitSha != "")
{
addPreviousTreeToCurrentTree(prevCommitSha, tree);
}
String sha = createTree(tree);
date1 = new Date(java.lang.System.currentTimeMillis());
commit = new File("commit");
PrintWriter pw = new PrintWriter(new FileWriter("commit"));
pw.println(sha);
//print location
pw.println(prevCommitSha);
//print empty line
pw.println();
//print name
pw.println(author);
//print date
pw.println(date1);
//print summary
pw.print(summary);
pw.close();
//shaOfThisCommit = encryptPassword(getContents(commit));
rename(commit);
updateChildShaOfParent(shaOfThisCommit);
}
public String getShaOfPrevTree() throws IOException
{
shaOfPrevTree = getLineOne(prevSha);
return shaOfPrevTree;
}
//public void setFirstToNext
public void updateChildShaOfParent(String newestShaToAddToParent) throws IOException
{
String tempprevsha = prevSha;
if (prevSha == "")
{
return;
}
File f = new File("./objects/" + prevSha);
if (f.exists())
{
Index ind = new Index();
String parentCommitContents = ind.fileToString("./objects/" + prevSha);
System.out.println(parentCommitContents);
int secondNewLine = Commit.ordinalIndexOf(parentCommitContents, "\n", 2);
int thirdNewLine = Commit.ordinalIndexOf(parentCommitContents, "\n", 3);
//String toWrite = Commit.insertString("", prevSha, secondNewLine);
//System.out.println(toWrite);
String parentCommitContentsFirstTwoLines = parentCommitContents.substring(0,secondNewLine+1);
String parentCommitContentsLastThreeLines = parentCommitContents.substring(thirdNewLine, parentCommitContents.length());
parentCommitContents = parentCommitContentsFirstTwoLines + newestShaToAddToParent + parentCommitContentsLastThreeLines;
PrintWriter pw = new PrintWriter("./objects/" + prevSha);
pw.write(parentCommitContents);
pw.close();
/*
BufferedReader br = new BufferedReader(new FileReader(f));
StringBuilder sb = new StringBuilder();
PrintWriter writer = new PrintWriter(f);
sb.append(br.readLine() + "\n");
sb.append(br.readLine() + "\n");
sb.append(toWrite +"\n");
sb.append(br.readLine() + "\n");
sb.append(br.readLine() + "\n");
sb.append(br.readLine() + "\n");
String temp = sb.toString();
writer.write(temp);
br.close();
*/
//writer.close();
}
}
public static String insertString( String originalString, String stringToBeInserted, int index)
{
// Create a new string
String newString = new String();
for (int i = 0; i < originalString.length(); i++) {
// Insert the original string character
// into the new string
newString += originalString.charAt(i);
if (i == index) {
// Insert the string to be inserted
// into the new string
newString += stringToBeInserted;
}
}
// return the modified String
return newString;
}
public static int ordinalIndexOf(String str, String substr, int n) {
int pos = str.indexOf(substr);
while (--n > 0 && pos != -1)
pos = str.indexOf(substr, pos + 1);
return pos;
}
public void addPreviousTreeToCurrentTree(String parentCommit, Tree tree) throws Exception {
tree.add("tree : " + getLineOne(parentCommit));
}
public Commit(String author, String summary) throws Exception
{
this("", author, summary);
/*File f = new File("./objects/" + currSha);
BufferedReader br = new BufferedReader(new FileReader(f));
StringBuilder sb = new StringBuilder();
PrintWriter writer = new PrintWriter(f);
sb.append(br.readLine() + "\n");
sb.append(br.readLine() + "\n");
sb.append(prevSha);
sb.append(br.readLine() + "\n");
sb.append(br.readLine() + "\n");
sb.append(br.readLine() + "\n");
writer.write(sb.toString());
br.close();
writer.close();
*/
//sha1 = createTree();
/*
Index ind = new Index();
ind.init();
File f = new File("first");
if (f.exists())
{
String str = ind.fileToString("first");
prevSha = Blob.encryptThisString(str);
}
this.mainTree = new Tree();
this.author = author;
this.summary = summary;
this.date1 = new Date(java.lang.System.currentTimeMillis());
contentsOfFile = mainTree.getSha() + "\n" + prevSha + "\n" + nextSha + "\n" + author + "\n" + date1 + "\n" + summary;
sha1 = encryptPassword(contentsOfFile);
File commitFile = new File("./objects/" + sha1);
if (!commitFile.exists())
{
commitFile.createNewFile();
}
PrintWriter writer = new PrintWriter(new FileWriter(commitFile));
writer.print(contentsOfFile);
writer.close();
//linkToNextCom();
FileWriter fw = new FileWriter("first");
fw.write(sha1);
fw.close();
new FileWriter("./index", false).close();
*/
}
/*public void linkToNextCom() throws IOException {
File firstFile = new File("first");
if (!firstFile.exists())
{
return;
}
File file = new File("./objects/" + prevSha);
File fileToReset = new File("./objects/" + "holder");
try (BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter bw = new BufferedWriter(new FileWriter(fileToReset))) {
String line = "";
for (int i = 0; i < 5; i++)
{
line = br.readLine();
if (i == 2)
{
bw.write(sha1 + "\n");
}
else
{
bw.write(line + "\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
fileToReset.renameTo(file);
}
*/
public void checkout(String shaOfCommit) throws IOException
{
String rootTree = getLineOne(shaOfCommit);
Index ind = new Index();
BufferedReader br = new BufferedReader(new FileReader("./objects/" + rootTree));
HashMap<String, String> hMap = new HashMap<>();
ArrayList<String> hardCoded = new ArrayList<String>(Arrays.asList("dir", "dir2", "directoryTest", "directoryTestFile", "Folder", "lib", "obects", "ScreenShotsForCommitReqs", "tests", "addDirectoryTest.java", "Blob.java", "BlobTest.java", "codeConfig.json", "commit", "Commit.java", "CommitTest.java", "createTwoCommits.java", "first", "index", "Index.java", "IndexTest.java", "main.java", "README.md", "test1", "test2", "tester.java", "testFile.txt", "testFile2.txt", "Tree.java", "TreeTest.java"));
File f = new File("./GitPrereqs");
for (File file : f.listFiles())
{
if (!hardCoded.contains(file.getName()))
{
file.delete();
}
}
while (br.ready())
{
String read = br.readLine();
if (read.contains("tree"))
{
if (read.length() > 50)
{
File dir = new File("./GitPrereqs" + read.substring(ordinalIndexOf(read, " : ", 2), read.length()));
dir.mkdir();
checkout(read.substring(ordinalIndexOf(read, " : ", 1), ordinalIndexOf(read, " : ", 2)+1));
}
else if (read.length() < 50)
{
checkout(read.substring(ordinalIndexOf(read, " : ", 1), read.length()));
}
}
else if (read.contains("blob"))
{
hMap.put(read.substring(ordinalIndexOf(read, " : ", 2)+1, read.length()), read.substring(ordinalIndexOf(read, " : ", 1), ordinalIndexOf(read, " : ", 2)+1));
}
}
br.close();
for (String file : hMap.keySet())
{
File inObjectsFolder = new File("./GitPrereqs" + file);
PrintWriter pw = new PrintWriter(inObjectsFolder);
pw.write(ind.fileToString(hMap.get(file)));
pw.close();
}
}
public String createTree(Tree tree) throws Exception {
writeIndexFileToTreeFile(tree);
File indexClear = new File("index");
indexClear.delete();
indexClear.createNewFile();
String sha = tree.getSha();
return sha;
}
public String getDate() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
return timeStamp;
}
public void writeIndexFileToTreeFile(Tree tree) throws Exception {
File indexFile = new File("index");
if (!indexFile.exists()) {
throw new Exception("file doesn't exist");
}
BufferedReader br = new BufferedReader(new FileReader(indexFile));
String read;
while (br.ready()) {
read = br.readLine();
if (!read.contains("*deleted*"))
{
tree.add(read);
}
}
br.close();
tree.writeToFile();
}
public static String getLineOne(String prevCommitSha) throws IOException
{
if (prevCommitSha == "")
{
return "";
}
String path = "./objects/" + prevCommitSha;
File file = new File (path);
BufferedReader br = new BufferedReader(new FileReader(file));
String ret = br.readLine();
br.close();
return ret;
}
String getContents(File fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
StringBuilder sb = new StringBuilder();
while (br.ready())
{
sb.append(br.readLine() + "\n");
}
br.close();
return sb.toString();
/*BufferedReader br = new BufferedReader(new FileReader(fileName));
String str = "";
*/
/*while(br.ready()) {
str += br.readLine()+"\n";
}*/
/*
for(int i=1; i<=6; i++) {
if(i!=3) {
str += br.readLine()+"\n";
}
}
str = str.trim();//get rid of extra line
br.close();
return str;
*/
}
/*public static String getSHA(String contents) {
String sha1 = "";
try {
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(contents.getBytes("UTF-8"));
sha1 = byteToHex(crypt.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return sha1;
}
*/
public String getSha() {
return shaOfThisCommit;
}
private void rename(File fileName) throws IOException {
/*BufferedReader br = new BufferedReader(new FileReader(fileName));
String str = "";
while(br.ready()) {
str += br.readLine()+"\n";
}
for(int i=1; i<=6; i++) {
if(i!=3) {
str += br.readLine()+"\n";
}
}
str = str.trim();//get rid of extra line
br.close();
*/
String str = getContents(fileName);
str = str.stripTrailing();
//converting to sha1
// TODO - INCLUDES 3rd LINE IN THE COMMIT
shaOfThisCommit = encryptPassword(str);
//printing to objects folder
String dirName = "./objects/";
File dir = new File (dirName);
File newFile = new File (dir, shaOfThisCommit);
PrintWriter pw = new PrintWriter(newFile);
/*StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(fileName));
sb.append(br.readLine() + "\n");
sb.append(br.readLine() + "\n");
sb.append("\n");
sb.append(br.readLine() + "\n");
sb.append(br.readLine() + "\n");
sb.append(br.readLine());
br.close();
pw.print(sb.toString());
pw.close();
*/
//str = str.stripTrailing();
pw.write(str);
pw.close();
File tempCommitFile = new File("commit");
tempCommitFile.delete();
tempCommitFile.createNewFile();
}
String encryptPassword(String password)
{
String sha1 = "";
try
{
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(password.getBytes("UTF-8"));
sha1 = byteToHex(crypt.digest());
}
catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
return sha1;
}
private static String byteToHex(final byte[] hash)
{
Formatter formatter = new Formatter();
for (byte b : hash)
{
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
}