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:org.eclipse.egit.internal.relengtools.RepoCheck.java

License:Open Source License

public void build() throws IOException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    builder.setWorkTree(repoDir);// ww w. ja  v a 2 s.  c  om
    repo = builder.readEnvironment().findGitDir(repoDir).build();
}

From source file:org.eclipse.emf.compare.doc.WikiTextToHTML.java

License:Open Source License

private String gitDescribe() {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    try {//from w w w. ja v a  2 s . c  om
        Repository repo = builder.setWorkTree(new File(".")).readEnvironment() // scan environment GIT_* variables
                .findGitDir() // scan up the file system tree
                .build();
        Git git = new Git(repo);
        DescribeCommand command = git.describe();
        return command.call();
    } catch (IOException e) {
        new RuntimeException(e);
    } catch (GitAPIException e) {
        new RuntimeException(e);
    }
    return "";
}

From source file:org.eclipse.orion.server.git.GitFileDecorator.java

License:Open Source License

/**
 * If this  server is configured to use git by default, then each project creation that is not
 * already in a repository needs to have a git -init performed to initialize the repository.
 *//*from   w  ww .  j  a  v  a  2 s .c o m*/
private void initGitRepository(HttpServletRequest request, IPath targetPath, JSONObject representation) {
    //project creation URL is of the form POST /workspace/<id>
    if (!(targetPath.segmentCount() == 2 && "workspace".equals(targetPath.segment(0)) //$NON-NLS-1$
            && Method.POST.equals(Method.fromString(request.getMethod()))))
        return;
    String scm = PreferenceHelper.getString(ServerConstants.CONFIG_FILE_DEFAULT_SCM, "").toLowerCase(); //$NON-NLS-1$
    if (!"git".equals(scm)) //$NON-NLS-1$
        return;
    try {
        IFileStore store = WebProject.fromId(representation.optString(ProtocolConstants.KEY_ID))
                .getProjectStore();
        //create repository in each project if it doesn't already exist
        File localFile = store.toLocalFile(EFS.NONE, null);
        File gitDir = GitUtils.getGitDir(localFile);
        if (gitDir == null) {
            gitDir = new File(localFile, Constants.DOT_GIT);
            FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
            repo.create();
            //we need to perform an initial commit to workaround JGit bug 339610.
            Git git = new Git(repo);
            git.add().addFilepattern(".").call(); //$NON-NLS-1$
            git.commit().setMessage("Initial commit").call();
        }
    } catch (Exception e) {
        //just log it - this is not the purpose of the file decorator
        LogHelper.log(e);
    }
}

From source file:org.eclipse.osee.ote.version.git.GitVersionBase.java

License:Open Source License

protected Repository buildRepository(File gitFolder) throws IOException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.setGitDir(gitFolder).readEnvironment().findGitDir().build();
    repository.resolve("HEAD");
    return repository;
}

From source file:org.eclipse.ptp.internal.rdt.sync.git.core.JGitRepo.java

License:Open Source License

/**
 * Build the JGit repository - creating resources, such as Git-specific files, if necessary.
 *
 * @param localDirectory//  w w w .  ja  va  2s . com
 *          Repository location
 * @param monitor
 * @return
 * @throws GitAPIException
 *          on JGit-specific problems
 * @throws IOException
 *          on file system problems
 */
private Git buildRepo(String localDirectory, IProgressMonitor monitor) throws GitAPIException, IOException {
    final RecursiveSubMonitor subMon = RecursiveSubMonitor.convert(monitor, 10);
    try {
        // Get local Git repository, creating it if necessary.
        File localDir = new File(localDirectory);
        FileRepositoryBuilder repoBuilder = new FileRepositoryBuilder();
        File gitDirFile = new File(localDirectory + File.separator + GitSyncService.gitDir);
        Repository repository = repoBuilder.setWorkTree(localDir).setGitDir(gitDirFile).build();
        boolean repoExists = gitDirFile.exists();
        if (!repoExists) {
            repository.create(false);
        }
        git = new Git(repository);

        if (!repoExists) {
            fileFilter = new GitSyncFileFilter(this, SyncManager.getDefaultFileFilter());
            fileFilter.saveFilter();
        } else {
            fileFilter = new GitSyncFileFilter(this);
            fileFilter.loadFilter();
        }
        subMon.worked(5);

        // An initial commit to create the master branch.
        subMon.subTask(Messages.JGitRepo_0);
        if (!repoExists) {
            commit(subMon.newChild(4));
        } else {
            subMon.worked(4);
        }

        return git;
    } finally {
        if (monitor != null) {
            monitor.done();
        }
    }
}

From source file:org.eclipse.ptp.rdt.sync.git.core.GitRemoteSyncConnection.java

License:Open Source License

/**
 * //from  w  w  w  .  j a v a  2 s . c  om
 * @param monitor
 * @param localDirectory
 * @param remoteHost
 * @return the repository
 * @throws IOException
 *             on problems writing to the file system.
 * @throws RemoteExecutionException
 *             on failure to run remote commands.
 * @throws RemoteSyncException
 *             on problems with initial local commit. TODO: Consider the
 *             consequences of exceptions that occur at various points,
 *             which can leave the repo in a partial state. For example, if
 *             the repo is created but the initial commit fails. TODO:
 *             Consider evaluating the output of "git init".
 */
private Git buildRepo(IProgressMonitor monitor)
        throws IOException, RemoteExecutionException, RemoteSyncException {
    SubMonitor subMon = SubMonitor.convert(monitor, 100);
    subMon.subTask(Messages.GitRemoteSyncConnection_building_repo);
    try {
        final File localDir = new File(localDirectory);
        final FileRepositoryBuilder repoBuilder = new FileRepositoryBuilder();
        File gitDirFile = new File(localDirectory + File.separator + gitDir);
        Repository repository = repoBuilder.setWorkTree(localDir).setGitDir(gitDirFile).build();
        git = new Git(repository);

        // Create and configure local repository if it is not already present
        if (!(gitDirFile.exists())) {
            repository.create(false);

            // An initial commit to create the master branch.
            doCommit();
        }

        // Create remote directory if necessary.
        try {
            CommandRunner.createRemoteDirectory(connection, remoteDirectory, subMon.newChild(5));
        } catch (final CoreException e) {
            throw new RemoteSyncException(e);
        }

        // Initialize remote directory if necessary
        boolean existingGitRepo = doRemoteInit(subMon.newChild(5));

        // Prepare remote site for committing (stage files using git) and
        // then commit remote files if necessary
        // Include untracked files for new git
        boolean needToCommitRemote = prepareRemoteForCommit(subMon.newChild(90), !existingGitRepo);
        // repos
        if (needToCommitRemote) {
            commitRemoteFiles(subMon.newChild(5));
        }

        return git;
    } finally {
        if (monitor != null) {
            monitor.done();
        }
    }
}

From source file:org.eclipse.recommenders.snipmatch.GitSnippetRepository.java

License:Open Source License

private boolean isUpdatePossible() throws IOException {
    if (RepositoryCache.FileKey.isGitRepository(gitFile, FS.DETECTED)) {
        Repository localRepo = new FileRepositoryBuilder().setGitDir(gitFile).build();
        for (Ref ref : localRepo.getAllRefs().values()) {
            if (ref.getObjectId() != null) {
                return true;
            }//from   w w  w  . j a  v a  2s  . c  o  m
        }
    }
    return false;
}

From source file:org.eclipse.recommenders.snipmatch.GitSnippetRepository.java

License:Open Source License

private Git fetch() throws GitAPIException, IOException {
    localRepo = new FileRepositoryBuilder().setGitDir(gitFile).build();
    Git git = new Git(localRepo);
    git.fetch().call();/*from w ww. ja  v a2 s .c o m*/
    return git;
}

From source file:org.eclipse.tycho.extras.buildtimestamp.jgit.JGitBuildTimestampProvider.java

License:Open Source License

public Date getTimestamp(MavenSession session, MavenProject project, MojoExecution execution)
        throws MojoExecutionException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder() //
            .readEnvironment() //
            .findGitDir(project.getBasedir()) //
            .setMustExist(true);/*  ww w .  j a v a 2  s. c  om*/
    try {
        Repository repository = builder.build();
        try {
            RevWalk walk = new RevWalk(repository);
            try {
                String relPath = getRelPath(repository, project);
                if (relPath != null && relPath.length() > 0) {
                    walk.setTreeFilter(AndTreeFilter.create(new PathFilter(relPath, getIgnoreFilter(execution)),
                            TreeFilter.ANY_DIFF));
                }

                // TODO make sure working tree is clean

                ObjectId headId = repository.resolve(Constants.HEAD);
                walk.markStart(walk.parseCommit(headId));
                RevCommit commit = walk.next();
                return new Date(commit.getCommitTime() * 1000L);
            } finally {
                walk.release();
            }
        } finally {
            repository.close();
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Could not determine git commit timestamp", e);
    }
}

From source file:org.eclipse.tycho.extras.sourceref.jgit.JGitSourceReferencesProvider.java

License:Open Source License

public String getSourceReferencesHeader(MavenProject project, ScmUrl scmUrl) throws MojoExecutionException {
    File basedir = project.getBasedir().getAbsoluteFile();
    FileRepositoryBuilder builder = new FileRepositoryBuilder().readEnvironment().findGitDir(basedir)
            .setMustExist(true);/*from   w  w  w. ja v a2s. c  o  m*/
    Repository repo;
    Git git;
    try {
        repo = builder.build();
        git = Git.wrap(repo);
    } catch (IOException e) {
        throw new MojoExecutionException("IO exception trying to create git repo ", e);
    }
    ObjectId head = resolveHead(repo);

    StringBuilder result = new StringBuilder(scmUrl.getUrl());
    result.append(";path=\"");
    result.append(getRelativePath(basedir, repo.getWorkTree()));
    result.append("\"");

    String tag = findTagForHead(git, head);
    if (tag != null) {
        // may contain e.g. spaces, so we quote it
        result.append(";tag=\"");
        result.append(tag);
        result.append("\"");
    }
    result.append(";commitId=");
    result.append(head.getName());
    return result.toString();
}