Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added an api so we can get the current branches in git
  • Loading branch information
jstrachan committed May 16, 2013
1 parent d7e3fc4 commit b307f50
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
49 changes: 43 additions & 6 deletions hawtio-git/src/main/java/io/hawt/git/GitFacade.java
Expand Up @@ -10,14 +10,15 @@
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.InitCommand;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.diff.DiffEntry;
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.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
Expand All @@ -42,8 +43,10 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.SortedSet;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeSet;
import java.util.concurrent.Callable;

/**
Expand Down Expand Up @@ -83,31 +86,42 @@ public void init() throws Exception {
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());
if (LOG.isDebugEnabled()) {
LOG.debug("Pulled from remote repository " + getRemoteRepository());
}
return null;
}
};
TimerTask task = new TimerTask() {
@Override
public void run() {
gitOperation(stashPersonIdent, emptyCallable);
gitOperation(getStashPersonIdent(), emptyCallable);
}
};
t.schedule(task, timePeriod, timePeriod);
}
super.init();
}

public PersonIdent getStashPersonIdent() {
if (stashPersonIdent == null) {
stashPersonIdent = new PersonIdent("dummy", "dummy");
}
return stashPersonIdent;
}

public void setStashPersonIdent(PersonIdent stashPersonIdent) {
this.stashPersonIdent = stashPersonIdent;
}

@Override
protected String getDefaultObjectName() {
return "io.hawt.git:type=GitFacade";
}

public String getRemoteRepository() {
if (remoteRepository == null) {
remoteRepository = getSystemPropertyOrEnvironmentVariable("hawtio.config.repo", "HAWTIO_CONFIG_REPO");
Expand Down Expand Up @@ -347,6 +361,29 @@ public RevCommit call() throws Exception {
});
}

@Override
public List<String> branches() {
return gitOperation(getStashPersonIdent(), new Callable<List<String>>() {
@Override
public List<String> call() throws Exception {
SortedSet<String> names = new TreeSet<String>();

List<Ref> call = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
for (Ref ref : call) {
String name = ref.getName();
int idx = name.lastIndexOf('/');
if (idx >= 0) {
name = name.substring(idx + 1);
}
if (name.length() > 0) {
names.add(name);
}
}
return new ArrayList<String>(names);
}
});
}

protected RevCommit commitThenPush(CommitCommand commit) throws GitAPIException {
RevCommit answer = commit.call();
LOG.info("Committed " + answer);
Expand Down
4 changes: 4 additions & 0 deletions hawtio-git/src/main/java/io/hawt/git/GitFacadeMXBean.java
Expand Up @@ -50,4 +50,8 @@ void remove(String branch, String path, String commitMessage,
void revertTo(String branch, String objectId, String blobPath, String commitMessage,
String authorName, String authorEmail);

/**
* Returns all the branch names we can use in the local repo
*/
List<String> branches();
}

0 comments on commit b307f50

Please sign in to comment.