Skip to content

Commit

Permalink
Add current feature branch as the default choice for feature-finish - c…
Browse files Browse the repository at this point in the history
…loses #227
  • Loading branch information
aleksandr-m committed Apr 29, 2020
1 parent c3ccbee commit ab106da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,19 @@ private String removeQuotes(String str) {
return str;
}

/**
* Gets the current branch name.
*
* @return Current branch name.
* @throws MojoFailureException
* @throws CommandLineException
*/
protected String gitCurrentBranch() throws MojoFailureException, CommandLineException {
String name = executeGitCommandReturn("symbolic-ref", "-q", "--short", "HEAD");
name = StringUtils.strip(name);
return name;
}

/**
* Checks if local branch with given name exists.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

private String promptBranchName()
throws MojoFailureException, CommandLineException {
private String promptBranchName() throws MojoFailureException, CommandLineException {
// git for-each-ref --format='%(refname:short)' refs/heads/feature/*
final String featureBranches = gitFindBranches(
gitFlowConfig.getFeatureBranchPrefix(), false);
final String featureBranches = gitFindBranches(gitFlowConfig.getFeatureBranchPrefix(), false);

final String currentBranch = gitCurrentBranch();

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

List<String> numberedList = new ArrayList<String>();
String defaultChoice = null;
StringBuilder str = new StringBuilder("Feature branches:").append(LS);
for (int i = 0; i < branches.length; i++) {
str.append((i + 1) + ". " + branches[i] + LS);
numberedList.add(String.valueOf(i + 1));
if (branches[i].equals(currentBranch)) {
defaultChoice = String.valueOf(i + 1);
}
}
str.append("Choose feature branch to finish");

String featureNumber = null;
try {
while (StringUtils.isBlank(featureNumber)) {
featureNumber = prompter.prompt(str.toString(), numberedList);
featureNumber = prompter.prompt(str.toString(), numberedList, defaultChoice);
}
} catch (PrompterException e) {
throw new MojoFailureException("feature-finish", e);
Expand Down

0 comments on commit ab106da

Please sign in to comment.