View Javadoc
1   package org.apache.maven.scm.provider.git.jgit.command.blame;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.command.blame.AbstractBlameCommand;
25  import org.apache.maven.scm.command.blame.BlameLine;
26  import org.apache.maven.scm.command.blame.BlameScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.git.command.GitCommand;
29  import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
30  import org.eclipse.jgit.api.Git;
31  import org.eclipse.jgit.blame.BlameResult;
32  
33  import java.io.File;
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  /**
38   * @author Dominik Bartholdi (imod)
39   * @since 1.9
40   */
41  public class JGitBlameCommand
42      extends AbstractBlameCommand
43      implements GitCommand
44  {
45  
46      @Override
47      public BlameScmResult executeBlameCommand( ScmProviderRepository repo, ScmFileSet workingDirectory, String filename )
48          throws ScmException
49      {
50  
51          Git git = null;
52          File basedir = workingDirectory.getBasedir();
53          try
54          {
55              git = Git.open( basedir );
56              BlameResult blameResult = git.blame().setFilePath( filename ).call();
57  
58              List<BlameLine> lines = new ArrayList<BlameLine>();
59  
60              int i = 0;
61              while ( ( i = blameResult.computeNext() ) != -1 )
62              {
63                  lines.add( new BlameLine( blameResult.getSourceAuthor( i ).getWhen(),
64                                            blameResult.getSourceCommit( i ).getName(),
65                                            blameResult.getSourceAuthor( i ).getName(),
66                                            blameResult.getSourceCommitter( i ).getName() ) );
67              }
68  
69              return new BlameScmResult( "JGit blame", lines );
70          }
71          catch ( Exception e )
72          {
73              throw new ScmException( "JGit blame failure!", e );
74          }
75          finally
76          {
77              JGitUtils.closeRepo( git );
78          }
79      }
80  
81  }