001package org.apache.maven.scm.provider.git.jgit.command.info; 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 022 023import java.io.File; 024import java.util.Collections; 025 026import org.apache.maven.scm.CommandParameters; 027import org.apache.maven.scm.ScmException; 028import org.apache.maven.scm.ScmFileSet; 029import org.apache.maven.scm.ScmResult; 030import org.apache.maven.scm.command.AbstractCommand; 031import org.apache.maven.scm.command.info.InfoItem; 032import org.apache.maven.scm.command.info.InfoScmResult; 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.JGitUtils; 036import org.codehaus.plexus.util.StringUtils; 037import org.eclipse.jgit.api.Git; 038import org.eclipse.jgit.lib.ObjectId; 039 040/** 041 * @since 1.9.5 042 */ 043public class JGitInfoCommand 044 extends AbstractCommand 045 implements GitCommand 046{ 047 @Override 048 protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet, 049 CommandParameters parameters ) 050 throws ScmException 051 { 052 Git git = null; 053 try 054 { 055 File basedir = fileSet.getBasedir(); 056 057 git = Git.open( basedir ); 058 059 ObjectId objectId = git.getRepository().resolve( "HEAD" ); 060 061 InfoItem infoItem = new InfoItem(); 062 infoItem.setRevision( StringUtils.trim( objectId.name() ) ); 063 infoItem.setURL( basedir.toPath().toUri().toASCIIString() ); 064 065 return new InfoScmResult( Collections.singletonList( infoItem ), 066 new ScmResult( "JGit.resolve(HEAD)", "", objectId.toString(), true ) ); 067 } 068 catch ( Exception e ) 069 { 070 throw new ScmException( "JGit resolve failure!", e ); 071 } 072 finally 073 { 074 JGitUtils.closeRepo( git ); 075 } 076 } 077}