|
18 | 18 | */
|
19 | 19 | package org.apache.maven.scm.provider.git.gitexe.command.info;
|
20 | 20 |
|
21 |
| -import java.util.ArrayList; |
22 |
| -import java.util.List; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.time.format.DateTimeFormatter; |
23 | 23 |
|
24 | 24 | import org.apache.commons.lang3.StringUtils;
|
25 |
| -import org.apache.maven.scm.ScmFileSet; |
26 | 25 | import org.apache.maven.scm.command.info.InfoItem;
|
27 | 26 | import org.apache.maven.scm.util.AbstractConsumer;
|
| 27 | +import org.codehaus.plexus.util.cli.Arg; |
| 28 | +import org.codehaus.plexus.util.cli.Commandline; |
28 | 29 |
|
29 | 30 | /**
|
| 31 | + * Parses output of {@code git log} with a particular format and populates a {@link InfoItem}. |
| 32 | + * |
30 | 33 | * @author Olivier Lamy
|
31 | 34 | * @since 1.5
|
| 35 | + * @see <a href="https://git-scm.com/docs/git-log#_pretty_formats">Pretty Formats</a> |
32 | 36 | */
|
33 | 37 | public class GitInfoConsumer extends AbstractConsumer {
|
34 | 38 |
|
35 |
| - // $ git show |
36 |
| - // commit cd3c0dfacb65955e6fbb35c56cc5b1bf8ce4f767 |
| 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); |
37 | 54 |
|
38 |
| - private final List<InfoItem> infoItems = new ArrayList<>(1); |
| 55 | + private final int index; |
39 | 56 |
|
40 |
| - private final ScmFileSet scmFileSet; |
| 57 | + LineParts(int index) { |
| 58 | + this.index = index; |
| 59 | + } |
41 | 60 |
|
42 |
| - public GitInfoConsumer(ScmFileSet scmFileSet) { |
43 |
| - this.scmFileSet = scmFileSet; |
| 61 | + public int getIndex() { |
| 62 | + return index; |
| 63 | + } |
44 | 64 | }
|
45 | 65 |
|
46 | 66 | /**
|
| 67 | + * @param line the line which is supposed to have the format as specified by {@link #getFormatArgument()}. |
47 | 68 | * @see org.codehaus.plexus.util.cli.StreamConsumer#consumeLine(java.lang.String)
|
48 | 69 | */
|
49 | 70 | public void consumeLine(String line) {
|
50 | 71 | if (logger.isDebugEnabled()) {
|
51 |
| - logger.debug("consume line " + line); |
| 72 | + logger.debug("consume line {}", line); |
52 | 73 | }
|
53 | 74 |
|
54 |
| - if (infoItems.isEmpty()) { |
55 |
| - if (!(line == null || line.isEmpty())) { |
56 |
| - InfoItem infoItem = new InfoItem(); |
57 |
| - infoItem.setRevision(StringUtils.trim(line)); |
58 |
| - infoItem.setURL(scmFileSet.getBasedir().toPath().toUri().toASCIIString()); |
59 |
| - infoItems.add(infoItem); |
60 |
| - } |
| 75 | + // name must be last token as it may contain separators |
| 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); |
61 | 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 | + // do not truncate below 4 characters |
| 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; |
62 | 95 | }
|
63 | 96 |
|
64 |
| - public List<InfoItem> getInfoItems() { |
65 |
| - return infoItems; |
| 97 | + /** |
| 98 | + * The format argument to use with {@code git log} |
| 99 | + * @return the format argument to use {@code git log} command |
| 100 | + * @see <a href="https://git-scm.com/docs/git-log#_pretty_formats">Pretty Formats</a> |
| 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; |
66 | 106 | }
|
67 | 107 | }
|
0 commit comments