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:net.aprendizajengrande.gitrecommender.UpdateLog.java

License:Open Source License

public static void main(String[] args) throws Exception {

    if (args.length == 0) {
        System.err.println("Usage: UpdateLog <git dir> <db dir>");
        System.exit(-1);/*from   w ww.  j av  a 2 s.c om*/
    }

    File gitDir = new File(args[0]);
    File dbDir = new File(args[1]);

    DB db = new DB(dbDir);

    System.out.println("Git dir: " + gitDir);
    System.out.println("DB dir: " + dbDir);

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

    Git git = new Git(repository);
    Iterable<RevCommit> log = git.log().call();

    // go through all commits and process them in reverse order
    List<RevCommit> allCommits = new ArrayList<RevCommit>();

    int newCommits = 0;
    boolean seen = false;
    for (RevCommit commit : log) {
        String name = commit.name();
        if (db.lastCommit().equals(name)) {
            seen = true;
        }
        if (!seen)
            newCommits++;

        allCommits.add(commit);
    }
    System.out.println("Found " + allCommits.size() + " commits (" + newCommits + " new).");

    int commitNum = 0;
    List<Integer> files = new ArrayList<Integer>();
    for (int i = newCommits - 1; i >= 0; i--) {
        if (commitNum % 500 == 0)
            db.save();

        RevCommit commit = allCommits.get(i);

        String author = commit.getAuthorIdent().getName();
        int authorId = db.idAuthor(author);

        files.clear();

        if (i < allCommits.size() - 1) {
            AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, allCommits.get(i + 1));
            AbstractTreeIterator newTreeParser = prepareTreeParser(repository, commit);
            // then the procelain diff-command returns a list of diff
            // entries
            List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call();
            for (DiffEntry entry : diff) {
                // System.out.println("\tFile: " + entry.getNewPath());
                String file = entry.getNewPath();
                files.add(db.idFile(file));
            }
        }
        db.observeCommit(commit.name(), authorId, files);
    }

    db.save();
    repository.close();
}

From source file:net.fatlenny.datacitation.service.GitCitationDBService.java

License:Apache License

private void prepareGitRepository() {
    File gitFolder = new File(databaseLocation, GIT_FOLDER);
    LOG.debug("Using {} as database location.", databaseLocation);

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    try {//  w ww  .ja  v a 2 s  .c o  m
        repository = builder.setGitDir(gitFolder).build();
        if (!Files.exists(gitFolder.toPath())) {
            repository.create();
        }
    } catch (IOException e) {
        throw new CitationDBException("Error creating repository", e);
    }
}

From source file:net.morimekta.gittool.GitTool.java

License:Apache License

public Repository getRepository() throws IOException {
    if (repository == null) {
        repository = new FileRepositoryBuilder().setGitDir(new File(getRepositoryRoot(), DOT_GIT)).build();
    }/*from w ww  . j  a v  a2  s .c  o  m*/
    return repository;
}

From source file:net.orpiske.ssps.common.scm.git.GitSCM.java

License:Apache License

private void update(final File repositoryDir) throws ScmUpdateException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository;/* w  ww.j  a  v a 2  s .co  m*/

    try {
        repository = builder.setGitDir(repositoryDir).readEnvironment().findGitDir().build();
    } catch (IOException e) {
        throw new ScmUpdateException(e.getMessage(), e);
    }

    Git git = new Git(repository);
    PullCommand pullCommand = git.pull();

    pullCommand.setProgressMonitor(new TextProgressMonitor());

    try {
        pullCommand.call();
    } catch (Exception e) {
        e.printStackTrace();

        throw new ScmUpdateException(e.getMessage(), e);
    }
}

From source file:net.orpiske.ssps.common.scm.git.GitSCM.java

License:Apache License

/**
 * Access the repository/*from ww w .  ja v  a  2  s  .c  om*/
 * @param file the repository directory or file
 * @return A repository pointer object
 * @throws ScmAccessException
 */
private Repository accessRepository(final File file) throws ScmAccessException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository;

    try {
        if (file.isDirectory()) {
            repository = builder.setGitDir(file).readEnvironment().findGitDir().build();
        } else {
            repository = builder.setGitDir(file.getParentFile()).readEnvironment().findGitDir().build();
        }

    } catch (IOException e) {
        throw new ScmAccessException(e.getMessage(), e);
    }
    return repository;
}

From source file:net.polydawn.mdm.Mdm.java

License:Open Source License

/**
 * Like the main method (does full args parsing, takes your cwd as serious
 * business, etc) except passes out exceptions instead of logging or halting the
 * jvm, so it can be used in tests./*  ww w.jav  a2 s .  co  m*/
 * @throws Exception
 */
public static MdmExitMessage run(String... args) throws Exception {
    // find the repo to operate on
    Repository repo = null;
    try {
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        builder.findGitDir();
        if (builder.getGitDir() != null)
            repo = builder.build();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    // parse args
    ArgumentParser parser = new MdmArgumentParser().parser;
    Namespace parsedArgs = null;
    try {
        parsedArgs = parser.parseArgs(args);
    } catch (HelpScreenException e) {
        if (real)
            System.exit(0);
        return null;
    } catch (MdmArgumentParser.VersionExit e) {
        if (real)
            System.exit(0);
        return null;
    } catch (ArgumentParserException e) {
        parser.handleError(e);
        if (real)
            System.exit(1);
        return null;
    }

    // fire command
    MdmCommand cmd = getCommand(parsedArgs.getString("subcommand"), repo, parsedArgs);
    cmd.parse(parsedArgs);
    cmd.validate();
    return cmd.call();
}

From source file:net.polydawn.mdm.MdmModuleRelease.java

License:Open Source License

/**
 * Open the repo at given path, throw if there's no repo or if it's missing the
 * telltales of being an mdm release repo.
 *
 * @param relRepoPath path to repository to load.  Doubles as name.
 * @return a repository that smells like a proper mdm release repo.
 * @throws MdmRepositoryNonexistant if there's no repository there
 * @throws MdmRepositoryIOException if there were errors reading the repository
 * @throws MdmModuleTypeException if the repository doesn't look like an {@link MdmModuleRelease}.
 *//*from w w w .j  ava 2  s.c o m*/
public static MdmModuleRelease load(String relRepoPath)
        throws MdmRepositoryNonexistant, MdmRepositoryIOException, MdmModuleTypeException {
    Repository relRepo;
    try {
        relRepo = new FileRepositoryBuilder().setWorkTree(new File(relRepoPath).getCanonicalFile()) // must use getCanonicalFile to work around https://bugs.eclipse.org/bugs/show_bug.cgi?id=407478
                .build();
    } catch (IOException e) {
        throw new MdmRepositoryIOException(false, relRepoPath, e);
    }
    if (relRepo == null) // check that releases-repo is a git repo at all
        throw new MdmRepositoryNonexistant(relRepoPath);
    return new MdmModuleRelease(relRepo, relRepoPath, null, null, null);
}

From source file:net.tietema.versioning.GitJavaVersioning.java

License:Apache License

public String getRevision(File projectDir) throws MojoExecutionException {
    // XXX we use our own findGitDir because they JGit one doesn't find the git dir in a multi project build
    File gitDir = findGitDir(projectDir);
    String revision = "Unknown";
    if (gitDir == null)
        return revision;

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = null;//from ww  w  . j a v  a 2s.  c  o m
    try {
        repository = builder.setGitDir(gitDir).readEnvironment() // scan environment GIT_* variables
                .findGitDir(projectDir) // scan up the file system tree
                .build();

        log.info("Git dir: " + gitDir.toString());
        RepositoryState state = repository.getRepositoryState();
        log.info(state.getDescription());
        String branch = repository.getBranch();
        log.info("Branch is: " + branch);
        Git git = new Git(repository);
        String fullBranch = repository.getFullBranch();
        log.info("Full branch is: " + fullBranch);
        ObjectId id = repository.resolve(fullBranch);
        log.info("Branch " + repository.getBranch() + " points to " + id.name());

        Status status = git.status().call();
        boolean strictClean = status.isClean();
        // no untracked files
        boolean loseClean = status.getAdded().isEmpty() && status.getChanged().isEmpty()
                && status.getConflicting().isEmpty() && status.getMissing().isEmpty()
                && status.getModified().isEmpty() && status.getRemoved().isEmpty();

        StringWriter buffer = new StringWriter();
        JavaWriter writer = new JavaWriter(buffer);
        writer.emitPackage(packageName)
                .beginType(packageName + "." + className, "class", Modifier.PUBLIC | Modifier.FINAL)
                .emitField("String", "BRANCH", Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC,
                        String.format(Locale.US, "\"%s\"", branch))
                .emitField("String", "REVISION", Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC,
                        String.format(Locale.US, "\"%s\"", id.name()))
                .emitField("String", "REVISION_SHORT", Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC,
                        String.format(Locale.US, "\"%s\"", id.name().substring(0, 8)))
                .emitJavadoc("Strict Clean means no changes, not even untracked files")
                .emitField("boolean", "STRICT_CLEAN", Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC,
                        (strictClean ? "true" : "false"))
                .emitJavadoc("Lose Clean means no changes except untracked files.")
                .emitField("boolean", "LOSE_CLEAN", Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC,
                        (loseClean ? "true" : "false"))
                .endType();
        revision = buffer.toString();

        return revision;
    } catch (IOException e) {
        log.error(e);
        throw new MojoExecutionException(e.getMessage());
    } catch (GitAPIException e) {
        log.error(e);
        throw new MojoExecutionException(e.getMessage());
    } finally {
        if (repository != null)
            repository.close();
    }
}

From source file:net.vexelon.jdevlog.vcs.git.GitSource.java

License:Open Source License

@Override
public void initialize() throws SCMException {

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    File repoPath = new File(configuration.get(ConfigOptions.SOURCE));

    try {//w w  w .java  2  s  .  c  o m
        //         repository = builder.setGitDir(repoPath).build();
        repository = RepositoryCache.open(RepositoryCache.FileKey.lenient(repoPath, FS.DETECTED), true);
    } catch (IOException e) {
        throw new SCMException("Could not locate Git repository in " + repoPath.getAbsolutePath(), e);
    }
}

From source file:nl.architolk.ldt.license.GitLookUp.java

public GitLookUp(File anyFile) throws IOException {
    super();//w ww.ja v a  2s .c om
    this.repository = new FileRepositoryBuilder().findGitDir(anyFile).build();
    /* A workaround for  https://bugs.eclipse.org/bugs/show_bug.cgi?id=457961 */
    this.repository.getObjectDatabase().newReader().getShallowCommits();
    this.pathResolver = new GitPathResolver(repository.getWorkTree().getAbsolutePath());
}