-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStagingArea.java
More file actions
109 lines (91 loc) · 3.26 KB
/
StagingArea.java
File metadata and controls
109 lines (91 loc) · 3.26 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
package gitlet;
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class StagingArea implements Serializable {
/* Stages files to be added or removed for later commits.
Also stores a set corresponding to tracked and removed files */
private static StagingArea stagingArea = null;
private HashSet<String> removedFiles;
public HashMap<String, CommitNode.Blob> stagedBlobs;
public StagingArea() {
removedFiles = new HashSet<>();
stagedBlobs = new HashMap<>();
}
public static StagingArea getStagingArea() {
if (stagingArea == null) {
stagingArea = new StagingArea();
}
return stagingArea;
}
public Set<String> getAddedFiles() {
return stagedBlobs.keySet();
}
public HashSet<String> getRemovedFiles() {
return removedFiles;
}
public void addFile(String fileName) {
if (removedFiles.contains(fileName)) {
removedFiles.remove(fileName);
}
CommitNode.Blob stagedBlob = new CommitNode.Blob(fileName);
stagedBlobs.put(fileName, stagedBlob);
}
public void rm(String filename) {
File infile = new File(GitLet.REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(infile);
if (!tree.currPtr.fileNames.containsKey(filename)
&& !stagedBlobs.keySet().contains(filename)) {
System.out.println("No reason to remove the file");
System.exit(0);
}
if (tree.currPtr.fileNames.containsKey(filename)) {
/* write code that actually DELETES the file from disk */
removedFiles.add(filename);
File file = new File(filename);
Utils.restrictedDelete(filename);
}
if (stagedBlobs.keySet().contains(filename)) {
stagedBlobs.remove(filename);
}
}
public void commit(String commitMessage) {
File file = new File(GitLet.REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(file);
CommitNode newNode = new CommitNode(commitMessage);
newNode.blobs = tree.currPtr.blobs;
newNode.parentName = tree.currPtr.commitID;
newNode.fileNames = tree.currPtr.fileNames;
for (String removed : removedFiles) {
newNode.fileNames.remove(removed);
}
// changed here ^
for (String filename : stagedBlobs.keySet()) {
newNode.addBlob(stagedBlobs.get(filename));
}
removedFiles.clear();
// newNode.trackedFiles = trackedFiles;
stagedBlobs.clear();
tree.addCommitNode(newNode);
FileUtil.serialize(file, tree);
File commit = new File(GitLet.REPO + "/objects/" + newNode.commitID);
FileUtil.serialize(commit, newNode);
}
public int getRemovedSize() {
return removedFiles.size();
}
public int getAddedSize() {
return stagedBlobs.size();
}
public void clearAdd() {
stagedBlobs.clear();
}
public static void main(String[] args) {
StagingArea s = new StagingArea();
s.removedFiles.add("hi.txt");
s.addFile("hi.txt");
System.out.println(s.getRemovedSize());
}
}