-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIndex.java
More file actions
117 lines (110 loc) · 3.44 KB
/
Index.java
File metadata and controls
117 lines (110 loc) · 3.44 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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Index {
private static HashMap<String, String> hMap = new HashMap<>();
private static HashMap<String, String> mapForDirs = new HashMap<>();
private static ArrayList<String> arr = new ArrayList<>();
public void init() throws IOException {
hMap = new HashMap<>();
File file = new File("index");
File dir = new File("objects");
if (file.exists()) {
file.delete();
}
if (!file.exists()) {
file.createNewFile();
}
if (!dir.isDirectory()) {
dir.mkdir();
}
String str = fileToString("index");
Scanner sc = new Scanner(str);
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (!line.isEmpty()) {
String fileName = line.split(" : ")[0];
String hash = line.split(" : ")[1];
hMap.put(fileName, hash);
}
}
sc.close();
}
public String fileToString(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
StringBuilder sb = new StringBuilder();
while (br.ready()) {
sb.append((char) br.read());
}
br.close();
return sb.toString();
}
public void addBlob(String fileName) throws Exception {
File f = new File(fileName);
if (f.isDirectory())
{
Tree tree = new Tree();
mapForDirs.put(fileName, tree.addDirectory(fileName));
}
if(f.isFile())
{
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(f));
while (br.ready())
{
sb.append((char)br.read());
}
br.close();
String hash = Blob.encryptThisString(sb.toString());
hMap.put(fileName, hash);
}
writeInd();
}
public static void writeInd() throws IOException {
FileWriter fw = new FileWriter("./index");
//Index ind = new Index();
//Commit com = new Commit();
for (String file : hMap.keySet())
{
fw.write("blob : " + hMap.get(file) + " : " + file + "\n");
}
for (String dir : mapForDirs.keySet())
{
fw.write("tree : " + mapForDirs.get(dir) + " : " + dir + "\n");
}
for (String str : arr)
{
fw.write("*deleted* " + str + "\n");
}
fw.close();
for(String dir : mapForDirs.keySet())
{
mapForDirs.remove(dir);
}
}
/*public static void writeToTree(Commit com) throws IOException
{
Tree tree = new Tree();
Index ind = new Index();
FileWriter fw = new FileWriter(tree.getSha());
fw.write(ind.fileToString("./index"));
String commitPrevTreeSha = com.getLineOne();
fw.write("tree : " + commitPrevTreeSha);
}
*/
public void removeBlob(String fileName) throws IOException {
arr.add(fileName);
hMap.remove(fileName);
writeInd();
}
}