-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaging.java
More file actions
117 lines (91 loc) · 3.08 KB
/
Staging.java
File metadata and controls
117 lines (91 loc) · 3.08 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
package gitlet;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import static gitlet.Utils.*;
public class Staging implements Serializable {
HashMap<String, String> stageFiles;
HashMap<String, String> rmFiles;
ArrayList<String> orderedFiles = new ArrayList<String>();
ArrayList<String> orderedRmFiles = new ArrayList<String>();
public Staging() {
this.stageFiles = new HashMap<String, String>();
this.rmFiles = new HashMap<String, String>();
}
public void addFile(String fileName) {
File baseFile = join(Repository.CWD, fileName);
byte[] content = readContents(baseFile);
String iD = sha1(content);
Commit commitToCheck = Repository.loadLastCommit();
HashMap<String, String> blobs = commitToCheck.getBlobs();
boolean add = true;
if (blobs == null) {
stageFiles.put(fileName, iD);
orderedFiles.add(fileName);
save();
add = false;
} else {
for (String blob : blobs.values()) {
if (iD.equals(blob)) {
add = false;
if (stageFiles.get(fileName) != null) {
stageFiles.remove(fileName);
orderedFiles.remove(fileName);
save();
}
if (rmFiles.get(fileName) != null) {
rmFiles.remove(fileName);
orderedRmFiles.remove(fileName);
save();
}
}
}
}
if (add) {
stageFiles.put(fileName, iD);
orderedFiles.add(fileName);
save();
}
}
public void save() {
writeObject(Repository.STAGING, this);
}
public HashMap<String, String> getStageFiles() {
return stageFiles;
}
public HashMap<String, String> getRemoveFiles() {
return rmFiles;
}
/** Returns false if the file is not in the staging area,
* returns true if it is in the staging area */
public boolean inStaging(String fileName) {
return stageFiles.get(fileName) != null;
}
public static void clear() {
try {
restrictedDelete(Repository.STAGING);
} catch (IllegalArgumentException e) {
Staging emptyStage = new Staging();
writeObject(Repository.STAGING, emptyStage);
}
}
public void addRemoveFile(String fileName) {
rmFiles.put(fileName, Repository.getFileSHA(fileName));
orderedRmFiles.add(fileName);
}
public void addRemoveFile(String fileName, String fileSha) {
rmFiles.put(fileName, fileSha);
orderedRmFiles.add(fileName);
}
public ArrayList<String> getOrderedFiles() {
return orderedFiles;
}
public ArrayList<String> getOrderedRmFiles() {
return orderedRmFiles;
}
public void remove(String fileName) {
stageFiles.remove(fileName);
orderedFiles.remove(fileName);
}
}