forked from m4rkyma/Git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit.java
More file actions
98 lines (95 loc) · 3.22 KB
/
Git.java
File metadata and controls
98 lines (95 loc) · 3.22 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
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.Files;
import java.nio.file.Path;
import java.util.HashMap;
public class Git{
// public HashMap <String, String> blobList = new HashMap<>();
public static void initialize() throws IOException
{
if (!(new File ("index").isFile()))
{
File index = new File("index");
index.createNewFile();
}
// else
// {
// File index = new File("index");
// FileWriter fw = new FileWriter ("index");
// fw.flush();
// fw.close();
// }
if (!(new File ("objects").isDirectory()))
{
File objects = new File("objects");
objects.mkdir();
}
}
public static String readFile(String from) throws IOException {
return Files.readString(Path.of(from));
}
public static void add (String fileName) throws IOException
{
String s = Blob.Sha1(Blob.getContents(fileName));
if (new File ("objects/" + s).exists())
{
return;
}
System.out.println ("added " + fileName);
Blob blob = new Blob (fileName);
// File f = new File (fileName);
FileWriter fw = new FileWriter ("index",true);
// PrintWriter pw = new PrintWriter (fw);
// if (!readFile("index").contains(s)) {
// if (readFile("index").isEmpty())
// pw.print(f.getName() + " : " + s);
// else
// pw.print("\n" + f.getName() + " : " + s);
// }
fw.write(fileName + " : " + Blob.Sha1(Blob.getContents(fileName)) + "\n");
fw.close();
// pw.close();
// blobList.put(fileName, Blob.Sha1(Blob.getContents(fileName)));
// writeIndex();
// File index = new File("index");
// index.createNewFile();
// if (new File ("index").isFile())
// {
// FileWriter fw = new FileWriter ("index",true);
// fw.write(fileName + " : " + Blob.Sha1(Blob.getContents(fileName)) + "\n");
// fw.close();
// }
// else
// {
// System.out.println ("poop");
// }
}
public static void remove (String fileName) throws IOException
{
String s = Blob.Sha1(Blob.getContents(fileName));
File bruh = new File ("objects/" + s);
if (bruh.exists())
{
bruh.delete();
}
File f = new File ("index");
File temp = new File ("temp.txt");
BufferedReader br = new BufferedReader(new FileReader(f));
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
String lineToRemove = fileName + " : " + Blob.Sha1(Blob.getContents(fileName));
String currentLine;
while((currentLine = br.readLine()) != null) {
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
bw.write(currentLine + System.getProperty("line.separator"));
}
bw.close();
br.close();
temp.renameTo(f);
}
}