Skip to content

Commit ab106da

Browse files
committed
Add current feature branch as the default choice for feature-finish - closes #227
1 parent c3ccbee commit ab106da

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,19 @@ private String removeQuotes(String str) {
523523
return str;
524524
}
525525

526+
/**
527+
* Gets the current branch name.
528+
*
529+
* @return Current branch name.
530+
* @throws MojoFailureException
531+
* @throws CommandLineException
532+
*/
533+
protected String gitCurrentBranch() throws MojoFailureException, CommandLineException {
534+
String name = executeGitCommandReturn("symbolic-ref", "-q", "--short", "HEAD");
535+
name = StringUtils.strip(name);
536+
return name;
537+
}
538+
526539
/**
527540
* Checks if local branch with given name exists.
528541
*

src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
203203
}
204204
}
205205

206-
private String promptBranchName()
207-
throws MojoFailureException, CommandLineException {
206+
private String promptBranchName() throws MojoFailureException, CommandLineException {
208207
// git for-each-ref --format='%(refname:short)' refs/heads/feature/*
209-
final String featureBranches = gitFindBranches(
210-
gitFlowConfig.getFeatureBranchPrefix(), false);
208+
final String featureBranches = gitFindBranches(gitFlowConfig.getFeatureBranchPrefix(), false);
209+
210+
final String currentBranch = gitCurrentBranch();
211211

212212
if (StringUtils.isBlank(featureBranches)) {
213213
throw new MojoFailureException("There are no feature branches.");
@@ -216,17 +216,21 @@ private String promptBranchName()
216216
final String[] branches = featureBranches.split("\\r?\\n");
217217

218218
List<String> numberedList = new ArrayList<String>();
219+
String defaultChoice = null;
219220
StringBuilder str = new StringBuilder("Feature branches:").append(LS);
220221
for (int i = 0; i < branches.length; i++) {
221222
str.append((i + 1) + ". " + branches[i] + LS);
222223
numberedList.add(String.valueOf(i + 1));
224+
if (branches[i].equals(currentBranch)) {
225+
defaultChoice = String.valueOf(i + 1);
226+
}
223227
}
224228
str.append("Choose feature branch to finish");
225229

226230
String featureNumber = null;
227231
try {
228232
while (StringUtils.isBlank(featureNumber)) {
229-
featureNumber = prompter.prompt(str.toString(), numberedList);
233+
featureNumber = prompter.prompt(str.toString(), numberedList, defaultChoice);
230234
}
231235
} catch (PrompterException e) {
232236
throw new MojoFailureException("feature-finish", e);

0 commit comments

Comments
 (0)