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.JGitUtils; 030import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository; 031import org.eclipse.jgit.api.Git; 032import org.eclipse.jgit.api.LsRemoteCommand; 033import org.eclipse.jgit.lib.Ref; 034import org.eclipse.jgit.lib.Repository; 035import org.eclipse.jgit.transport.CredentialsProvider; 036 037import java.util.Collection; 038import java.util.HashMap; 039import java.util.Map; 040 041/** 042 * @author Dominik Bartholdi (imod) 043 * @since 1.9 044 */ 045public class JGitRemoteInfoCommand 046 extends AbstractRemoteInfoCommand 047 implements GitCommand 048{ 049 050 @Override 051 public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repository, ScmFileSet fileSet, 052 CommandParameters parameters ) 053 throws ScmException 054 { 055 056 GitScmProviderRepository repo = (GitScmProviderRepository) repository; 057 Git git = null; 058 try 059 { 060 git = Git.open( fileSet.getBasedir() ); 061 CredentialsProvider credentials = JGitUtils.getCredentials( repo ); 062 063 LsRemoteCommand lsCommand = 064 git.lsRemote().setRemote( repo.getPushUrl() ).setCredentialsProvider( credentials ); 065 066 Map<String, String> tag = new HashMap<String, String>(); 067 Collection<Ref> allTags = lsCommand.setHeads( false ).setTags( true ).call(); 068 for ( Ref ref : allTags ) 069 { 070 tag.put( Repository.shortenRefName( ref.getName() ), ref.getObjectId().name() ); 071 } 072 073 Map<String, String> heads = new HashMap<String, String>(); 074 Collection<Ref> allHeads = lsCommand.setHeads( true ).setTags( false ).call(); 075 for ( Ref ref : allHeads ) 076 { 077 heads.put( Repository.shortenRefName( ref.getName() ), ref.getObjectId().name() ); 078 } 079 080 return new RemoteInfoScmResult( "JGit remoteinfo", heads, tag ); 081 } 082 catch ( Exception e ) 083 { 084 throw new ScmException( "JGit remoteinfo failure!", e ); 085 } 086 finally 087 { 088 JGitUtils.closeRepo( git ); 089 } 090 } 091}