1414import java .util .logging .Level ;
1515import java .util .logging .Logger ;
1616import jenkins .model .Jenkins ;
17- import org .jenkinsci .plugins .gitclient .jgit .PreemptiveAuthHttpClientConnectionFactory ;
1817import org .jenkinsci .plugins .gitclient .verifier .HostKeyVerifierFactory ;
1918import org .jenkinsci .plugins .gitclient .verifier .NoHostKeyVerificationStrategy ;
2019
@@ -42,6 +41,7 @@ public class Git implements Serializable {
4241 private TaskListener listener ;
4342 private EnvVars env ;
4443 private String exe ;
44+ private HostKeyVerifierFactory hostKeyFactory ;
4545
4646 /**
4747 * Constructor for a Git object. Either <code>Git.with(listener, env)</code>
@@ -116,6 +116,11 @@ public Git using(String exe) {
116116 return this ;
117117 }
118118
119+ public Git withHostKeyVerifierFactory (HostKeyVerifierFactory hostKeyFactory ) {
120+ this .hostKeyFactory = hostKeyFactory ;
121+ return this ;
122+ }
123+
119124 /**
120125 * {@link org.jenkinsci.plugins.gitclient.GitClient} implementation. The {@link org.jenkinsci.plugins.gitclient.GitClient} interface
121126 * provides the key operations which can be performed on a git repository.
@@ -125,14 +130,15 @@ public Git using(String exe) {
125130 * @throws java.lang.InterruptedException if interrupted.
126131 */
127132 public GitClient getClient () throws IOException , InterruptedException {
128- HostKeyVerifierFactory hostKeyFactory ;
129- if (Jenkins .getInstanceOrNull () == null ) {
130- LOGGER .log (Level .FINE , "No Jenkins instance, skipping host key checking by default" );
131- hostKeyFactory = new NoHostKeyVerificationStrategy ().getVerifier ();
132- } else {
133- hostKeyFactory = GitHostKeyVerificationConfiguration .get ()
134- .getSshHostKeyVerificationStrategy ()
135- .getVerifier ();
133+ if (this .hostKeyFactory == null ) {
134+ if (Jenkins .getInstanceOrNull () == null ) {
135+ LOGGER .log (Level .FINE , "No Jenkins instance, skipping host key checking by default" );
136+ this .hostKeyFactory = new NoHostKeyVerificationStrategy ().getVerifier ();
137+ } else {
138+ this .hostKeyFactory = GitHostKeyVerificationConfiguration .get ()
139+ .getSshHostKeyVerificationStrategy ()
140+ .getVerifier ();
141+ }
136142 }
137143 jenkins .MasterToSlaveFileCallable <GitClient > callable = new GitAPIMasterToSlaveFileCallable (hostKeyFactory );
138144 GitClient git = (repository != null ? repository .act (callable ) : callable .invoke (null , null ));
@@ -199,9 +205,7 @@ public GitClient invoke(File f, VirtualChannel channel) throws IOException, Inte
199205 }
200206
201207 if (JGitApacheTool .MAGIC_EXENAME .equalsIgnoreCase (exe )) {
202- final PreemptiveAuthHttpClientConnectionFactory factory =
203- new PreemptiveAuthHttpClientConnectionFactory ();
204- return new JGitAPIImpl (f , listener , factory , hostKeyFactory );
208+ return new JGitAPIImpl (f , listener , hostKeyFactory );
205209 }
206210 // Ensure we return a backward compatible GitAPI, even API only claim to provide a GitClient
207211 GitAPI gitAPI = new GitAPI (exe , f , listener , env );
0 commit comments