Skip to content

Commit 53a6029

Browse files
committed
Git Integration
1 parent 2b18d1f commit 53a6029

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Binary file not shown.

app/lib/slf4j-api-1.7.21.jar

40.1 KB
Binary file not shown.

app/lib/slf4j-simple-1.7.21.jar

10.6 KB
Binary file not shown.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package processing.app.git;
2+
3+
import org.eclipse.jgit.api.Git;
4+
import org.eclipse.jgit.api.errors.GitAPIException;
5+
import org.eclipse.jgit.api.errors.NoHeadException;
6+
import org.eclipse.jgit.errors.RepositoryNotFoundException;
7+
import org.eclipse.jgit.lib.Repository;
8+
import org.eclipse.jgit.revwalk.RevCommit;
9+
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.util.function.Consumer;
14+
15+
16+
public class GitManager {
17+
18+
public void init(File repoDir) {
19+
try (Git git = Git.init().setDirectory(repoDir).call()) {
20+
System.out.println("Having repository: " + git.getRepository().getDirectory());
21+
} catch (GitAPIException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
public void commit(File repoDir, String message) {
27+
if (!commitMessageIsValid(message)) {
28+
System.err.println("Commit message isn't valid.");
29+
return;
30+
}
31+
32+
File[] files = repoDir.listFiles();
33+
34+
Consumer<Git> commitCommand = git -> {
35+
try {
36+
addFiles(files, git);
37+
commit(message, git);
38+
System.out.printf("Committed file to repository at: %s%n", git.getRepository().getDirectory());
39+
} catch (GitAPIException e) {
40+
e.printStackTrace();
41+
}
42+
};
43+
44+
runGitCommand(repoDir, commitCommand);
45+
}
46+
47+
public void log(File repoDir) {
48+
49+
Consumer<Git> logCommand = git -> {
50+
try {
51+
Iterable<RevCommit> logs = git.log()
52+
.call();
53+
54+
int count = 0;
55+
for (RevCommit rev : logs) {
56+
System.out.printf("Commit: %s, name: %s%n", rev, rev.getName());
57+
count++;
58+
}
59+
60+
System.out.printf("Had %d commits overall on current branch%n", count);
61+
62+
} catch (NoHeadException e) {
63+
System.err.printf("Repository %s is empty. Try to commit something.%n", git.getRepository().getDirectory());
64+
} catch (GitAPIException e) {
65+
e.printStackTrace();
66+
}
67+
};
68+
69+
runGitCommand(repoDir, logCommand);
70+
}
71+
72+
private boolean commitMessageIsValid(String message) {
73+
return !message.trim().isEmpty();
74+
}
75+
76+
private void addFiles(File[] files, Git git) throws GitAPIException {
77+
for (File file : files) {
78+
git.add()
79+
.addFilepattern(file.getName())
80+
.call();
81+
}
82+
}
83+
84+
private void commit(String message, Git git) throws GitAPIException {
85+
git.commit()
86+
.setMessage(message)
87+
.call();
88+
}
89+
90+
private void runGitCommand(File repoDir, Consumer<Git> command) {
91+
File gitDir = new File(repoDir, ".git");
92+
if (gitDir.exists()) {
93+
repoDir = gitDir;
94+
}
95+
96+
FileRepositoryBuilder builder = new FileRepositoryBuilder();
97+
try (Repository repository = builder.setGitDir(repoDir)
98+
.readEnvironment() // scan environment GIT_* variables
99+
.findGitDir(repoDir) // scan up the file system tree
100+
.setMustExist(true)
101+
.build()) {
102+
103+
try (Git git = new Git(repository)) {
104+
command.accept(git);
105+
}
106+
107+
} catch (RepositoryNotFoundException e) {
108+
System.err.printf("Repository not found: %s. Try create git repository.%n", repoDir);
109+
} catch (IOException e) {
110+
e.printStackTrace();
111+
}
112+
113+
}
114+
115+
}

0 commit comments

Comments
 (0)