001package org.apache.maven.scm.provider.git.jgit.command.remoteinfo; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.scm.CommandParameters; 023import org.apache.maven.scm.ScmException; 024import org.apache.maven.scm.ScmFileSet; 025import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand; 026import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult; 027import org.apache.maven.scm.provider.ScmProviderRepository; 028import org.apache.maven.scm.provider.git.command.GitCommand; 029import org.apache.maven.scm.provider.git.jgit.command.JGitTransportConfigCallback; 030import org.apache.maven.scm.provider.git.jgit.command.JGitUtils; 031import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository; 032import org.eclipse.jgit.api.Git; 033import org.eclipse.jgit.api.LsRemoteCommand; 034import org.eclipse.jgit.lib.Ref; 035import org.eclipse.jgit.lib.Repository; 036import org.eclipse.jgit.transport.CredentialsProvider; 037 038import java.util.Collection; 039import java.util.HashMap; 040import java.util.Map; 041 042/** 043 * @author Dominik Bartholdi (imod) 044 * @since 1.9 045 */ 046public class JGitRemoteInfoCommand 047 extends AbstractRemoteInfoCommand 048 implements GitCommand 049{ 050 051 @Override 052 public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repository, ScmFileSet fileSet, 053 CommandParameters parameters ) 054 throws ScmException 055 { 056 057 GitScmProviderRepository repo = (GitScmProviderRepository) repository; 058 Git git = null; 059 try 060 { 061 git = JGitUtils.openRepo( fileSet.getBasedir() ); 062 CredentialsProvider credentials = JGitUtils.getCredentials( repo ); 063 064 LsRemoteCommand lsCommand = 065 git.lsRemote().setRemote( repo.getPushUrl() ).setCredentialsProvider( credentials ) 066 .setTransportConfigCallback( new JGitTransportConfigCallback( repo, getLogger() ) ); 067 068 Map<String, String> tag = new HashMap<String, String>(); 069 Collection<Ref> allTags = lsCommand.setHeads( false ).setTags( true ).call(); 070 for ( Ref ref : allTags ) 071 { 072 tag.put( Repository.shortenRefName( ref.getName() ), ref.getObjectId().name() ); 073 } 074 075 Map<String, String> heads = new HashMap<String, String>(); 076 Collection<Ref> allHeads = lsCommand.setHeads( true ).setTags( false ).call(); 077 for ( Ref ref : allHeads ) 078 { 079 heads.put( Repository.shortenRefName( ref.getName() ), ref.getObjectId().name() ); 080 } 081 082 return new RemoteInfoScmResult( "JGit remoteinfo", heads, tag ); 083 } 084 catch ( Exception e ) 085 { 086 throw new ScmException( "JGit remoteinfo failure!", e ); 087 } 088 finally 089 { 090 JGitUtils.closeRepo( git ); 091 } 092 } 093}