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