forked from markusgeorge1207/ProgrammingGit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndex.java
More file actions
60 lines (47 loc) · 1.79 KB
/
Index.java
File metadata and controls
60 lines (47 loc) · 1.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
import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Index {
private static final String indexFile = "index";
private Map<String, String> blobMap;
public Index() {
blobMap = new HashMap<>();
}
public void initProject() throws IOException {
Path indexPath = Paths.get(indexFile);
if (!Files.exists(indexPath)) {
Files.createFile(indexPath);
}
Path objectsDir = Paths.get("./objects");
if (!Files.exists(Paths.get("./objects"))) {
Files.createFile(Paths.get("./objects"));
}
}
public void createBlobs(String originalFileName, String sha1Hash) throws IOException {
blobMap.put(originalFileName, sha1Hash);
String entry = originalFileName + " : " + sha1Hash;
Files.write(Paths.get(indexFile), (entry + System.lineSeparator()).getBytes(), StandardOpenOption.APPEND);
}
public void removeBlobs(String originalFileName) throws IOException {
blobMap.remove(originalFileName);
// Read the current content of the 'index' file
List<String> lines = Files.readAllLines(Paths.get(indexFile));
List<String> newLines = new ArrayList<>();
// Iterate through the lines and exclude the entry with the specified
// originalFileName
for (String line : lines) {
String[] parts = line.split(" : ");
if (parts.length == 2 && !parts[0].equals(originalFileName)) {
newLines.add(line);
}
}
// Write the updated content back to the 'index' file
Files.write(Paths.get(indexFile), newLines);
}
public Map<String, String> getBlobMap() {
return blobMap;
}
}