-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommit.java
More file actions
231 lines (202 loc) · 6.95 KB
/
commit.java
File metadata and controls
231 lines (202 loc) · 6.95 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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class Commit {
static Tree t;
private String summary;
private String author;
private String parentTree;
private String parentCommit;
private String nextSha;
// This is the initial commit, sorry jake wrote this weird to me
public Commit(String parentCommit, String author, String summary) throws IOException {
this.parentTree = createTree();
this.summary = summary;
this.author = author;
this.parentCommit = parentCommit;
clearIndex();
this.nextSha = "";
writeOut();
}
public Commit(String parentTree, String parentCommit, String author, String summary) throws IOException {
this.parentTree = parentTree;
Tree t = new Tree();
t.addIndex(parentTree);
this.summary = summary;
this.author = author;
this.parentCommit = parentCommit;
getTreeFromSha(parentTree);
clearIndex();
this.nextSha = "";
writeOut();
}
// Clears the inex
private void clearIndex() throws IOException {
FileWriter fw = new FileWriter(new File("index"));
fw.write("");
fw.close();
}
// Generates date for the commit
public String getDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return (dateFormat.format(date));
}
// Creates the tree if it doesn't exist
public String createTree() throws IOException {
Tree t = new Tree();
t.addIndex(parentTree);
t.writeToObjects();
return (t.getSHA1());
}
public String generateSha1() throws IOException {
File f = new File("temp.txt");
f.createNewFile();
PrintWriter pw = new PrintWriter(f);
pw.println(parentTree);
pw.println(parentCommit);
pw.println();
pw.println(author);
pw.println(getDate());
pw.print(summary);
pw.close();
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader("temp.txt"));
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
f.delete();
return (Blob.hashStringToSHA1(sb.toString()));
}
// What was the point of this mark
public void writeOut() throws IOException {
File f = new File("./objects/" + generateSha1());
f.createNewFile();
FileWriter fw = new FileWriter(f);
fw.append(getParentTree() + "\n");
fw.append(parentCommit + "\n");
File a = new File(parentCommit);
if (a.exists()) {
int lineNumber = 3;
BufferedReader reader = new BufferedReader(new FileReader(parentCommit));
StringBuilder content = new StringBuilder();
String line;
int currentLine = 1;
while ((line = reader.readLine()) != null) {
if (currentLine == lineNumber) {
content.append(generateSha1()).append("\n");
}
content.append(line).append("\n");
currentLine++;
}
reader.close();
FileWriter writer = new FileWriter(parentCommit);
writer.write(content.toString());
writer.close();
BufferedReader file_reader = new BufferedReader(new FileReader(parentCommit));
StringBuilder file_content = new StringBuilder();
String line1;
int line_number = 0;
while ((line1 = file_reader.readLine()) != null) {
line_number++;
if (line_number != 4) {
file_content.append(line1).append("\n");
}
}
file_reader.close();
FileWriter file_writer = new FileWriter(parentCommit);
file_writer.write(file_content.toString());
file_writer.close();
}
fw.append("\n");
fw.append(author + "\n");
fw.append(getDate() + "\n");
fw.append(summary + "\n");
fw.close();
writeInNewCommit();
}
// This is the complicated part! Writes the new sha into the old file
public void writeInNewCommit() throws IOException {
if (parentCommit == "") {
return;
}
File originalFile = new File("objects/" + parentCommit);
System.out.println(originalFile.getAbsolutePath());
File newFile = new File("balls");
FileWriter ballWriter = new FileWriter(newFile);
try (FileReader kevin = new FileReader(originalFile);
BufferedReader reader = new BufferedReader(kevin);
BufferedWriter writer = new BufferedWriter(new FileWriter(newFile))) {
String curr;
int i = 0;
while ((curr = reader.readLine()) != null) {
if (i == 2) {
ballWriter.write(generateSha1());
} else {
ballWriter.write(curr);
}
if (i != 5) {
ballWriter.write("\n");
}
i++;
}
}
ballWriter.close();
Files.move(newFile.toPath(), originalFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// File newFile = new File("balls");
// FileReader kevin = new FileReader(originalFile);
// BufferedReader reader = new BufferedReader(kevin);
// BufferedWriter writer = new BufferedWriter(new FileWriter(newFile));
// String curr;
// int i = 0;
// while ((curr = reader.readLine()) != null) {
// if (i == 2) {
// writer.write(generateSha1());
// } else
// writer.write(curr);
// if (i != 5)
// writer.write("\n");
// i++;
// }
// writer.close();
// reader.close();
// newFile.renameTo(originalFile);
}
// Gets the tree associated with a hash
public static String getTreeFromSha(String sha) throws IOException {
String treeString = "";
try (BufferedReader br = new BufferedReader(new FileReader("objects/" + sha))) {
treeString = br.readLine();
br.close();
}
return treeString;
}
public String getParentTree() {
return parentTree;
}
public String getParentCommit() {
return parentCommit;
}
public String getAuthor() {
return author;
}
public String getSummary() {
return summary;
}
public String getNextSha() {
return nextSha;
}
public void deleteFile(String file) {
}
}