View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.jgit;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.function.Consumer;
26  
27  import org.apache.maven.scm.provider.ScmProvider;
28  import org.apache.maven.scm.provider.git.command.GitCommand;
29  import org.apache.maven.scm.provider.git.jgit.command.CustomizableSshSessionFactoryCommand;
30  import org.apache.maven.scm.provider.git.jgit.command.branch.JGitBranchCommand;
31  import org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommand;
32  import org.apache.maven.scm.provider.git.jgit.command.checkout.JGitCheckOutCommand;
33  import org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand;
34  import org.apache.maven.scm.provider.git.jgit.command.tag.JGitTagCommand;
35  import org.apache.maven.scm.provider.git.jgit.command.untag.JGitUntagCommand;
36  import org.codehaus.plexus.components.interactivity.Prompter;
37  import org.eclipse.jgit.api.TransportCommand;
38  import org.eclipse.sisu.Priority;
39  
40  /**
41   * Allows to register callbacks for all commands leveraging {@link TransportCommand}.
42   */
43  @Singleton
44  @Named("jgit")
45  @Priority(1) // must have higher priority than default JGitScmProvider
46  public class JGitTestScmProvider extends JGitScmProvider implements ScmProvider {
47      private Consumer<? super JGitCheckInCommand> checkInCommandCallback;
48      private Consumer<? super JGitCheckOutCommand> checkOutCommandCallback;
49      private Consumer<? super JGitRemoteInfoCommand> remoteInfoCommandCallback;
50      private Consumer<? super JGitTagCommand> tagCommandCallback;
51      private Consumer<? super JGitUntagCommand> untagCommandCallback;
52      private Consumer<? super JGitBranchCommand> branchCommandCallback;
53  
54      @Inject
55      public JGitTestScmProvider(Prompter prompter) {
56          super(prompter);
57      }
58  
59      public void registerCheckInCommandCallback(Consumer<? super JGitCheckInCommand> gitCommandConsumer) {
60          checkInCommandCallback = gitCommandConsumer;
61      }
62  
63      public void registerCheckOutCommandCallback(Consumer<? super JGitCheckOutCommand> gitCommandConsumer) {
64          checkOutCommandCallback = gitCommandConsumer;
65      }
66  
67      public void registerRemoteInfoCommandCallback(Consumer<? super JGitRemoteInfoCommand> gitCommandConsumer) {
68          remoteInfoCommandCallback = gitCommandConsumer;
69      }
70  
71      public void registerTagCommandCallback(Consumer<? super JGitTagCommand> gitCommandConsumer) {
72          tagCommandCallback = gitCommandConsumer;
73      }
74  
75      public void registerUntagCommandCallback(Consumer<? super JGitUntagCommand> gitCommandConsumer) {
76          untagCommandCallback = gitCommandConsumer;
77      }
78  
79      public void registerBranchCommandCallback(Consumer<? super JGitBranchCommand> gitCommandConsumer) {
80          branchCommandCallback = gitCommandConsumer;
81      }
82  
83      @Override
84      protected GitCommand getCheckInCommand() {
85          JGitCheckInCommand command = (JGitCheckInCommand) super.getCheckInCommand();
86          if (checkInCommandCallback != null) {
87              checkInCommandCallback.accept(command);
88          }
89          return command;
90      }
91  
92      @Override
93      protected GitCommand getCheckOutCommand() {
94          JGitCheckOutCommand command = (JGitCheckOutCommand) super.getCheckOutCommand();
95          if (checkOutCommandCallback != null) {
96              checkOutCommandCallback.accept(command);
97          }
98          return command;
99      }
100 
101     @Override
102     protected GitCommand getRemoteInfoCommand() {
103         JGitRemoteInfoCommand command = (JGitRemoteInfoCommand) super.getRemoteInfoCommand();
104         if (remoteInfoCommandCallback != null) {
105             remoteInfoCommandCallback.accept(command);
106         }
107         return command;
108     }
109 
110     @Override
111     protected GitCommand getBranchCommand() {
112         JGitBranchCommand command = (JGitBranchCommand) super.getBranchCommand();
113         if (branchCommandCallback != null) {
114             branchCommandCallback.accept(command);
115         }
116         return command;
117     }
118 
119     @Override
120     protected GitCommand getTagCommand() {
121         JGitTagCommand command = (JGitTagCommand) super.getTagCommand();
122         if (tagCommandCallback != null) {
123             tagCommandCallback.accept(command);
124         }
125         return command;
126     }
127 
128     @Override
129     protected GitCommand getUntagCommand() {
130         JGitUntagCommand command = (JGitUntagCommand) super.getUntagCommand();
131         if (untagCommandCallback != null) {
132             untagCommandCallback.accept(command);
133         }
134         return command;
135     }
136 
137     /**
138      * Uses a custom SSHD session factory which accepts all hosts for all commands which (potentially) involve
139      * a server connection.
140      */
141     public void useLenientSshdSessionFactory() {
142         Consumer<CustomizableSshSessionFactoryCommand> configureLenientSshSessionFactory = command -> {
143             command.setSshSessionFactorySupplier(AcceptAllHostsSshdSessionFactory::new);
144         };
145         registerCheckOutCommandCallback(configureLenientSshSessionFactory);
146         registerCheckInCommandCallback(configureLenientSshSessionFactory);
147         registerBranchCommandCallback(configureLenientSshSessionFactory);
148         registerTagCommandCallback(configureLenientSshSessionFactory);
149         registerUntagCommandCallback(configureLenientSshSessionFactory);
150     }
151 }