-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndex.java
More file actions
196 lines (176 loc) · 5.37 KB
/
Index.java
File metadata and controls
196 lines (176 loc) · 5.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Index {
private final File repoDir;
private final File objectsDir;
private final File indexFile;
public Index() {
this.repoDir = new File("git");
this.objectsDir = new File(this.repoDir, "objects");
this.indexFile = new File(this.repoDir, "index");
}
public void ensureStructure() throws IOException {
if (!repoDir.exists()) {
repoDir.mkdir();
}
if (!objectsDir.exists()) {
objectsDir.mkdir();
}
if (!indexFile.exists()) {
indexFile.createNewFile();
}
}
public void add(String sourcePath) throws IOException {
ensureStructure();
String path = normalizePath(sourcePath);
String hash = Blob.createBlobFromPath(path);
List<String> oldLines = readAll(indexFile.toPath());
List<String> newLines = new ArrayList<String>();
boolean alreadyExact = false;
int i = 0;
while (i < oldLines.size()) {
String line = oldLines.get(i);
if (line != null) {
int space = line.indexOf(' ');
if (space != -1) {
String left = line.substring(0, space);
String right = line.substring(space + 1);
if (right.equals(path)) {
if (left.equals(hash)) {
alreadyExact = true;
newLines.add(line);
}
} else {
newLines.add(line);
}
} else {
newLines.add(line);
}
} else {
newLines.add(line);
}
i = i + 1;
}
if (alreadyExact == false) {
newLines.add(hash + " " + path);
}
writeExact(indexFile.toPath(), newLines);
}
private static String normalizePath(String p) {
if (p == null) {
return "";
}
String s = p.replace('\\', '/');
if (s.startsWith("./")) {
s = s.substring(2);
}
if (s.equals(".")) {
s = "";
}
return s;
}
private static String fileNameOnly(String path) {
Path p = Paths.get(path);
Path name = p.getFileName();
if (name == null) {
return path;
} else {
return name.toString();
}
}
private static List<String> readAll(Path p) throws IOException {
if (Files.exists(p)) {
return Files.readAllLines(p, StandardCharsets.UTF_8);
} else {
return new ArrayList<String>();
}
}
private static void writeExact(Path p, List<String> lines) throws IOException {
FileWriter w = new FileWriter(p.toFile(), false);
int i = 0;
while (i < lines.size()) {
w.write(lines.get(i));
if (i < lines.size() - 1) {
w.write("\n");
}
i = i + 1;
}
w.flush();
w.close();
}
public void resetObjects() {
File objects = new File("git" + File.separator + "objects");
if (objects.exists()) {
deleteRecursively(objects);
}
File gitDir = new File("git");
if (!gitDir.exists()) {
gitDir.mkdir();
}
File newObjects = new File(gitDir, "objects");
if (!newObjects.exists()) {
newObjects.mkdir();
}
}
public void clearIndex() {
try {
File idx = new File("git" + File.separator + "index");
File parent = idx.getParentFile();
if (parent != null) {
if (!parent.exists()) {
parent.mkdirs();
}
}
FileWriter w = new FileWriter(idx, false);
w.write("");
w.flush();
w.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteNonJavaInCwd() {
File cwd = new File(".");
File[] items = cwd.listFiles();
if (items != null) {
int i = 0;
while (i < items.length) {
File f = items[i];
boolean isJava = false;
boolean isGit = false;
if (f.getName().endsWith(".java")) {
isJava = true;
}
if (f.getName().equals("git")) {
isGit = true;
}
if (f.isFile()) {
if (isJava == false && isGit == false) {
f.delete();
}
}
i = i + 1;
}
}
}
private void deleteRecursively(File f) {
if (f.isDirectory()) {
File[] kids = f.listFiles();
if (kids != null) {
int i = 0;
while (i < kids.length) {
deleteRecursively(kids[i]);
i = i + 1;
}
}
}
f.delete();
}
}