String prefixedReleaseName = gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.RELEASE.configKey()) + releaseName;
requireLocalBranchExists(prefixedReleaseName);
requireCleanWorkingTree();
MergeResult developResult = new MergeResult(null, null, new ObjectId[]{null, null}, MergeResult.MergeStatus.ALREADY_UP_TO_DATE, MergeStrategy.RESOLVE, null);
MergeResult masterResult = new MergeResult(null, null, new ObjectId[]{null, null}, MergeResult.MergeStatus.ALREADY_UP_TO_DATE, MergeStrategy.RESOLVE, null);
try
{
if (fetch)
{
RefSpec developSpec = new RefSpec("+" + Constants.R_HEADS + gfConfig.getDevelop() + ":" + Constants.R_REMOTES + "origin/" + gfConfig.getDevelop());
RefSpec masterSpec = new RefSpec("+" + Constants.R_HEADS + gfConfig.getMaster() + ":" + Constants.R_REMOTES + "origin/" + gfConfig.getMaster());
git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(masterSpec).call();
git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(developSpec).call();
git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).call();
}
if (GitHelper.remoteBranchExists(git, prefixedReleaseName, reporter))
{
requireLocalBranchNotBehindRemote(prefixedReleaseName);
}
if (GitHelper.remoteBranchExists(git, gfConfig.getMaster(), reporter))
{
requireLocalBranchNotBehindRemote(gfConfig.getMaster());
}
if (GitHelper.remoteBranchExists(git, gfConfig.getDevelop(), reporter))
{
requireLocalBranchNotBehindRemote(gfConfig.getDevelop());
}
Ref releaseBranch = GitHelper.getLocalBranch(git, prefixedReleaseName);
if(!noMerge)
{
/*
try to merge into master
in case a previous attempt to finish this release branch has failed,
but the merge into master was successful, we skip it now
*/
if (!GitHelper.isMergedInto(git, prefixedReleaseName, gfConfig.getMaster()))
{
git.checkout().setName(gfConfig.getMaster()).call();
reporter.infoText(getCommandName(), "merging '" + prefixedReleaseName + "' into '" + gfConfig.getMaster() + "'...");
if (squash)
{
reporter.infoText(getCommandName(), "squashing merge");
masterResult = git.merge().setSquash(true).include(releaseBranch).call();
if(masterResult.getMergeStatus().isSuccessful())
{
git.commit().setMessage(getScmMessagePrefix() + "squashing '" + prefixedReleaseName + "' into '" + gfConfig.getMaster() + "'").call();
}
}
else
{
masterResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(releaseBranch).call();
}
}
reporter.mergeResult(getCommandName(), masterResult);
if (!masterResult.getMergeStatus().isSuccessful())
{
reporter.errorText(getCommandName(), "merge into '" + gfConfig.getMaster() + "' was not successful! Aborting the release...");
if (masterResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING))
{
reporter.errorText(getCommandName(), "please resolve your merge conflicts and re-run " + getCommandName());
}
else
{
reporter.errorText(getCommandName(), "until JGit supports merge resets, please run 'git reset --merge' to get back to a clean state");
}
}
/*
try to merge into develop
in case a previous attempt to finish this release branch has failed,
but the merge into develop was successful, we skip it now
*/
if (!GitHelper.isMergedInto(git, prefixedReleaseName, gfConfig.getDevelop()))
{
reporter.infoText(getCommandName(), "merging '" + prefixedReleaseName + "' into '" + gfConfig.getDevelop() + "'...");
git.checkout().setName(gfConfig.getDevelop()).call();
if (squash)
{
reporter.infoText(getCommandName(), "squashing merge");
developResult = git.merge().setSquash(true).include(releaseBranch).call();
if(developResult.getMergeStatus().isSuccessful())
{
git.commit().setMessage(getScmMessagePrefix() + "squashing '" + prefixedReleaseName + "' into '" + gfConfig.getDevelop() + "'").call();
}
}
else
{
developResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(releaseBranch).call();
}
}
reporter.mergeResult(getCommandName(), developResult);
if (!developResult.getMergeStatus().isSuccessful())
{
reporter.errorText(getCommandName(), "merge into '" + gfConfig.getDevelop() + "' was not successful! Aborting the release...");
if (developResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING))
{
reporter.errorText(getCommandName(), "please resolve your merge conflicts and re-run " + getCommandName());
}
else
{
reporter.errorText(getCommandName(), "until JGit supports merge resets, please run 'git reset --merge' to get back to a clean state");
}
}
}
if (!noTag && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
{
/*
Change to the release branch to create the tag so that
'git describe' will continue to work.
See: https://github.com/nvie/gitflow/commit/aa93d2346f840e16a05c6546e1e22c1dddfbc997
*/
git.checkout().setName(releaseBranch.getName()).call();
/*
try to tag the release
in case a previous attempt to finish this release branch has failed,
but the tag was successful, we skip it now
*/
String tagName = gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.VERSIONTAG.configKey()) + releaseName;
if (!GitHelper.tagExists(git, tagName))
{
reporter.infoText(getCommandName(), "tagging release with name:" + tagName);
git.tag().setName(tagName).setMessage(getScmMessagePrefix() + message).call();
}
}
if (push && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
{
reporter.infoText(getCommandName(), "pushing changes to origin...");
//push to develop
reporter.infoText(getCommandName(), "pushing '" + gfConfig.getDevelop() + "'");
RefSpec developSpec = new RefSpec(gfConfig.getDevelop());
git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(developSpec).call();
//push to master
reporter.infoText(getCommandName(), "pushing '" + gfConfig.getMaster() + "'");
RefSpec masterSpec = new RefSpec(gfConfig.getMaster());
git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(masterSpec).call();
if (!noTag)
{
reporter.infoText(getCommandName(), "pushing tags");
git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setPushTags().call();
}
if (GitHelper.remoteBranchExists(git, prefixedReleaseName, reporter))
{
reporter.infoText(getCommandName(), "pushing release branch");
RefSpec branchSpec = new RefSpec(prefixedReleaseName);
git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(branchSpec).call();
}
}
if (!keepBranch && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
{
reporter.infoText(getCommandName(), "deleting local release branch");
git.checkout().setName(gfConfig.getDevelop()).call();
git.branchDelete().setForce(true).setBranchNames(prefixedReleaseName).call();