-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGit.java
More file actions
107 lines (94 loc) · 2.89 KB
/
Git.java
File metadata and controls
107 lines (94 loc) · 2.89 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
import java.io.*;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
public class Git {
File gitFolder;
File objectsFolder;
File indexFile;
File newIndexFile;
public Git() {
gitFolder = new File("git/");
objectsFolder = new File("git/objects/");
indexFile = new File("git/index/");
//newIndexFile = new File("git/newIndex");
if (gitFolder.exists() && objectsFolder.exists() && indexFile.exists()) {
System.out.println("Git Repository already exists");
} else {
if (!gitFolder.exists()) {
gitFolder.mkdir();
}
if (!objectsFolder.exists()) {
objectsFolder.mkdir();
}
if (!indexFile.exists()) {
try {
indexFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public boolean deleteGit() {
File git = new File("git/");
if (git.exists()) {
deleteFolder(git);
git.delete();
}
if (!git.exists())
return true;
else
return false;
}
public void deleteFolder(File folder) {
if (!folder.exists()){
return;
}
if (folder.isDirectory())
{
File [] files = folder.listFiles();
if (files != null)
{
for (File file : files)
{
if(file.isDirectory())
{
deleteFolder(file);
}
if (!file.delete())
{
System.out.println("Failed to delete file: " + file);
}
}
}
}
}
private static String byteToHex(final byte[] hash) { // shamelessly copied from stack overflow:
// https://stackoverflow.com/questions/4895523/java-string-to-sha1
Formatter formatter = new Formatter();
for (byte b : hash) {
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
public static String calculateTreeSHA1(String treeContent)
{
String sha1 = "";
try
{
MessageDigest encrypter = MessageDigest.getInstance("SHA-1");
encrypter.reset();
encrypter.update(treeContent.getBytes("UTF-8"));
sha1 = byteToHex(encrypter.digest());
}
catch (Exception e)
{
e.printStackTrace();
}
return sha1;
}
}