Skip to content

Instantly share code, notes, and snippets.

@jasonish
Created October 29, 2014 22:59
Show Gist options
  • Select an option

  • Save jasonish/5d363f357b9928629786 to your computer and use it in GitHub Desktop.

Select an option

Save jasonish/5d363f357b9928629786 to your computer and use it in GitHub Desktop.
Playing with JGit
package rulemanager;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
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.treewalk.TreeWalk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class GitRulesetRepository {
private Logger logger = LoggerFactory.getLogger(getClass());
private File root;
private File bareRepositoryPath;
private Git bareRepository;
public GitRulesetRepository(File root) throws IOException, GitAPIException {
this.root = root;
this.bareRepositoryPath = new File(root, "rulesets.git");
if (!this.bareRepositoryPath.exists()) {
logger.info("Creating bare repository {}", this.bareRepositoryPath);
initializeBareRepository();
}
this.bareRepository = Git.open(bareRepositoryPath);
}
public void initializeBareRepository() throws IOException, GitAPIException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(bareRepositoryPath).build();
repository.create();
Git master = Git.cloneRepository()
.setURI(bareRepositoryPath.toURI().toString())
.setDirectory(new File(root, "master"))
.setCloneAllBranches(true)
.setRemote("origin")
.call();
master.commit()
.setMessage("Initial commit.")
.setAuthor("server", "server")
.call();
master.push().setRemote("origin").call();
master.close();
repository.close();
}
public List<Ref> getBranches() throws GitAPIException {
List<Ref> branches = bareRepository.branchList().call();
return branches;
}
public List<String> getFilenames(String refName) throws IOException {
Repository repository = bareRepository.getRepository();
Ref ref = repository.getRef(refName);
RevWalk revWalk = new RevWalk(repository);
RevCommit revCommit = revWalk.parseCommit(ref.getObjectId());
RevTree revTree = revCommit.getTree();
TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.addTree(revTree);
treeWalk.setRecursive(true);
List<String> filenames = new ArrayList<>();
while (treeWalk.next()) {
filenames.add(treeWalk.getPathString());
}
return filenames;
}
public Git getRepositoryForBranch(String name) throws GitAPIException, IOException {
File path = new File(root, name);
Git repo;
if (!path.exists()) {
logger.info("Creating {}", path);
repo = Git.cloneRepository()
.setURI(this.bareRepositoryPath.toURI().toString())
.setDirectory(path)
.call();
repo.checkout()
.setOrphan(true)
.setCreateBranch(true)
.setName(name)
.call();
} else {
repo = Git.open(path);
}
return repo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment