Skip to content

Commit

Permalink
Remote interaction (git fetch, git push)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-m committed Sep 24, 2016
1 parent 5f19e77 commit d0d17ee
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -265,6 +280,8 @@ protected void initGitFlowConfig() throws MojoFailureException,
gitFlowConfig.getSupportBranchPrefix());
gitSetConfig("gitflow.prefix.versiontag",
gitFlowConfig.getVersionTagPrefix());

gitSetConfig("gitflow.origin", gitFlowConfig.getOrigin());
}

/**
Expand Down Expand Up @@ -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 <code>--follow-tags</code>
* argument.
*
* @param branchName
* Branch name to push.
* @param pushTags
* If <code>true</code> adds <code>--follow-tags</code> argument
* to the git <code>push</code> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -48,6 +50,7 @@ public GitFlowConfig() {
this.hotfixBranchPrefix = "hotfix/";
this.supportBranchPrefix = "support/";
this.versionTagPrefix = "";
this.origin = "origin";
}

/**
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> numberedList = new ArrayList<String>();
Expand Down Expand Up @@ -154,6 +159,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
gitBranchDelete(featureBranchName);
}
}

if (pushRemote) {
gitPush(gitFlowConfig.getDevelopmentBranch(), false);
}
} catch (CommandLineException e) {
getLog().error(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> numberedList = new ArrayList<String>();
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit d0d17ee

Please sign in to comment.