Example usage for org.eclipse.jgit.storage.file FileRepositoryBuilder FileRepositoryBuilder

List of usage examples for org.eclipse.jgit.storage.file FileRepositoryBuilder FileRepositoryBuilder

Introduction

In this page you can find the example usage for org.eclipse.jgit.storage.file FileRepositoryBuilder FileRepositoryBuilder.

Prototype

FileRepositoryBuilder

Source Link

Usage

From source file:io.fabric8.maven.enricher.api.util.GitUtil.java

License:Apache License

public static Repository getGitRepository(MavenProject project) throws IOException {
    MavenProject rootProject = MavenUtil.getRootProject(project);
    File baseDir = rootProject.getBasedir();
    if (baseDir == null) {
        baseDir = project.getBasedir();/*from www  .  ja  v  a 2  s .  co  m*/
    }
    if (baseDir == null) {
        // TODO: Why is this check needed ?
        baseDir = new File(System.getProperty("basedir", "."));
    }
    File gitFolder = GitHelpers.findGitFolder(baseDir);
    if (gitFolder == null) {
        // No git repository found
        return null;
    }
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.readEnvironment().setGitDir(gitFolder).build();
    return repository;
}

From source file:io.fabric8.maven.HelmPushMojo.java

License:Apache License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (!isRootReactorBuild()) {
        getLog().info("Not the root reactor build so not committing changes");
        return;/*from   ww  w  . jav  a2 s.c  om*/
    }

    File outputDir = getHelmRepoFolder();
    if (!Files.isDirectory(outputDir)) {
        throw new MojoExecutionException(
                "No helm repository exists for " + outputDir + ". Did you run `mvn fabric8:helm` yet?");
    }
    File gitFolder = new File(outputDir, ".git");
    if (!Files.isDirectory(gitFolder)) {
        throw new MojoExecutionException(
                "No helm git repository exists for " + gitFolder + ". Did you run `mvn fabric8:helm` yet?");
    }
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Git git = null;

    try {
        Repository repository = builder.setGitDir(gitFolder).readEnvironment() // scan environment GIT_* variables
                .findGitDir() // scan up the file system tree
                .build();

        git = new Git(repository);

        git.add().addFilepattern(".").call();
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to add files to the helm git repository: " + e, e);
    }
    CommitCommand commit = git.commit().setAll(true).setMessage(commitMessage);
    PersonIdent author = null;
    if (Strings.isNotBlank(userName) && Strings.isNotBlank(emailAddress)) {
        author = new PersonIdent(userName, emailAddress);
    }
    if (author != null) {
        commit = commit.setAuthor(author);
    }

    try {
        RevCommit answer = commit.call();
        getLog().info("Committed " + answer.getId() + " " + answer.getFullMessage());
    } catch (GitAPIException e) {
        throw new MojoExecutionException("Failed to commit changes to help repository: " + e, e);
    }

    if (pushChanges) {
        PushCommand push = git.push();
        try {
            push.setRemote(remoteRepoName).call();

            getLog().info("Pushed commits upstream to " + getHelmGitUrl());
        } catch (GitAPIException e) {
            throw new MojoExecutionException(
                    "Failed to push helm git changes to remote repository " + remoteRepoName + ": " + e, e);
        }
    }

}

From source file:io.fabric8.maven.MigrateMojo.java

License:Apache License

private void tryAddFilesToGit(String filePattern) {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    try {//from w  w w.j  a  va 2  s .c  om
        Repository repository = builder.readEnvironment() // scan environment GIT_* variables
                .findGitDir() // scan up the file system tree
                .build();

        Git git = new Git(repository);
        git.add().addFilepattern(filePattern).call();
    } catch (Exception e) {
        getLog().warn("Failed to add generated files to the git repository: " + e, e);
    }
}

From source file:io.fabric8.openshift.agent.CartridgeGitRepository.java

License:Apache License

/**
 * Clones or pulls the remote repository and returns the directory with the checkout
 *///w ww.j a va 2  s  . co m
public void cloneOrPull(final String repo, final CredentialsProvider credentials) throws Exception {
    if (!localRepo.exists() && !localRepo.mkdirs()) {
        throw new IOException("Failed to create local repository");
    }
    File gitDir = new File(localRepo, ".git");
    if (!gitDir.exists()) {
        LOG.info("Cloning remote repo " + repo);
        CloneCommand command = Git.cloneRepository().setCredentialsProvider(credentials).setURI(repo)
                .setDirectory(localRepo).setRemote(remoteName);
        git = command.call();
    } else {
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(gitDir).readEnvironment() // scan environment GIT_* variables
                .findGitDir() // scan up the file system tree
                .build();

        git = new Git(repository);

        // update the remote repo just in case
        StoredConfig config = repository.getConfig();
        config.setString("remote", remoteName, "url", repo);
        config.setString("remote", remoteName, "fetch", "+refs/heads/*:refs/remotes/" + remoteName + "/*");

        String branch = "master";
        config.setString("branch", branch, "remote", remoteName);
        config.setString("branch", branch, "merge", "refs/heads/" + branch);

        try {
            config.save();
        } catch (IOException e) {
            LOG.error("Failed to save the git configuration to " + localRepo + " with remote repo: " + repo
                    + ". " + e, e);
        }

        // now pull
        LOG.info("Pulling from remote repo " + repo);
        git.pull().setCredentialsProvider(credentials).setRebase(true).call();
    }
}

From source file:io.fabric8.project.support.GitUtils.java

License:Apache License

/**
 * Returns the git repository for the current folder or null if none can be found
 */// w  ww.  j a  v a 2s.  c o  m
public static Repository findRepository(File baseDir) throws IOException {
    File gitFolder = io.fabric8.utils.GitHelpers.findGitFolder(baseDir);
    if (gitFolder == null) {
        // No git repository found
        return null;
    }
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.readEnvironment().setGitDir(gitFolder).build();
    return repository;
}

From source file:io.fabric8.vertx.maven.plugin.util.GitUtil.java

License:Apache License

/**
 * Get the git {@link Repository} if the the project has one
 *
 * @param project - the {@link MavenProject} whose Git repo needs to be returned in {@link Repository}
 * @return Repository  - the Git Repository of the project
 * @throws IOException - any error that might occur finding the Git folder
 *///w  w  w. j  av  a 2 s  . c om
public static Repository getGitRepository(MavenProject project) throws IOException {

    MavenProject rootProject = getRootProject(project);

    File baseDir = rootProject.getBasedir();

    if (baseDir == null) {
        baseDir = project.getBasedir();
    }

    File gitFolder = findGitFolder(baseDir);
    if (gitFolder == null) {
        // No git repository found
        return null;
    }
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.readEnvironment().setGitDir(gitFolder).build();
    return repository;
}

From source file:io.github.gitfx.util.GitFXGsonUtil.java

License:Apache License

public static GitRepoMetaData getGitRepositoryMetaData(String repoPath) {
    try {//from   www  . ja  v a  2 s  . com
        if (!repoPath.endsWith(".git"))
            repoPath = repoPath + "/.git";
        System.out.println("repopath:  " + repoPath);
        GitRepoMetaData gitMetaData = new GitRepoMetaData();
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(new File(repoPath)).readEnvironment().setMustExist(true)
                .findGitDir().build();
        RevWalk walk = new RevWalk(repository);
        AnyObjectId id = repository.resolve("HEAD");
        if (id == null)
            return null;//Empty repository
        RevCommit rCommit = walk.parseCommit(id);
        walk.markStart(rCommit);
        gitMetaData.setRepository(repository);
        gitMetaData.setRevWalk(walk);
        return gitMetaData;
    } catch (IOException exception) {
        logger.debug("IOException getGitRepositoryMetaData", exception);
    }
    ;
    return null;
}

From source file:io.github.thefishlive.updater.Updater.java

License:Open Source License

public void run() {
    System.out.println("-------------------------");
    System.out.println(gitDir.getAbsolutePath());
    File updateFile = new File(basedir, "UPDATE");
    Git git = null;//from   w  ww .j ava  2 s  .c o  m

    try {
        if (!gitDir.exists()) {
            git = Git.cloneRepository().setDirectory(basedir).setURI(GitUpdater.remote)
                    .setProgressMonitor(buildProgressMonitor()).call();

            System.out.println("Repository cloned");
        } else {
            updateFile.createNewFile();

            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            Repository repo = builder.setGitDir(gitDir).readEnvironment() // scan environment GIT_* variables
                    .findGitDir() // scan up the file system tree
                    .build();

            git = new Git(repo);

            PullResult result = git.pull().setProgressMonitor(buildProgressMonitor()).call();

            if (!result.isSuccessful() || result.getMergeResult().getMergeStatus().equals(MergeStatus.MERGED)) {
                System.out.println("Update Failed");
                FileUtils.deleteDirectory(basedir);
                basedir.mkdir();
                System.out.println("Re-cloning repository");

                git = Git.cloneRepository().setDirectory(basedir).setURI(GitUpdater.remote)
                        .setProgressMonitor(buildProgressMonitor()).call();

                System.out.println("Repository cloned");
            }

            System.out.println("State: " + result.getMergeResult().getMergeStatus());
        }

        File configdir = new File("config");

        if (configdir.exists()) {
            FileUtils.copyDirectory(configdir, new File(basedir, "config"));
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        git.getRepository().close();
    }

    updateFile.delete();
    System.out.println("-------------------------");
}

From source file:jbenchmarker.GitMain.java

License:Open Source License

private boolean gotMerge(String gitdir, String path) throws IOException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(new File(gitdir + "/.git")).readEnvironment().findGitDir().build();
    RevWalk revwalk = new RevWalk(repo);
    revwalk.setTreeFilter(AndTreeFilter.create(PathFilter.create(path), TreeFilter.ANY_DIFF));
    revwalk.markStart(revwalk.parseCommit(repo.resolve("HEAD")));
    Iterator<RevCommit> it = revwalk.iterator();
    while (it.hasNext()) {
        if (it.next().getParentCount() > 1) {
            return true;
        }//from   www.  jav  a2s .c  o  m
    }
    return false;
}

From source file:jbenchmarker.trace.git.GitTrace.java

License:Open Source License

public static GitTrace create(String gitdir, CouchConnector cc, String path, boolean cleanDB, boolean detectMaU,
        int ut, int mt) throws IOException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(new File(gitdir + "/.git")).readEnvironment().findGitDir().build();
    CouchDbInstance dbInstance = cc.getDbInstance();

    String prefix = clearName(gitdir, path), co = prefix + "_commit", pa = prefix + "_patch";
    CouchDbConnector dbcc = new StdCouchDbConnector(co, dbInstance);
    CouchDbConnector dbcp = new StdCouchDbConnector(pa, dbInstance);
    CommitCRUD commitCRUD;//from  w w w  .  j av a 2 s  .c o m
    PatchCRUD patchCRUD;

    if (cleanDB || !dbInstance.checkIfDbExists(new DbPath(co)) || !dbInstance.checkIfDbExists(new DbPath(pa))) {
        clearDB(dbInstance, co);
        clearDB(dbInstance, pa);
        commitCRUD = new CommitCRUD(dbcc);
        patchCRUD = new PatchCRUD(dbcp);
        GitExtraction ge = new GitExtraction(repo, commitCRUD, patchCRUD, GitExtraction.defaultDiffAlgorithm,
                path, detectMaU, ut, mt);
        ge.parseRepository();
        UpdBefore = ge.nbUpdBlockBefore;
        MoveBefore = ge.nbMoveBefore;
        MergeBefore = ge.nbrMergeBefore;
        returnStat = ge.returnLastStat;
        commitRevert = ge.commitReverted;
    } else {
        commitCRUD = new CommitCRUD(dbcc);
        patchCRUD = new PatchCRUD(dbcp);
    }

    return new GitTrace(commitCRUD, patchCRUD, detectMaU, ut, mt);
}