Search in sources :

Example 21 with FileRepositoryBuilder

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);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Example 22 with FileRepositoryBuilder

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);
}
Also used : Path(java.nio.file.Path) GHRepository(org.kohsuke.github.GHRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) ObjectId(org.eclipse.jgit.lib.ObjectId) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 23 with FileRepositoryBuilder

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;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 24 with FileRepositoryBuilder

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;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Git(org.eclipse.jgit.api.Git) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) TransportException(org.eclipse.jgit.errors.TransportException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) TransportException(org.eclipse.jgit.errors.TransportException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Example 25 with FileRepositoryBuilder

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;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)32 Repository (org.eclipse.jgit.lib.Repository)30 File (java.io.File)25 Git (org.eclipse.jgit.api.Git)12 IOException (java.io.IOException)8 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)6 ObjectId (org.eclipse.jgit.lib.ObjectId)4 Path (java.nio.file.Path)3 CloneCommand (org.eclipse.jgit.api.CloneCommand)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 Ref (org.eclipse.jgit.lib.Ref)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)3 URIish (org.eclipse.jgit.transport.URIish)3 RefLogEntry (com.gitblit.models.RefLogEntry)2 Reader (java.io.Reader)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 TaskAction (org.gradle.api.tasks.TaskAction)2 Test (org.junit.Test)2 GHRepository (org.kohsuke.github.GHRepository)2