-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommit.java
More file actions
230 lines (202 loc) · 6.34 KB
/
Commit.java
File metadata and controls
230 lines (202 loc) · 6.34 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
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.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Formatter;
import java.util.Map;
import java.util.List;
import java.nio.file.*;
public class Commit {
private String shaOfTreeObject;
private String shaOfPreviousCommit;
private String shaOfNextCommit;
private String author;
private String summary;
public Commit(String parentSha1, String author, String summary, Index index) throws IOException {
shaOfPreviousCommit = parentSha1;
shaOfTreeObject = createTree(index);
this.author = author;
this.summary = summary;
}
public Commit(String author, String summary, Index index) throws IOException {
shaOfTreeObject = createTree(index);
this.author = author;
this.summary = summary;
}
public String getSHAofPreviousCommit ()
{
return shaOfPreviousCommit;
}
public void saveCommit() throws IOException {
FileWriter fw = new FileWriter(new File("objects/" + generateSha1()));
fw.write(shaOfTreeObject + "\n");
fw.write(shaOfPreviousCommit + "\n");
fw.write(shaOfNextCommit + "\n");
fw.write(author + "\n");
fw.write(getDate() + "\n");
fw.write(summary);
fw.close();
if (shaOfPreviousCommit != null && !shaOfPreviousCommit.isEmpty()) {
changeNextCommitOfPreviousCommit();
}
}
public void changeNextCommitOfPreviousCommit() throws IOException {
File inputFile = new File("objects/" + shaOfPreviousCommit);
File tempFile = new File("__temp__");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String currentLine;
int i = 0;
while ((currentLine = reader.readLine()) != null) {
if (i == 2) {
writer.write(generateSha1() + "\n");
} else if (i != 5) {
writer.write(currentLine + "\n");
} else {
writer.write(currentLine);
}
i++;
}
writer.close();
reader.close();
tempFile.renameTo(inputFile);
}
public String getNextCommitSHA1()
{
return shaOfNextCommit;
}
public String createTree(Index index) throws IOException {
Tree tree = new Tree();
ArrayList<String> deletedFiles = new ArrayList<>();
ArrayList<String> editedFiles = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader ("index"));
String str = "";
while((str = br.readLine())!=null)
{
if (str.contains("*deleted"))
{
deletedFiles.add (str);
}
if (str.contains("*edited"))
{
editedFiles.add(str);
}
}
br.close();
Map <String, String> blobMap = index.getBlobMap();
List <Tree> treeList = index.getTreeList();
for (Map.Entry<String, String> entry : blobMap.entrySet())
{
tree.addTreeEntry ("blob",entry.getValue(), entry.getKey());
}
for (Tree dir : treeList)
{
String directoryName = dir.getFileName();
String treeSHA1 = dir.getSHA1();
tree.addTreeEntry ("tree", treeSHA1, directoryName);
}
if (shaOfPreviousCommit != null && !shaOfPreviousCommit.isEmpty())
{
tree.addTreeEntry("tree",shaOfPreviousCommit,"prev_commit");
}
tree.handleDeletedFiles(deletedFiles);
tree.handleEditedFiles (editedFiles);
index.clearIndexFile();
tree.save();
return tree.calculateTreeSHA1();
}
public String getCommitTree (String commitSHA1)
{
Path commitPath = Paths.get ("objects",commitSHA1);
List <String> lines = new ArrayList<>();
try
{
lines = Files.readAllLines (commitPath);
}
catch (IOException e)
{
e.printStackTrace();
}
if (lines.isEmpty())
{
throw new IllegalArgumentException();
}
String firstLine = lines.get(0);
String [] parts = firstLine.split (" : ");
if (parts.length < 2 || !parts[0].equals("tree"))
{
throw new IllegalArgumentException();
}
String treeSHA1 = parts[1];
return treeSHA1;
}
public String getSHA1 ()
{
return shaOfTreeObject;
}
public String getSHAOfNextCommit ()
{
return shaOfNextCommit;
}
public void update (String prevSHA1, String newSHA1)
{
Path commitPath = Paths.get("objects", prevSHA1);
List<String> lines = new ArrayList<String> ();
try
{
lines = Files.readAllLines (commitPath);
}
catch (IOException e)
{
e.printStackTrace();
}
lines.set(2, newSHA1);
try
{
Files.write (commitPath, lines);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public String generateSha1() throws IOException {
String toEncrypt = shaOfTreeObject + "\n" + shaOfPreviousCommit + "\n" + author + "\n" + getDate() + "\n"
+ summary;
String passwordString = toEncrypt;
String sha1 = "";
try {
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(passwordString.getBytes("UTF-8"));
sha1 = byteToHex(crypt.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return sha1;
}
public 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;
}
public String getDate() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
}