forked from m4rkyma/Git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.java
More file actions
91 lines (85 loc) · 3.25 KB
/
Tree.java
File metadata and controls
91 lines (85 loc) · 3.25 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
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.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Tree {
static ArrayList<String> tree = new ArrayList<String>();
static HashMap<String, String> hMap = new HashMap<String, String>();
static StringBuilder sb = new StringBuilder();
public Tree() throws IOException {
File f = new File("tree");
f.createNewFile();
}
public static void add(String line) throws IOException {
if (line.substring(0, 4).equals("blob")) // checks if adding type: blob
{
Scanner sc = new Scanner(line);
while (sc.hasNextLine()) {
sc.nextLine();
if (!line.isEmpty()) {
String type = line.split(" : ")[0]; // dummy variable so I can access hash and name
String hash = line.split(" : ")[1]; // gets the center of the line (hash)
String name = line.split(" : ")[2]; // gets the end of the line (fileName)
hMap.put(hash, name); // adds to hashMap of blobs (Strings)
}
}
sc.close(); // necessary close of Scanner (sc)
} else if (line.substring(0, 4).equals("tree")) // chekcs if adding type: tree
{
Scanner k = new Scanner(line);
while (k.hasNextLine()) {
k.nextLine();
if (!line.isEmpty()) {
String type = line.split(" : ")[0]; // dummy variable so I can access hash and name
String hash = line.split(" : ")[1]; // gets end of the line (hash)
tree.add(hash); // adds the hash value to an ArrayList (tree)
}
}
k.close(); // necessary close of Scanner (k)
}
}
public static void remove(String remove) {
if (hMap.containsKey(remove) || hMap.containsValue(remove)) {
hMap.remove(remove);
} else if (tree.indexOf(remove) != -1) {
tree.remove(remove);
}
}
public static void getContents() throws IOException {
for (Map.Entry<String, String> k : hMap.entrySet()) {
String line = "blob : " + k.getKey() + " : " + k.getValue();
sb.append(line);
sb.append("\n");
}
for (int i = 0; i < tree.size(); i++) {
String line = "tree : " + tree.get(i);
sb.append(line);
sb.append("\n");
}
File fileToDelete = new File("tree");
fileToDelete.delete();
sb = new StringBuilder(sb.toString().stripTrailing());
}
// Made this for JUnit Tester
public static String printSB() {
return sb.toString();
}
public static void writeToObjects() throws IOException {
File shaName = new File("objects/" + Blob.Sha1(sb.toString()));
if (!shaName.exists()) {
shaName.createNewFile();
}
if (shaName.exists()) {
FileWriter fw = new FileWriter(shaName);
sb.toString().stripTrailing();
fw.write(sb.toString());
fw.close();
}
}
}