001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.jgit;
020
021import javax.annotation.Priority;
022import javax.inject.Inject;
023import javax.inject.Named;
024import javax.inject.Singleton;
025
026import java.util.function.Consumer;
027
028import org.apache.maven.scm.provider.ScmProvider;
029import org.apache.maven.scm.provider.git.command.GitCommand;
030import org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommand;
031import org.apache.maven.scm.provider.git.jgit.command.checkout.JGitCheckOutCommand;
032import org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand;
033import org.codehaus.plexus.components.interactivity.Prompter;
034import org.eclipse.jgit.api.TransportCommand;
035
036/**
037 * Allows to register callbacks for all commands leveraging {@link TransportCommand}.
038 */
039@Singleton
040@Named("jgit")
041@Priority(1) // must have higher priority than default JGitScmProvider
042public class JGitTestScmProvider extends JGitScmProvider implements ScmProvider {
043    private Consumer<JGitCheckInCommand> checkInCommandCallback;
044    private Consumer<JGitCheckOutCommand> checkOutCommandCallback;
045    private Consumer<JGitRemoteInfoCommand> remoteInfoCommandCallback;
046
047    @Inject
048    public JGitTestScmProvider(Prompter prompter) {
049        super(prompter);
050    }
051
052    public void registerCheckInCommandCallback(Consumer<JGitCheckInCommand> gitCommandConsumer) {
053        checkInCommandCallback = gitCommandConsumer;
054    }
055
056    public void registerCheckOutCommandCallback(Consumer<JGitCheckOutCommand> gitCommandConsumer) {
057        checkOutCommandCallback = gitCommandConsumer;
058    }
059
060    public void registerRemoteInfoCommandCallback(Consumer<JGitRemoteInfoCommand> gitCommandConsumer) {
061        remoteInfoCommandCallback = gitCommandConsumer;
062    }
063
064    @Override
065    protected GitCommand getCheckInCommand() {
066        JGitCheckInCommand command = (JGitCheckInCommand) super.getCheckInCommand();
067        if (checkInCommandCallback != null) {
068            checkInCommandCallback.accept(command);
069        }
070        return command;
071    }
072
073    @Override
074    protected GitCommand getCheckOutCommand() {
075        JGitCheckOutCommand command = (JGitCheckOutCommand) super.getCheckOutCommand();
076        if (checkOutCommandCallback != null) {
077            checkOutCommandCallback.accept(command);
078        }
079        return command;
080    }
081
082    @Override
083    protected GitCommand getRemoteInfoCommand() {
084        JGitRemoteInfoCommand command = (JGitRemoteInfoCommand) super.getRemoteInfoCommand();
085        if (remoteInfoCommandCallback != null) {
086            remoteInfoCommandCallback.accept(command);
087        }
088        return command;
089    }
090}