Skip to content

Commit

Permalink
feat: suggest previously used scopes
Browse files Browse the repository at this point in the history
The output of the 'git log' command is parsed to extract the various
change scopes.

Closes #7
  • Loading branch information
darrachequesne committed Nov 17, 2018
1 parent c6e5fcf commit 4c4f3e2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
63 changes: 63 additions & 0 deletions src/com/leroymerlin/commit/Command.java
Original file line number Diff line number Diff line change
@@ -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<String> output;

Result(int exitValue) {
this.exitValue = exitValue;
this.output = null;
}

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

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

List<String> 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<String> 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;
}
}

}
2 changes: 1 addition & 1 deletion src/com/leroymerlin/commit/CommitDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions src/com/leroymerlin/commit/CommitPanel.form
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
<text value="Scope of this change"/>
</properties>
</component>
<component id="cbe5c" class="javax.swing.JTextField" binding="changeScope">
<component id="549f7" class="javax.swing.JComboBox" binding="changeScope">
<constraints>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
<properties>
<editable value="true"/>
</properties>
</component>
<component id="19a59" class="javax.swing.JLabel">
<constraints>
Expand Down
15 changes: 11 additions & 4 deletions src/com/leroymerlin/commit/CommitPanel.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
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
*/
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() {
Expand All @@ -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(),
Expand Down

0 comments on commit 4c4f3e2

Please sign in to comment.