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, 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 }