From 4102d4462c1d1cdda0dbc570b0ba03acc048b629 Mon Sep 17 00:00:00 2001 From: Aleksandr Mashchenko Date: Wed, 26 Oct 2016 00:07:42 +0300 Subject: [PATCH] better way to check if local branch exists --- .../plugin/gitflow/AbstractGitFlowMojo.java | 17 ++++++++++++++--- .../plugin/gitflow/GitFlowFeatureStartMojo.java | 4 ++-- .../plugin/gitflow/GitFlowHotfixStartMojo.java | 4 ++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java index cd314f97..0f7466ef 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java @@ -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 true if local branch exists, false + * 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; } /** diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java index 6dbae6e0..b2655b11 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java @@ -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."); } diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java index da705e6a..650ee63e 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java @@ -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."); }