Fetch changes and cache cloned repos

This commit is contained in:
Marcin Grzejszczak
2019-05-02 13:04:40 +04:00
parent 7c9c2ee958
commit 9fc4b0f81b
4 changed files with 53 additions and 3 deletions

View File

@@ -103,8 +103,9 @@ public class Releaser {
log.info("\n\nProject was successfully updated to [{}]", changedVersion);
}
public void buildProject(ProjectVersion versionFromScRelease) {
this.projectBuilder.build(versionFromScRelease);
public void buildProject(ProjectVersion versionFromScRelease,
ProjectBuilder.MavenProfile... profiles) {
this.projectBuilder.build(versionFromScRelease, profiles);
log.info("\nProject was successfully built");
}

View File

@@ -34,6 +34,7 @@ import com.jcraft.jsch.agentproxy.usocket.JNAUSocketFactory;
import org.eclipse.jgit.api.CheckoutCommand;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.CreateBranchCommand;
import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.PushCommand;
@@ -43,6 +44,7 @@ import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.RefSpec;
@@ -126,6 +128,20 @@ class GitRepo {
}
}
/**
* Pull changes.
*/
void fetch() {
try {
log.info("Pull changes for repo [{}]", this.basedir);
fetch(this.basedir);
log.info("Successfully pulled the changes");
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
/**
* Performs a commit.
* @param message - commit message
@@ -282,6 +298,21 @@ class GitRepo {
return new File(destinationFolder, projectUrl.getHumanishName());
}
private FetchResult fetch(File projectDir) throws GitAPIException {
Git git = this.gitFactory.open(projectDir);
FetchCommand command = git.fetch();
try {
return command.call();
}
catch (GitAPIException e) {
deleteBaseDirIfExists();
throw e;
}
finally {
git.close();
}
}
private Ref checkoutBranch(File projectDir, String branch) throws GitAPIException {
Git git = this.gitFactory.open(projectDir);
CheckoutCommand command = git.checkout().setName(branch);

View File

@@ -19,6 +19,8 @@ package org.springframework.cloud.release.internal.git;
import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jgit.transport.URIish;
import org.slf4j.Logger;
@@ -37,6 +39,8 @@ import org.springframework.util.StringUtils;
*/
public class ProjectGitHandler implements ReleaserPropertiesAware {
private static final Map<URIish, File> CACHE = new ConcurrentHashMap<>();
private static final Logger log = LoggerFactory.getLogger(ProjectGitHandler.class);
private static final String MSG = "Bumping versions";
@@ -195,11 +199,22 @@ public class ProjectGitHandler implements ReleaserPropertiesAware {
File cloneProject(String url) {
try {
URIish urIish = new URIish(url);
// retrieve from cache and fetch if from cache
File clonedProject = CACHE.get(urIish);
if (clonedProject != null) {
log.info(
"Project has already been cloned. Will fetch the latest changes and return the cached location");
gitRepo(clonedProject).fetch();
return clonedProject;
}
File destinationDir = this.properties.getGit()
.getCloneDestinationDir() != null
? new File(this.properties.getGit().getCloneDestinationDir())
: Files.createTempDirectory("releaser").toFile();
return gitRepo(destinationDir).cloneProject(new URIish(url));
File clonedLocation = gitRepo(destinationDir).cloneProject(urIish);
CACHE.put(urIish, clonedLocation);
return clonedLocation;
}
catch (Exception e) {
throw new IllegalStateException(e);

View File

@@ -44,6 +44,9 @@ import org.springframework.util.StringUtils;
*/
public class ProjectBuilder implements ReleaserPropertiesAware {
/**
* Enumeration over commonly used Maven profiles.
*/
public enum MavenProfile {
/**