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

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

Introduction

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

Prototype

@Override
public Repository build() throws IOException 

Source Link

Document

Create a repository matching the configuration in this builder.

Usage

From source file:br.com.riselabs.cotonet.crawler.RepositoryCrawler.java

License:Open Source License

private Repository openRepository() throws IOException {
    // now open the resulting repository with a FileRepositoryBuilder
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    builder.setWorkTree(repositoryDir);/*from ww  w  . j a v a2s  .c  o  m*/
    // builder.readEnvironment(); // scan environment GIT_* variables
    // builder.findGitDir(); // scan up the file system tree
    builder.setMustExist(true);
    Repository repository = builder.build();
    return repository;
}

From source file:ch.dals.buildtools.git.core.GitUtil.java

License:Open Source License

public static String currentBranch(final File location) throws IOException {
    final FileRepositoryBuilder builder = new FileRepositoryBuilder();
    final File gitDir = builder.findGitDir(location).readEnvironment().getGitDir();
    if (gitDir == null) {
        throw new FileNotFoundException("Cannot find Git directory upwards from: '" + location + "'");
    }/*from   ww w .  ja v  a2s.  c o  m*/
    final Repository repository = builder.build();
    try {
        final String branchName = repository.getBranch();
        if (branchName == null) {
            throw new FileNotFoundException("Failed to find HEAD reference in Git directory: '" + gitDir + "'");
        }
        return branchName;
    } finally {
        repository.close();
    }
}

From source file:com.diffplug.gradle.spotless.GitAttributesLineEndingPolicy.java

License:Apache License

public static GitAttributesLineEndingPolicy create(File rootFolder) {
    return Errors.rethrow().get(() -> {
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        builder.findGitDir(rootFolder);/*from ww w  .  j av  a 2 s.  c  o m*/
        if (builder.getGitDir() != null) {
            // we found a repository, so we can grab all the values we need from it
            Repository repo = builder.build();
            AttributesNodeProvider nodeProvider = repo.createAttributesNodeProvider();
            Function<AttributesNode, List<AttributesRule>> getRules = node -> node == null
                    ? Collections.emptyList()
                    : node.getRules();
            return new GitAttributesLineEndingPolicy(repo.getConfig(),
                    getRules.apply(nodeProvider.getInfoAttributesNode()), repo.getWorkTree(),
                    getRules.apply(nodeProvider.getGlobalAttributesNode()));
        } else {
            // there's no repo, so it takes some work to grab the system-wide values
            Config systemConfig = SystemReader.getInstance().openSystemConfig(null, FS.DETECTED);
            Config userConfig = SystemReader.getInstance().openUserConfig(systemConfig, FS.DETECTED);
            if (userConfig == null) {
                userConfig = new Config();
            }

            List<AttributesRule> globalRules = Collections.emptyList();
            // copy-pasted from org.eclipse.jgit.lib.CoreConfig
            String globalAttributesPath = userConfig.getString(ConfigConstants.CONFIG_CORE_SECTION, null,
                    ConfigConstants.CONFIG_KEY_ATTRIBUTESFILE);
            // copy-pasted from org.eclipse.jgit.internal.storage.file.GlobalAttributesNode
            if (globalAttributesPath != null) {
                FS fs = FS.detect();
                File attributesFile;
                if (globalAttributesPath.startsWith("~/")) { //$NON-NLS-1$
                    attributesFile = fs.resolve(fs.userHome(), globalAttributesPath.substring(2));
                } else {
                    attributesFile = fs.resolve(null, globalAttributesPath);
                }
                globalRules = parseRules(attributesFile);
            }
            return new GitAttributesLineEndingPolicy(userConfig,
                    // no git info file
                    Collections.emptyList(), null, globalRules);
        }
    });
}

From source file:com.github.koraktor.mavanagaiata.git.jgit.JGitRepository.java

License:Open Source License

/**
 * Creates a new instance from a JGit repository object
 *
 * @param workTree The worktree of the repository or {@code null}
 * @param gitDir The GIT_DIR of the repository or {@code null}
 *///w w w . j av  a  2 s . c o m
public JGitRepository(File workTree, File gitDir) throws GitRepositoryException {
    FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
    repositoryBuilder.readEnvironment();

    if (gitDir == null && workTree == null) {
        throw new GitRepositoryException("Neither worktree nor GIT_DIR is set.");
    } else {
        if (workTree != null && !workTree.exists()) {
            throw new GitRepositoryException("The worktree " + workTree + " does not exist");
        }
        if (gitDir != null && !gitDir.exists()) {
            throw new GitRepositoryException("The GIT_DIR " + gitDir + " does not exist");
        }
    }

    repositoryBuilder.setWorkTree(workTree);
    if (gitDir != null) {
        repositoryBuilder.setGitDir(gitDir);
    } else {
        repositoryBuilder.findGitDir(workTree);

        if (repositoryBuilder.getGitDir() == null) {
            throw new GitRepositoryException(
                    workTree + " is not inside a Git repository. Please specify the GIT_DIR separately.");
        }
    }

    try {
        this.repository = repositoryBuilder.build();
    } catch (IOException e) {
        throw new GitRepositoryException("Could not initialize repository", e);
    }

    this.commitCache = new HashMap<ObjectId, RevCommit>();
}

From source file:com.github.koraktor.mavanagaiata.git.jgit.JGitRepositoryTest.java

License:Open Source License

@Before
public void setup() throws Exception {
    FileRepository tmpRepo = mock(FileRepository.class, RETURNS_DEEP_STUBS);
    FileRepositoryBuilder repoBuilder = mock(FileRepositoryBuilder.class);
    whenNew(FileRepositoryBuilder.class).withNoArguments().thenReturn(repoBuilder);
    when(repoBuilder.build()).thenReturn(tmpRepo);

    File someDir = mock(File.class);
    when(someDir.exists()).thenReturn(true);

    this.repo = mock(Repository.class, RETURNS_DEEP_STUBS);
    this.repository = new JGitRepository(someDir, someDir);
    this.repository.repository = this.repo;
}

From source file:com.spotify.docker.Git.java

License:Apache License

public Git() throws IOException {
    final FileRepositoryBuilder builder = new FileRepositoryBuilder();
    // scan environment GIT_* variables
    builder.readEnvironment();// w  w  w. ja  va2  s .  co m
    // scan up the file system tree
    builder.findGitDir();
    // if getGitDir is null, then we are not in a git repository
    repo = builder.getGitDir() == null ? null : builder.build();
}

From source file:com.spotify.docker.Utils.java

License:Apache License

public static String getGitCommitId()
        throws GitAPIException, DockerException, IOException, MojoExecutionException {

    final FileRepositoryBuilder builder = new FileRepositoryBuilder();
    builder.readEnvironment(); // scan environment GIT_* variables
    builder.findGitDir(); // scan up the file system tree

    if (builder.getGitDir() == null) {
        throw new MojoExecutionException("Cannot tag with git commit ID because directory not a git repo");
    }//from  ww  w.  j av  a  2s . com

    final StringBuilder result = new StringBuilder();
    final Repository repo = builder.build();

    try {
        // get the first 7 characters of the latest commit
        final ObjectId head = repo.resolve("HEAD");
        result.append(head.getName().substring(0, 7));
        final Git git = new Git(repo);

        // append first git tag we find
        for (Ref gitTag : git.tagList().call()) {
            if (gitTag.getObjectId().equals(head)) {
                // name is refs/tag/name, so get substring after last slash
                final String name = gitTag.getName();
                result.append(".");
                result.append(name.substring(name.lastIndexOf('/') + 1));
                break;
            }
        }

        // append '.DIRTY' if any files have been modified
        final Status status = git.status().call();
        if (status.hasUncommittedChanges()) {
            result.append(".DIRTY");
        }
    } finally {
        repo.close();
    }

    return result.length() == 0 ? null : result.toString();
}

From source file:de.codesourcery.gittimelapse.GitHelper.java

License:Apache License

public GitHelper(File currentWorkingDir) throws IOException {
    if (currentWorkingDir == null) {
        throw new IllegalArgumentException("currentWorkingDir must not be NULL");
    }//from ww  w . j  a  v  a  2s. co  m

    this.currentWorkingDir = currentWorkingDir;
    this.gitDir = findGitDir(this.currentWorkingDir);

    if (Main.DEBUG_MODE) {
        System.out.println("GIT repo: " + this.gitDir);
    }

    this.repoBaseDir = this.gitDir.getParentFile();

    if (Main.DEBUG_MODE) {
        System.out.println("Base dir: " + this.repoBaseDir);
    }

    final FileRepositoryBuilder builder = new FileRepositoryBuilder();
    builder.setGitDir(gitDir);
    builder.readEnvironment(); // scan environment GIT_* variables
    repository = builder.build();
}

From source file:eu.atos.paas.git.Repository.java

License:Open Source License

public Repository(File projectDir) throws IOException {

    if (!projectDir.isDirectory()) {
        throw new IllegalArgumentException(projectDir + " must be a directory");
    }//from   w ww .  j  a  v  a2s  . c  o m
    this.projectDir = Objects.requireNonNull(projectDir);

    FileRepositoryBuilder builder = new FileRepositoryBuilder().findGitDir(this.projectDir);

    if (builder.getGitDir() == null) {
        throw new IllegalArgumentException("Could not find a Git repository up from " + projectDir);
    }

    this.repo = builder.build();
}

From source file:GitBackend.GitAPI.java

License:Apache License

private Git openOrCreate(File localDirectory, String remoteRepo) throws IOException, GitAPIException {
    Git git = null;//from  www  . j  a  v a 2 s .  c  o m
    FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
    repositoryBuilder.addCeilingDirectory(localDirectory);
    repositoryBuilder.findGitDir(localDirectory);
    if (repositoryBuilder.getGitDir() == null) {
        // git = Git.init().setDirectory( gitDirectory.getParentFile() ).call();
        try {

            Git.cloneRepository().setURI(remoteRepo).setDirectory(localDirectory).call();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
    } else {
        git = new Git(repositoryBuilder.build());
    }
    return git;
}