From 4c4f3e23a77de10991a48155fcdd1e5677e55a56 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Sat, 17 Nov 2018 07:56:10 +0100 Subject: [PATCH] feat: suggest previously used scopes The output of the 'git log' command is parsed to extract the various change scopes. Closes #7 --- src/com/leroymerlin/commit/Command.java | 63 ++++++++++++++++++++ src/com/leroymerlin/commit/CommitDialog.java | 2 +- src/com/leroymerlin/commit/CommitPanel.form | 6 +- src/com/leroymerlin/commit/CommitPanel.java | 15 +++-- 4 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 src/com/leroymerlin/commit/Command.java diff --git a/src/com/leroymerlin/commit/Command.java b/src/com/leroymerlin/commit/Command.java new file mode 100644 index 0000000..82377e9 --- /dev/null +++ b/src/com/leroymerlin/commit/Command.java @@ -0,0 +1,63 @@ +package com.leroymerlin.commit; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +class Command { + private final File workingDirectory; + private final String command; + + Command(File workingDirectory, String command) { + this.workingDirectory = workingDirectory; + this.command = command; + } + + static class Result { + static Result ERROR = new Result(-1); + + private final int exitValue; + private final List output; + + Result(int exitValue) { + this.exitValue = exitValue; + this.output = null; + } + + Result(int exitValue, List output) { + this.exitValue = exitValue; + this.output = output; + } + + boolean isSuccess() { + return exitValue == 0; + } + + List getOutput() { + return output; + } + } + + Result execute() { + try { + Process process = new ProcessBuilder("/bin/sh", "-c", this.command) + .directory(workingDirectory) + .start(); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + + List output = reader.lines().collect(Collectors.toList()); + + process.waitFor(2, TimeUnit.SECONDS); + process.destroy(); + process.waitFor(); + + return new Result(process.exitValue(), output); + } catch (Exception e) { + return Result.ERROR; + } + } + +} \ No newline at end of file diff --git a/src/com/leroymerlin/commit/CommitDialog.java b/src/com/leroymerlin/commit/CommitDialog.java index e01c784..8266f59 100644 --- a/src/com/leroymerlin/commit/CommitDialog.java +++ b/src/com/leroymerlin/commit/CommitDialog.java @@ -15,7 +15,7 @@ public class CommitDialog extends DialogWrapper { CommitDialog(@Nullable Project project) { super(project); - panel = new CommitPanel(this); + panel = new CommitPanel(project); setTitle("Commit"); setOKButtonText("OK"); init(); diff --git a/src/com/leroymerlin/commit/CommitPanel.form b/src/com/leroymerlin/commit/CommitPanel.form index a62bc25..da4b5ab 100644 --- a/src/com/leroymerlin/commit/CommitPanel.form +++ b/src/com/leroymerlin/commit/CommitPanel.form @@ -29,13 +29,15 @@ - + - + + + diff --git a/src/com/leroymerlin/commit/CommitPanel.java b/src/com/leroymerlin/commit/CommitPanel.java index db46542..840d80d 100644 --- a/src/com/leroymerlin/commit/CommitPanel.java +++ b/src/com/leroymerlin/commit/CommitPanel.java @@ -1,8 +1,10 @@ package com.leroymerlin.commit; -import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VfsUtil; import javax.swing.*; +import java.io.File; /** * @author Damien Arrachequesne @@ -10,16 +12,21 @@ public class CommitPanel { private JPanel mainPanel; private JComboBox changeType; - private JTextField changeScope; + private JComboBox changeScope; private JTextField shortDescription; private JTextArea longDescription; private JTextField closedIssues; private JTextField breakingChanges; - CommitPanel(DialogWrapper dialog) { + CommitPanel(Project project) { for (ChangeType type : ChangeType.values()) { 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(); + if (result.isSuccess()) { + result.getOutput().forEach(changeScope::addItem); + } } JPanel getMainPanel() { @@ -29,7 +36,7 @@ JPanel getMainPanel() { CommitMessage getCommitMessage() { return new CommitMessage( (ChangeType) changeType.getSelectedItem(), - changeScope.getText().trim(), + (String) changeScope.getSelectedItem(), shortDescription.getText().trim(), longDescription.getText().trim(), closedIssues.getText().trim(),