001package org.apache.maven.scm.provider.git.jgit.command.blame;
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.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.command.blame.AbstractBlameCommand;
025import org.apache.maven.scm.command.blame.BlameLine;
026import org.apache.maven.scm.command.blame.BlameScmResult;
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.eclipse.jgit.api.Git;
031import org.eclipse.jgit.blame.BlameResult;
032
033import java.io.File;
034import java.util.ArrayList;
035import java.util.List;
036
037/**
038 * @author Dominik Bartholdi (imod)
039 * @since 1.9
040 */
041public class JGitBlameCommand
042    extends AbstractBlameCommand
043    implements GitCommand
044{
045
046    @Override
047    public BlameScmResult executeBlameCommand( ScmProviderRepository repo, ScmFileSet workingDirectory, String filename )
048        throws ScmException
049    {
050
051        Git git = null;
052        File basedir = workingDirectory.getBasedir();
053        try
054        {
055            git = Git.open( basedir );
056            BlameResult blameResult = git.blame().setFilePath( filename ).call();
057
058            List<BlameLine> lines = new ArrayList<BlameLine>();
059
060            int i = 0;
061            while ( ( i = blameResult.computeNext() ) != -1 )
062            {
063                lines.add( new BlameLine( blameResult.getSourceAuthor( i ).getWhen(),
064                                          blameResult.getSourceCommit( i ).getName(),
065                                          blameResult.getSourceAuthor( i ).getName(),
066                                          blameResult.getSourceCommitter( i ).getName() ) );
067            }
068
069            return new BlameScmResult( "JGit blame", lines );
070        }
071        catch ( Exception e )
072        {
073            throw new ScmException( "JGit blame failure!", e );
074        }
075        finally
076        {
077            JGitUtils.closeRepo( git );
078        }
079    }
080
081}