Repository navigation
Expand file tree
/
Copy pathCommit.java
More file actions
182 lines (159 loc) · 5.79 KB
/
Copy pathCommit.java
File metadata and controls
182 lines (159 loc) · 5.79 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
package gitlet;
import java.io.File;
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/** Commit keeps track of the user's commits and ensures that all associated
* local variables are kept up to date
*/
public class Commit implements Serializable {
/** Constructs a commit given parameters */
/** the time of the commit */
private ZonedDateTime time;
/** the time of the commit given as a string*/
private String date;
/** The parent of the commit. */
private String parentId;
/** The second parent of the commit */
private String parentId2;
/** the sha-1 of the commit */
private String sha;
/** The String holds the commit message. */
private String message;
/** The String holds the branch made started by this commit. */
private String branchName;
/** The Hashmap stores all the file on this commit, saved by file name. */
private HashMap<String, Blobs> files;
/** Boolean that keeps track of merge */
private boolean merge;
public Commit(String message, Commit parent, Commit parent2,
HashMap<String, Blobs> stagingMap, boolean init,
boolean merged, String branchName, List<String> rmList) {
if (init) {
date = "Wed Dec 31 16:00:00 1969 -0800";
parentId = null;
files = new HashMap<>();
} else {
time = ZonedDateTime.now();
date = time.format(DateTimeFormatter.ofPattern
("EEE MMM d HH:mm:ss yyyy xxxx"));
parentId = parent.getSha();
files = copyAllFiles(parent);
update(files, stagingMap, rmList);
}
this.message = message;
sha = getSha();
this.branchName = branchName;
merge = merged;
}
/** initializes a new Commit */
static Commit init() {
return new Commit("initial commit", null, null, null, true,
false, "master", null);
}
/** Given the name of a file, return the Blob */
public Blobs getBlobs(String fileName) {
if (files.containsKey(fileName))
return files.get(fileName);
return null;
}
/** Returns the commit's hash (ie: the string of all file names along with the
* time, their parent, and the associated message) */
public String getSha() {
String allfile = "";
for (Map.Entry<String, Blobs> entry : files.entrySet()) {
String key = entry.getKey();
allfile += key;
}
if (parentId == null) {
return Utils.sha1(date, message, allfile);
}
String parentToString = parentId;
if (merge) {
parentToString += parentId2;
}
parentToString += parentId2;
return Utils.sha1(date, message, parentToString, allfile);
}
/** returns the instance variable that keeps track of merging*/
public boolean getMerge() {
return merge;
}
/** to be added to status to keep track of merging in output */
public String mergeString(){
return getParentId().substring(0, 7) + " " + getParentId2().substring(0, 7);
}
/** Returns the branch of the commit */
public String getBranchName() {
return branchName;
}
/** Returns all of the files in the commit */
public LinkedList<String> getAllFiles() {
LinkedList<String> result = new LinkedList<String>();
for (Map.Entry<String, Blobs> entry : files.entrySet()) {
String key = entry.getKey();
result.add(key);
}
return result;
}
/** Copies files from the parent */
public HashMap<String, Blobs> copyAllFiles(Commit parent) {
HashMap<String, Blobs> result = new HashMap<String, Blobs>();
for (Map.Entry<String, Blobs> entry : parent.files.entrySet()) {
String key = entry.getKey();
Blobs b = entry.getValue();
result.put(key, b);
}
return result;
}
/** Update the hashmap with all the file names for the commit */
public void update(HashMap<String, Blobs> filemap, HashMap<String, Blobs> stagingMap, List<String> removeList) {
if (!stagingMap.isEmpty()) {
for (Map.Entry<String, Blobs> entry : stagingMap.entrySet()) {
String filename = entry.getKey();
Blobs stagingB = entry.getValue();
if (!filemap.containsKey(filename))
filemap.put(filename, stagingB);
else {
Blobs commitB = (Blobs) filemap.get(filename);
if (!commitB.getsha().equals(stagingB.getsha()))
filemap.replace(filename, stagingB);
}
File toDelete = new File(".gitlet/staging/" + stagingB.getsha());
toDelete.delete();
}
}
if (!removeList.isEmpty()) {
while (!removeList.isEmpty())
filemap.remove(removeList.remove(0));
}
}
/** Return the parent of this commit's sha-1 */
public String getParentId() {
return parentId;
}
/** Return the commit's second parent's sha-1. */
public String getParentId2() {
return parentId2;
}
/** check if the commit is tracking a file. */
public boolean tracking(String fileName) {
return files.containsKey(fileName);
}
/** return the time of the commit */
public String getDate() {
return date;
}
/** return the message of the commit */
public String getMessage() {
return message;
}
/** return the hashmap of all the files*/
public HashMap<String, Blobs> getFiles() {
return files;
}
}