-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGit.java
More file actions
414 lines (368 loc) · 13.7 KB
/
Git.java
File metadata and controls
414 lines (368 loc) · 13.7 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import java.io.*;
import java.nio.file.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Git{
public static File git;
public static File objects;
public static File index;
public static File HEAD;
public static boolean COMPRESS_BLOBS = false;
public static boolean AlreadyExists = true;
static {
git = makeFolder("git");
objects = makeFolder("objects", git);
try {
index = makeFile("index", git);
HEAD = makeFile("HEAD", git);
} catch (IOException e) {
throw new RuntimeException("Failed to initialize files", e);
}
}
public static File makeFolder(String folderName){
File dir = new File(folderName);
if (!dir.exists()){
AlreadyExists = false;
}
dir.mkdir();
return dir;
}
public static File makeFolder(String folderName, File folder){
if (!folder.exists()){
throw new IllegalArgumentException("Attempted to create a folder within a folder that does not exist!");
}
File dir = new File(folder.getPath(),folderName);
if (!dir.exists()){
AlreadyExists = false;
}
dir.mkdir();
return dir;
}
public static File makeFile(String fileName) throws IOException{
File file = new File(fileName);
if (!file.exists()){
AlreadyExists = false;
}
file.createNewFile();
return file;
}
public static File makeFile(String fileName, File folder) throws IOException{
if (!folder.exists()){
throw new IllegalArgumentException("Attempted to create a file within a folder that does not exist!");
}
File file = new File(folder.getPath(), fileName);
if (!file.exists()){
AlreadyExists = false;
}
file.createNewFile();
return file;
}
public static String blob(File file) throws IOException {
if (file == null || !file.isFile()) {
throw new IllegalArgumentException("blob: source must be an existing file");
}
byte[] raw = Files.readAllBytes(file.toPath());
byte[] content;
if (COMPRESS_BLOBS){
content = compress(raw);
} else {
content = raw;
}
String blobName = SHA1(content);
File blob = makeFile(blobName, objects);
try (OutputStream out = new FileOutputStream(blob)) {
out.write(content);
}
return blobName;
}
public static String SHA1(String message){
return SHA1(message.getBytes(java.nio.charset.StandardCharsets.UTF_8));
}
public static String SHA1(byte[] bytes){
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digestedMessage = md.digest(bytes);
BigInteger number = new BigInteger(1, digestedMessage);
String hashtext = number.toString(16);
while (hashtext.length() < 40){
hashtext = "0" + hashtext;
}
return hashtext;
} catch (NoSuchAlgorithmException e){
throw new RuntimeException(e);
}
}
public static boolean blobExists(String hash) {
// check if blob's hash is valid
if (hash == null || hash.length() != 40){
return false;
}
File f = new File(objects, hash);
// checks if the file connected to blob exists and is a file
return f.exists() && f.isFile();
}
public static void resetObjects() throws IOException {
if (!objects.exists()) {
return;
}
File[] kids = objects.listFiles();
if (kids == null){
return;
} else{
for (File k : kids){
k.delete();
}
}
}
public static byte[] compress(byte[] input) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (DeflaterOutputStream dos = new DeflaterOutputStream(bos, new Deflater())) {
dos.write(input);
}
return bos.toByteArray();
}
public static String relPath(File f) {
Path base = Paths.get("").toAbsolutePath().normalize();
Path p = f.toPath().toAbsolutePath().normalize();
return base.relativize(p).toString();
}
public static void writeIndexLines(List<String> lines) throws IOException {
try (FileOutputStream out = new FileOutputStream(index, false)) {
for (int i = 0; i < lines.size(); i++) {
if (i > 0) {
out.write('\n');
}
out.write(lines.get(i).getBytes(java.nio.charset.StandardCharsets.UTF_8));
}
}
}
public static String addToIndex(File file) throws IOException {
if (file == null || !file.isFile()) {
throw new IllegalArgumentException("must come from an existing file");
}
byte[] raw = Files.readAllBytes(file.toPath());
byte[] maybeSquished;
if (COMPRESS_BLOBS) {
maybeSquished = compress(raw);
} else{
maybeSquished = raw;
}
String newHash = SHA1(maybeSquished);
String givenPath = relPath(file);
List<String> lines = readIndexLines();
int existsIndex = -1;
String oldHash = null;
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
int spaceIndex = line.indexOf(' ');
String hash = line.substring(0, spaceIndex);
String pth = line.substring(spaceIndex + 1);
if (pth.equals(givenPath)) {
existsIndex = i;
oldHash = hash;
}
}
if (existsIndex >= 0) {
if (newHash.equals(oldHash)) {
return newHash;
} else {
lines.set(existsIndex, newHash + " " + givenPath);
writeIndexLines(lines);
File blob = new File(objects, newHash);
if (!blob.exists()) {
blob(file);
}
return newHash;
}
} else {
appendIndexLine(newHash + " " + givenPath);
File blob = new File(objects, newHash);
if (!blob.exists()) {
blob(file);
}
return newHash;
}
}
public static void appendIndexLine(String line) throws IOException {
byte[] bytes = line.getBytes(java.nio.charset.StandardCharsets.UTF_8);
long len = index.length();
try (FileOutputStream out = new FileOutputStream(index, true)) {
if (len > 0){
out.write('\n');
}
out.write(bytes);
}
}
public static List<String> readIndexLines() throws IOException {
if (!index.exists()){
return new ArrayList<>();
}
return Files.readAllLines(index.toPath(), java.nio.charset.StandardCharsets.UTF_8);
}
public static void resetIndex() throws IOException {
Files.write(index.toPath(), new byte[0]);
}
public static void deleteIfExists(File... files) {
if (files == null) return;
for (File f : files) {
if (f == null) continue;
if (f.isDirectory()) {
File[] kids = f.listFiles();
if (kids != null) for (File k : kids) k.delete();
}
f.delete();
}
}
public static String tree(File dir) throws IOException {
if (dir == null || !dir.isDirectory()) {
throw new IllegalArgumentException("tree must be a directory");
}
File[] kids = dir.listFiles();
if (kids == null){
kids = new File[0];
}
StringBuilder content = new StringBuilder();
boolean first = true;
for (File k : kids) {
if (k.isFile()) {
String blobHash = blob(k);
if (!first){
content.append('\n');
}
content.append("blob " + blobHash + " " + k.getName());
first = false;
} else if (k.isDirectory()) {
String subHash = tree(k);
if (!first){
content.append('\n');
}
content.append("tree " + subHash + " " + k.getName());
first = false;
}
}
byte[] bytes = content.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8);
String treeHash = SHA1(bytes);
File treeBlob = new File(objects, treeHash);
if (!treeBlob.exists()) {
try (FileOutputStream out = new FileOutputStream(treeBlob)) {
out.write(bytes);
}
}
return treeHash;
}
public static int getSlashes(String input){
int slashes = 0;
for (int i = 0; i < input.length(); i++){
if (input.charAt(i) == '/'){ // change to '\' if it's not mac
slashes++;
}
}
return slashes;
}
public static void workingList() throws FileNotFoundException, IOException{
FileReader indexReader = new FileReader(index);
StringBuilder indexString = new StringBuilder();
while (indexReader.ready()){
indexString.append((char)(indexReader.read()));
}
indexReader.close();
String[] indexLines = (indexString.toString()).split("\n");
ArrayList <String> indexArray = new ArrayList<String>(Arrays.asList(indexLines));
for (int i = 0; i < indexArray.size(); i++){
String line = indexArray.get(i);
indexArray.set(i, line.substring(41) + " " + line.substring(0, 40));
}
Collections.sort(indexArray); //credit to Zid for teaching me the idea to easily sort
int maxSlash = 0;
int maxI = 0;
while (indexArray.size() > 1){
for (int i = 0; i < indexArray.size(); i++){
String line = indexArray.get(i);
int slashes = getSlashes(line);
if (slashes > maxSlash){
maxSlash = slashes;
maxI = i;
}
}
String max = indexArray.get(maxI);
String path = max.substring(0, max.indexOf(' '));
// String hash = max.substring(max.length()-40); //check off by one (not needed)
String parent = "";
File f = new File(path);
if (f.exists()){
parent = f.getParent();
}
File parentF = new File(parent);
String treename = "";
if (parentF.exists()){
treename = tree(parentF);
}
File parentTree = new File(treename);
int numbertoremove = 1;
if (parentTree.exists()){
FileReader treeReader = new FileReader(index);
StringBuilder treeIndexString = new StringBuilder();
while (treeReader.ready()){
treeIndexString.append((char)treeReader.read());
}
treeReader.close();
numbertoremove = (treeIndexString.toString()).split("\n").length;
}
for (int j = 0; j < numbertoremove; j++){
indexArray.remove(maxI);
}
}
File workingListFile = new File(git, "workinglist");
try (FileWriter writer = new FileWriter(workingListFile)) {
writer.write("tree " + indexArray.get(0).substring(indexArray.get(0).indexOf(' ') + 1) + " (root)");
}
}
public static String commit(String author, String message) throws IOException {
if (author == null || author.isEmpty() || message == null) {
throw new IllegalArgumentException("Need author and message");
}
File[] gitDirs = git.listFiles();
if (gitDirs == null) gitDirs = new File[0];
String rootHash = "";
File projDir = new File("projects");
if (!projDir.exists() || !projDir.isDirectory()) {
throw new IllegalStateException("No projects directory found to commit to");
}
rootHash = tree(projDir);
String parentHash = "";
if (HEAD.exists()) {
List<String> headLines = Files.readAllLines(HEAD.toPath(), java.nio.charset.StandardCharsets.UTF_8);
if (!headLines.isEmpty()) {
parentHash = headLines.get(0).trim();
}
}
String date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").format(new Date());
StringBuilder commit = new StringBuilder();
commit.append("tree: ").append(rootHash).append("\n");
if (!parentHash.isEmpty()) {
commit.append("parent: ").append(parentHash).append("\n");
}
commit.append("author: ").append(author).append("\n");
commit.append("date: ").append(date).append("\n");
commit.append("message: ").append(message).append("\n");
byte[] bytes = commit.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8);
String commitHash = SHA1(bytes);
File commitFile = new File(objects, commitHash);
if (!commitFile.exists()) {
try (FileOutputStream out = new FileOutputStream(commitFile)) {
out.write(bytes);
}
}
Files.write(HEAD.toPath(), commitHash.getBytes(java.nio.charset.StandardCharsets.UTF_8));
return commitHash;
}
}