-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGit.java
More file actions
89 lines (74 loc) · 2.45 KB
/
Git.java
File metadata and controls
89 lines (74 loc) · 2.45 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
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Git {
private final File repo;
public Git() {
this.repo = new File("git");
}
public void initializeRepository() {
// Code to initialize a Git repository
File objects = new File(repo, "objects");
File index = new File(repo, "index");
File head = new File(repo, "HEAD");
boolean repoOk = repo.exists() && repo.isDirectory();
boolean objectsOk = objects.exists() && objects.isDirectory();
boolean indexOk = index.exists() && index.isFile();
boolean headOk = head.exists() && head.isFile();
if (repoOk && objectsOk && indexOk && headOk) {
System.out.println("Git Repository Already Exists");
return;
}
if (!repoOk) {
repo.mkdir();
}
if (!objectsOk) {
objects.mkdir();
}
if (!indexOk) {
try {
index.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if (!headOk) {
try {
head.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Git Repository Created");
}
public static String sha1FromFile(File file) throws IOException {
if (file == null || !file.exists() || !file.isFile()) {
throw new IOException("File not found: " + file);
}
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-1 algorithm not available");
}
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
md.update(buffer, 0, bytesRead);
}
fis.close();
byte[] hashBytes = md.digest();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.length; i++) {
String hex = Integer.toHexString(hashBytes[i] & 0xff);
if (hex.length() == 1) {
sb.append("0");
}
sb.append(hex);
}
return sb.toString();
}
}