-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIndex.java
More file actions
163 lines (136 loc) · 4.37 KB
/
Index.java
File metadata and controls
163 lines (136 loc) · 4.37 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.io.*;
import java.nio.file.*;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Index {
private String indexFile = "index";
private Map<String, String> blobMap;
private ArrayList <Tree> treeList;
public Index() {
blobMap = new HashMap<>();
treeList = new ArrayList<>();
}
public boolean initProject(String indexFile) throws IOException {
this.indexFile = indexFile;
Path indexPath = Paths.get(indexFile);
if (!Files.exists(indexPath)) {
Files.createFile(indexPath);
return true;
}
return false;
}
public void addBlob(String originalFileName) throws IOException {
String fileContents = new String (Blob.readFile(originalFileName));
String hash = "";
try
{
hash = Blob.calculateSHA1(originalFileName);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
blobMap.put (originalFileName, hash);
File objectsFile = new File ("objects/" + hash);
if (!objectsFile.exists())
{
try (PrintWriter pw = new PrintWriter (objectsFile))
{
pw.print(fileContents);
}
}
try (PrintWriter writer = new PrintWriter (new FileWriter ("Index", true)))
{
writer.println ("blob" + ": " + hash + ": " + originalFileName + "\n");
}
}
public void removeBlobs(String originalFileName) throws IOException {
blobMap.remove(originalFileName);
List<String> lines = Files.readAllLines(Paths.get(indexFile));
List<String> newLines = new ArrayList<>();
for (String line : lines) {
String[] parts = line.split(" : ");
if (parts.length == 2 && !parts[0].equals(originalFileName)) {
newLines.add(line);
}
}
Files.write(Paths.get(indexFile), newLines);
}
public void addDirectory (String directoryName)
{
Tree dir = new Tree ("objects");
File directory = new File (directoryName);
writeDirectory (directory, dir);
treeList.add(dir);
}
public void clearIndexFile ()
{
try
{
Files.deleteIfExists(Paths.get(indexFile));
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void deleteFile (String fileName)
{
try (BufferedWriter writer = new BufferedWriter(new FileWriter ("index")))
{
writer.write ("*deleted* : " + fileName);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void editFile (String fileName)
{
try (BufferedWriter writer = new BufferedWriter(new FileWriter ("index")))
{
writer.write ("*edited* : " + fileName);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void writeDirectory (File directory, Tree tree)
{
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
Tree subdirectoryTree = new Tree();
writeDirectory(file, subdirectoryTree);
tree.addTreeEntry("tree", subdirectoryTree.getSHA1(), file.getName());
} else if (file.isFile()) {
StringBuilder content = new StringBuilder();
try (FileReader reader = new FileReader(file)) {
int character;
while ((character = reader.read()) != -1) {
content.append((char) character);
}
}
catch (IOException e)
{
e.printStackTrace();
}
String sha1 = Tree.hashFromString(content.toString());
tree.addTreeEntry("blob", sha1, file.getName());
}
}
}
}
public Map<String, String> getBlobMap() {
return blobMap;
}
public ArrayList<Tree> getTreeList ()
{
return treeList;
}
}