Search in sources :

Example 1 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project bazel by bazelbuild.

the class GitCloner method isUpToDate.

private static boolean isUpToDate(GitRepositoryDescriptor descriptor) {
    // Initializing/checking status of/etc submodules cleanly is hard, so don't try for now.
    if (descriptor.initSubmodules) {
        return false;
    }
    Repository repository = null;
    try {
        repository = new FileRepositoryBuilder().setGitDir(descriptor.directory.getChild(Constants.DOT_GIT).getPathFile()).setMustExist(true).build();
        ObjectId head = repository.resolve(Constants.HEAD);
        ObjectId checkout = repository.resolve(descriptor.checkout);
        if (head != null && checkout != null && head.equals(checkout)) {
            Status status = Git.wrap(repository).status().call();
            if (!status.hasUncommittedChanges()) {
                // new_git_repository puts (only) BUILD and WORKSPACE, and
                // git_repository doesn't add any files.
                Set<String> untracked = status.getUntracked();
                if (untracked.isEmpty() || (untracked.size() == 2 && untracked.contains("BUILD") && untracked.contains("WORKSPACE"))) {
                    return true;
                }
            }
        }
    } catch (GitAPIException | IOException e) {
    // Any exceptions here, we'll just blow it away and try cloning fresh.
    // The fresh clone avoids any weirdness due to what's there and has nicer
    // error reporting.
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return false;
}
Also used : Status(org.eclipse.jgit.api.Status) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Example 2 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project gitblit by gitblit.

the class PushLogTest method testPushLog.

@Test
public void testPushLog() throws IOException {
    String name = "~james/helloworld.git";
    File gitDir = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, name), FS.DETECTED);
    Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
    List<RefLogEntry> pushes = RefLogUtils.getRefLog(name, repository);
    GitBlitSuite.close(repository);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) RefLogEntry(com.gitblit.models.RefLogEntry) Test(org.junit.Test)

Example 3 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project camel by apache.

the class AbstractGitConsumer method getLocalRepository.

private Repository getLocalRepository() throws IOException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = null;
    try {
        repo = // scan environment GIT_* variables
        builder.setGitDir(new File(endpoint.getLocalPath(), ".git")).readEnvironment().findGitDir().build();
    } catch (IOException e) {
        LOG.error("There was an error, cannot open " + endpoint.getLocalPath() + " repository");
        throw e;
    }
    return repo;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) IOException(java.io.IOException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) File(java.io.File)

Example 4 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project camel by apache.

the class GitTestSupport method getTestRepository.

protected Repository getTestRepository() throws IOException, IllegalStateException, GitAPIException {
    File gitRepo = new File(gitLocalRepo, ".git");
    Git.init().setDirectory(new File(gitLocalRepo, "")).setBare(false).call();
    // now open the resulting repository with a FileRepositoryBuilder
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = // scan
    builder.setGitDir(gitRepo).readEnvironment().findGitDir().build();
    return repo;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Example 5 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project Corgi by kevinYin.

the class JGitTest method testGetVersion.

@Test
public void testGetVersion() throws Exception {
    Repository existingRepo = new FileRepositoryBuilder().setGitDir(new File("/Users/kevin/beiliao/gitlab/platform-parent/.git")).build();
    Git git = new Git(existingRepo);
    CredentialsProvider cp = new UsernamePasswordCredentialsProvider("K-Priest", "kevin711");
    Collection<Ref> refs = git.lsRemote().setCredentialsProvider(cp).setRemote("origin").call();
    for (Ref ref : refs) {
        System.out.println(ref.getObjectId().getName());
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) File(java.io.File) Test(org.junit.Test)

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