1 package org.apache.maven.scm.provider.git.jgit.command.blame;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39
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 }