Example usage for org.eclipse.jgit.api Git open

List of usage examples for org.eclipse.jgit.api Git open

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git open.

Prototype

public static Git open(File dir) throws IOException 

Source Link

Document

Open repository

Usage

From source file:org.wandora.application.tools.git.AbstractGitTool.java

License:Open Source License

public Git getGit() {
    String currentProject = null;
    try {// w  w  w. j  av a  2  s . com
        Wandora wandora = Wandora.getWandora();
        currentProject = wandora.getCurrentProjectFileName();
        if (currentProject == null)
            return null;
        File currentProjectFile = new File(currentProject);
        if (currentProjectFile.exists() && currentProjectFile.isDirectory()) {
            Git git = Git.open(currentProjectFile);
            return git;
        }
    } catch (IOException ex) {
        // log("Can't read git repository in '"+currentProject+"'.");
    }
    return null;
}

From source file:org.wso2.security.tools.dependencycheck.scanner.handler.GitHandler.java

License:Open Source License

/**
 * @param productPath The repository to open. May be either the GIT_DIR, or the working tree directory that
 *                    contains {@code .git}
 * @return {@link Git} object for the existing git repository
 * @throws IOException// w  ww  .j a va 2  s . c o  m
 */
public static Git gitOpen(String productPath) throws IOException {
    return Git.open(new File(productPath));
}

From source file:org.xwiki.git.GitHelper.java

License:Open Source License

public void add(File repo, String path, String content, PersonIdent author, PersonIdent committer,
        String message) throws Exception {
    File file = new File(repo.getParentFile(), path);
    if (!file.getParentFile().exists()) {
        Assert.assertTrue(file.getParentFile().mkdirs());
    }//from   w  w  w  .j av a  2  s  .  c om
    if (!file.exists()) {
        Assert.assertTrue(file.createNewFile());
    }
    PrintWriter writer = new PrintWriter(file);
    if (content == null)
        content = "";
    try {
        writer.print(content);
    } finally {
        writer.close();
    }
    Git git = Git.open(repo);
    git.add().addFilepattern(path).call();
    RevCommit commit = git.commit().setOnly(path).setMessage(message).setAuthor(author).setCommitter(committer)
            .call();
    Assert.assertNotNull(commit);
}

From source file:org.xwiki.git.internal.GitScriptServiceTest.java

License:Open Source License

private void add(File repo, String path, String content, String message) throws Exception {
    File file = new File(repo.getParentFile(), path);
    if (!file.getParentFile().exists()) {
        Assert.assertTrue(file.getParentFile().mkdirs());
    }//  w  w w. ja  va  2s .  c o m
    if (!file.exists()) {
        Assert.assertTrue(file.createNewFile());
    }
    PrintWriter writer = new PrintWriter(file);
    if (content == null)
        content = "";
    try {
        writer.print(content);
    } finally {
        writer.close();
    }
    Git git = Git.open(repo);
    git.add().addFilepattern(path).call();
    RevCommit commit = git.commit().setOnly(path).setMessage(message).setAuthor("test author", "john@does.com")
            .setCommitter("test committer", "john@does.com").call();
    Assert.assertNotNull(commit);
}

From source file:org.yakindu.sct.examples.wizard.service.git.GitRepositoryExampleService.java

License:Open Source License

protected IStatus updateRepository(IProgressMonitor monitor) {
    String repoURL = getPreference(ExamplesPreferenceConstants.REMOTE_LOCATION);
    try {//w  ww  . j a v a  2  s  . c o  m
        java.nio.file.Path storageLocation = getStorageLocation();
        PullResult result = Git.open(storageLocation.toFile()).pull()
                .setProgressMonitor(new EclipseGitProgressTransformer(monitor)).call();
        if (!result.isSuccessful()) {
            return new Status(IStatus.ERROR, ExampleActivator.PLUGIN_ID,
                    "Unable to update repository " + repoURL + "!");
        }
    } catch (GitAPIException | IOException e) {
        return new Status(IStatus.ERROR, ExampleActivator.PLUGIN_ID,
                "Unable to update repository " + repoURL + "!");
    }
    return Status.OK_STATUS;
}

From source file:org.yakindu.sct.examples.wizard.service.git.GitRepositoryExampleService.java

License:Open Source License

@Override
public boolean isUpToDate(IProgressMonitor monitor) {
    java.nio.file.Path storageLocation = getStorageLocation();
    try {/*from  w  w  w .jav  a2s  .co m*/
        FetchCommand fetch = Git.open(storageLocation.toFile()).fetch();
        FetchResult result = fetch.setProgressMonitor(new EclipseGitProgressTransformer(monitor))
                .setDryRun(true).call();
        Collection<TrackingRefUpdate> trackingRefUpdates = result.getTrackingRefUpdates();
        return trackingRefUpdates.size() == 0;
    } catch (RepositoryNotFoundException ex) {
        // This is the case when the examples are imported manually
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
        return true;
    }
}

From source file:org.z2env.impl.helper.GitTools.java

License:Apache License

private static ValidationResult isValidLocalRepository(URIish repoUri) {
    ValidationResult result;//ww  w .jav a  2s  .c o  m
    try {
        if (Git.open(new File(repoUri.getPath())).getRepository().getObjectDatabase().exists()) {
            result = ValidationResult.valid;
        } else {
            result = ValidationResult.invalid;
        }
    } catch (IOException e) {
        result = ValidationResult.invalid;
    }
    return result;
}

From source file:org.zanata.sync.jobs.plugin.git.service.impl.CacheableRepoSyncService.java

License:Open Source License

@Override
public void cloneRepo(SyncJobDetail jobDetail, Path workingDir) throws RepoSyncException {

    try (Git git = Git.open(workingDir.toFile())) {
        log.info("reuse previous repository");
        GitSyncService.doGitFetch(git);/* w w  w  .j a  v  a  2  s.co m*/
        GitSyncService.checkOutBranch(git, getBranchOrDefault(jobDetail.getSrcRepoBranch()));
        GitSyncService.cleanUpCurrentBranch(git);
    } catch (RepositoryNotFoundException rnfe) {
        log.info("repository is not yet available. Try to clone it from remote");
        delegate.cloneRepo(jobDetail, workingDir);
    } catch (IOException | GitAPIException e) {
        throw new RepoSyncException("failed clone repo:" + jobDetail, e);
    }
}

From source file:org.zanata.sync.jobs.plugin.git.service.impl.GitSyncService.java

License:Open Source License

@Override
public void syncTranslationToRepo(SyncJobDetail jobDetail, Path workingDir) {
    UsernamePasswordCredential user = new UsernamePasswordCredential(jobDetail.getSrcRepoUsername(),
            jobDetail.getSrcRepoSecret());
    try (Git git = Git.open(workingDir.toFile())) {
        if (log.isDebugEnabled()) {
            log.debug("before syncing translation, current branch: {}", git.getRepository().getBranch());
        }//from w  w w.  jav a 2s  . c  o m
        StatusCommand statusCommand = git.status();
        Status status = statusCommand.call();
        Set<String> uncommittedChanges = status.getUncommittedChanges();
        uncommittedChanges.addAll(status.getUntracked());
        if (!uncommittedChanges.isEmpty()) {
            log.info("uncommitted files in git repo: {}", uncommittedChanges);
            AddCommand addCommand = git.add();
            // ignore zanata cache folder
            uncommittedChanges.stream().filter(file -> !file.startsWith(".zanata-cache/"))
                    .forEach(addCommand::addFilepattern);
            addCommand.call();

            log.info("commit changed files");
            CommitCommand commitCommand = git.commit();
            commitCommand.setAuthor(commitAuthorName(), commitAuthorEmail());
            commitCommand.setMessage(commitMessage(jobDetail.getZanataUsername()));
            RevCommit revCommit = commitCommand.call();

            log.info("push to remote the commit: {}", revCommit);
            PushCommand pushCommand = git.push();
            setUserIfProvided(pushCommand, user);
            pushCommand.call();
        } else {
            log.info("nothing changed so nothing to do");
        }
    } catch (IOException e) {
        log.error("what the heck", e);
        throw new RepoSyncException("failed opening " + workingDir + " as git repo", e);
    } catch (GitAPIException e) {
        log.error("what the heck", e);
        throw new RepoSyncException("Failed committing translations into the repo", e);
    }
}

From source file:org.zanata.sync.plugin.git.service.impl.GitSyncService.java

License:Open Source License

private void doGitFetch(File destPath) {
    try {//from   w ww  .  j a  va  2 s.  c om
        Git git = Git.open(destPath);
        git.fetch().setRemote("origin").call();
    } catch (IOException ioe) {
        // ignore
    } catch (Exception e) {
        log.error("fail to call git pull", e);
        throw new RepoSyncException(e);
    }
}