forked from YoussefAhmed256/gitlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommitStore.java
More file actions
62 lines (50 loc) · 1.69 KB
/
CommitStore.java
File metadata and controls
62 lines (50 loc) · 1.69 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
package gitlet;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static gitlet.Utils.*;
public class CommitStore {
private final File COMMIT_DIR;
public CommitStore(File commitDir) {
this.COMMIT_DIR = commitDir;
if(!commitDir.exists()) {
commitDir.mkdirs();
}
}
public void saveCommit (Commit commit){
File commitFile = join(COMMIT_DIR,commit.getHash());
writeObject(commitFile, commit);
}
public Commit getCommitByHash(String commitId) {
if (COMMIT_DIR == null) {
return null;
}
File commitFile = join(COMMIT_DIR, commitId);
if(commitFile.exists()) {
return readObject(commitFile, Commit.class);
}
// search by hash prefix
String targetCommitHash = getAllCommitHashes().stream()
.filter(hash -> hash.startsWith(commitId))
.findFirst()
.orElse(null);
if(targetCommitHash != null){
return readObject(commitFile, Commit.class);
}
return null;
}
public List<Commit> getCommitByMessage(String message) {
return getAllCommits().stream()
.filter(commit -> commit.getMessage().equals(message))
.collect(Collectors.toList());
}
public List<Commit> getAllCommits(){
return Objects.requireNonNull(plainFilenamesIn(COMMIT_DIR)).stream()
.map(hash -> getCommitByHash(hash))
.collect(Collectors.toList());
}
public List<String> getAllCommitHashes() {
return Objects.requireNonNull(plainFilenamesIn(COMMIT_DIR));
}
}