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.info;
20
21 import java.nio.file.Path;
22 import java.time.format.DateTimeFormatter;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.apache.maven.scm.command.info.InfoItem;
26 import org.apache.maven.scm.util.AbstractConsumer;
27 import org.codehaus.plexus.util.cli.Arg;
28 import org.codehaus.plexus.util.cli.Commandline;
29
30
31
32
33
34
35
36
37 public class GitInfoConsumer extends AbstractConsumer {
38
39 private final InfoItem infoItem;
40 private final int revisionLength;
41
42 public GitInfoConsumer(Path path, int revisionLength) {
43 infoItem = new InfoItem();
44 infoItem.setPath(path.toString());
45 infoItem.setURL(path.toUri().toASCIIString());
46 this.revisionLength = revisionLength;
47 }
48
49 enum LineParts {
50 HASH(0),
51 AUTHOR_NAME(3),
52 AUTHOR_EMAIL(2),
53 AUTHOR_LAST_MODIFIED(1);
54
55 private final int index;
56
57 LineParts(int index) {
58 this.index = index;
59 }
60
61 public int getIndex() {
62 return index;
63 }
64 }
65
66
67
68
69
70 public void consumeLine(String line) {
71 if (logger.isDebugEnabled()) {
72 logger.debug("consume line {}", line);
73 }
74
75
76 String[] parts = line.split("\\s", 4);
77 if (parts.length != 4) {
78 throw new IllegalArgumentException(
79 "Unexpected line: expecting 4 tokens separated by whitespace but got " + line);
80 }
81 infoItem.setLastChangedAuthor(
82 parts[LineParts.AUTHOR_NAME.getIndex()] + " <" + parts[LineParts.AUTHOR_EMAIL.getIndex()] + ">");
83 String revision = parts[LineParts.HASH.getIndex()];
84 if (revisionLength > -1) {
85
86 revision = StringUtils.truncate(revision, Integer.max(4, revisionLength));
87 }
88 infoItem.setRevision(revision);
89 infoItem.setLastChangedDateTime(
90 DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(parts[LineParts.AUTHOR_LAST_MODIFIED.getIndex()]));
91 }
92
93 public InfoItem getInfoItem() {
94 return infoItem;
95 }
96
97
98
99
100
101
102 public static Arg getFormatArgument() {
103 Commandline.Argument arg = new Commandline.Argument();
104 arg.setValue("--format=format:%H %aI %aE %aN");
105 return arg;
106 }
107 }