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

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

Introduction

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

Prototype

public Git(Repository repo) 

Source Link

Document

Construct a new org.eclipse.jgit.api.Git object which can interact with the specified git repository.

Usage

From source file:edu.nju.cs.inform.jgit.unfinished.TestSubmodules.java

License:Apache License

private static void addSubmodule(Repository mainRepo) throws GitAPIException {
    System.out.println("Adding submodule");
    try (Git git = new Git(mainRepo)) {
        try (Repository subRepoInit = git.submoduleAdd().setURI("https://github.com/github/testrepo.git")
                .setPath("testrepo").call()) {
            if (subRepoInit.isBare()) {
                throw new IllegalStateException(
                        "Repository at " + subRepoInit.getDirectory() + " should not be bare");
            }/*from   www. j a  v  a 2 s. co m*/
        }
    }
}

From source file:edu.nju.cs.inform.jgit.unfinished.TrackMaster.java

License:Apache License

public static void main(String[] args)
        throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    localPath.delete();//from  www  .  j  av a2s.co  m

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call()) {
        // now open the created repository
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        try (Repository repository = builder.setGitDir(localPath).readEnvironment() // scan environment GIT_* variables
                .findGitDir() // scan up the file system tree
                .build()) {
            try (Git git = new Git(repository)) {
                git.branchCreate().setName("master")
                        // ?!? .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
                        .setStartPoint("origin/master").setForce(true).call();
            }

            System.out.println("Now tracking master in repository at " + repository.getDirectory()
                    + " from origin/master at " + REMOTE_URL);
        }
    }
}

From source file:edu.nju.cs.inform.jgit.unfinished.UpdateIndex.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    try (final Repository repo = CookbookHelper.openJGitCookbookRepository()) {
        try (final Git git = new Git(repo)) {
            final String testFile = "README.md";

            // Modify the file
            FileUtils.write(new File(testFile), new Date().toString());
            System.out.println("Modified files: " + getModifiedFiles(git));

            new AssumeChangedCommand(repo, testFile, true).call();
            System.out.println("Modified files after assume-changed: " + getModifiedFiles(git));

            new AssumeChangedCommand(repo, testFile, false).call();
            System.out.println("Modified files after no-assume-changed: " + getModifiedFiles(git));

            git.checkout().addPath(testFile).call();
            System.out.println("Modified files after reset: " + getModifiedFiles(git));
        }/* w  w w  .  j a  v  a  2  s .  c om*/
    }
}

From source file:edu.tum.cs.mylyn.provisioning.git.ui.GitProvisioningWizard.java

License:Open Source License

private void pull(Repository repository) throws IOException {
    Git git = new Git(repository);
    PullCommand pull = git.pull();/*from  w  w  w .j  a v a 2 s  . c om*/
    pull.setCredentialsProvider(new EGitCredentialsProvider());
    try {
        pull.call();
    } catch (TransportException e) {
        throw new IOException(e);
    } catch (GitAPIException e) {
        throw new IOException(e);
    }

    SubmoduleUpdateCommand update = git.submoduleUpdate();
    try {
        update.call();
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:edu.wustl.lookingglass.community.CommunityRepository.java

License:Open Source License

private Git load() throws IOException, URISyntaxException, IllegalStateException, GitAPIException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.setGitDir(this.gitDir).readEnvironment().build();

    Git newGit;// w w  w. ja  v  a  2s  . c  o m
    if (hasAtLeastOneReference(repository)) {
        // The repository is valid
        newGit = new Git(repository);
    } else {
        // The current git dir isn't valid... so let's make a new one.
        Logger.warning("invalid git dir found " + this.gitDir + "; deleting.");
        FileUtils.forceDelete(this.gitDir);
        newGit = init();
    }

    // We need to make sure this repo stays in shape...
    // so let's do some maintenance every now and then...
    Properties gcStats = newGit.gc().getStatistics();
    int looseObjects = Integer.valueOf(gcStats.getProperty("numberOfLooseObjects", "0"));
    if ((AUTO_GC_LOOSE_OBJECTS != 0) && (looseObjects > AUTO_GC_LOOSE_OBJECTS)) {
        newGit.gc().call();
    }

    return newGit;
}

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

License:Open Source License

public Git buildGit() {
    return new Git(repo);
}

From source file:eu.cloud4soa.cli.roo.addon.commands.GitManager.java

License:Apache License

public void init() throws Exception {
    FileRepositoryBuilder builder;//from ww  w  .  j ava2  s  .c o  m

    gitRepository = new FileRepository(localPath + "/" + ".git");
    builder = new FileRepositoryBuilder();
    gitRepository = builder.setGitDir(new File(localPath + "/.git")).readEnvironment().findGitDir().build();
    git = new Git(gitRepository);

    GithubSshSessionFactory factory = new GithubSshSessionFactory();
    factory.setKeyLocation(sshKeyDir);
    factory.setPassphrase(passphrase);
    SshSessionFactory.setInstance(factory);

}

From source file:eu.trentorise.opendata.josman.test.GitTest.java

private static File createFile(Repository repository, String filePath) {

    try {// www.  ja  v a  2s.  c o  m
        // create the file

        File targetFile = new File(repository.getDirectory().getParent(), filePath);
        File parent = targetFile.getParentFile();
        if (!parent.exists() && !parent.mkdirs()) {
            throw new IllegalStateException("Couldn't create dir: " + parent);
        }

        targetFile.createNewFile();

        PrintWriter pw = new PrintWriter(targetFile);
        pw.println("hello");
        pw.close();

        // run the add-call
        new Git(repository).add().addFilepattern(filePath).call();

        return targetFile;

    } catch (Exception ex) {
        throw new RuntimeException("Error while creating file!", ex);
    }
}

From source file:eu.trentorise.opendata.josman.test.GitTest.java

/**
 * Creates a sample josman repo/*from w ww.  jav  a 2s .  c  o m*/
 *
 *
 * @return
 * @throws IOException
 * @throws GitAPIException
 */
private static File createJosmanSampleRepo() {
    Repository repository;
    try {
        repository = CookbookHelper.createNewRepository();

        LOG.log(Level.INFO, "Temporary repository at {0}", repository.getDirectory());

        createFiles(repository, "docs/README.md", "docs/CHANGES.md", "docs/img/a.jpg", "src/main/java/a.java",
                "src/main/java/b.java", "README.md", "LICENSE.txt");

        // and then commit the changes
        new Git(repository).commit().setMessage("Added test files").call();

        File dir = repository.getDirectory();

        repository.close();

        return dir;
    } catch (Exception ex) {
        throw new RuntimeException("Error while creating new repo!", ex);
    }

}

From source file:eu.trentorise.opendata.josman.test.GitTest.java

private static File createSampleGitRepo() throws IOException, GitAPIException {
    Repository repository = CookbookHelper.createNewRepository();

    System.out.println("Temporary repository at " + repository.getDirectory());

    // create the file
    File myfile = new File(repository.getDirectory().getParent(), "testfile");
    myfile.createNewFile();//from w w  w. j  av  a 2 s .  c  o m

    // run the add-call
    new Git(repository).add().addFilepattern("testfile").call();

    // and then commit the changes
    new Git(repository).commit().setMessage("Added testfile").call();

    LOG.info("Added file " + myfile + " to repository at " + repository.getDirectory());

    File dir = repository.getDirectory();

    repository.close();

    return dir;

}