forked from Jukkobee/git-project-JACOBM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit.java
More file actions
215 lines (199 loc) · 6.33 KB
/
Git.java
File metadata and controls
215 lines (199 loc) · 6.33 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
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.ByteArrayOutputStream;
public class Git
{
public static void main (String [] args)
{
initializesGitRepo();
System.out.println("Git Repository Initialized");
Git git = new Git();
hash = null;
try
{
hash = git.putFileToObjectsFolder();
System.out.println("File added to objects folder " + hash);
}
catch (IOException e)
{
e.printStackTrace();
}
if (hash != null)
{
File objectFile = new File ("git/objects/" + hash);
if (objectFile.exists())
{
System.out.println("File added to the objects folder");
}
else
{
System.out.println("File not found in the objects folder");
}
}
try {
git.putFileToIndex(hash, "plainTextFile.txt");
System.out.println("File added to the index");
}
catch (IOException e)
{
e.printStackTrace();
}
File index = new File ("git/index");
try
{
String insideIndex = new String (Files.readAllBytes(index.toPath()));
if (insideIndex.contains (hash + " plainTextFile.txt"))
{
System.out.println("File added to the index");
}
else
{
System.out.println("File not found in the index");
}
}
catch (IOException e)
{
e.printStackTrace();
}
checkForAndDelete(gitDirectory);
System.out.println ("Deleted");
}
private static File gitDirectory = new File("git");
private static String hash;
private static boolean compression = true;
public static void initializesGitRepo ()
{
//create gitDirectory
if (!gitDirectory.exists())
{
gitDirectory.mkdir();
}
else
{
System.out.println ("Git Repository already exists");
}
//create objects inside gitDirectory
File objects = new File("git/objects");
if (!objects.exists())
{
objects.mkdir();
}
else
{
System.out.println ("Git Repository already exists");
}
//creating index file
File index = new File("git/index");
if (!index.exists())
{
try
{
index.createNewFile();
} catch (IOException e)
{
e.printStackTrace();
}
}
else
{
System.out.println ("Git Repository already exists");
}
}
private static void checkForAndDelete(File file)
{
if (file.isDirectory())
{
File[] files = file.listFiles();
for (File f : files)
{
checkForAndDelete(f);
f.delete();
}
}
}
public static String encryptThisString(String input) {
try {
// getInstance() method is called with algorithm SHA-1
MessageDigest md = MessageDigest.getInstance("SHA-1");
// digest() method is called
// to calculate message digest of the input string
// returned as array of byte
byte[] messageDigest = md.digest(input.getBytes());
// Convert byte array into signum representation
BigInteger no = new BigInteger(1, messageDigest);
// Convert message digest into hex value
String hashtext = no.toString(16);
// Add preceding 0s to make it 40 digits long
while (hashtext.length() < 40) {
hashtext = "0" + hashtext;
}
// return the HashText
return hashtext;
}
// For specifying wrong message digest algorithms
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public String putFileToObjectsFolder()throws IOException
{
File regularFile = new File ("plainTextFile.txt");
try (FileWriter writer2 = new FileWriter (regularFile))
{
writer2.write("top secret data");
}
byte[] fileContentInBytes = Files.readAllBytes(regularFile.toPath());
byte [] hashData = null;
if (compression)
{
hashData = zipBytes(regularFile.getName(), fileContentInBytes);
}
else
{
hashData = fileContentInBytes;
}
//reads the contents of the file into the byte array and then we can generate the hash by using our encryptString method
String fileContent = new String (Files.readAllBytes(regularFile.toPath()));
hash = encryptThisString(new String (hashData));
File objects = new File ("git/objects");
if (!objects.exists())
{
objects.mkdir();
}
File objectFile = new File ("git/objects/" + hash);
//Writes out bytes better
try (FileOutputStream writer = new FileOutputStream (objectFile))
{
writer.write(hashData);
}
return hash;
}
public void putFileToIndex(String hash, String filename)throws IOException
{
File index = new File ("git/index");
//this append mode is used so that we can add to an existing file without altering the og data
try (FileWriter writer = new FileWriter (index, true))
{
writer.write(hash + " " + filename + "\n");
}
}
public static byte[] zipBytes(String filename, byte[] input) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry(filename);
entry.setSize(input.length);
zos.putNextEntry(entry);
zos.write(input);
zos.closeEntry();
zos.close();
return baos.toByteArray();
}
}