Skip to content

Commit

Permalink
feat: add support for parsing scopes in Windows
Browse files Browse the repository at this point in the history
Merged from #15

Closes #13
  • Loading branch information
darrachequesne committed Nov 1, 2020
1 parent 0a0ab98 commit 1397b50
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 65 deletions.
63 changes: 0 additions & 63 deletions src/com/leroymerlin/commit/Command.java

This file was deleted.

5 changes: 3 additions & 2 deletions src/com/leroymerlin/commit/CommitPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public class CommitPanel {
changeType.addItem(type);
}
File workingDirectory = VfsUtil.virtualToIoFile(project.getBaseDir());
Command.Result result = new Command(workingDirectory, "git log --all --format=%s | grep -Eo '^[a-z]+(\\(.*\\)):.*$' | sed 's/^.*(\\(.*\\)):.*$/\\1/' | sort -n | uniq").execute();
GitLogQuery.Result result = new GitLogQuery(workingDirectory).execute();
if (result.isSuccess()) {
result.getOutput().forEach(changeScope::addItem);
changeScope.addItem(""); // no value by default
result.getScopes().forEach(changeScope::addItem);
}
}

Expand Down
86 changes: 86 additions & 0 deletions src/com/leroymerlin/commit/GitLogQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.leroymerlin.commit;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;

class GitLogQuery {
private static final String GIT_LOG_COMMAND = "git log --all --format=%s";
private static final Pattern COMMIT_FIRST_LINE_FORMAT = Pattern.compile("^[a-z]+\\((.+)\\):.*");

private final File workingDirectory;

GitLogQuery(File workingDirectory) {
this.workingDirectory = workingDirectory;
}

static class Result {
static Result ERROR = new Result(-1);

private final int exitValue;
private final List<String> logs;

Result(int exitValue) {
this(exitValue, emptyList());
}

Result(int exitValue, List<String> logs) {
this.exitValue = exitValue;
this.logs = logs;
}

boolean isSuccess() {
return exitValue == 0;
}

public Set<String> getScopes() {
Set<String> scopes = new HashSet<>();

this.logs.forEach(s -> {
Matcher matcher = COMMIT_FIRST_LINE_FORMAT.matcher(s);
if (matcher.find()) {
scopes.add(matcher.group(1));
}
});

return scopes;
}
}

Result execute() {
try {
ProcessBuilder processBuilder;
String osName = System.getProperty("os.name");
if (osName.contains("Windows")) {
processBuilder = new ProcessBuilder("cmd", "/C", GIT_LOG_COMMAND);
} else {
processBuilder = new ProcessBuilder("sh", "-c", GIT_LOG_COMMAND);
}

Process process = processBuilder
.directory(workingDirectory)
.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

List<String> output = reader.lines().collect(toList());

process.waitFor(2, TimeUnit.SECONDS);
process.destroy();
process.waitFor();

return new Result(process.exitValue(), output);
} catch (Exception e) {
return Result.ERROR;
}
}

}
19 changes: 19 additions & 0 deletions tests/com/leroymerlin/commit/GitLogQueryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.leroymerlin.commit;

import org.junit.Ignore;
import org.junit.Test;

import java.io.File;

public class GitLogQueryTest {

@Test
@Ignore("manual testing")
public void testExecute() {
GitLogQuery.Result result = new GitLogQuery(new File("<absolute path>")).execute();

System.out.println(result.isSuccess());
System.out.println(result.getScopes());
}

}

0 comments on commit 1397b50

Please sign in to comment.