Skip to content

Commit

Permalink
Merge pull request hudson2-plugins#39 from onemanbucket/master
Browse files Browse the repository at this point in the history
[FIXES JENKINS-10131] (for simple cases)
Poll changes without workspace using git ls-remote
  • Loading branch information
ndeloof committed Aug 2, 2011
2 parents e437692 + a33a0de commit c750b63
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 33 deletions.
41 changes: 26 additions & 15 deletions src/main/java/hudson/plugins/git/GitAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private int[] getGitVersion() {

return new int[]{majorVer,minorVer};
}

public void init() throws GitException {
if (hasGitRepo()) {
throw new GitException(".git directory already exists! Has it already been initialised?");
Expand Down Expand Up @@ -199,7 +199,7 @@ public void fetch() throws GitException {
public void clone(final RemoteConfig remoteConfig) throws GitException {
listener.getLogger().println("Cloning repository " + remoteConfig.getName());
final int[] gitVer = getGitVersion();

// TODO: Not here!
try {
workspace.deleteRecursive();
Expand All @@ -220,7 +220,7 @@ public String invoke(File workspace,
VirtualChannel channel) throws IOException {
final ArgumentListBuilder args = new ArgumentListBuilder();
args.add("clone");
if ((gitVer[0] >= 1) && (gitVer[1] >= 7)) {
if ((gitVer[0] >= 1) && (gitVer[1] >= 7)) {
args.add("--progress");
}
args.add("-o", remoteConfig.getName());
Expand Down Expand Up @@ -258,11 +258,11 @@ public void prune(RemoteConfig repository) throws GitException {
!getRemoteUrl(repository.getName()).equals("")) {
ArgumentListBuilder args = new ArgumentListBuilder();
args.add("remote", "prune", repository.getName());

launchCommand(args);
}
}

private String firstLine(String result) {
BufferedReader reader = new BufferedReader(new StringReader(result));
String line;
Expand Down Expand Up @@ -325,7 +325,7 @@ public List<String> showRevision(Revision r) throws GitException {

return revShow;
}

/**
* Merge any changes into the head.
*
Expand Down Expand Up @@ -358,12 +358,12 @@ public void submoduleSync() throws GitException {
launchCommand("submodule", "sync");
}


/**
* Update submodules.
*
* @param recursive if true, will recursively update submodules (requires git>=1.6.5)
*
*
* @throws GitException if executing the Git command fails
*/
public void submoduleUpdate(boolean recursive) throws GitException {
Expand All @@ -372,7 +372,7 @@ public void submoduleUpdate(boolean recursive) throws GitException {
if (recursive) {
args.add("--init", "--recursive");
}

launchCommand(args);
}

Expand All @@ -390,7 +390,7 @@ public void submoduleClean(boolean recursive) throws GitException {
args.add("--recursive");
}
args.add("git clean -fdx");

launchCommand(args);
}

Expand Down Expand Up @@ -664,10 +664,10 @@ public void setupSubmoduleUrls( Revision rev, TaskListener listener ) throws Git
String b = bi.next().getName();
if (b != null) {
int slash = b.indexOf('/');

if ( slash == -1 )
throw new GitException("no remote from branch name ("+b+")");

remote = getDefaultRemote( b.substring(0,slash) );
}
else {
Expand Down Expand Up @@ -831,7 +831,7 @@ public void checkoutBranch(String branch, String commitish) throws GitException
throw new GitException("Could not checkout " + branch + " with start point " + commitish, e);
}
}

public boolean tagExists(String tagName) throws GitException {
tagName = tagName.replace(' ', '_');

Expand All @@ -846,7 +846,7 @@ public void deleteBranch(String name) throws GitException {
}

}

public void deleteTag(String tagName) throws GitException {
tagName = tagName.replace(' ', '_');
try {
Expand Down Expand Up @@ -916,7 +916,7 @@ public boolean isCommitInRepo(String sha1) {
return false;
}
}

public void add(String filePattern) throws GitException {
try {
launchCommand("add", filePattern);
Expand Down Expand Up @@ -1012,4 +1012,15 @@ public Set<String> getTagNames(String tagPattern) throws GitException {
throw new GitException("Error retrieving tag names", e);
}
}

public String getHeadRev(String remoteRepoUrl, String branch) throws GitException {
String[] branchExploded = branch.split("/");
branch = branchExploded[branchExploded.length-1];
ArgumentListBuilder args = new ArgumentListBuilder("ls-remote");
args.add("-h");
args.add(remoteRepoUrl);
args.add(branch);
String result = launchCommand(args);
return result.length()>=40 ? result.substring(0,40) : "";
}
}
63 changes: 56 additions & 7 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hudson.plugins.git;

import static hudson.Util.fixEmptyAndTrim;

import hudson.AbortException;
import hudson.EnvVars;
import hudson.Extension;
Expand All @@ -11,7 +10,6 @@
import hudson.Util;
import hudson.matrix.MatrixRun;
import hudson.matrix.MatrixBuild;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Descriptor.FormException;
import hudson.model.Items;
Expand All @@ -30,16 +28,17 @@
import hudson.plugins.git.util.Build;
import hudson.plugins.git.util.BuildChooser;
import hudson.plugins.git.util.BuildChooserDescriptor;
import hudson.plugins.git.util.BuildData;
import hudson.plugins.git.util.DefaultBuildChooser;
import hudson.plugins.git.util.GitUtils;
import hudson.plugins.git.util.BuildData;
import hudson.remoting.VirtualChannel;
import hudson.scm.ChangeLogParser;
import hudson.scm.PollingResult;
import hudson.scm.SCMDescriptor;
import hudson.scm.SCMRevisionState;
import hudson.scm.SCM;
import hudson.util.FormValidation;
import hudson.util.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -61,7 +60,6 @@

import javax.servlet.ServletException;

import hudson.util.IOUtils;
import net.sf.json.JSONObject;

import org.eclipse.jgit.lib.Config;
Expand Down Expand Up @@ -119,6 +117,8 @@ public class GitSCM extends SCM implements Serializable {
private boolean clean;
private boolean wipeOutWorkspace;
private boolean pruneBranches;
private boolean remotePoll;

/**
* @deprecated
* Replaced by {@link #buildChooser} instead.
Expand Down Expand Up @@ -166,7 +166,7 @@ public GitSCM(String repositoryUrl) {
null,
false, Collections.<SubmoduleConfig>emptyList(), false,
false, new DefaultBuildChooser(), null, null, false, null,
null, null, null, false, false, null, null, false);
null, null, null, false, false, false, null, null, false);
}

@DataBoundConstructor
Expand All @@ -188,6 +188,7 @@ public GitSCM(
String localBranch,
boolean recursiveSubmodules,
boolean pruneBranches,
boolean remotePoll,
String gitConfigName,
String gitConfigEmail,
boolean skipTag) {
Expand Down Expand Up @@ -235,6 +236,19 @@ public GitSCM(
this.excludedUsers = excludedUsers;
this.recursiveSubmodules = recursiveSubmodules;
this.pruneBranches = pruneBranches;
if (remotePoll
&& (branches.size() != 1
|| branches.get(0).getName().contains("*")
|| repo.size() != 1
|| (excludedRegions != null && excludedRegions.length() > 0)
|| (submoduleCfg.size() != 0)
|| (excludedUsers != null && excludedUsers.length() > 0))) {
LOGGER.log(Level.WARNING, "Cannot poll remotely with current configuration.");
this.remotePoll = false;
} else {
this.remotePoll = remotePoll;
}

this.gitConfigName = gitConfigName;
this.gitConfigEmail = gitConfigEmail;
this.skipTag = skipTag;
Expand Down Expand Up @@ -450,6 +464,10 @@ public boolean getPruneBranches() {
return this.pruneBranches;
}

public boolean getRemotePoll() {
return this.remotePoll;
}

public boolean getWipeOutWorkspace() {
return this.wipeOutWorkspace;
}
Expand Down Expand Up @@ -560,6 +578,13 @@ public SCMRevisionState calcRevisionsFromBuild(AbstractBuild<?, ?> abstractBuild
return SCMRevisionState.NONE;
}

@Override
public boolean requiresWorkspaceForPolling() {
if(remotePoll)
return false;
return true;
}

@Override
protected PollingResult compareRemoteRevisionWith(AbstractProject<?, ?> project, Launcher launcher, FilePath workspace, final TaskListener listener, SCMRevisionState baseline) throws IOException, InterruptedException {
// Poll for changes. Are there any unbuilt revisions that Hudson ought to build ?
Expand All @@ -582,6 +607,30 @@ protected PollingResult compareRemoteRevisionWith(AbstractProject<?, ?> project,
listener.getLogger().println("[poll] Last Built Revision: " + buildData.lastBuild.revision);
}

final String singleBranch = getSingleBranch(lastBuild);

if (singleBranch != null && this.remotePoll) {
String gitExe = "";
GitTool[] installations = ((hudson.plugins.git.GitTool.DescriptorImpl)Hudson.getInstance().getDescriptorByType(GitTool.DescriptorImpl.class)).getInstallations();
for(GitTool i : installations) {
if(i.getName().equals(gitTool)) {
gitExe = i.getGitExe();
break;
}
}
final EnvVars environment = GitUtils.getPollEnvironment(project, workspace, launcher, listener);
IGitAPI git = new GitAPI(gitExe, workspace, listener, environment);
String gitRepo = getParamExpandedRepos(lastBuild).get(0).getURIs().get(0).toString();
String headRevision = git.getHeadRev(gitRepo, getBranches().get(0).getName());

if(buildData.lastBuild.getRevision().getSha1String().equals(headRevision)) {
return PollingResult.NO_CHANGES;
} else {
return PollingResult.BUILD_NOW;
}

}

final String gitExe;
{
//If this project is tied onto a node, it's built always there. On other cases,
Expand Down Expand Up @@ -611,7 +660,7 @@ protected PollingResult compareRemoteRevisionWith(AbstractProject<?, ?> project,

final EnvVars environment = GitUtils.getPollEnvironment(project, workspace, launcher, listener);
final List<RemoteConfig> paramRepos = getParamExpandedRepos(lastBuild);
final String singleBranch = getSingleBranch(lastBuild);
// final String singleBranch = getSingleBranch(lastBuild);

boolean pollChangesResult = workingDirectory.act(new FileCallable<Boolean>() {

Expand Down Expand Up @@ -1581,7 +1630,7 @@ public BuildData getBuildData(Run build, boolean clone) {
/**
* Given the workspace, gets the working directory, which will be the workspace
* if no relative target dir is specified. Otherwise, it'll be "workspace/relativeTargetDir".
*
*
* @param workspace
* @return working directory
*/
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/hudson/plugins/git/IGitAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public interface IGitAPI {
String getGitExe();
EnvVars getEnvironment();
Repository getRepository() throws IOException;
Repository getRepository() throws IOException;

public void init() throws GitException;

Expand Down Expand Up @@ -58,7 +58,7 @@ public interface IGitAPI {
void clone(RemoteConfig source) throws GitException;
void clean() throws GitException;
void prune(RemoteConfig repository) throws GitException;

ObjectId revParse(String revName) throws GitException;
List<Branch> getBranches() throws GitException;
List<Branch> getRemoteBranches() throws GitException, IOException;
Expand Down Expand Up @@ -92,15 +92,16 @@ public interface IGitAPI {
* If non-null, move/create the branch in this name at the specified commit-ish and check out that branch.
*/
void checkoutBranch(String branch, String commitish) throws GitException;

void add(String filePattern) throws GitException;
void branch(String name) throws GitException;
void deleteBranch(String name) throws GitException;

void commit(File f) throws GitException;

ObjectId mergeBase(ObjectId sha1, ObjectId sha12);
String getAllLogEntries(String branch);

List<String> showRevision(Revision r) throws GitException;
String getHeadRev(String remoteRepoUrl, String branch) throws GitException;
}
3 changes: 3 additions & 0 deletions src/main/resources/hudson/plugins/git/GitSCM/config.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
<f:entry title="Clean after checkout" field="clean" help="/plugin/git/clean.html">
<f:checkbox />
</f:entry>
<f:entry title="Fast remote polling" help="/plugin/git/help-fastremote.html">
<f:checkbox name="git.remotePoll" checked="${scm.remotePoll}" />
</f:entry>
<f:entry title="Recursively update submodules" field="recursiveSubmodules" help="/plugin/git/help-recursiveSubmodules.html">
<f:checkbox />
</f:entry>
Expand Down
3 changes: 3 additions & 0 deletions src/main/webapp/help-fastremote.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Use git ls-remote polling mechanism. This will compare latest built commit SHA with remote branch without cloning a local copy of the repo
</div>
Loading

0 comments on commit c750b63

Please sign in to comment.