-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gitapi.java
154 lines (130 loc) · 5.47 KB
/
Gitapi.java
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package gitapi;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Gitapi {
public static CredentialsProvider connect(String email, String password, String apiToken){
UsernamePasswordCredentialsProvider provider = new UsernamePasswordCredentialsProvider(apiToken, "");
return provider;
}
// 克隆远程仓库
public static Git fromCloneRepository(String repoUrl, String cloneDir, CredentialsProvider provider) throws GitAPIException {
Git git = Git.cloneRepository()
.setCredentialsProvider(provider)
.setURI(repoUrl)
.setDirectory(new File(cloneDir)).call();
return git;
}
// 读取已有仓库
public static Repository getRepositoryFromDir(String dir) throws IOException {
return new FileRepositoryBuilder()
.setGitDir(Paths.get(dir, ".git").toFile())
.build();
}
// commit 方法
public static void commit(Git git, String message, CredentialsProvider provider) throws GitAPIException {
git.add().addFilepattern(".").call();
git.commit()
.setMessage(message)
.call();
}
public static void push(Git git, CredentialsProvider provider) throws GitAPIException, IOException {
push_with_branch(git,null, provider);
}
public static void push_with_branch(Git git, String branch, CredentialsProvider provider) throws GitAPIException, IOException, IOException {
if (branch == null) {
branch = git.getRepository().getBranch();
}
git.push()
.setCredentialsProvider(provider)
.setRemote("origin").setRefSpecs(new RefSpec(branch)).call();
}
// 通过 revWalk 读取仓库日志
public static List<String> getLogs(Repository repository) throws IOException {
return getLogsSinceCommit(repository, null, null);
}
// git tag 命令
public static void tag(String name) {
try {
git.tag()
.setName(name)
.call();
} catch (GitAPIException ex) {
throw new IllegalStateException("git tag failed", ex);
}
}
public static List<String> getLogsSinceCommit(Repository repository, String commit) throws IOException {
return getLogsSinceCommit(repository, null, commit);
}
public static List<String> getLogsSinceCommit(Repository repository, String branch, String commit) throws IOException {
if (branch == null) {
branch = repository.getBranch();
}
Ref head = repository.findRef("refs/heads/" + branch);
List<String> commits = new ArrayList<>();
if (head != null) {
try (RevWalk revWalk = new RevWalk(repository)) {
revWalk.markStart(revWalk.parseCommit(head.getObjectId()));
for (RevCommit revCommit : revWalk) {
if (revCommit.getId().getName().equals(commit)) {
break;
}
commits.add(revCommit.getFullMessage());
System.out.println("\nCommit-Message: " + revCommit.getFullMessage());
}
revWalk.dispose();
}
}
return commits;
}
public static void main(String[] args) throws GitAPIException, IOException {
String apiToken = "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";
CredentialsProvider credProvider = Gitapi.connect(email, password, apiToken);
String cloneDir = "/tmp/test";
String repoUrl = "https://github.com/Hoping-for-morning/testforAPI.git";
// String repoUrl = "[email protected]:Hoping-for-morning/testforAPI.git";
// 从远程仓库获取
// Git git = Gitapi.fromCloneRepository(repoUrl, cloneDir, credProvider);
Git git = Git.wrap(Gitapi.getRepositoryFromDir(cloneDir));
String yaml = "dependencies:\n" +
"- name: springboot-rest-demo\n" +
" version: 0.0.5\n" +
" repository: http://hub.hubHOST.com/chartrepo/ainote\n" +
" alias: demo\n" +
"- name: exposecontroller\n" +
" version: 2.3.82\n" +
" repository: http://chartmuseum.jenkins-x.io\n" +
" alias: cleanup\n";
// 修改文件
FileWriter writer;
try {
writer = new FileWriter(cloneDir + "/requirement.yaml");
writer.write(yaml);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
// commit
Gitapi.commit(git, "write yaml", credProvider);
Gitapi.push_with_branch(git, "edit", credProvider);
git.clean().call();
git.close();
}
}