use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project tutorials by eugenp.
the class OpenRepository method main.
public static void main(String[] args) throws IOException, GitAPIException {
// first create a test-repository, the return is including the .get directory here!
File repoDir = createSampleGitRepo();
// now open the resulting repository with a FileRepositoryBuilder
FileRepositoryBuilder builder = new FileRepositoryBuilder();
try (Repository repository = builder.setGitDir(repoDir).readEnvironment().findGitDir().build()) {
System.out.println("Having repository: " + repository.getDirectory());
// the Ref holds an ObjectId for any type of object (tree, commit, blob, tree)
Ref head = repository.exactRef("refs/heads/master");
System.out.println("Ref of refs/heads/master: " + head);
}
}
use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project contribution by checkstyle.
the class NotesBuilder method getCommitsBetweenReferences.
/**
* Returns a list of commits between two references.
* @param repoPath path to local git repository.
* @param startRef start reference.
* @param endRef end reference.
* @return a list of commits.
* @throws IOException if I/O error occurs.
* @throws GitAPIException if an error occurs when accessing Git API.
* @noinspection ResultOfMethodCallIgnored
*/
private static Set<RevCommit> getCommitsBetweenReferences(String repoPath, String startRef, String endRef) throws IOException, GitAPIException {
final FileRepositoryBuilder builder = new FileRepositoryBuilder();
final Path path = Paths.get(repoPath);
final Repository repo = builder.findGitDir(path.toFile()).readEnvironment().build();
final ObjectId startCommit = getActualRefObjectId(repo, startRef);
Verify.verifyNotNull(startCommit, "Start reference \"" + startRef + "\" is invalid!");
final ObjectId endCommit = getActualRefObjectId(repo, endRef);
final Iterable<RevCommit> commits = new Git(repo).log().addRange(startCommit, endCommit).call();
return Sets.newLinkedHashSet(commits);
}
use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project checkstyle by checkstyle.
the class CommitValidationTest method getCommitsToCheck.
private static List<RevCommit> getCommitsToCheck() throws Exception {
final List<RevCommit> commits;
try (Repository repo = new FileRepositoryBuilder().findGitDir().build()) {
final RevCommitsPair revCommitsPair = resolveRevCommitsPair(repo);
if (COMMITS_RESOLUTION_MODE == CommitsResolutionMode.BY_COUNTER) {
commits = getCommitsByCounter(revCommitsPair.getFirst());
commits.addAll(getCommitsByCounter(revCommitsPair.getSecond()));
} else {
commits = getCommitsByLastCommitAuthor(revCommitsPair.getFirst());
commits.addAll(getCommitsByLastCommitAuthor(revCommitsPair.getSecond()));
}
}
return commits;
}
use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project bndtools by bndtools.
the class GitUtils method getRepository.
public static synchronized Repository getRepository(File gitRoot, String branch, String gitUrl, String gitPushUrl) throws IOException, ConfigInvalidException, JGitInternalException, GitAPIException {
File dotGit;
if (gitRoot.getName().equals(Constants.DOT_GIT)) {
dotGit = gitRoot;
} else {
dotGit = new File(gitRoot, Constants.DOT_GIT);
}
Repository repository = localRepos.get(dotGit);
if (repository != null && dotGit.exists()) {
return repository;
}
if (!dotGit.exists()) {
Git.cloneRepository().setDirectory(gitRoot).setCloneAllBranches(true).setURI(gitUrl).call();
FileBasedConfig config = new FileBasedConfig(new File(dotGit, "config"), FS.DETECTED);
config.load();
if (gitPushUrl != null) {
config.setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin", "pushurl", gitPushUrl);
}
config.save();
}
FileRepositoryBuilder builder = new FileRepositoryBuilder();
repository = builder.setGitDir(dotGit).readEnvironment().findGitDir().build();
localRepos.put(dotGit, repository);
try {
repository.incrementOpen();
Git git = Git.wrap(repository);
// Check branch
boolean pull = true;
String currentBranch = repository.getBranch();
if (branch != null && !branch.equals(currentBranch)) {
CheckoutCommand checkout = git.checkout();
if (!branchExists(git, branch)) {
checkout.setCreateBranch(true);
pull = false;
}
checkout.setName(branch);
checkout.call();
}
if (pull) {
git.pull().call();
} else {
git.fetch().call();
}
} catch (Exception e) {
if (!(e.getCause() instanceof TransportException)) {
throw new RuntimeException(e);
}
} finally {
if (repository != null) {
repository.close();
}
}
return repository;
}
use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project sevntu.checkstyle by sevntu-checkstyle.
the class CommitValidationTest method getCommitsToCheck.
private static List<RevCommit> getCommitsToCheck() throws Exception {
final List<RevCommit> commits;
try (Repository repo = new FileRepositoryBuilder().findGitDir().build()) {
final RevCommitsPair revCommitsPair = resolveRevCommitsPair(repo);
if (COMMITS_RESOLUTION_MODE == CommitsResolutionMode.BY_COUNTER) {
commits = getCommitsByCounter(revCommitsPair.getFirst());
commits.addAll(getCommitsByCounter(revCommitsPair.getSecond()));
} else {
commits = getCommitsByLastCommitAuthor(revCommitsPair.getFirst());
commits.addAll(getCommitsByLastCommitAuthor(revCommitsPair.getSecond()));
}
}
return commits;
}
Aggregations