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.annotation.Priority;
22  import javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
26  import java.util.function.Consumer;
27  
28  import org.apache.maven.scm.provider.ScmProvider;
29  import org.apache.maven.scm.provider.git.command.GitCommand;
30  import org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommand;
31  import org.apache.maven.scm.provider.git.jgit.command.checkout.JGitCheckOutCommand;
32  import org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand;
33  import org.codehaus.plexus.components.interactivity.Prompter;
34  import org.eclipse.jgit.api.TransportCommand;
35  
36  /**
37   * Allows to register callbacks for all commands leveraging {@link TransportCommand}.
38   */
39  @Singleton
40  @Named("jgit")
41  @Priority(1) // must have higher priority than default JGitScmProvider
42  public class JGitTestScmProvider extends JGitScmProvider implements ScmProvider {
43      private Consumer<JGitCheckInCommand> checkInCommandCallback;
44      private Consumer<JGitCheckOutCommand> checkOutCommandCallback;
45      private Consumer<JGitRemoteInfoCommand> remoteInfoCommandCallback;
46  
47      @Inject
48      public JGitTestScmProvider(Prompter prompter) {
49          super(prompter);
50      }
51  
52      public void registerCheckInCommandCallback(Consumer<JGitCheckInCommand> gitCommandConsumer) {
53          checkInCommandCallback = gitCommandConsumer;
54      }
55  
56      public void registerCheckOutCommandCallback(Consumer<JGitCheckOutCommand> gitCommandConsumer) {
57          checkOutCommandCallback = gitCommandConsumer;
58      }
59  
60      public void registerRemoteInfoCommandCallback(Consumer<JGitRemoteInfoCommand> gitCommandConsumer) {
61          remoteInfoCommandCallback = gitCommandConsumer;
62      }
63  
64      @Override
65      protected GitCommand getCheckInCommand() {
66          JGitCheckInCommand command = (JGitCheckInCommand) super.getCheckInCommand();
67          if (checkInCommandCallback != null) {
68              checkInCommandCallback.accept(command);
69          }
70          return command;
71      }
72  
73      @Override
74      protected GitCommand getCheckOutCommand() {
75          JGitCheckOutCommand command = (JGitCheckOutCommand) super.getCheckOutCommand();
76          if (checkOutCommandCallback != null) {
77              checkOutCommandCallback.accept(command);
78          }
79          return command;
80      }
81  
82      @Override
83      protected GitCommand getRemoteInfoCommand() {
84          JGitRemoteInfoCommand command = (JGitRemoteInfoCommand) super.getRemoteInfoCommand();
85          if (remoteInfoCommandCallback != null) {
86              remoteInfoCommandCallback.accept(command);
87          }
88          return command;
89      }
90  }