Skip to content

Commit

Permalink
[fixed JENKINS-17604] Added remote checkout class for Git
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgarnet committed Apr 14, 2013
1 parent cd3e805 commit 3612e72
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 14 deletions.
Expand Up @@ -125,7 +125,7 @@ public Performer( AbstractBuild<?, ?> build, Launcher launcher, FilePath workspa
public abstract C getInitialConfiguration() throws ConfigurationRotatorException, IOException;
public abstract C getNextConfiguration( ConfigurationRotatorBuildAction action ) throws ConfigurationRotatorException;
public abstract void checkConfiguration( C configuration ) throws ConfigurationRotatorException;
public abstract void createWorkspace( C configuration ) throws ConfigurationRotatorException;
public abstract void createWorkspace( C configuration ) throws ConfigurationRotatorException, IOException, InterruptedException;
public Class getSCMClass() {
return AbstractConfigurationRotatorSCM.this.getClass();
}
Expand Down
@@ -0,0 +1,44 @@
package net.praqma.jenkins.configrotator.scm.git;

import hudson.FilePath;
import hudson.remoting.VirtualChannel;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import java.io.File;
import java.io.IOException;

/**
* @author cwolfgang
*/
public class Checkout implements FilePath.FileCallable< Boolean> {

private String commitId;
private String name;
private String branch;

public Checkout( String name, String branch, String commitId ) {
this.commitId = commitId;
this.name = name;
this.branch = branch;
}

@Override
public Boolean invoke( File workspace, VirtualChannel channel ) throws IOException, InterruptedException {

File local = new File( workspace, name );

FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir( new File( local, ".git" ) ).readEnvironment().findGitDir().build();
org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git( repo );

try {
git.checkout().setName( branch ).setAllPaths( true ).setForce( true ).setStartPoint( commitId ).call();
} catch( GitAPIException e ) {
throw new IOException( e );
}

return true;
}
}
Expand Up @@ -76,8 +76,8 @@ public void checkConfiguration( GitConfiguration configuration ) {
}

@Override
public void createWorkspace( GitConfiguration configuration ) {
/* No need */
public void createWorkspace( GitConfiguration configuration ) throws IOException, InterruptedException {
configuration.checkout( workspace, listener );
}

@Override
Expand Down Expand Up @@ -166,13 +166,12 @@ protected List<ConfigRotatorChangeLogEntry> getChangeLogEntries( GitConfiguratio

@Override
public AbstractConfiguration nextConfiguration( TaskListener listener, AbstractConfiguration configuration, FilePath workspace ) throws ConfigurationRotatorException {
logger.fine("Getting next Git configuration: " + configuration);
logger.fine( "Getting next Git configuration: " + configuration);

RevCommit oldest = null;
GitConfigurationComponent chosen = null;
GitConfiguration nconfig = (GitConfiguration) configuration.clone();


/* Find oldest commit, newer than current */
for( GitConfigurationComponent config : nconfig.getList() ) {
if( !config.isFixed() ) {
Expand Down
Expand Up @@ -5,6 +5,7 @@
import net.praqma.jenkins.configrotator.*;
import net.praqma.jenkins.configrotator.scm.ConfigRotatorChangeLogEntry;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -33,6 +34,12 @@ public GitConfiguration( List<GitTarget> targets, FilePath workspace, TaskListen
}
}

public void checkout( FilePath workspace, TaskListener listener ) throws IOException, InterruptedException {
for( GitConfigurationComponent c : getList() ) {
c.checkout( workspace, listener );
}
}

@Override
public List<ConfigRotatorChangeLogEntry> difference( GitConfigurationComponent component, GitConfigurationComponent other ) throws ConfigurationRotatorException {
return null; //To change body of implemented methods use File | Settings | File Templates.
Expand Down
@@ -1,8 +1,11 @@
package net.praqma.jenkins.configrotator.scm.git;

import hudson.FilePath;
import hudson.model.TaskListener;
import net.praqma.jenkins.configrotator.AbstractConfigurationComponent;
import org.eclipse.jgit.revwalk.RevCommit;

import java.io.IOException;
import java.util.logging.Logger;

public class GitConfigurationComponent extends AbstractConfigurationComponent {
Expand Down Expand Up @@ -34,6 +37,10 @@ public GitConfigurationComponent( String name, String repository, String branch,
this.repository = repository;
}

public void checkout( FilePath workspace, TaskListener listener ) throws IOException, InterruptedException {
workspace.act( new Checkout( name, branch, commitId ) );
}

public String getBranch() {
return branch;
}
Expand Down
Expand Up @@ -15,6 +15,8 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -68,6 +70,7 @@ public GitConfigurationComponent invoke( File workspace, VirtualChannel channel

try {
logger.fine( "Cloning repo from " + repository );

try {
org.eclipse.jgit.api.Git.cloneRepository().setURI( repository ).setDirectory( local ).setCloneAllBranches( true ).call();
} catch( JGitInternalException e ) {
Expand All @@ -79,7 +82,7 @@ public GitConfigurationComponent invoke( File workspace, VirtualChannel channel
org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git( repo );

try {
logger.fine( "Updating to " + branch );
logger.fine( "Creating branch " + branch );
git.branchCreate().setUpstreamMode( CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM ).setName( branch ).setStartPoint( "origin/" + branch ).call();
} catch( Exception e ) {
logger.fine( e.getMessage() );
Expand All @@ -89,17 +92,14 @@ public GitConfigurationComponent invoke( File workspace, VirtualChannel channel
try {
logger.fine( "Pulling" );
git.pull().call();
//git.fetch().setRefSpecs( new RefSpec( "refs/heads/*" ).setForceUpdate( true ) ).call();
} catch( GitAPIException e ) {
throw new IOException( e );
}


//repo.updateRef( branch );
git.checkout().setName( branch ).call();
logger.fine( "BRANCH: " + repo.getBranch() );
RevWalk w = new RevWalk( repo );

logger.fine( "The commit id: " + commitId );

if( commitId == null || commitId.matches( "^\\s*$" ) ) {
logger.fine( "Initial commit not defined, using HEAD" );
listener.getLogger().println( ConfigurationRotator.LOGGERNAME + "Initial commit not defined, using HEAD" );
Expand All @@ -117,4 +117,11 @@ public GitConfigurationComponent invoke( File workspace, VirtualChannel channel
throw new IOException( e );
}
}

private void listPath( PrintStream logger, File path ) {
logger.println( "PATH: " + path );
for( String f : path.list() ) {
logger.println( " * " + f );
}
}
}
Expand Up @@ -31,14 +31,14 @@ public RevCommit invoke( File workspace, VirtualChannel virtualChannel ) throws

File local = new File( workspace, name );
FileRepositoryBuilder builder = new FileRepositoryBuilder();
logger.fine("Initializing repo");
logger.fine( "Initializing repo" );
Repository repo = builder.setGitDir( new File( local, ".git" ) ).readEnvironment().findGitDir().build();
org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git( repo );

logger.fine("Updating to " + branch);
logger.fine( "Updating to " + branch );

try {
logger.fine("Pulling");
logger.fine( "Pulling" );
git.pull().call();
} catch( GitAPIException e ) {
throw new IOException( e );
Expand Down

0 comments on commit 3612e72

Please sign in to comment.