forked from YoussefAhmed256/gitlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStagingArea.java
More file actions
99 lines (85 loc) · 3.04 KB
/
StagingArea.java
File metadata and controls
99 lines (85 loc) · 3.04 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
package gitlet;
import gitlet.Utils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import static gitlet.Utils.*;
public class StagingArea {
private final File ADDITION_Dir;
private final File REMOVAL_Dir;
public StagingArea(File additionDir, File removalDir) {
this.ADDITION_Dir = additionDir;
this.REMOVAL_Dir = removalDir;
}
public File StageFileForAddition(File file){
String name=file.getName();
File stagedFile = join(ADDITION_Dir, name);
writeContents(stagedFile, readContentsAsString(file));
return stagedFile;
}
public void StageFileForAddition(String contents , String fileName){
File stagedFile = join(ADDITION_Dir, fileName);
writeContents(stagedFile, contents);
}
public File StageFileForRemoval(File file){
String name=file.getName();
File stagedFile = join(REMOVAL_Dir, name);
writeContents(stagedFile, readContentsAsString(file));
return stagedFile;
}
public File GetFileForAddition(File file){
String name=file.getName();
File stagedFile = Utils.join(ADDITION_Dir, name);
return stagedFile;
}
public File GetFileForRemoval(File file){
String name=file.getName();
File stagedFile = Utils.join(REMOVAL_Dir, name);
return stagedFile;
}
public boolean isStagedForAddition(String fileName){
File stagedFile = Utils.join(ADDITION_Dir, fileName);
return stagedFile.exists();
}
public boolean isStagedForRemoval(String fileName){
File stagedFile = Utils.join(REMOVAL_Dir, fileName);
return stagedFile.exists();
}
public boolean UnstageFileForAddition(File file){
String name=file.getName();
File stagedFile = Utils.join(ADDITION_Dir, name);
return stagedFile.delete();
}
public boolean UnstageFileForRemoval(File file){
String name=file.getName();
File stagedFile = Utils.join(REMOVAL_Dir, name);
return stagedFile.delete();
}
public List<String> GetAllFilesForAddition(){
List<String> files = new ArrayList<String>();
for (String name : plainFilenamesIn(ADDITION_Dir))
files.add(name);
return files;
}
public List<String> GetAllFilesForRemoval(){
List<String> files = new ArrayList<String>();
for (String name : plainFilenamesIn(REMOVAL_Dir))
files.add(name);
return files;
}
public void clear(){
for (String fileName : GetAllFilesForAddition()) {
File stagedFile = join(ADDITION_Dir, fileName);
UnstageFileForAddition(stagedFile);
}
for (String fileName : GetAllFilesForRemoval()) {
File stagedFile = join(REMOVAL_Dir, fileName);
UnstageFileForRemoval(stagedFile);
}
}
public boolean IsEmpty(){
return plainFilenamesIn(ADDITION_Dir).isEmpty() || plainFilenamesIn(REMOVAL_Dir).isEmpty() ;
}
}