12
12
13
13
import com .jcraft .jsch .Session ;
14
14
import org .apache .commons .io .FileUtils ;
15
+ import org .eclipse .jgit .api .CloneCommand ;
15
16
import org .eclipse .jgit .api .Git ;
16
17
import org .eclipse .jgit .api .ListBranchCommand .ListMode ;
18
+ import org .eclipse .jgit .api .PullCommand ;
17
19
import org .eclipse .jgit .diff .DiffEntry ;
18
20
import org .eclipse .jgit .diff .DiffFormatter ;
19
21
import org .eclipse .jgit .diff .RawText ;
26
28
import org .eclipse .jgit .transport .JschConfigSessionFactory ;
27
29
import org .eclipse .jgit .transport .OpenSshConfig ;
28
30
import org .eclipse .jgit .transport .SshSessionFactory ;
31
+ import org .eclipse .jgit .transport .UsernamePasswordCredentialsProvider ;
29
32
import org .eclipse .jgit .treewalk .AbstractTreeIterator ;
30
33
import org .eclipse .jgit .treewalk .CanonicalTreeParser ;
31
34
import org .eclipse .jgit .util .StringUtils ;
@@ -56,12 +59,16 @@ protected void configure(OpenSshConfig.Host hc, Session session) {
56
59
* @param tmpPath
57
60
* @throws Exception
58
61
*/
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 {
60
63
File localPath = new File (tmpPath );
61
64
if (localPath .exists ()) {
62
65
FileUtils .deleteDirectory (localPath );
63
66
}
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 ()) {
65
72
for (Ref b : git .branchList ().setListMode (ListMode .ALL ).call ())
66
73
logger .debug ("cloned branch:" + b .getName ());
67
74
}
@@ -73,9 +80,17 @@ public static void cloneGit(String remoteURI, String tmpPath) throws Exception {
73
80
* @param tmpPath
74
81
* @throws Exception
75
82
*/
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
+ }
77
88
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 ();
79
94
}
80
95
}
81
96
0 commit comments