Skip to content

Commit

Permalink
better way to check if local branch exists
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-m committed Oct 25, 2016
1 parent 1f841ea commit 4102d44
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,21 @@ protected String gitFindBranches(final String branchName,
return branches;
}

protected String gitFindBranch(final String branchName)
/**
* Checks if local branch with given name exists.
*
* @param branchName
* Name of the branch to check.
* @return <code>true</code> if local branch exists, <code>false</code>
* otherwise.
* @throws MojoFailureException
* @throws CommandLineException
*/
protected boolean gitCheckBranchExists(final String branchName)
throws MojoFailureException, CommandLineException {
return executeGitCommandReturn("for-each-ref", "refs/heads/"
+ branchName);
CommandResult commandResult = executeGitCommandExitCode("show-ref",
"--verify", "--quiet", "refs/heads/" + branchName);
return commandResult.getExitCode() == SUCCESS_EXIT_CODE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
featureName = StringUtils.deleteWhitespace(featureName);

// git for-each-ref refs/heads/feature/...
final String featureBranch = gitFindBranch(gitFlowConfig
final boolean featureBranchExists = gitCheckBranchExists(gitFlowConfig
.getFeatureBranchPrefix() + featureName);

if (StringUtils.isNotBlank(featureBranch)) {
if (featureBranchExists) {
throw new MojoFailureException(
"Feature branch with that name already exists. Cannot start feature.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

// git for-each-ref refs/heads/hotfix/...
final String hotfixBranch = gitFindBranch(gitFlowConfig
final boolean hotfixBranchExists = gitCheckBranchExists(gitFlowConfig
.getHotfixBranchPrefix() + version);

if (StringUtils.isNotBlank(hotfixBranch)) {
if (hotfixBranchExists) {
throw new MojoFailureException(
"Hotfix branch with that name already exists. Cannot start hotfix.");
}
Expand Down

0 comments on commit 4102d44

Please sign in to comment.