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

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

Introduction

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

Prototype

public AddCommand add() 

Source Link

Document

Return a command object to execute a Add command

Usage

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void createNextCommit() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    try (FileWriter writer = new FileWriter(file1, true)) {
        writer.write("hi world!\nhello hi");
    }/*from   w  w  w .j  av  a2s.com*/
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated file1").call();

    try (FileWriter writer = new FileWriter(file2, true)) {
        writer.write("world");
    }
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated file2").call();

    git.push().setRemote("origin").add("master").call();

    git.close();
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void renameFile() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    if (!file2.renameTo(file2.toPath().resolveSibling("file3.adoc").toFile()))
        throw new IOException("Could not rename file2 to file3");

    git.add().setUpdate(true).addFilepattern(".").call();
    git.add().addFilepattern(".").call();
    git.commit().setMessage("renamed file2 to file3").call();
    git.push().setRemote("origin").add("master").call();
    git.close();/* w  w w  .j a va 2s. c om*/
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void deleteFile() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    if (!file2.delete())
        throw new IOException("Could not delete file2");

    git.add().setUpdate(true).addFilepattern(".").call();
    git.commit().setMessage("deleted file2").call();
    git.push().setRemote("origin").add("master").call();
    git.close();/* ww w  . j  a  va2 s.c  o  m*/
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void addNotRelevantFile(final String fileName) throws GitAPIException, IOException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    try (FileWriter writer = new FileWriter(file1.toPath().resolveSibling(fileName + ".mustache").toFile(),
            true)) {/*from www.  ja  va 2s  .c o  m*/
        writer.write("hello {{world}}");
    }

    git.add().addFilepattern(".").call();
    git.commit().setMessage("added template").call();
    git.push().setRemote("origin").add("master").call();
    git.close();
}

From source file:com.stormcloud.ide.api.git.GitManager.java

License:Open Source License

@Override
public DirCache add(String repository, String pattern) throws GitManagerException {

    try {// w w  w. java  2s  . c o  m

        Git git = Git.open(new File(repository));

        AddCommand add = git.add();

        add.addFilepattern(pattern);

        DirCache result = add.call();

        return result;

    } catch (IOException e) {
        LOG.error(e);
        throw new GitManagerException(e);
    } catch (GitAPIException e) {
        LOG.error(e);
        throw new GitManagerException(e);
    }
}

From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacadeTest.java

License:Open Source License

@Test
public void testFetch() throws Exception {
    LocalRepoBean sourceRepoBean = TestUtility.createTestRepo();
    String sourceDirectory = sourceRepoBean.getRepoDirectory().toString();

    LocalRepoBean targetRepoBean = new LocalRepoBean();
    targetRepoBean.setLocalBare(false);/*ww w . ja  va  2 s  .c o m*/
    targetRepoBean.setProjectKey(sourceRepoBean.getProjectKey());
    targetRepoBean.setSlug(sourceRepoBean.getSlug() + "_clone");
    targetRepoBean.setCloneSourceURI(sourceDirectory);
    underTest.clone(targetRepoBean);
    underTest.addRemote(targetRepoBean, "source_repo", sourceDirectory);
    Map<String, String> remotes = underTest.getRemotes(targetRepoBean);

    assertTrue(remotes.containsKey("source_repo") && remotes.containsValue(sourceDirectory));
    assertFalse(underTest.fetch(targetRepoBean, "source_repo"));

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository sourceRepository = builder.setGitDir(sourceRepoBean.getGitConfigDirectory().toFile())
            .findGitDir().build();

    String filename = "should_be_in_both.txt";
    Files.write(sourceRepoBean.getRepoDirectory().resolve(filename), Arrays.asList("Hello World"),
            StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
    Git git = new Git(sourceRepository);
    git.add().addFilepattern(filename).call();
    git.commit().setMessage("Added " + filename).call();

    assertTrue(underTest.fetch(targetRepoBean, "source_repo"));

    TestUtility.destroyTestRepo(sourceRepoBean);
    TestUtility.destroyTestRepo(targetRepoBean);
}

From source file:com.surevine.gateway.scm.git.jgit.TestUtility.java

License:Open Source License

public static LocalRepoBean createTestRepoMultipleBranches() throws Exception {
    final String projectKey = "test_" + UUID.randomUUID().toString();
    final String repoSlug = "testRepo";
    final String remoteURL = "ssh://fake_url";
    final Path repoPath = Paths.get(PropertyUtil.getGitDir(), "local_scm", projectKey, repoSlug);
    Files.createDirectories(repoPath);
    final Repository repo = new FileRepository(repoPath.resolve(".git").toFile());
    repo.create();// w  w  w  . j  av a  2  s  .com
    final StoredConfig config = repo.getConfig();
    config.setString("remote", "origin", "url", remoteURL);
    config.save();

    final LocalRepoBean repoBean = new LocalRepoBean();
    repoBean.setProjectKey(projectKey);
    repoBean.setSlug(repoSlug);
    repoBean.setLocalBare(false);

    final Git git = new Git(repo);

    // add some files to some branches
    for (int i = 0; i < 3; i++) {
        final String filename = "newfile" + i + ".txt";
        Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        git.add().addFilepattern(filename).call();
        git.commit().setMessage("Added " + filename).call();
    }

    git.checkout().setName("branch1").setCreateBranch(true).call();
    for (int i = 0; i < 3; i++) {
        final String filename = "branch1" + i + ".txt";
        Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        git.add().addFilepattern(filename).call();
        git.commit().setMessage("Added " + filename).call();
    }

    git.checkout().setName("master").call();
    git.checkout().setName("branch2").setCreateBranch(true).call();
    for (int i = 0; i < 3; i++) {
        final String filename = "branch2" + i + ".txt";
        Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        git.add().addFilepattern(filename).call();
        git.commit().setMessage("Added " + filename).call();
    }

    repo.close();
    return repoBean;
}

From source file:com.surevine.gateway.scm.git.jgit.TestUtility.java

License:Open Source License

public static LocalRepoBean createTestRepo() throws Exception {
    final String projectKey = "test_" + UUID.randomUUID().toString();
    final String repoSlug = "testRepo";
    final String remoteURL = "ssh://fake_url";
    final Path repoPath = Paths.get(PropertyUtil.getGitDir(), "local_scm", projectKey, repoSlug);
    Files.createDirectories(repoPath);
    final Repository repo = new FileRepository(repoPath.resolve(".git").toFile());
    repo.create();/* ww  w  .java 2s  .  c o m*/
    final StoredConfig config = repo.getConfig();
    config.setString("remote", "origin", "url", remoteURL);
    config.save();

    final LocalRepoBean repoBean = new LocalRepoBean();
    repoBean.setProjectKey(projectKey);
    repoBean.setSlug(repoSlug);
    repoBean.setLocalBare(false);
    repoBean.setSourcePartner("partner");

    final Git git = new Git(repo);

    for (int i = 0; i < 3; i++) {
        final String filename = "newfile" + i + ".txt";
        Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        git.add().addFilepattern(filename).call();
        git.commit().setMessage("Added " + filename).call();
    }

    git.checkout().setName("master").call();

    repo.close();
    return repoBean;
}

From source file:com.tasktop.c2c.server.internal.profile.service.template.GitServiceCloner.java

License:Open Source License

private void copyRepo(ScmRepository scmRepo, CloneContext context)
        throws IOException, JGitInternalException, GitAPIException {

    File workDirectory = null;//from  ww w  .  j a  v  a2  s  .c  o m
    try {
        Project templateProject = context.getTemplateService().getProjectServiceProfile().getProject();
        String cloneUrl = jgitProvider.computeRepositoryUrl(templateProject.getIdentifier(), scmRepo.getName());

        AuthUtils.assumeSystemIdentity(templateProject.getIdentifier());
        tenancyManager.establishTenancyContext(context.getTemplateService());

        workDirectory = createTempDirectory();
        Git git = Git.cloneRepository().setDirectory(workDirectory)
                .setBranch(Constants.R_HEADS + Constants.MASTER).setURI(cloneUrl).call();

        AuthUtils.assumeSystemIdentity(
                context.getTargetService().getProjectServiceProfile().getProject().getIdentifier());
        tenancyManager.establishTenancyContext(context.getTargetService());

        FileUtils.deleteDirectory(git.getRepository().getDirectory());

        git = Git.init().setDirectory(git.getRepository().getDirectory().getParentFile()).call();

        maybeRewriteRepo(workDirectory, context);

        String pushUrl = jgitProvider.computeRepositoryUrl(
                context.getTargetService().getProjectServiceProfile().getProject().getIdentifier(),
                scmRepo.getName());

        // FIXME: User's locale is not defined here
        String commitMessage = messageSource.getMessage("project.template.git.commitMessage",
                new Object[] { templateProject.getName() }, null);

        git.add().addFilepattern(".").call();
        git.commit().setCommitter(committerName, committerEmail).setMessage(commitMessage).call();
        git.getRepository().getConfig().setString("remote", "target", "url", pushUrl);
        git.push().setRemote("target").setPushAll().call();
    } finally {
        if (workDirectory != null) {
            FileUtils.deleteDirectory(workDirectory);
        }
    }
}

From source file:com.tasktop.c2c.server.scm.service.GitServiceTestBase.java

License:Open Source License

protected void commitAndPushFile(Git git, String path, String content, String message)
        throws GitAPIException, JGitInternalException, IOException {
    File f = new File(git.getRepository().getDirectory().getParentFile(), path);
    ensureDirExists(f.getParentFile());//from  w w  w .ja  va  2  s  . c o m
    FileOutputStream writer = new FileOutputStream(f);
    writer.write(content.getBytes());
    writer.close();
    git.add().addFilepattern(path).call();
    git.commit().setMessage(message).call();
    git.push().call();
}