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.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.function.Consumer;
026
027import org.apache.maven.scm.provider.ScmProvider;
028import org.apache.maven.scm.provider.git.command.GitCommand;
029import org.apache.maven.scm.provider.git.jgit.command.CustomizableSshSessionFactoryCommand;
030import org.apache.maven.scm.provider.git.jgit.command.branch.JGitBranchCommand;
031import org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommand;
032import org.apache.maven.scm.provider.git.jgit.command.checkout.JGitCheckOutCommand;
033import org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand;
034import org.apache.maven.scm.provider.git.jgit.command.tag.JGitTagCommand;
035import org.apache.maven.scm.provider.git.jgit.command.untag.JGitUntagCommand;
036import org.codehaus.plexus.components.interactivity.Prompter;
037import org.eclipse.jgit.api.TransportCommand;
038import org.eclipse.sisu.Priority;
039
040/**
041 * Allows to register callbacks for all commands leveraging {@link TransportCommand}.
042 */
043@Singleton
044@Named("jgit")
045@Priority(1) // must have higher priority than default JGitScmProvider
046public class JGitTestScmProvider extends JGitScmProvider implements ScmProvider {
047    private Consumer<? super JGitCheckInCommand> checkInCommandCallback;
048    private Consumer<? super JGitCheckOutCommand> checkOutCommandCallback;
049    private Consumer<? super JGitRemoteInfoCommand> remoteInfoCommandCallback;
050    private Consumer<? super JGitTagCommand> tagCommandCallback;
051    private Consumer<? super JGitUntagCommand> untagCommandCallback;
052    private Consumer<? super JGitBranchCommand> branchCommandCallback;
053
054    @Inject
055    public JGitTestScmProvider(Prompter prompter) {
056        super(prompter);
057    }
058
059    public void registerCheckInCommandCallback(Consumer<? super JGitCheckInCommand> gitCommandConsumer) {
060        checkInCommandCallback = gitCommandConsumer;
061    }
062
063    public void registerCheckOutCommandCallback(Consumer<? super JGitCheckOutCommand> gitCommandConsumer) {
064        checkOutCommandCallback = gitCommandConsumer;
065    }
066
067    public void registerRemoteInfoCommandCallback(Consumer<? super JGitRemoteInfoCommand> gitCommandConsumer) {
068        remoteInfoCommandCallback = gitCommandConsumer;
069    }
070
071    public void registerTagCommandCallback(Consumer<? super JGitTagCommand> gitCommandConsumer) {
072        tagCommandCallback = gitCommandConsumer;
073    }
074
075    public void registerUntagCommandCallback(Consumer<? super JGitUntagCommand> gitCommandConsumer) {
076        untagCommandCallback = gitCommandConsumer;
077    }
078
079    public void registerBranchCommandCallback(Consumer<? super JGitBranchCommand> gitCommandConsumer) {
080        branchCommandCallback = gitCommandConsumer;
081    }
082
083    @Override
084    protected GitCommand getCheckInCommand() {
085        JGitCheckInCommand command = (JGitCheckInCommand) super.getCheckInCommand();
086        if (checkInCommandCallback != null) {
087            checkInCommandCallback.accept(command);
088        }
089        return command;
090    }
091
092    @Override
093    protected GitCommand getCheckOutCommand() {
094        JGitCheckOutCommand command = (JGitCheckOutCommand) super.getCheckOutCommand();
095        if (checkOutCommandCallback != null) {
096            checkOutCommandCallback.accept(command);
097        }
098        return command;
099    }
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}