-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommit.java
More file actions
300 lines (245 loc) · 9.36 KB
/
Commit.java
File metadata and controls
300 lines (245 loc) · 9.36 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
import java.io.BufferedReader;
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.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Optional;
public class Commit {
private Tree myTree;
private String treeSHA1FileLocation = "";
private String parentSHA1 = "";
private String childSHA1 = "";
private String author = "";
private String summary = "";
//private long timestamp
public Commit(String author, String summary) throws Exception {
this.myTree = new Tree();
treeSHA1FileLocation = createTree();
this.author = author;
this.summary = summary;
//this.timestamp = System.currentTimeMillis();
}
public Commit(String author, String summary, String parentSHA1) throws Exception {
this.myTree = new Tree();
treeSHA1FileLocation = createTree();
this.author = author;
this.summary = summary;
this.parentSHA1 = parentSHA1; // setting the parent commit SHA1
}
public void writeToFile () throws Exception
{
FileWriter writer = new FileWriter("./objects/" + generateCommitSHA1(),false);
PrintWriter out = new PrintWriter(writer);
String parentSHA1ToBeUsed = parentSHA1;
String childSHA1ToBeUsed = childSHA1;
if (parentSHA1ToBeUsed == null)
{
parentSHA1ToBeUsed = "";
}
if (childSHA1ToBeUsed == null)
{
childSHA1ToBeUsed = "";
}
out.print (treeSHA1FileLocation + "\n" + parentSHA1ToBeUsed + "\n" + childSHA1ToBeUsed + "\n" + author + "\n" + getDate () + "\n" + summary);
writer.close ();
out.close ();
if (!parentSHA1.equals(""))
{
Utils.writeFileLine("./objects/" + parentSHA1, 3, generateCommitSHA1());
}
}
public String generateCommitSHA1()
{
if (myTree == null)
{
return ""; // or throw an exception depending on your design
}
String parentSHA1ToBeUsed = parentSHA1;
if (parentSHA1ToBeUsed == null) {
parentSHA1ToBeUsed = "";
}
System.out.println("parentSHA1 during hash generation: " + parentSHA1ToBeUsed);
//return myTree.encryptPassword(treeSHA1FileLocation + "\n" + parentSHA1ToBeUsed + "\n" + author + "\n" + preciseTimestamp + "\n" + summary);
// return myTree.encryptPassword(treeSHA1FileLocation + "\n" + parentSHA1ToBeUsed + "\n" + author + "\n" + getDate() + "\n" + summary);
return myTree.encryptPassword(author + "\n" + summary + "\n" + getDate() + "\n" + treeSHA1FileLocation + "\n" + parentSHA1ToBeUsed);
}
public static String getDate ()
{
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Calendar cal = Calendar.getInstance();
return(dateFormat.format(cal.getTime()));
}
// Old method
public String createTree () throws Exception
{
ArrayList<String> list = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader("./index"));
while (br.ready())
{
list.add(br.readLine());
}
br.close();
ArrayList<String> indexContents = list;
Tree currentIndexTree = new Tree();
for (String s : indexContents)
{
String type = s.substring(0, s.indexOf(" "));
if (type.equals("blob")) {
currentIndexTree.addToTree(s);
}
else {
if (s == null || s.isEmpty()) {
return "";
}
String[] words = s.trim().split("\\s+");
String lastWord = words[words.length - 1];
currentIndexTree.addDirectory(lastWord);
}
}
if (!Utils.readFromFile("./index").equals(""))
{
System.out.println();
}
if (!parentSHA1.isEmpty()) {
File f = new File("./objects/" + childSHA1);
if (childSHA1.isEmpty() || !f.exists()) throw new Exception("commit doesn't exist");
ArrayList<String> arrList = new ArrayList<String>();
BufferedReader br2 = new BufferedReader(new FileReader("./objects/" + childSHA1));
while (br2.ready())
{
arrList.add(br2.readLine());
}
br2.close();
String result = arrList.get(0);
currentIndexTree.addToTree("tree : " + arrList);
}
// To link the childSHA1 of this commit to treeSHA1 of the previous commit:
//this.childSHA1 = parentSHA1;
//Optional<String> previousTreeSHA1 = getTreeSHA1OfPreviousCommit();
// Now assign it to childSHA1
// if (previousTreeSHA1 == null || previousTreeSHA1.isEmpty()) {
// this.childSHA1 = null; // or some default value
// } else {
// this.childSHA1 = previousTreeSHA1;
// }
currentIndexTree.saveToObjects();
treeSHA1FileLocation = currentIndexTree.getEncryption();
return currentIndexTree.getEncryption();
}
// public String createTree() throws Exception {
// ArrayList<String> list = new ArrayList<String>();
// BufferedReader br = new BufferedReader(new FileReader("/Users/ryancheng/p/Honors Topics/programminggit//index.txt"));
// StringBuilder treeContent = new StringBuilder();
// while (br.ready()) {
// String line = br.readLine();
// list.add(line);
// treeContent.append(line).append("\n");
// }
// br.close();
// ArrayList<String> indexContents = list;
// Tree currentIndexTree = new Tree();
// for (String s : indexContents) {
// String type = s.substring(0, s.indexOf(" "));
// if (type.equals("blob")) {
// currentIndexTree.addToTree(s);
// } else {
// if (s == null || s.isEmpty()) {
// return "";
// }
// String[] words = s.trim().split("\\s+");
// String lastWord = words[words.length - 1];
// currentIndexTree.addDirectory(lastWord);
// }
// }
// // return a hash of the tree content to ensure uniqueness
// return myTree.encryptPassword(treeContent.toString());
// }
// public String getTreeFromCommit(String curCommit) throws Exception {
// File f = new File("/Users/ryancheng/p/Honors Topics/programminggit/objects/" + curCommit);
// if (curCommit.isEmpty() || !f.exists()) throw new Exception("commit doesn't exist");
// //BufferedReader br2 = new BufferedReader(new FileReader("/Users/ryancheng/p/Honors Topics/programminggit/objects/" + childSHA1));
// //BufferedReader br2 = new BufferedReader(new FileReader("/Users/ryancheng/p/Honors Topics/programminggit/objects/" + curCommit));
// BufferedReader br2 = new BufferedReader(new FileReader(f));
// ArrayList<String> arrList = new ArrayList<String>();
// while (br2.ready())
// {
// arrList.add(br2.readLine());
// }
// br2.close();
// return arrList.get(0);
// }
public String getTreeFromCommit(String curCommit) throws Exception {
if (curCommit == null || curCommit.isEmpty()) {
curCommit = "";
}
// File f = new File("/Users/ryancheng/p/Honors Topics/programminggit/objects/" + curCommit);
// if (!f.exists())
// {
// return "";
// }
File f = new File("./objects/" + curCommit);
if (!f.exists()) {
System.out.println("Attempting to access: " + f.getAbsolutePath());
return "";
}
BufferedReader br2 = new BufferedReader(new FileReader(f));
ArrayList<String> arrList = new ArrayList<String>();
while (br2.ready()) {
arrList.add(br2.readLine());
}
br2.close();
if (arrList.isEmpty()) {
throw new Exception("File is empty");
}
return arrList.get(0);
}
public Tree getMyTree()
{
return this.myTree;
}
public void setMyTree(Tree myTree)
{
this.myTree = myTree;
}
public void setTreeSHA1FileLocation(String treeSHA1FileLocation)
{
this.treeSHA1FileLocation = treeSHA1FileLocation;
}
public String getTreeSHA1FileLocation ()
{
return treeSHA1FileLocation;
}
public String getParentSHA1() {
return this.parentSHA1;
}
public void setParentSHA1(String parentSHA1) {
this.parentSHA1 = parentSHA1;
}
public String getChildSHA1() {
return this.childSHA1;
}
public void setChildSHA1(String c1Hash) {
this.childSHA1 = c1Hash;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getSummary() {
return this.summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
}