Skip to content

Commit

Permalink
Improve remote fetching and checking out
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-m committed May 5, 2022
1 parent 7f9146b commit a11b0a3
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -978,78 +978,57 @@ protected void gitBranchDeleteForce(final String branchName)
}

/**
* Fetches and checkouts from remote if local branch doesn't exist.
* Executes git fetch and checks if local branch exists. If local branch is
* present then compares it with the remote, if not then branch is checked out.
*
* @param branchName
* Branch name to check.
* @throws MojoFailureException
* If command line execution returns false code.
* @throws CommandLineException
* If command line execution fails.
*/
protected void gitFetchRemoteAndCreate(final String branchName)
throws MojoFailureException, CommandLineException {
if (!gitCheckBranchExists(branchName)) {
getLog().info(
"Local branch '"
+ branchName
+ "' doesn't exist. Trying to fetch and check it out from '"
+ gitFlowConfig.getOrigin() + "'.");
gitFetchRemote(branchName);
gitCreateAndCheckout(branchName, gitFlowConfig.getOrigin() + "/" + branchName);
}
}

/**
* Executes git fetch and compares local branch with the remote.
*
* @param branchName
* Branch name to fetch and compare.
* @throws MojoFailureException
* If command line execution returns false code or remote branch is
* ahead of the local branch.
* @throws CommandLineException
* If command line execution fails.
*/
protected void gitFetchRemoteAndCompare(final String branchName)
throws MojoFailureException, CommandLineException {
if (gitFetchRemote(branchName)) {
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 && !"0".equals(org.apache.commons.lang3.StringUtils.deleteWhitespace(counts[1]))) {
throw new MojoFailureException("Remote branch '" + gitFlowConfig.getOrigin() + "/" + branchName
+ "' is ahead of the local branch '" + branchName + "'. Execute git pull.");
protected void gitFetchRemoteAndCompareCreate(final String branchName) throws MojoFailureException, CommandLineException {
final boolean fetchOk = gitFetchRemote();
if (gitCheckBranchExists(branchName)) {
if (fetchOk) {
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 && !"0".equals(org.apache.commons.lang3.StringUtils.deleteWhitespace(counts[1]))) {
throw new MojoFailureException("Remote branch '" + gitFlowConfig.getOrigin() + "/" + branchName
+ "' is ahead of the local branch '" + branchName + "'. Execute git pull.");
}
}
} else {
getLog().info("Local branch '" + branchName + "' doesn't exist. Trying check it out from '" + gitFlowConfig.getOrigin() + "'.");
gitCreateAndCheckout(branchName, gitFlowConfig.getOrigin() + "/" + branchName);
}
}

/**
* Executes git fetch and git for-each-ref with <code>refname:short</code>
* format. Searches <code>refs/remotes/{remoteName}/</code>.
* format. Searches <code>refs/remotes/{gitFlowConfig#origin}/</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>.
* @return Branch names which matches
* <code>refs/remotes/{gitFlowConfig#origin}/{branchName}*</code>.
* @throws MojoFailureException
* If command line execution returns false code.
* @throws CommandLineException
* If command line execution fails.
*/
protected String gitFetchAndFindRemoteBranches(final String remoteName, final String branchName,
final boolean firstMatch) throws MojoFailureException, CommandLineException {
protected String gitFetchAndFindRemoteBranches(final String branchName, final boolean firstMatch)
throws MojoFailureException, CommandLineException {
gitFetchRemote();
return gitFindBranches("refs/remotes/" + remoteName + "/", branchName, firstMatch);
return gitFindBranches("refs/remotes/" + gitFlowConfig.getOrigin() + "/", branchName, firstMatch);
}

/**
Expand All @@ -1058,40 +1037,21 @@ protected String gitFetchAndFindRemoteBranches(final String remoteName, final St
* @return <code>true</code> if git fetch returned success exit code,
* <code>false</code> otherwise.
* @throws MojoFailureException
* If command line execution returns false code.
* @throws CommandLineException
* If command line execution fails.
*/
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,
* <code>false</code> otherwise.
* @throws MojoFailureException
* Shouldn't happen, actually.
* @throws CommandLineException
* If command line execution fails.
*/
private boolean gitFetchRemote(final String branchName) throws MojoFailureException, CommandLineException {
getLog().info(
"Fetching remote branch '" + gitFlowConfig.getOrigin() + " " + branchName + "'.");
private boolean gitFetchRemote() throws MojoFailureException, CommandLineException {
getLog().info("Fetching remote from '" + gitFlowConfig.getOrigin() + "'.");

CommandResult result = executeGitCommandExitCode("fetch", "--quiet", gitFlowConfig.getOrigin(), branchName);
CommandResult result = executeGitCommandExitCode("fetch", "--quiet", gitFlowConfig.getOrigin());

boolean success = result.getExitCode() == SUCCESS_EXIT_CODE;
if (!success) {
getLog().warn(
"There were some problems fetching remote branch '"
"There were some problems fetching from '"
+ gitFlowConfig.getOrigin()
+ " "
+ branchName
+ "'. You can turn off remote branch fetching by setting the 'fetchRemote' parameter to false.");
+ "'. You can turn off remote fetching by setting the 'fetchRemote' parameter to false.");
}

return success;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// fetch and check remote
if (fetchRemote) {
gitFetchRemoteAndCompare(featureBranchName);
gitFetchRemoteAndCompareCreate(featureBranchName);

gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch());
gitFetchRemoteAndCompareCreate(gitFlowConfig.getDevelopmentBranch());
}

if (!skipTestProject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// fetch and check remote
if (fetchRemote) {
// checkout from remote if doesn't exist
gitFetchRemoteAndCreate(gitFlowConfig.getDevelopmentBranch());

// fetch and check remote
gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch());
gitFetchRemoteAndCompareCreate(gitFlowConfig.getDevelopmentBranch());
}

String featureBranchName = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,15 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// fetch and check remote
if (fetchRemote) {
gitFetchRemoteAndCompare(hotfixBranchName);
gitFetchRemoteAndCompareCreate(hotfixBranchName);

if (supportBranchName != null) {
gitFetchRemoteAndCreate(supportBranchName);
gitFetchRemoteAndCompare(supportBranchName);
gitFetchRemoteAndCompareCreate(supportBranchName);
} else {
if (notSameProdDevName()) {
gitFetchRemoteAndCreate(gitFlowConfig.getDevelopmentBranch());
gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch());
gitFetchRemoteAndCompareCreate(gitFlowConfig.getDevelopmentBranch());
}
gitFetchRemoteAndCreate(gitFlowConfig.getProductionBranch());
gitFetchRemoteAndCompare(gitFlowConfig.getProductionBranch());
gitFetchRemoteAndCompareCreate(gitFlowConfig.getProductionBranch());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// fetch and check remote
if (fetchRemote) {
gitFetchRemoteAndCompare(branchName);
gitFetchRemoteAndCompareCreate(branchName);
}

// get current project version from pom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
checkUncommittedChanges();

// git for-each-ref --format='%(refname:short)' refs/heads/release/*
String releaseBranch = gitFindBranches(gitFlowConfig.getReleaseBranchPrefix(), false).trim();
String releaseBranch = gitFindBranches(gitFlowConfig.getReleaseBranchPrefix(), false);

if (StringUtils.isBlank(releaseBranch)) {
if (fetchRemote) {
releaseBranch = gitFetchAndFindRemoteBranches(gitFlowConfig.getOrigin(),
gitFlowConfig.getReleaseBranchPrefix(), false).trim();
releaseBranch = gitFetchAndFindRemoteBranches(gitFlowConfig.getReleaseBranchPrefix(), false);
if (StringUtils.isBlank(releaseBranch)) {
throw new MojoFailureException("There is no remote or local release branch.");
}
Expand Down Expand Up @@ -225,22 +224,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

if (fetchRemote) {
// fetch and check remote
gitFetchRemoteAndCompare(releaseBranch);
gitFetchRemoteAndCompareCreate(releaseBranch);

// checkout from remote if doesn't exist
gitFetchRemoteAndCreate(gitFlowConfig.getDevelopmentBranch());

// fetch and check remote
gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch());
gitFetchRemoteAndCompareCreate(gitFlowConfig.getDevelopmentBranch());

if (notSameProdDevName()) {
// checkout from remote if doesn't exist
gitFetchRemoteAndCreate(gitFlowConfig.getProductionBranch());

// fetch and check remote
gitFetchRemoteAndCompare(gitFlowConfig
.getProductionBranch());
gitFetchRemoteAndCompareCreate(gitFlowConfig.getProductionBranch());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

if (fetchRemote) {
// checkout from remote if doesn't exist
gitFetchRemoteAndCreate(gitFlowConfig.getDevelopmentBranch());

// fetch and check remote
gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch());
gitFetchRemoteAndCompareCreate(gitFlowConfig.getDevelopmentBranch());

if (notSameProdDevName()) {
// checkout from remote if doesn't exist
gitFetchRemoteAndCreate(gitFlowConfig.getProductionBranch());

// fetch and check remote
gitFetchRemoteAndCompare(gitFlowConfig
.getProductionBranch());
gitFetchRemoteAndCompareCreate(gitFlowConfig.getProductionBranch());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

if (fetchRemote) {
// checkout from remote if doesn't exist
gitFetchRemoteAndCreate(gitFlowConfig.getDevelopmentBranch());

// fetch and check remote
gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch());
gitFetchRemoteAndCompareCreate(gitFlowConfig.getDevelopmentBranch());
}

final String startPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// fetch and check remote
if (fetchRemote) {
gitFetchRemoteAndCompare(branchName);
gitFetchRemoteAndCompareCreate(branchName);
}

// get current project version from pom
Expand Down

0 comments on commit a11b0a3

Please sign in to comment.