Search in sources :

Example 6 with FileRepositoryBuilder

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

the class RepositoryFinder method findInDirectory.

private void findInDirectory(final IContainer container, final File path) {
    if (GitTraceLocation.CORE.isActive())
        GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), // $NON-NLS-1$
        "Looking at candidate dir: " + path);
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    File parent = path.getParentFile();
    if (parent != null)
        builder.addCeilingDirectory(parent);
    builder.findGitDir(path);
    File gitDir = builder.getGitDir();
    if (gitDir != null)
        register(container, gitDir);
}
Also used : FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) File(java.io.File)

Example 7 with FileRepositoryBuilder

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

the class XdocPublisher method publish.

/**
 * Publish release notes.
 * @throws IOException if problem with access to files appears.
 * @throws GitAPIException for problems with jgit.
 */
public void publish() throws IOException, GitAPIException {
    changeLocalRepoXdoc();
    final FileRepositoryBuilder builder = new FileRepositoryBuilder();
    final File localRepo = new File(localRepoPath);
    final Repository repo = builder.findGitDir(localRepo).readEnvironment().build();
    final Git git = new Git(repo);
    git.add().addFilepattern(PATH_TO_XDOC_IN_REPO).call();
    git.commit().setMessage(String.format(Locale.ENGLISH, COMMIT_MESSAGE_TEMPLATE, releaseNumber)).call();
    if (doPush) {
        final CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(authToken, "");
        git.push().setCredentialsProvider(credentialsProvider).call();
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) 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)

Example 8 with FileRepositoryBuilder

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

the class GitService method getOrCheckoutRepository.

/**
 * Get the local repository for a given remote repository URL.
 * If the local repo does not exist yet, it will be checked out.
 *
 * @param repoUrl The remote repository.
 * @return
 * @throws IOException
 * @throws GitAPIException
 */
public Repository getOrCheckoutRepository(URL repoUrl) throws IOException, GitAPIException {
    Path localPath = new java.io.File(REPO_CLONE_PATH + folderNameForRepositoryUrl(repoUrl)).toPath();
    // check if Repository object already created and available in cachedRepositories
    if (cachedRepositories.containsKey(localPath)) {
        return cachedRepositories.get(localPath);
    }
    // Check if the repository is already checked out on the server
    if (!Files.exists(localPath)) {
        // Repository is not yet available on the server
        // We need to check it out from the remote repository
        log.info("Cloning from " + repoUrl + " to " + localPath);
        Git result = Git.cloneRepository().setURI(repoUrl.toString()).setCredentialsProvider(new UsernamePasswordCredentialsProvider(GIT_USER, GIT_PASSWORD)).setDirectory(localPath.toFile()).call();
        result.close();
    } else {
        log.info("Repository at " + localPath + " already exists");
    }
    // Open the repository from the filesystem
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    builder.setGitDir(new java.io.File(localPath + "/.git")).readEnvironment().findGitDir().setup();
    // Create the JGit repository object
    Repository repository = new Repository(builder);
    repository.setLocalPath(localPath);
    // Cache the JGit repository object for later use
    // Avoids the expensive re-opening of local repositories
    cachedRepositories.put(localPath, repository);
    return repository;
}
Also used : Path(java.nio.file.Path) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Repository(de.tum.in.www1.artemis.domain.Repository) Git(org.eclipse.jgit.api.Git) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) File(de.tum.in.www1.artemis.domain.File)

Example 9 with FileRepositoryBuilder

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

the class ConfigGuiceModule method getRepository.

@Provides
@Singleton
@Named("config")
public ConfigRepository getRepository(@Named("repo.config.path") final File workingDirectory, @Named("repo.config.force-reclone-on-startup") final boolean reclone, @Named("repo.config.remote") final String remote) throws IOException, URISyntaxException, GitAPIException {
    log.info("Repo path " + workingDirectory + ", reclone: " + reclone);
    if (reclone && workingDirectory.exists()) {
        log.info("Recloning " + workingDirectory);
        File[] files = workingDirectory.listFiles();
        if (files != null)
            for (File file : files) {
                FileUtils.deleteQuietly(file);
                if (!file.exists()) {
                    throw new RuntimeException("Tried to delete local checkout contents but did not succeed. File was: " + file);
                }
            }
    }
    final File gitDir = new File(workingDirectory, ".git");
    final boolean newlyCreated;
    // Create the git repository if it doesn't already exist
    if (!gitDir.exists()) {
        log.info("Initialising new git dir at: " + workingDirectory);
        FileUtils.forceMkdir(workingDirectory);
        InitCommand init = new InitCommand();
        init.setBare(false).setDirectory(workingDirectory).setGitDir(gitDir).call();
        newlyCreated = true;
    } else {
        newlyCreated = false;
    }
    FileRepositoryBuilder frb = new FileRepositoryBuilder();
    Repository repo = frb.setGitDir(gitDir).readEnvironment().findGitDir().build();
    final boolean hasRemote = !remote.equalsIgnoreCase("none");
    final CredentialsProvider credentials;
    if (hasRemote) {
        // Try to extract username/password from the remote URL
        final URIish uri = new URIish(remote);
        if (uri.getUser() != null && uri.getPass() != null)
            credentials = new UsernamePasswordCredentialsProvider(uri.getUser(), uri.getPass());
        else
            credentials = null;
    } else {
        credentials = null;
    }
    if (newlyCreated) {
        // Add the remote and pull from it
        if (hasRemote) {
            RepoHelper.addRemote(repo, "origin", remote);
            RepoHelper.pull(repo, "origin", credentials);
        }
        Git git = new Git(repo);
        // If there are no commits in this repository, create one
        if (!git.log().setMaxCount(1).call().iterator().hasNext()) {
            git.commit().setAll(true).setAuthor("system", "system@localhost").setMessage("initial commit").call();
            if (hasRemote)
                RepoHelper.push(repo, "origin", credentials);
        }
    }
    return new ConfigRepository(repo, hasRemote, credentials);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) ConfigRepository(com.peterphi.configuration.service.git.ConfigRepository) Repository(org.eclipse.jgit.lib.Repository) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) InitCommand(org.eclipse.jgit.api.InitCommand) ConfigRepository(com.peterphi.configuration.service.git.ConfigRepository) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) Named(com.google.inject.name.Named) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 10 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project maven-git-versioning-extension by qoomon.

the class App method main.

public static void main(String[] args) throws Exception {
    Repository repository = new FileRepositoryBuilder().findGitDir(new File("/Users/bengt.brodersen/Workspace/git-sandbox")).build();
    ObjectId head = repository.resolve(Constants.HEAD);
    Optional<String> headTag = repository.getTags().values().stream().map(repository::peel).filter(ref -> {
        ObjectId objectId;
        if (ref.getPeeledObjectId() != null) {
            objectId = ref.getPeeledObjectId();
        } else {
            objectId = ref.getObjectId();
        }
        return objectId.equals(head);
    }).map(ref -> ref.getName().replaceFirst("^refs/tags/", "")).sorted((tagLeft, tagRight) -> {
        DefaultArtifactVersion tagVersionLeft = new DefaultArtifactVersion(tagLeft);
        DefaultArtifactVersion tagVersionRight = new DefaultArtifactVersion(tagRight);
        return Math.negateExact(tagVersionLeft.compareTo(tagVersionRight));
    }).findFirst();
    if (headTag.isPresent()) {
        System.out.println(headTag.get());
    } else {
        System.out.println("no tag");
    }
}
Also used : FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) Optional(java.util.Optional) Constants(org.eclipse.jgit.lib.Constants) Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) ObjectId(org.eclipse.jgit.lib.ObjectId) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) File(java.io.File)

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