-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommit.java
More file actions
271 lines (249 loc) · 8.85 KB
/
Commit.java
File metadata and controls
271 lines (249 loc) · 8.85 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
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
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.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class Commit {
private String author, desc, treeSha, prevCommitSha, nextSha;
// This is to commit a file that has the previous sha
public Commit(String prevCommitSha, String author, String desc) throws IOException {
if (prevCommitSha!= "")
{
this.prevCommitSha = prevCommitSha;
}
treeSha = treeify();
this.author = author;
this.desc = desc;
// if (prevCommitSha!= "")
// {
// // String treeSha = getPrevCommitTree();
// File f = new File (treeSha);
// FileWriter fw = new FileWriter (f);
// // fw.write ("tree : " + )
// }
}
// this is if you do not have the previous sha
// public Commit(String author, String desc) throws IOException {
// treeSha = treeify();
// this.author = author;
// this.desc = desc;
// }
// gets the sha of the tree, pretty damn inportant
public String treeify() throws IOException {
Tree tree = new Tree();
BufferedReader br = new BufferedReader(new FileReader("index"));
String line;
while ((line = br.readLine()) != null)
{
tree.add(line);
}
if (prevCommitSha != "" && prevCommitSha != null){
tree.add(getPrevCommitTree());
}
br.close();
tree.getContents();
tree.writeToObjects();
FileWriter fw = new FileWriter ("index");
fw.write("");
fw.close();
return tree.getSha();
}
// Gets a date (not to homecoming)
public String getDate() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
return dtf.format(now);
}
// This is actually what saves the file into the object folder, the new commit
public void push() throws IOException {
// if (prevCommitSha != "" && prevCommitSha != null && prevCommitSha != "da39a3ee5e6b4b0d3255bfef95601890afd80709")
// {
// File z = new File (prevCommitSha);
// }
// if (prevCommitSha != "" && prevCommitSha != null && prevCommitSha != "da39a3ee5e6b4b0d3255bfef95601890afd80709")
// {
// // File f = new File (prevCommitSha);
// File z = new File ("objects/"+prevCommitSha);
// int lineNumber = 3;
// BufferedReader reader = new BufferedReader(new FileReader(z));
// StringBuilder content = new StringBuilder();
// String line;
// int currentLine = 1;
// while ((line = reader.readLine()) != null) {
// if (currentLine == lineNumber) {
// content.append(getSha()).append("\n");
// }
// content.append(line).append("\n");
// currentLine++;
// }
// reader.close();
// FileWriter writer = new FileWriter(prevCommitSha);
// writer.write(content.toString());
// writer.close();
// // BufferedReader br = new BufferedReader(new FileReader(f));
// // FileWriter fw = new FileWriter (f);
// // String curline;
// // int cur = 0;
// // while (br.readLine() != null)
// // {
// // cur++;
// // if (cur == 3)
// // {
// // fw.write
// // }
// // }
// }
// if (nextSha == null)
// {
// nextSha = "";
// }
// FileWriter write = new FileWriter(new File("objects/" + getSha()));
// write.write(treeSha + "\n" + prevCommitSha + "\n" + nextSha + "\n" + author + "\n" + getDate() + "\n" + desc);
// write.close();
File f = new File("objects/"+getSha());
f.createNewFile();
PrintWriter pw = new PrintWriter (f);
pw.println(treeSha);
if (prevCommitSha == null)
{
pw.println ();
}
else
{
pw.println (prevCommitSha);
}
File a = new File ("objects/"+prevCommitSha);
if (a.exists())
{
int lineNumber = 3;
BufferedReader reader = new BufferedReader(new FileReader(a));
StringBuilder content = new StringBuilder();
String line;
int currentLine = 1;
while ((line = reader.readLine()) != null) {
if (currentLine == lineNumber) {
content.append(getSha()).append("\n");
}
content.append(line).append("\n");
currentLine++;
}
reader.close();
FileWriter writer = new FileWriter(a);
writer.write(content.toString());
writer.close();
//
File input_file = new File("objects/"+prevCommitSha);
BufferedReader file_reader = new BufferedReader(new FileReader("objects/"+prevCommitSha));
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("objects/"+prevCommitSha);
file_writer.write(file_content.toString());
file_writer.close();
}
pw.println();
pw.println (author);
pw.println (getDate());
pw.print (desc);
pw.close();
}
// Gets the sha, this is code from blob.java
public String getSha() {
String input = (treeSha + "\n" + prevCommitSha + "\n" + author + "\n" + getDate() + "\n" + desc);
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
// This is the complicated part! Writes the new sha into the old file
public void writeInNewCommit() throws IOException {
File orginalFile = new File("objects/" + prevCommitSha);
File newFile = new File("balls");
BufferedReader reader = new BufferedReader(new FileReader(orginalFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(newFile));
String curr;
int i = 0;
while ((curr = reader.readLine()) != null) {
if (i == 2) {
writer.write(getSha());
} else
writer.write(curr);
if (i != 5)
writer.write("\n");
i++;
}
writer.close();
reader.close();
newFile.renameTo(orginalFile);
}
public String getPrevCommitTree() throws IOException
{
File f = new File ("objects/" + prevCommitSha);
BufferedReader br = new BufferedReader(new FileReader(f));
String line = br.readLine();
br.close();
return line;
}
public String getTreeSha() {
return treeSha;
}
public String getPrevCommitSha() {
return prevCommitSha;
}
public String getNextSha() {
return nextSha;
}
public void setNextSha(String nextSha)
{
this.nextSha = nextSha;
}
public void makeHead() throws IOException
{
File f = new File ("HEAD");
if (f.exists())
{
f.delete();
}
f.createNewFile();
FileWriter fw = new FileWriter (f);
fw.write(getSha());
fw.close();
}
public static void main(String[] args) throws IOException {
Git.initialize();
Git.addFile("a.txt");
Commit c = new Commit ("","mark","poo");
// c.writeInNewCommit();
System.out.println(c.getTreeSha());
System.out.println(c.getTreeSha());
c.push();
System.out.println (c.getSha());
Commit d = new Commit (c.getSha(),"mark2","poo2");
d.push();
Commit e = new Commit (d.getSha(),"mark3","poo3");
e.push();
}
}