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.command.list;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.List;
024import java.util.function.BiFunction;
025
026import org.apache.maven.scm.ScmException;
027import org.apache.maven.scm.ScmFile;
028import org.apache.maven.scm.ScmFileSet;
029import org.apache.maven.scm.ScmFileStatus;
030import org.apache.maven.scm.ScmVersion;
031import org.apache.maven.scm.command.list.AbstractListCommand;
032import org.apache.maven.scm.command.list.ListScmResult;
033import org.apache.maven.scm.provider.ScmProviderRepository;
034import org.apache.maven.scm.provider.git.command.GitCommand;
035import org.apache.maven.scm.provider.git.jgit.command.JGitTransportConfigCallback;
036import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
037import org.apache.maven.scm.provider.git.jgit.command.ScmProviderAwareSshdSessionFactory;
038import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
039import org.eclipse.jgit.api.Git;
040import org.eclipse.jgit.lib.Ref;
041import org.eclipse.jgit.transport.CredentialsProvider;
042import org.slf4j.Logger;
043
044/**
045 * @author Dominik Bartholdi (imod)
046 * @since 1.9
047 */
048public class JGitListCommand extends AbstractListCommand implements GitCommand {
049
050    private BiFunction<GitScmProviderRepository, Logger, ScmProviderAwareSshdSessionFactory> sshSessionFactorySupplier;
051
052    public JGitListCommand() {
053        sshSessionFactorySupplier = ScmProviderAwareSshdSessionFactory::new;
054    }
055
056    public void setSshSessionFactorySupplier(
057            BiFunction<GitScmProviderRepository, Logger, ScmProviderAwareSshdSessionFactory>
058                    sshSessionFactorySupplier) {
059        this.sshSessionFactorySupplier = sshSessionFactorySupplier;
060    }
061
062    @Override
063    protected ListScmResult executeListCommand(
064            ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive, ScmVersion scmVersion)
065            throws ScmException {
066
067        Git git = null;
068        try {
069            git = JGitUtils.openRepo(fileSet.getBasedir());
070            CredentialsProvider credentials = JGitUtils.prepareSession(git, (GitScmProviderRepository) repo);
071
072            List<ScmFile> list = new ArrayList<>();
073            Collection<Ref> lsResult = git.lsRemote()
074                    .setCredentialsProvider(credentials)
075                    .setTransportConfigCallback(new JGitTransportConfigCallback(
076                            sshSessionFactorySupplier.apply((GitScmProviderRepository) repo, logger)))
077                    .call();
078            for (Ref ref : lsResult) {
079                logger.debug(
080                        ref.getObjectId().getName() + "  " + ref.getTarget().getName());
081                list.add(new ScmFile(ref.getName(), ScmFileStatus.CHECKED_IN));
082            }
083
084            return new ListScmResult("JGit ls-remote", list);
085        } catch (Exception e) {
086            throw new ScmException("JGit ls-remote failure!", e);
087        } finally {
088            JGitUtils.closeRepo(git);
089        }
090    }
091}