-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGit.java
More file actions
107 lines (101 loc) · 3.77 KB
/
Git.java
File metadata and controls
107 lines (101 loc) · 3.77 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.*;
import java.nio.file.attribute.BasicFileAttributes;
public class Git{
public static void main (String [] args){
testRepoInit();
}
public static void initGitRepo () {
int pathExistsCounter=0;
//Creates the "git" directory
File git = new File ("git");
if (!git.exists()){
try {
Files.createDirectory(Paths.get("git"));
} catch (IOException e) {
e.printStackTrace();
pathExistsCounter++;
}
}
else{
System.out.println ("git directory already exists.");
}
//Creates the "objects" directory inside in "git" directory
File objects = new File ("git/objects");
if (!objects.exists()){
try {
Files.createDirectory(Paths.get("git/objects"));
} catch (IOException e) {
e.printStackTrace();
pathExistsCounter++;
}
}
else{
System.out.println ("objects directory already exists.");
}
//Creates the "index" file in the "git" folder
File index = new File ("git/index");
if (!index.exists()){
try {
Files.createFile(Paths.get("git/index"));
} catch (IOException e) {
e.printStackTrace();
pathExistsCounter++;
}
}
else{
System.out.println ("index file already exists.");
}
//Prints custom message if all paths already exist
if (pathExistsCounter>=3) {
System.out.println("Git Repository already exists");
}
}
//Tests main methods
private static void testRepoInit() {
//Testing file creation
initGitRepo();
//Checks for created files
System.out.println("git dir exists: " + doesPathExist("git"));
System.out.println("git/objects dir exists: " + doesPathExist("git/objects"));
System.out.println("git/index file exists: " + doesPathExist("git/index"));
//Testing custom already created message
initGitRepo();
}
//Checks if a path exists, returns the boolean answer
private static boolean doesPathExist (String path) {
return Files.exists(Paths.get(path));
}
//Deletes chosen path, returns true if path deleted, false if path did not exist
private static boolean deletePath(String path) {
try {
Path targetPath = Paths.get(path);
//Case for nonexistent path
if (Files.notExists(targetPath)) {
return false;
}
//If the path is a regular file, just deletes it
if (Files.isRegularFile(targetPath)) {
Files.delete(targetPath);
return true;
}
//Recursively deletes elements in the file tree
Files.walkFileTree(targetPath, new SimpleFileVisitor<Path>() {
@Override //This is just used to help catch errors
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}//This basically says, whenever you encounter a file while walking through the tree, delete it
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}//Ensures that the now-empty directory is deleted
});
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}