Skip to content

Commit

Permalink
Fetch release branch from remote on release-finish goal - #196
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-m committed Nov 28, 2019
1 parent f5276b1 commit ca66fde
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>refname:short</code> format.
*
* @param refs
* Refs to search.
* @param branchName
* Branch name to find.
* @param firstMatch
* Return first match.
* @return Branch names which matches <code>{refs}{branchName}*</code>.
* @throws MojoFailureException
* @throws CommandLineException
*/
private String gitFindBranches(final String refs, final String branchName,
final boolean firstMatch) throws MojoFailureException,
CommandLineException {
String wildcard = "*";
Expand All @@ -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
Expand Down Expand Up @@ -850,9 +866,41 @@ protected void gitFetchRemoteAndCompare(final String branchName)
}
}

/**
* Executes git fetch and git for-each-ref with <code>refname:short</code>
* format. Searches <code>refs/remotes/{remoteName}/</code>.
*
* @param remoteName
* Name of the remote.
* @param branchName
* Branch name to find.
* @param firstMatch
* Return first match.
* @return Branch names which matches <code>refs/heads/{branchName}*</code>.
* @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 <code>true</code> if git fetch returned success exit code,
* <code>false</code> 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 <code>true</code> if git fetch returned success exit code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down

0 comments on commit ca66fde

Please sign in to comment.