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