-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGitWrapper.java
More file actions
93 lines (88 loc) · 3.45 KB
/
GitWrapper.java
File metadata and controls
93 lines (88 loc) · 3.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
90
91
92
93
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class GitWrapper {
/**
* Initializes a new Git repository. This method creates the necessary directory structure and
* initial files (index, HEAD) required for a Git repository.
*/
public void init() {
// to-do: implement functionality here
Git.createRepository();
};
/**
* Stages a file for the next commit. This method adds a file to the index file. If the file
* does not exist, it throws an IOException. If the file is a directory, it throws an
* IOException. If the file is already in the index, it does nothing. If the file is
* successfully staged, it creates a blob for the file.
*
* @param filePath The path to the file to be staged.
*/
public void add(String filePath) {
// to-do: implement functionality here
try {
File file = new File(filePath);
Git.createBlob(file.toPath().toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
/**
* Creates a commit with the given author and message. It should capture the current state of
* the repository by building trees based on the index file, writing the tree to the objects
* directory, writing the commit to the objects directory, updating the HEAD file, and returning
* the commit hash.
*
* The commit should be formatted as follows: tree: <tree_sha> parent: <parent_sha> author:
* <author> date: <date> summary: <summary>
*
* @param author The name of the author making the commit.
* @param message The commit message describing the changes.
* @return The SHA1 hash of the new commit.
*/
public String commit(String author, String message) {
// to-do: implement functionality here
try {
Git.createIndexTree();
} catch (Exception e) {
// TODO: handle exception
}
Git.commit(author, message);
File objects = new File("git/objects");
File[] things = objects.listFiles();
try {
String rootTree = "";
for (File file : things) {
if (new String(Files.readAllBytes(Paths.get("git/objects/" + file.getName())))
.contains("(root)")) {
rootTree = file.getName();
return rootTree;
}
}
} catch (Exception e) {
// TODO: handle exception
System.err.println(e);
}
return null;
};
/**
* EXTRA CREDIT: Checks out a specific commit given its hash. This method should read the HEAD
* file to determine the "checked out" commit. Then it should update the working directory to
* match the state of the repository at that commit by tracing through the root tree and all its
* children.
*
* @param commitHash The SHA1 hash of the commit to check out.
*/
public void checkout(String commitHash) {
// to-do: implement functionality here
System.out.println(
"Man I'm just trying to get this in by 8am ain't got no time for extra credit");
};
}