-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommit.java
167 lines (142 loc) · 4.95 KB
/
Commit.java
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
package gitlet;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
/** Commit Class is used to represent the contents of
* one commit in the gitlet repository.
* @author Ria Vora*/
public class Commit implements Serializable {
/** Stores the String timestamp for the commit.*/
private String _timestamp;
/** Stores the String message for the commit.*/
private String _message;
/** Stores the mapping of files and SHA-1 IDS of
* file's contents added to this commit.*/
private HashMap<String, String> _fileToID;
/** The commit for this ID based off of timestamp,
* file contents, and message.*/
private String _ID;
/** Stores the String ID of the parent commit.*/
private String _parent;
/** Constructor that creates a blank commit with
* initialized fields.*/
public Commit() {
_fileToID = new HashMap<String, String>();
_ID = createID();
_parent = "";
_message = "";
_timestamp = "";
}
/** Constructor used to create the first commit,
* which is initialized to a certain date and time.
* @param firstCommit is a boolean on whether
* the user wants the first commit*/
public Commit(boolean firstCommit) {
this();
if (firstCommit) {
_timestamp = String.format("%1$ta %1$tb %1$td %1$tT %1$tY %1$tz",
new Date(0));
_ID = Utils.sha1(_timestamp);
_message = "initial commit";
}
}
/** Instance method to create a timestamp for the commit,
* called when the commit is being commited.*/
public void createTimestamp() {
Date currentDate = new Date(System.currentTimeMillis());
_timestamp = String.format("%1$ta %1$tb %1$td %1$tT %1$tY %1$tz",
currentDate);
_ID = createID();
}
/** Uses the timestamp, message, and list of sorted file contents
* in the form of SHA-1 IDS to make a unique SHA-1 ID for this commit.
* @return the unique SHA-1 ID*/
private String createID() {
List<String> idsList = new ArrayList<String>(_fileToID.values());
Collections.sort(idsList);
idsList.add(_timestamp);
idsList.add(_message);
return Utils.sha1(idsList.toString());
}
/** Getter method for the unique SHA-1 ID for this commit.
* @return unique SHA-1 ID*/
public String getID() {
return _ID;
}
/** Getter method for the message of this commit.
* @return message of this commit*/
public String getMessage() {
return _message;
}
/** Uses the hashmap to return the SHA-ID of the
* contents of the given file from this commit.
* @param file is the canonical file path
* @return ID of the contents*/
public String getIDFromFile(String file) {
if (!_fileToID.containsKey(file)) {
return "";
}
return _fileToID.get(file);
}
/** Uses the hashmap to return the file canonical
* pathway based off of the given SHA-ID of the
* file's contents.
* @param id is SHA-1 ID
* @return file canonical pathway*/
public String getFileFromID(String id) {
for (String filePath: _fileToID.keySet()) {
if (_fileToID.get(filePath).equals(id)) {
return filePath;
}
}
return null;
}
/** Getter method, return the hashmap of the file pathways
* to SHA-1 IDS.
* @return commit's hashmap*/
public HashMap<String, String> getFileToID() {
return _fileToID;
}
/** Getter method, return the ID of the parent
* commit for this commit.
* @return ID of parent commit*/
public String getParent() {
return _parent;
}
/** Getter method, returns the timestamp of this commit.
* @return String version of timestamp*/
public String getTimestamp() {
return _timestamp;
}
/** Adds the given file and its contents to the
* commit's hashmap.
* @param f is the file to be added*/
public void addFile(File f) throws IOException {
String id = Utils.sha1(Utils.readContents(f));
_fileToID.put(f.getCanonicalPath(), id);
_ID = createID();
}
/** Adds the given file to be removed as part of this
* commit.
* @param f is the file to be removed*/
public void addRemoveFile(File f) throws IOException {
_fileToID.put(f.getCanonicalPath(), "remove*" + f.getName());
_ID = createID();
}
/** Setter method, changes the ID of the parent
* commit for this commit.
* @param id of parent commit*/
public void setParent(String id) {
_parent = id;
}
/** Setter method, changes the message of this commit.
* @param message of the commit*/
public void setMessage(String message) {
_message = message;
}
}