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, 048 String filename ) 049 throws ScmException 050 { 051 052 Git git = null; 053 File basedir = workingDirectory.getBasedir(); 054 try 055 { 056 git = JGitUtils.openRepo( basedir ); 057 BlameResult blameResult = git.blame().setFilePath( filename ).call(); 058 059 List<BlameLine> lines = new ArrayList<BlameLine>(); 060 061 int i = 0; 062 while ( ( i = blameResult.computeNext() ) != -1 ) 063 { 064 lines.add( new BlameLine( blameResult.getSourceAuthor( i ).getWhen(), 065 blameResult.getSourceCommit( i ).getName(), 066 blameResult.getSourceAuthor( i ).getName(), 067 blameResult.getSourceCommitter( i ).getName() ) ); 068 } 069 070 return new BlameScmResult( "JGit blame", lines ); 071 } 072 catch ( Exception e ) 073 { 074 throw new ScmException( "JGit blame failure!", e ); 075 } 076 finally 077 { 078 JGitUtils.closeRepo( git ); 079 } 080 } 081 082}