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