1 package org.apache.maven.scm.provider.git.gitexe.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.command.blame.BlameLine;
23 import org.apache.maven.scm.log.ScmLogger;
24 import org.apache.maven.scm.util.AbstractConsumer;
25
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.*;
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class GitBlameConsumer
43 extends AbstractConsumer
44 {
45 private final static String GIT_COMMITTER_PREFIX = "committer";
46 private final static String GIT_COMMITTER = GIT_COMMITTER_PREFIX + " ";
47 private final static String GIT_COMMITTER_TIME = GIT_COMMITTER_PREFIX + "-time ";
48 private final static String GIT_AUTHOR = "author ";
49
50
51 private List<BlameLine> lines = new ArrayList<BlameLine>();
52
53
54
55
56
57
58
59
60
61 private Map<String, BlameLine> commitInfo = new HashMap<String, BlameLine>();
62
63 private boolean expectRevisionLine = true;
64
65 private String revision = null;
66 private String author = null;
67 private String committer = null;
68 private Date time = null;
69
70 public GitBlameConsumer( ScmLogger logger )
71 {
72 super( logger );
73 }
74
75 public void consumeLine( String line )
76 {
77 if ( line == null )
78 {
79 return;
80 }
81
82 if (expectRevisionLine)
83 {
84
85 String parts[] = line.split( "\\s", 4 );
86
87 if ( parts.length >= 1)
88 {
89 revision = parts[0];
90
91 BlameLine oldLine = commitInfo.get( revision );
92
93 if ( oldLine != null )
94 {
95
96 author = oldLine.getAuthor();
97 committer = oldLine.getCommitter();
98 time = oldLine.getDate();
99 }
100
101 expectRevisionLine = false;
102 }
103 }
104 else
105 {
106 if ( line.startsWith( GIT_AUTHOR ) )
107 {
108 author = line.substring( GIT_AUTHOR.length() );
109 return;
110 }
111
112 if ( line.startsWith( GIT_COMMITTER ) )
113 {
114 committer = line.substring( GIT_COMMITTER.length() );
115 return;
116 }
117
118 if ( line.startsWith( GIT_COMMITTER_TIME ) )
119 {
120 String timeStr = line.substring( GIT_COMMITTER_TIME.length() );
121 time = new Date( Long.parseLong( timeStr ) * 1000L );
122 return;
123 }
124
125
126 if ( line.startsWith( "\t" ) )
127 {
128
129
130 BlameLine blameLine = new BlameLine( time, revision, author, committer );
131 getLines().add( blameLine );
132
133
134 commitInfo.put( revision, blameLine );
135
136 if ( getLogger().isDebugEnabled() )
137 {
138 DateFormat df = SimpleDateFormat.getDateTimeInstance();
139 getLogger().debug( author + " " + df.format( time ) );
140 }
141
142 expectRevisionLine = true;
143 }
144
145 }
146 }
147
148 public List<BlameLine> getLines()
149 {
150 return lines;
151 }
152 }