From d0d17eee76686176543a4cfe3d26cd9a69469fb8 Mon Sep 17 00:00:00 2001 From: Aleksandr Mashchenko Date: Sat, 24 Sep 2016 18:27:29 +0300 Subject: [PATCH] Remote interaction (git fetch, git push) --- .../plugin/gitflow/AbstractGitFlowMojo.java | 91 ++++++++++++++++++- .../maven/plugin/gitflow/GitFlowConfig.java | 18 ++++ .../gitflow/GitFlowFeatureFinishMojo.java | 9 ++ .../gitflow/GitFlowFeatureStartMojo.java | 5 + .../gitflow/GitFlowHotfixFinishMojo.java | 15 +++ .../gitflow/GitFlowHotfixStartMojo.java | 5 + .../gitflow/GitFlowReleaseFinishMojo.java | 11 +++ .../plugin/gitflow/GitFlowReleaseMojo.java | 11 +++ .../gitflow/GitFlowReleaseStartMojo.java | 5 + 9 files changed, 169 insertions(+), 1 deletion(-) 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 a6016be0..c9327b7d 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java @@ -41,7 +41,6 @@ * */ public abstract class AbstractGitFlowMojo extends AbstractMojo { - /** A full name of the versions-maven-plugin set goal. */ private static final String VERSIONS_MAVEN_PLUGIN_SET_GOAL = "org.codehaus.mojo:versions-maven-plugin:2.1:set"; /** Name of the tycho-versions-plugin set-version goal. */ @@ -94,6 +93,22 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { @Parameter(property = "allowSnapshots", defaultValue = "false") protected boolean allowSnapshots = false; + /** + * Whether to fetch remote branch and compare it with the local one. + * + * @since 1.3.0 + */ + @Parameter(property = "fetchRemote", defaultValue = "true") + protected boolean fetchRemote; + + /** + * Whether to push to the remote. + * + * @since 1.3.0 + */ + @Parameter(property = "pushRemote", defaultValue = "true") + protected boolean pushRemote; + /** * Whether to print commands output into the console. * @@ -265,6 +280,8 @@ protected void initGitFlowConfig() throws MojoFailureException, gitFlowConfig.getSupportBranchPrefix()); gitSetConfig("gitflow.prefix.versiontag", gitFlowConfig.getVersionTagPrefix()); + + gitSetConfig("gitflow.origin", gitFlowConfig.getOrigin()); } /** @@ -478,6 +495,78 @@ protected void gitBranchDeleteForce(final String branchName) executeGitCommand("branch", "-D", branchName); } + /** + * Executes git fetch and compares local branch with the remote. + * + * @param branchName + * Branch name to fetch and compare. + * @throws MojoFailureException + * @throws CommandLineException + */ + protected void gitFetchRemoteAndCompare(final String branchName) + throws MojoFailureException, CommandLineException { + getLog().info( + "Fetching remote branch '" + gitFlowConfig.getOrigin() + " " + + branchName + "'."); + + CommandResult result = executeGitCommandExitCode("fetch", "--quiet", + gitFlowConfig.getOrigin(), branchName); + + if (result.getExitCode() == SUCCESS_EXIT_CODE) { + getLog().info( + "Comparing local branch '" + branchName + "' with remote '" + + gitFlowConfig.getOrigin() + "/" + branchName + + "'."); + String revlistout = executeGitCommandReturn("rev-list", + "--left-right", "--count", branchName + "..." + + gitFlowConfig.getOrigin() + "/" + branchName); + + String[] counts = org.apache.commons.lang3.StringUtils.split( + revlistout, '\t'); + if (counts != null && counts.length > 1) { + if (!"0".equals(org.apache.commons.lang3.StringUtils + .deleteWhitespace(counts[1]))) { + throw new MojoFailureException( + "Remote branch is ahead of the local branch. Execute git pull."); + } + } + } else { + getLog().warn( + "There were some problems fetching remote branch '" + + gitFlowConfig.getOrigin() + + " " + + branchName + + "'. You can turn off remote branch fetching by setting the 'fetchRemote' parameter to false."); + } + } + + /** + * Executes git push, optionally with the --follow-tags + * argument. + * + * @param branchName + * Branch name to push. + * @param pushTags + * If true adds --follow-tags argument + * to the git push command. + * @throws MojoFailureException + * @throws CommandLineException + */ + protected void gitPush(final String branchName, boolean pushTags) + throws MojoFailureException, CommandLineException { + getLog().info( + "Pushing '" + branchName + "' branch" + " to '" + + gitFlowConfig.getOrigin() + "'."); + + if (pushTags) { + executeGitCommand("push", "--quiet", "--follow-tags", + gitFlowConfig.getOrigin(), branchName); + } else { + executeGitCommand("push", "--quiet", gitFlowConfig.getOrigin(), + branchName); + } + } + /** * Executes 'set' goal of versions-maven-plugin or 'set-version' of * tycho-versions-plugin in case it is tycho build. diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowConfig.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowConfig.java index 30f880af..6131557e 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowConfig.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowConfig.java @@ -36,6 +36,8 @@ public class GitFlowConfig { private String supportBranchPrefix; /** Prefix of the version tag. */ private String versionTagPrefix; + /** Name of the default remote. */ + private String origin; /** * Default constructor. @@ -48,6 +50,7 @@ public GitFlowConfig() { this.hotfixBranchPrefix = "hotfix/"; this.supportBranchPrefix = "support/"; this.versionTagPrefix = ""; + this.origin = "origin"; } /** @@ -154,4 +157,19 @@ public String getVersionTagPrefix() { public void setVersionTagPrefix(String versionTagPrefix) { this.versionTagPrefix = versionTagPrefix; } + + /** + * @return the origin + */ + public String getOrigin() { + return origin; + } + + /** + * @param origin + * the origin to set + */ + public void setOrigin(String origin) { + this.origin = origin; + } } diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java index c9a7a2c9..f51659ab 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java @@ -71,6 +71,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { throw new MojoFailureException("There are no feature branches."); } + // fetch and check remote + if (fetchRemote) { + gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch()); + } + final String[] branches = featureBranches.split("\\r?\\n"); List numberedList = new ArrayList(); @@ -154,6 +159,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { gitBranchDelete(featureBranchName); } } + + if (pushRemote) { + gitPush(gitFlowConfig.getDevelopmentBranch(), false); + } } catch (CommandLineException e) { getLog().error(e); } 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 93cc8261..6dbae6e0 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java @@ -54,6 +54,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { // check uncommitted changes checkUncommittedChanges(); + // fetch and check remote + if (fetchRemote) { + gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch()); + } + String featureName = null; try { while (StringUtils.isBlank(featureName)) { diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java index c2d04006..75ce7a34 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java @@ -70,6 +70,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { throw new MojoFailureException("There is no hotfix branches."); } + // fetch and check remote + if (fetchRemote) { + gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch()); + gitFetchRemoteAndCompare(gitFlowConfig.getProductionBranch()); + } + String[] branches = hotfixBranches.split("\\r?\\n"); List numberedList = new ArrayList(); @@ -184,6 +190,15 @@ public void execute() throws MojoExecutionException, MojoFailureException { // git branch -d hotfix/... gitBranchDelete(hotfixBranchName); } + + if (pushRemote) { + gitPush(gitFlowConfig.getProductionBranch(), !skipTag); + + // if no release branch + if (StringUtils.isBlank(releaseBranch)) { + gitPush(gitFlowConfig.getDevelopmentBranch(), !skipTag); + } + } } catch (CommandLineException e) { getLog().error(e); } 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 be05957b..da705e6a 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java @@ -49,6 +49,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { // git checkout master gitCheckout(gitFlowConfig.getProductionBranch()); + // fetch and check remote + if (fetchRemote) { + gitFetchRemoteAndCompare(gitFlowConfig.getProductionBranch()); + } + // get current project version from pom final String currentVersion = getCurrentProjectVersion(); 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 5c18d1a9..46f2a479 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseFinishMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseFinishMojo.java @@ -92,6 +92,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { "More than one release branch exists. Cannot finish release."); } + // fetch and check remote + if (fetchRemote) { + gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch()); + gitFetchRemoteAndCompare(gitFlowConfig.getProductionBranch()); + } + if (!skipTestProject) { // git checkout release/... gitCheckout(releaseBranch); @@ -158,6 +164,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { // git branch -d release/... gitBranchDelete(releaseBranch); } + + if (pushRemote) { + gitPush(gitFlowConfig.getProductionBranch(), !skipTag); + gitPush(gitFlowConfig.getDevelopmentBranch(), !skipTag); + } } catch (CommandLineException e) { getLog().error(e); } diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java index b0e1a3c6..edd0a462 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java @@ -80,6 +80,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { checkSnapshotDependencies(); } + // fetch and check remote + if (fetchRemote) { + gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch()); + gitFetchRemoteAndCompare(gitFlowConfig.getProductionBranch()); + } + // git for-each-ref --count=1 refs/heads/release/* final String releaseBranch = gitFindBranches( gitFlowConfig.getReleaseBranchPrefix(), true); @@ -193,6 +199,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { // mvn clean install mvnCleanInstall(); } + + if (pushRemote) { + gitPush(gitFlowConfig.getProductionBranch(), !skipTag); + gitPush(gitFlowConfig.getDevelopmentBranch(), !skipTag); + } } catch (CommandLineException e) { getLog().error(e); } diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java index 0c108f89..13603434 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java @@ -73,6 +73,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { "Release branch already exists. Cannot start release."); } + // fetch and check remote + if (fetchRemote) { + gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch()); + } + // need to be in develop to get correct project version // git checkout develop gitCheckout(gitFlowConfig.getDevelopmentBranch());