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,
48                                                 String filename )
49          throws ScmException
50      {
51  
52          Git git = null;
53          File basedir = workingDirectory.getBasedir();
54          try
55          {
56              git = JGitUtils.openRepo( basedir );
57              BlameResult blameResult = git.blame().setFilePath( filename ).call();
58  
59              List<BlameLine> lines = new ArrayList<BlameLine>();
60  
61              int i = 0;
62              while ( ( i = blameResult.computeNext() ) != -1 )
63              {
64                  lines.add( new BlameLine( blameResult.getSourceAuthor( i ).getWhen(),
65                                            blameResult.getSourceCommit( i ).getName(),
66                                            blameResult.getSourceAuthor( i ).getName(),
67                                            blameResult.getSourceCommitter( i ).getName() ) );
68              }
69  
70              return new BlameScmResult( "JGit blame", lines );
71          }
72          catch ( Exception e )
73          {
74              throw new ScmException( "JGit blame failure!", e );
75          }
76          finally
77          {
78              JGitUtils.closeRepo( git );
79          }
80      }
81  
82  }