Skip to content

Commit 234ecd6

Browse files
committed
加入Git用户名密码配置
1 parent f18bd30 commit 234ecd6

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ prepare the jacoco code coverage report
1212
config the necessary params and create tmp folder
1313
#### step4:
1414
run command:
15-
java -DGitURI=git@xxx/xxx.git -DNewVersion=refs/remotes/origin/daily -DOldVersion=refs/heads/master -DDiffFolder=/home/it/tmp/
15+
java -DGitURI=git@xxx/xxx.git -DGitUsername=xxx -DGitPassword=xxx -DNewVersion=refs/remotes/origin/daily -DOldVersion=refs/heads/master -DDiffFolder=/home/it/tmp/
1616
-DJacocoReport=/home/it/codeCoverage/Check_Order_related/ -jar jacoco-gitdiff-0.0.1-release.jar

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.keking</groupId>
66
<artifactId>jacoco-gitdiff</artifactId>
7-
<version>0.0.1-SNAPSHOT</version>
7+
<version>0.0.2-SNAPSHOT</version>
88

99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/com/keking/DiffClover.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ public class DiffClover {
1818
public static void main(String[] args) {
1919

2020
// step1:获取必要的配置信息
21-
String gitUrl, newVersion, oldVersion, diffFolder, jacocoReport = "";
21+
String gitUrl, gitUsername, gitPassword, newVersion, oldVersion, diffFolder, jacocoReport = "";
2222

2323
try {
2424
gitUrl = System.getProperty("GitURI").trim();
25+
gitUsername = System.getProperty("GitUsername") == null ? null : System.getProperty("GitUsername").trim();
26+
gitPassword = System.getProperty("GitPassword") == null ? null : System.getProperty("GitPassword").trim();
2527
diffFolder = System.getProperty("DiffFolder").trim();
2628
newVersion = System.getProperty("NewVersion").trim();
2729
oldVersion = System.getProperty("OldVersion").trim();
@@ -43,9 +45,9 @@ public static void main(String[] args) {
4345
// step2:获取最新的源代码
4446
List<File> fileList = (List<File>) FileUtils.listFiles(new File(diffFolder), null, true);
4547
if (fileList.size() < 10) {
46-
JGitUtil.cloneGit(gitUrl, diffFolder);
48+
JGitUtil.cloneGit(gitUrl, diffFolder, gitUsername, gitPassword);
4749
} else {
48-
JGitUtil.pullGit(diffFolder);
50+
JGitUtil.pullGit(diffFolder, gitUsername, gitPassword);
4951
}
5052
// step3:针对传入分支比较
5153
List<ClassDiffEntity> diffs = JGitUtil.diffBranch(diffFolder, newVersion, oldVersion);

src/main/java/com/keking/git/JGitUtil.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
import com.jcraft.jsch.Session;
1414
import org.apache.commons.io.FileUtils;
15+
import org.eclipse.jgit.api.CloneCommand;
1516
import org.eclipse.jgit.api.Git;
1617
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
18+
import org.eclipse.jgit.api.PullCommand;
1719
import org.eclipse.jgit.diff.DiffEntry;
1820
import org.eclipse.jgit.diff.DiffFormatter;
1921
import org.eclipse.jgit.diff.RawText;
@@ -26,6 +28,7 @@
2628
import org.eclipse.jgit.transport.JschConfigSessionFactory;
2729
import org.eclipse.jgit.transport.OpenSshConfig;
2830
import org.eclipse.jgit.transport.SshSessionFactory;
31+
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
2932
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
3033
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
3134
import org.eclipse.jgit.util.StringUtils;
@@ -56,12 +59,16 @@ protected void configure(OpenSshConfig.Host hc, Session session) {
5659
* @param tmpPath
5760
* @throws Exception
5861
*/
59-
public static void cloneGit(String remoteURI, String tmpPath) throws Exception {
62+
public static void cloneGit(String remoteURI, String tmpPath, String gitUsername, String gitPassword) throws Exception {
6063
File localPath = new File(tmpPath);
6164
if (localPath.exists()) {
6265
FileUtils.deleteDirectory(localPath);
6366
}
64-
try (Git git = Git.cloneRepository().setURI(remoteURI).setCloneAllBranches(true).setDirectory(localPath).call()) {
67+
CloneCommand gitCommand = Git.cloneRepository().setURI(remoteURI).setCloneAllBranches(true).setDirectory(localPath);
68+
if (gitUsername != null && gitPassword != null) {
69+
gitCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(gitUsername, gitPassword));
70+
}
71+
try (Git git = gitCommand.call()) {
6572
for (Ref b : git.branchList().setListMode(ListMode.ALL).call())
6673
logger.debug("cloned branch:" + b.getName());
6774
}
@@ -73,9 +80,17 @@ public static void cloneGit(String remoteURI, String tmpPath) throws Exception {
7380
* @param tmpPath
7481
* @throws Exception
7582
*/
76-
public static void pullGit(String tmpPath) throws Exception {
83+
public static void pullGit(String tmpPath, String gitUsername, String gitPassword) throws Exception {
84+
UsernamePasswordCredentialsProvider credential = null;
85+
if (gitUsername != null && gitPassword != null) {
86+
credential = new UsernamePasswordCredentialsProvider(gitUsername, gitPassword);
87+
}
7788
try (Git git = Git.open(new File(tmpPath))) {
78-
git.pull().call();
89+
PullCommand pullCommand = git.pull();
90+
if (credential != null) {
91+
pullCommand.setCredentialsProvider(credential);
92+
}
93+
pullCommand.call();
7994
}
8095
}
8196

0 commit comments

Comments
 (0)