From ca66fdec3575256ca98098524bbfbe3890a1dc47 Mon Sep 17 00:00:00 2001 From: Aleksandr Mashchenko Date: Thu, 28 Nov 2019 22:07:21 +0200 Subject: [PATCH] Fetch release branch from remote on release-finish goal - #196 --- .../plugin/gitflow/AbstractGitFlowMojo.java | 58 +++++++++++++++++-- .../gitflow/GitFlowReleaseFinishMojo.java | 27 +++++++-- 2 files changed, 75 insertions(+), 10 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 ac6fb255..564ccfa4 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java @@ -434,7 +434,25 @@ private void gitSetConfig(final String name, String value) * @throws MojoFailureException * @throws CommandLineException */ - protected String gitFindBranches(final String branchName, + protected String gitFindBranches(final String branchName, final boolean firstMatch) + throws MojoFailureException, CommandLineException { + return gitFindBranches("refs/heads/", branchName, firstMatch); + } + + /** + * Executes git for-each-ref with refname:short format. + * + * @param refs + * Refs to search. + * @param branchName + * Branch name to find. + * @param firstMatch + * Return first match. + * @return Branch names which matches {refs}{branchName}*. + * @throws MojoFailureException + * @throws CommandLineException + */ + private String gitFindBranches(final String refs, final String branchName, final boolean firstMatch) throws MojoFailureException, CommandLineException { String wildcard = "*"; @@ -445,12 +463,10 @@ protected String gitFindBranches(final String branchName, String branches; if (firstMatch) { branches = executeGitCommandReturn("for-each-ref", "--count=1", - "--format=\"%(refname:short)\"", "refs/heads/" + branchName - + wildcard); + "--format=\"%(refname:short)\"", refs + branchName + wildcard); } else { branches = executeGitCommandReturn("for-each-ref", - "--format=\"%(refname:short)\"", "refs/heads/" + branchName - + wildcard); + "--format=\"%(refname:short)\"", refs + branchName + wildcard); } // on *nix systems return values from git for-each-ref are wrapped in @@ -850,9 +866,41 @@ protected void gitFetchRemoteAndCompare(final String branchName) } } + /** + * Executes git fetch and git for-each-ref with refname:short + * format. Searches refs/remotes/{remoteName}/. + * + * @param remoteName + * Name of the remote. + * @param branchName + * Branch name to find. + * @param firstMatch + * Return first match. + * @return Branch names which matches refs/heads/{branchName}*. + * @throws MojoFailureException + * @throws CommandLineException + */ + protected String gitFetchAndFindRemoteBranches(final String remoteName, final String branchName, + final boolean firstMatch) throws MojoFailureException, CommandLineException { + gitFetchRemote(); + return gitFindBranches("refs/remotes/" + remoteName + "/", branchName, firstMatch); + } + /** * Executes git fetch. * + * @return true if git fetch returned success exit code, + * false otherwise. + * @throws MojoFailureException + * @throws CommandLineException + */ + private boolean gitFetchRemote() throws MojoFailureException, CommandLineException { + return gitFetchRemote(""); + } + + /** + * Executes git fetch with specific branch. + * * @param branchName * Branch name to fetch. * @return true if git fetch returned success exit code, diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseFinishMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseFinishMojo.java index e645a565..73a9cd8f 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseFinishMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseFinishMojo.java @@ -169,13 +169,30 @@ public void execute() throws MojoExecutionException, MojoFailureException { checkUncommittedChanges(); // git for-each-ref --format='%(refname:short)' refs/heads/release/* - final String releaseBranch = gitFindBranches( - gitFlowConfig.getReleaseBranchPrefix(), false).trim(); + String releaseBranch = gitFindBranches(gitFlowConfig.getReleaseBranchPrefix(), false).trim(); if (StringUtils.isBlank(releaseBranch)) { - throw new MojoFailureException("There is no release branch."); - } else if (StringUtils.countMatches(releaseBranch, - gitFlowConfig.getReleaseBranchPrefix()) > 1) { + if (fetchRemote) { + releaseBranch = gitFetchAndFindRemoteBranches(gitFlowConfig.getOrigin(), + gitFlowConfig.getReleaseBranchPrefix(), false).trim(); + if (StringUtils.isBlank(releaseBranch)) { + throw new MojoFailureException("There is no remote or local release branch."); + } + + // remove remote name with slash from branch name + releaseBranch = releaseBranch.substring(gitFlowConfig.getOrigin().length() + 1); + + if (StringUtils.countMatches(releaseBranch, gitFlowConfig.getReleaseBranchPrefix()) > 1) { + throw new MojoFailureException( + "More than one remote release branch exists. Cannot finish release."); + } + + gitCreateAndCheckout(releaseBranch, gitFlowConfig.getOrigin() + "/" + releaseBranch); + } else { + throw new MojoFailureException("There is no release branch."); + } + } + if (StringUtils.countMatches(releaseBranch, gitFlowConfig.getReleaseBranchPrefix()) > 1) { throw new MojoFailureException( "More than one release branch exists. Cannot finish release."); }