https://github.com/ethiclab/git4j
git4j is an attempt to learn how git works by implementing some parts of it.
https://github.com/ethiclab/git4j
from-scratch git java learn tdd test
Last synced: about 1 month ago
JSON representation
git4j is an attempt to learn how git works by implementing some parts of it.
- Host: GitHub
- URL: https://github.com/ethiclab/git4j
- Owner: ethiclab
- License: mit
- Created: 2017-06-22T20:02:12.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-01-27T11:13:14.000Z (over 5 years ago)
- Last Synced: 2025-01-26T07:41:25.002Z (over 1 year ago)
- Topics: from-scratch, git, java, learn, tdd, test
- Language: Java
- Size: 21.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# git4j
[](https://travis-ci.org/ethiclab/git4j)
[](https://codecov.io/gh/ethiclab/git4j)
git4j is an attempt to learn how git works by implementing some parts of it.
## Create Root Tree
Here we create an initial tree at root leve; with a file named pippo with the content "Hello World!\n\n"
```java
private GitTreeEntry createInitialTree() {
List objects = new ArrayList<>();
objects.add(createFileEntry("pippo", "Hello World!\n\n"));
return new GitTreeEntry(objects);
}
@Test
public void testTreeWith1Item() {
assertThat(g.binaryToHex(g.getTreeSha(createInitialTree())))
.isEqualTo("132bfb311556de7c60c34ef3d450c9e8bcc6310b");
}
```
## Create Initial Commit
Here we create a commit with a reference to the tree created in the previous example.
```java
/**
* tree 132bfb311556de7c60c34ef3d450c9e8bcc6310b
* author Montoya Edu 1496830486 +0200
* committer Montoya Edu 1496830486 +0200
*
* Add file.
*
*/
Git git = new Git();
GitCommit c = new GitCommit();
c.setMessage("Add file.");
c.setCommitter("Montoya Edu ");
c.setAuthor("Montoya Edu ");
Calendar calendar = g.getCalendar("Europe/Rome");
calendar.setTimeInMillis(1496830486);
c.setAuthoringDate(calendar.getTime());
c.setCommitDate(calendar.getTime());
c.setAuthoringTimezone("Europe/Rome");
c.setCommitTimezone("Europe/Rome");
c.setTree("132bfb311556de7c60c34ef3d450c9e8bcc6310b");
byte[] bytes = g.serialize(c);
byte[] compressed = g.compress(bytes);
String sha = g.binaryToHex(g.getSha(c));
// now that we have the SHA1 string, and the compressed bytes, we can store it:
// on the filesystem, as git normally does, or
// you can choose the kind persistence layer you want to use for your needs.
```
## Further details
Look for file [GitTest.java](src/test/java/it/ethiclab/git4j/GitTest.java)