Skip to content

Commit

Permalink
allow push on commit to be enabled; along with the ability to periodi…
Browse files Browse the repository at this point in the history
…cally poll the git repo
  • Loading branch information
jstrachan committed May 16, 2013
1 parent d922e16 commit 59e4a57
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 16 deletions.
131 changes: 115 additions & 16 deletions hawtio-git/src/main/java/io/hawt/git/GitFacade.java
Expand Up @@ -17,12 +17,15 @@
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.gitective.core.BlobUtils;
import org.gitective.core.CommitFinder;
import org.gitective.core.CommitUtils;
Expand All @@ -39,6 +42,8 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Callable;

/**
Expand All @@ -60,12 +65,42 @@ public class GitFacade extends MBeanSupport implements GitFacadeMXBean {
private boolean pullOnStartup = true;
private CredentialsProvider credentials;
private boolean cloneAllBranches = false;
private boolean pushOnCommit = false;
private boolean pullBeforeOperation = false;
private long pullTimePeriod;
private Timer timer;
private PersonIdent stashPersonIdent;


public void init() throws Exception {
// lets check if we have a config directory if not lets create one...
initialiseGitRepo();

long timePeriod = getPullTimePeriod();
if (timePeriod > 0 && isPullBeforeOperation()) {
Timer t = getTimer();
if (t == null) {
t = new Timer();
setTimer(t);
}
if (stashPersonIdent == null) {
stashPersonIdent = new PersonIdent("dummy", "dummy");
}
final Callable<Object> emptyCallable = new Callable<Object>() {
@Override
public Object call() throws Exception {
LOG.info("Pulled from remote repository " + getRemoteRepository());
return null;
}
};
TimerTask task = new TimerTask() {
@Override
public void run() {
gitOperation(stashPersonIdent, emptyCallable);
}
};
t.schedule(task, timePeriod, timePeriod);
}
super.init();
}

Expand Down Expand Up @@ -127,12 +162,28 @@ public void setCloneAllBranches(boolean cloneAllBranches) {
this.cloneAllBranches = cloneAllBranches;
}

public boolean isPushOnCommit() {
return pushOnCommit;
}

public void setPushOnCommit(boolean pushOnCommit) {
this.pushOnCommit = pushOnCommit;
}

public boolean isPullBeforeOperation() {
return pullBeforeOperation;
}

public void setPullBeforeOperation(boolean pullBeforeOperation) {
this.pullBeforeOperation = pullBeforeOperation;
}

public boolean isCloneRemoteRepoOnStartup() {
if (cloneRemoteRepoOnStartup == null) {
if (getCloneRemoteRepoOnStartup() == null) {
String flag = getSystemPropertyOrEnvironmentVariable("hawtio.config.cloneOnStartup", "HAWTIO_CONFIG_CLONEONSTARTUP");
cloneRemoteRepoOnStartup = flag == null || !flag.equals("false");
}
return cloneRemoteRepoOnStartup;
return getCloneRemoteRepoOnStartup();
}

public void setCloneRemoteRepoOnStartup(boolean cloneRemoteRepoOnStartup) {
Expand All @@ -147,6 +198,22 @@ public void setCredentials(CredentialsProvider credentials) {
this.credentials = credentials;
}

public long getPullTimePeriod() {
return pullTimePeriod;
}

public void setPullTimePeriod(long pullTimePeriod) {
this.pullTimePeriod = pullTimePeriod;
}

public Timer getTimer() {
return timer;
}

public void setTimer(Timer timer) {
this.timer = timer;
}

/**
* Reads the file contents of the given path
*
Expand Down Expand Up @@ -235,7 +302,7 @@ public RevCommit call() throws Exception {
add.call();

CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(commitMessage);
return commit.call();
return commitThenPush(commit);
}
});
}
Expand Down Expand Up @@ -272,14 +339,26 @@ public RevCommit call() throws Exception {
String filePattern = getFilePattern(path);
git.rm().addFilepattern(filePattern).call();
CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(commitMessage);
return commit.call();
return commitThenPush(commit);
} else {
return null;
}
}
});
}

protected RevCommit commitThenPush(CommitCommand commit) throws GitAPIException {
RevCommit answer = commit.call();
LOG.info("Committed " + answer);
if (isPushOnCommit()) {
Iterable<PushResult> results = git.push().setCredentialsProvider(getCredentials()).setRemote(getRemote()).call();
for (PushResult result : results) {
LOG.info("Pushed: " + results);
}
}
return answer;
}

@Override
public String getHEAD() {
RevCommit commit = CommitUtils.getHead(git.getRepository());
Expand Down Expand Up @@ -491,9 +570,10 @@ public void initialiseGitRepo() throws IOException, GitAPIException {
if (!gitDir.exists()) {
String repo = getRemoteRepository();
if (Strings.isNotBlank(repo) && isCloneRemoteRepoOnStartup()) {
LOG.info("Cloning git repo " + repo + " into directory " + confDir.getCanonicalPath());
CloneCommand command = Git.cloneRepository().setCredentialsProvider(credentials).
setCloneAllBranches(cloneAllBranches).setURI(repo).setDirectory(confDir).setRemote(remote);
boolean cloneAll = isCloneAllBranches();
LOG.info("Cloning git repo " + repo + " into directory " + confDir.getCanonicalPath() + " cloneAllBranches: " + cloneAll);
CloneCommand command = Git.cloneRepository().setCredentialsProvider(getCredentials()).
setCloneAllBranches(cloneAll).setURI(repo).setDirectory(confDir).setRemote(remote);
try {
git = command.call();
return;
Expand All @@ -517,19 +597,30 @@ public void initialiseGitRepo() throws IOException, GitAPIException {
git = new Git(repository);

if (isPullOnStartup()) {
try {
git.pull().setCredentialsProvider(credentials).setRebase(true).call();
LOG.info("Performed a git pull to update the local configuration repository at " + confDir.getCanonicalPath());
} catch (Throwable e) {
LOG.error("Failed to pull from the remote git repo. Reason: " + e, e);
// lets just use an empty repo instead
}
doPull();
} else {
LOG.info("git pull from remote config repo on startup is disabled");
}
}
}

protected void doPull() {
CredentialsProvider cp = getCredentials();
try {
git.pull().setCredentialsProvider(cp).setRebase(true).call();
if (LOG.isDebugEnabled()) {
LOG.debug("Performed a git pull to update the local configuration repository at " + getConfigDirectory().getCanonicalPath());
}
} catch (Throwable e) {
String credText = "";
if (cp instanceof UsernamePasswordCredentialsProvider) {

}
LOG.error("Failed to pull from the remote git repo with credentials " + cp + ". Reason: " + e, e);
}
}


/**
* Returns the file for the given path
*/
Expand Down Expand Up @@ -562,17 +653,25 @@ protected <T> T gitOperation(PersonIdent personIdent, Callable<T> callable) {
hasHead = false;
}

// TODO pull if we have a remote repo

if (hasHead) {
// lets stash any local changes just in case..
git.stashCreate().setPerson(personIdent).setWorkingDirectoryMessage("Stash before a write").setRef("HEAD").call();
}
if (isPullBeforeOperation() && Strings.isNotBlank(getRemoteRepository())) {
doPull();
}
return callable.call();
} catch (Exception e) {
throw new RuntimeIOException(e);
}
}
}

public Boolean getCloneRemoteRepoOnStartup() {
return cloneRemoteRepoOnStartup;
}

public void setCloneRemoteRepoOnStartup(Boolean cloneRemoteRepoOnStartup) {
this.cloneRemoteRepoOnStartup = cloneRemoteRepoOnStartup;
}
}
Expand Up @@ -7,6 +7,8 @@
<ext:property name="hawtio.config.dir" value=""/>
<ext:property name="hawtio.config.repo" value="https://github.com/hawtio/hawtio-config.git"/>
<ext:property name="hawtio.config.pullOnStartup" value="true"/>
<ext:property name="hawtio.config.pullBeforeOperation" value="true"/>
<ext:property name="hawtio.config.pullTimePeriod" value="5000"/>
<ext:property name="hawtio.config.cloneOnStartup" value="true"/>
<ext:property name="hawtio.config.remote" value="origin"/>
</ext:default-properties>
Expand All @@ -16,6 +18,8 @@
<property name="configDirName" value="${hawtio.config.dir}"/>
<property name="remoteRepository" value="${hawtio.config.repo}"/>
<property name="pullOnStartup" value="${hawtio.config.pullOnStartup}"/>
<property name="pullBeforeOperation" value="${hawtio.config.pullBeforeOperation}"/>
<property name="pullTimePeriod" value="${hawtio.config.pullTimePeriod}"/>
<property name="cloneRemoteRepoOnStartup" value="${hawtio.config.cloneOnStartup}"/>
<property name="remote" value="${hawtio.config.remote}"/>
</bean>
Expand Down

0 comments on commit 59e4a57

Please sign in to comment.