Skip to content

Commit dabdd5d

Browse files
Merge branch 'master' into pr/997-stephen
2 parents 4119fc9 + 5a6eaeb commit dabdd5d

229 files changed

Lines changed: 5901 additions & 1590 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎JGit/pom.xml‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>JGitSnippets</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<url>http://maven.apache.org</url>
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
<repositories>
15+
<repository>
16+
<id>jgit-repository</id>
17+
<url>https://repo.eclipse.org/content/groups/releases/</url>
18+
</repository>
19+
</repositories>
20+
21+
<!-- Core Library -->
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.eclipse.jgit</groupId>
25+
<artifactId>org.eclipse.jgit</artifactId>
26+
<version>4.5.0.201609210915-r</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.eclipse.jgit</groupId>
30+
<artifactId>org.eclipse.jgit.archive</artifactId>
31+
<version>4.5.0.201609210915-r</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>commons-io</groupId>
35+
<artifactId>commons-io</artifactId>
36+
<version>2.5</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.slf4j</groupId>
40+
<artifactId>slf4j-simple</artifactId>
41+
<version>1.7.21</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>junit</groupId>
45+
<artifactId>junit</artifactId>
46+
<version>4.12</version>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>3.2</version>
56+
<configuration>
57+
<source>1.7</source>
58+
<target>1.7</target>
59+
</configuration>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.jgit;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import org.apache.commons.io.FileUtils;
6+
import org.eclipse.jgit.api.Git;
7+
import org.eclipse.jgit.api.errors.GitAPIException;
8+
9+
/**
10+
* Simple snippet which shows how to create a new repository
11+
*
12+
*
13+
*/
14+
public class CreateNewRepository {
15+
16+
public static void main(String[] args) throws IOException, IllegalStateException, GitAPIException {
17+
// prepare a new folder
18+
File localPath = File.createTempFile("TestGitRepository", "");
19+
if(!localPath.delete()) {
20+
throw new IOException("Could not delete temporary file " + localPath);
21+
}
22+
23+
// create the directory
24+
try (Git git = Git.init().setDirectory(localPath).call()) {
25+
System.out.println("Having repository: " + git.getRepository().getDirectory());
26+
}
27+
28+
FileUtils.deleteDirectory(localPath);
29+
}
30+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.jgit;
2+
3+
import com.baeldung.jgit.helper.Helper;
4+
import org.eclipse.jgit.api.Git;
5+
import org.eclipse.jgit.api.errors.GitAPIException;
6+
import org.eclipse.jgit.lib.Ref;
7+
import org.eclipse.jgit.lib.Repository;
8+
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
/**
13+
* Simple snippet which shows how to open an existing repository
14+
*
15+
*
16+
*/
17+
public class OpenRepository {
18+
19+
public static void main(String[] args) throws IOException, GitAPIException {
20+
// first create a test-repository, the return is including the .get directory here!
21+
File repoDir = createSampleGitRepo();
22+
23+
// now open the resulting repository with a FileRepositoryBuilder
24+
FileRepositoryBuilder builder = new FileRepositoryBuilder();
25+
try (Repository repository = builder.setGitDir(repoDir)
26+
.readEnvironment() // scan environment GIT_* variables
27+
.findGitDir() // scan up the file system tree
28+
.build()) {
29+
System.out.println("Having repository: " + repository.getDirectory());
30+
31+
// the Ref holds an ObjectId for any type of object (tree, commit, blob, tree)
32+
Ref head = repository.exactRef("refs/heads/master");
33+
System.out.println("Ref of refs/heads/master: " + head);
34+
}
35+
}
36+
37+
private static File createSampleGitRepo() throws IOException, GitAPIException {
38+
try (Repository repository = Helper.createNewRepository()) {
39+
System.out.println("Temporary repository at " + repository.getDirectory());
40+
41+
// create the file
42+
File myfile = new File(repository.getDirectory().getParent(), "testfile");
43+
if(!myfile.createNewFile()) {
44+
throw new IOException("Could not create file " + myfile);
45+
}
46+
47+
// run the add-call
48+
try (Git git = new Git(repository)) {
49+
git.add()
50+
.addFilepattern("testfile")
51+
.call();
52+
53+
54+
// and then commit the changes
55+
git.commit()
56+
.setMessage("Added testfile")
57+
.call();
58+
}
59+
60+
System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory());
61+
62+
return repository.getDirectory();
63+
}
64+
}
65+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
package com.baeldung.jgit.helper;
3+
4+
import java.io.File;
5+
import java.io.IOException;
6+
import org.eclipse.jgit.lib.Repository;
7+
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
8+
9+
public class Helper {
10+
11+
public static Repository openJGitRepository() throws IOException {
12+
FileRepositoryBuilder builder = new FileRepositoryBuilder();
13+
return builder
14+
.readEnvironment() // scan environment GIT_* variables
15+
.findGitDir() // scan up the file system tree
16+
.build();
17+
}
18+
19+
public static Repository createNewRepository() throws IOException {
20+
// prepare a new folder
21+
File localPath = File.createTempFile("TestGitRepository", "");
22+
if(!localPath.delete()) {
23+
throw new IOException("Could not delete temporary file " + localPath);
24+
}
25+
26+
// create the directory
27+
Repository repository = FileRepositoryBuilder.create(new File(localPath, ".git"));
28+
repository.create();
29+
30+
return repository;
31+
}
32+
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.jgit.porcelain;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import com.baeldung.jgit.helper.Helper;
6+
import org.eclipse.jgit.api.Git;
7+
import org.eclipse.jgit.api.errors.GitAPIException;
8+
import org.eclipse.jgit.lib.Repository;
9+
10+
/**
11+
* Simple snippet which shows how to add a file to the index
12+
*
13+
*
14+
*/
15+
public class AddFile {
16+
17+
public static void main(String[] args) throws IOException, GitAPIException {
18+
// prepare a new test-repository
19+
try (Repository repository = Helper.createNewRepository()) {
20+
try (Git git = new Git(repository)) {
21+
// create the file
22+
File myfile = new File(repository.getDirectory().getParent(), "testfile");
23+
if(!myfile.createNewFile()) {
24+
throw new IOException("Could not create file " + myfile);
25+
}
26+
27+
// run the add-call
28+
git.add()
29+
.addFilepattern("testfile")
30+
.call();
31+
32+
System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory());
33+
}
34+
}
35+
}
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.jgit.porcelain;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.PrintWriter;
6+
import com.baeldung.jgit.helper.Helper;
7+
import org.eclipse.jgit.api.Git;
8+
import org.eclipse.jgit.api.errors.GitAPIException;
9+
import org.eclipse.jgit.lib.Repository;
10+
11+
/**
12+
* Simple snippet which shows how to commit all files
13+
*
14+
*
15+
*/
16+
public class CommitAll {
17+
18+
public static void main(String[] args) throws IOException, GitAPIException {
19+
// prepare a new test-repository
20+
try (Repository repository = Helper.createNewRepository()) {
21+
try (Git git = new Git(repository)) {
22+
// create the file
23+
File myfile = new File(repository.getDirectory().getParent(), "testfile");
24+
if(!myfile.createNewFile()) {
25+
throw new IOException("Could not create file " + myfile);
26+
}
27+
28+
// Stage all files in the repo including new files
29+
git.add().addFilepattern(".").call();
30+
31+
// and then commit the changes.
32+
git.commit()
33+
.setMessage("Commit all changes including additions")
34+
.call();
35+
36+
try(PrintWriter writer = new PrintWriter(myfile)) {
37+
writer.append("Hello, world!");
38+
}
39+
40+
// Stage all changed files, omitting new files, and commit with one command
41+
git.commit()
42+
.setAll(true)
43+
.setMessage("Commit changes to all files")
44+
.call();
45+
46+
47+
System.out.println("Committed all changes to repository at " + repository.getDirectory());
48+
}
49+
}
50+
}
51+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.jgit.porcelain;
2+
3+
import java.io.IOException;
4+
import com.baeldung.jgit.helper.Helper;
5+
import org.eclipse.jgit.api.Git;
6+
import org.eclipse.jgit.api.errors.GitAPIException;
7+
import org.eclipse.jgit.lib.ObjectId;
8+
import org.eclipse.jgit.lib.Ref;
9+
import org.eclipse.jgit.lib.Repository;
10+
import org.eclipse.jgit.revwalk.RevCommit;
11+
import org.eclipse.jgit.revwalk.RevWalk;
12+
13+
/**
14+
* Simple snippet which shows how to create a tag
15+
*
16+
*
17+
*/
18+
public class CreateAndDeleteTag {
19+
20+
public static void main(String[] args) throws IOException, GitAPIException {
21+
// prepare test-repository
22+
try (Repository repository = Helper.openJGitRepository()) {
23+
try (Git git = new Git(repository)) {
24+
// remove the tag before creating it
25+
git.tagDelete().setTags("tag_for_testing").call();
26+
27+
// set it on the current HEAD
28+
Ref tag = git.tag().setName("tag_for_testing").call();
29+
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
30+
31+
// remove the tag again
32+
git.tagDelete().setTags("tag_for_testing").call();
33+
34+
// read some other commit and set the tag on it
35+
ObjectId id = repository.resolve("HEAD^");
36+
try (RevWalk walk = new RevWalk(repository)) {
37+
RevCommit commit = walk.parseCommit(id);
38+
tag = git.tag().setObjectId(commit).setName("tag_for_testing").call();
39+
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
40+
41+
// remove the tag again
42+
git.tagDelete().setTags("tag_for_testing").call();
43+
44+
// create an annotated tag
45+
tag = git.tag().setName("tag_for_testing").setAnnotated(true).call();
46+
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
47+
48+
// remove the tag again
49+
git.tagDelete().setTags("tag_for_testing").call();
50+
51+
walk.dispose();
52+
}
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)