Skip to content

Commit

Permalink
gitCommits2Log add user filter
Browse files Browse the repository at this point in the history
  • Loading branch information
cofcool committed Mar 21, 2023
1 parent c6fa142 commit 46bf7e1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.cofcool</groupId>
<artifactId>my-toolbox</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.7</version>

<name>my-toolbox</name>
<url>https://github.com/cofcool/my-toolbox</url>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/cofcool/toolbox/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public interface Tool {
Expand All @@ -31,6 +32,14 @@ public void ifPresent(Consumer<Arg> consumer) {
}
}

public boolean test(Predicate<String> predicate) {
if (val != null) {
return predicate.test(val);
}

return true;
}

public static Arg of(String key, String val) {
return new Arg(key, val, null, false, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/**
* git-log PRETTY FORMATS
* <code>git log --format="%d;%h;%s"</code>
* <code>git log --format="%an;%d;%h;%s"</code>
*/
public class GitCommitsToChangelog implements Tool {

Expand All @@ -30,7 +30,7 @@ public ToolName name() {
return ToolName.gitCommits2Log;
}

// (HEAD -> master, origin/master, origin/HEAD);117826a;Converts: now, replace
// (HEAD -> master, origin/master, origin/HEAD);117826a;Converts: now, replace;CofCool
//;1788569;add debug mode
//;1ee4312;update doc
//;b676feb;update doc
Expand All @@ -50,7 +50,7 @@ public void run(Args args) throws Exception {
} else {
String path = args.readArg("path").val();

String command = "git log --format=%d;%h;%s";
String command = "git log --format=%an;%d;%h;%s";
getLogger().info("Run command: " + command);
Process process = Runtime
.getRuntime()
Expand All @@ -76,14 +76,15 @@ public void run(Args args) throws Exception {
var currentTag = new AtomicReference<>(LATEST);
Arrays.stream(commitLog.split("\n"))
.map(a -> a.split(";"))
.filter(a -> a.length > 2)
.map(a -> new Commit(a[0], a[1], String.join(";", Arrays.copyOfRange(a, 2, a.length))))
.filter(a -> a.length > 3)
.map(a -> new Commit(a[0], a[1], a[2], String.join(";", Arrays.copyOfRange(a, 3, a.length))))
.map(c -> {
c.tag().filter(t -> !noTag).ifPresent(currentTag::set);
return new Commit(currentTag.get(), c.hash, c.message);
return new Commit(c.username, currentTag.get(), c.hash, c.message);
})
.sorted(Comparator.comparing(Commit::ref).reversed())
.filter(c -> full || !c.ref.equalsIgnoreCase(LATEST))
.filter(c -> args.readArg("user").test(u -> u.equalsIgnoreCase(c.username())))
.collect(Collectors.groupingBy(Commit::ref, LinkedHashMap::new, Collectors.toList()))
.entrySet()
.stream()
Expand Down Expand Up @@ -117,6 +118,7 @@ public Args config() {
.arg(new Arg("path", null, "project directory, must set this or log", false, "./demo/"))
.arg(new Arg("log", null, "Git commit log fil", false, "./git-commit-log.txt"))
.arg(new Arg("out", "./target/changelog.md", "generate file output path", false, null))
.arg(new Arg("user", null, "username filter", false, "cofcool"))
.arg(new Arg("tag", null, "read commit log to the tag", false, "1.0.0"))
.arg(new Arg("no-tag", "false", "if true, read all commit log and write into file", false, null))
.arg(new Arg("full", "false", "if true, read all tags and commit log, then write into file", false, null))
Expand Down Expand Up @@ -144,9 +146,10 @@ public Optional<String> find(String val) {
}

private record Commit(
String ref,
String hash,
String message
String username,
String ref,
String hash,
String message
) {
public Optional<String> tag() {
if (ref.contains("tag")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ void runWithNoTag() throws Exception {
);
}

@Test
void runWithUser() throws Exception {
new GitCommitsToChangelog().run(args.arg("log", Utils.getTestResourcePath("/gitCommitsToChangelogStyleTest.txt"))
.arg("out", "./target/changelog-runWithUser.md")
.arg("full", "true")
.arg("user", "ZhangSan")
);
}

@Test
void style() {
Assertions.assertTrue(Style.angular.find("fix(git): test").isPresent());
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/gitCommitsToChangelogStyleTest.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(HEAD -> master, tag: 1.0.4, origin/master, origin/HEAD);117826a;feat(core): Converts: now, replace
;1788569;feat(core): add debug mode
;1ee4312;docs(common): update doc
;b676feb;fix(core): print error
zhangsan; (HEAD -> master, tag: 1.0.4, origin/master, origin/HEAD);117826a;feat(core): Converts: now, replace
cofcool;1788569;feat(core): add debug mode
lisi;1ee4312;docs(common): update doc
lisa;b676feb;fix(core): print error
32 changes: 16 additions & 16 deletions src/test/resources/gitCommitsToChangelogTest.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
(HEAD -> master, tag: 1.0.4, origin/master, origin/HEAD);117826a;Converts: now, replace
;1788569;add debug mode
;1ee4312;update doc
;b676feb;update doc
(tag: 1.0.1);a9d3e04;add Converts
(tag: 1.0.0);1b67da0;Update maven.yml
;6474b63;Update maven.yml
;97a383e;Update maven.yml
;52ba1b8;Update maven.yml
;2e71e27;Update maven.yml
;61bfdff;Update maven.yml
;6e26d48;github action
;c92e596;fix action
;24f7178;github action
;8f989ce;fix readme
;dc723bc;init
; (HEAD -> master, tag: 1.0.4, origin/master, origin/HEAD);117826a;Converts: now, replace
;;1788569;add debug mode
;;1ee4312;update doc
;;b676feb;update doc
; (tag: 1.0.1);a9d3e04;add Converts
; (tag: 1.0.0);1b67da0;Update maven.yml
;;6474b63;Update maven.yml
;;97a383e;Update maven.yml
;;52ba1b8;Update maven.yml
;;2e71e27;Update maven.yml
;;61bfdff;Update maven.yml
;;6e26d48;github action
;;c92e596;fix action
;;24f7178;github action
;;8f989ce;fix readme
;;dc723bc;init

0 comments on commit 46bf7e1

Please sign in to comment.