java代码操作git

java代码操作git

引入maven配置

1
2
3
4
5
6
<!-- git client -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.4.1.201607150455-r</version>
</dependency>

java代码GitClient.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
package com.schedulers.git;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;

import java.io.File;
import java.io.IOException;

/**
* Created by xiehui1956(@)gmail.com on 16-12-1.
*/
public final class GitClient {

private String localPath, localGitPath, remotePath;
private Repository localRepository;
private Git git;

public GitClient(String localPath, String remotePath) {
this.localPath = localPath;
this.remotePath = remotePath;
this.localGitPath = this.localPath + "/.git";
try {
localRepository = new FileRepository(localGitPath);
} catch (IOException e) {
e.printStackTrace();
}
git = new Git(localRepository);
}

public static void main(String[] args) throws Exception {
String localPath = "/home/bls/workspace/work/project/p-git",
remotePath = "git@code.core.:product/p-data.git";
GitClient gitClient = new GitClient(localPath, remotePath);
// 创建本地仓库
gitClient.create();

// 克隆远程分之
String branchName = "dev";
gitClient.cloneBranch(branchName);

// pull远程代码
gitClient.pull(branchName);

// 添加文件
String myClass = "MyClass.java";
gitClient.add(myClass);

// 添加备注
String message = "this is commit message";
gitClient.commit(message);

// 同步远程仓库
gitClient.push();
}

/**
* 创建本地仓库
*
* @throws IOException
*/
public void create() throws IOException {
Repository repository = new FileRepository(localGitPath);
repository.create();
System.out.println(" create success ");
}

/**
* clone克隆远程分之到本地
*
* @param branchName
* @throws GitAPIException
*/
public void cloneBranch(String branchName) throws GitAPIException {
Git.cloneRepository().setURI(remotePath).setBranch(branchName)
.setDirectory(new File(localPath)).call();
System.out.println(" clone success ");
}

/**
* pull远程代码
*
* @param branchName 远程分之名称
* @throws Exception
*/
public void pull(String branchName) throws Exception {
git.pull().setRemoteBranchName(branchName).call();
System.out.println(" pull success ");
}

/**
* 添加文件
*
* @param fileName 添加文件名
* @throws Exception
*/
public void add(String fileName) throws Exception {
File myFile = new File(localPath + fileName);
myFile.createNewFile();
git.add().addFilepattern(fileName).call();
System.out.println(" add success ");
}

/**
* 提交文件
*
* @param message 提交备注
* @throws Exception
*/
public void commit(String message) throws Exception {
git.commit().setMessage(message).call();
System.out.println(" commit success ");
}

/**
* 同步远程仓库
*
* @throws Exception
*/
public void push() throws Exception {
git.push().call();
System.out.println(" push success ");
}

}

运行结果:

1
2
3
4
5
6
7
bls@bls:~/workspace/work/project/p-git$ pwd
/home/bls/workspace/work/project/p-git
bls@bls:~/workspace/work/project/p-git$ git status
位于分支 dev
您的分支与上游分支 'origin/dev' 一致。
无文件要提交,干净的工作区
bls@bls:~/workspace/work/project/p-git$