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:au.id.soundadvice.systemdesign.versioning.jgit.GitVersionControl.java

License:Open Source License

public static void init(Path path) throws GitAPIException {
    Git repo = Git.init().setDirectory(path.toFile()).call();
    try {/*from w w  w. j  ava 2  s.  c o  m*/
        repo.add().addFilepattern(".").call();
    } finally {
        repo.close();
    }
}

From source file:br.com.riselabs.cotonet.test.helpers.ConflictBasedRepositoryTestCase.java

License:Open Source License

/**
 * Creates a collaboration scenario with five developers (Devs A, B, C, D,
 * and E) and two files (Foo.java and Bar.java).
 * //  www  .  ja  v a  2  s . c  om
 * @return
 * @throws Exception
 */
public MergeScenario setCollaborationScenarioInTempRepository() throws Exception {
    Git git = Git.wrap(db);

    RevCommit mergeBaseCommit, lastMasterCommit, lastSideCommit;

    // first versions of Foo and Bar
    writeTrashFile("Foo.java", "1");
    writeTrashFile("Bar.java", "1");
    git.add().addFilepattern("Foo.java").addFilepattern("Bar.java").call();
    git.commit().setMessage("initial commit").setAuthor(devs.get("devY")).call();

    writeTrashFile("Foo.java", "1\n2\n3\n4\n5\n6\n7\n8\n");
    writeTrashFile("Bar.java", "1\n2\n3\n4\n");
    git.add().addFilepattern("Foo.java").addFilepattern("Bar.java").call();
    mergeBaseCommit = git.commit().setMessage("m0").setAuthor(devs.get("devX")).call();

    // Dev E changes Foo
    writeTrashFile("Foo.java", "1\n2\n3\n4\n5-master\n6\n7\n8\n");
    git.add().addFilepattern("Foo.java").call();
    git.commit().setMessage("m1").setAuthor(devs.get("devE")).call();

    // Dev C changes Foo
    writeTrashFile("Foo.java", "1\n2\n3\n4-master\n5\n6\n7\n8\n");
    writeTrashFile("Bar.java", "1\n2\n3-master\n4-master\n");
    git.add().addFilepattern("Foo.java").addFilepattern("Bar.java").call();
    git.commit().setMessage("m2").setAuthor(devs.get("devC")).call();

    // Dev B changes
    writeTrashFile("Bar.java", "1\n2\n3\n4-master\n");
    git.add().addFilepattern("Bar.java").call();
    lastMasterCommit = git.commit().setMessage("m3").setAuthor(devs.get("devB")).call();

    // updating the tree with the changes
    createBranch(mergeBaseCommit, "refs/heads/side");
    checkoutBranch("refs/heads/side");

    // Dev D changes Foo
    writeTrashFile("Foo.java", "1\n2\n3\n4-side\n5\n6\n7\n8\n");
    git.add().addFilepattern("Foo.java").call();
    git.commit().setMessage("s1").setAuthor(devs.get("devD")).call();

    // Dev E changes Bar
    writeTrashFile("Bar.java", "1\n2\n3\n4\n5\n");
    git.add().addFilepattern("Bar.java").call();
    git.commit().setMessage("s2").setAuthor(devs.get("devE")).call();

    // Dev A changes Bar
    writeTrashFile("Bar.java", "1\n2\n3-side\n4-side\n5\n");
    git.add().addFilepattern("Bar.java").call();
    lastSideCommit = git.commit().setMessage("s3").setAuthor(devs.get("devA")).call();

    return new MergeScenario(null, mergeBaseCommit, lastMasterCommit, lastSideCommit, null, null);
}

From source file:br.com.riselabs.cotonet.test.helpers.ConflictBasedRepositoryTestCase.java

License:Open Source License

/**
 * Merges the scenario created by//w w  w  .  j  av a2  s . c o  m
 * {@code #setCollaborationScenarioInBareRepository()} or
 * {@code #setCollaborationScenarioInTempRepository()} .
 * 
 * @throws Exception
 */
public void setResolvedMergeConflictScenario() throws Exception {
    MergeScenario ms = setCollaborationScenarioInTempRepository();
    Git git = Git.wrap(db);
    checkoutBranch("refs/heads/master");
    // Dev Y changes Bar
    git.merge().include(ms.getRight()).call();
    writeTrashFile("Bar.java", "1\n2\n3-merged\n4-merged\n5\n");
    writeTrashFile("Foo.java", "1\n2\n3\n4-merged\n5\n6\n7\n8\n");
    git.add().addFilepattern("Bar.java").addFilepattern("Foo.java").call();
    git.commit().setMessage("merge s3 into m3").setAuthor(devs.get("devY")).call();
}

From source file:ch.sbb.releasetrain.git.GitRepoImpl.java

License:Apache License

@Override
public void addCommitPush() {
    try {/*from   w  w w  . j  a va 2 s. com*/
        Git git = gitOpen();

        git.add().addFilepattern(".").call();

        // status
        Status status = git.status().call();

        // rm the deleted ones
        if (status.getMissing().size() > 0) {
            for (String rm : status.getMissing()) {
                git.rm().addFilepattern(rm).call();
            }
        }

        // commit and push if needed
        if (!status.hasUncommittedChanges()) {
            log.debug("not commiting git, because there are no changes");
            return;
        }

        git.commit().setMessage("Automatic commit by releasetrain").call();
        git.push().setCredentialsProvider(credentialsProvider()).call();
    } catch (Exception e) {
        throw new GitException(e.getMessage(), e);
    }
}

From source file:com.amazonaws.eclipse.elasticbeanstalk.git.AWSGitPushCommand.java

License:Open Source License

private void commitChanges(Repository repository, String message) throws GitAPIException, IOException {
    Git git = new Git(repository);
    // Add once (without the update flag) to add new and modified files
    git.add().addFilepattern(".").call();
    // Then again with the update flag to add deleted and modified files
    git.add().addFilepattern(".").setUpdate(true).call();

    git.commit().setMessage(message).call();
}

From source file:com.athomas.androidkickstartr.util.GitHubber.java

License:Apache License

public Repository createCommit(File srcFolder, String applicationName) throws IOException, GitAPIException {
    Repository repository = repositoryService.createRepository(new Repository().setName(applicationName));

    String cloneUrl = repository.getCloneUrl();

    InitCommand init = new InitCommand();
    init.setDirectory(srcFolder);/* w  w  w.j  a v a  2 s . com*/
    init.setBare(false);
    Git git = init.call();

    StoredConfig config = git.getRepository().getConfig();
    config.setString("remote", "origin", "url", cloneUrl);
    config.save();

    UsernamePasswordCredentialsProvider user = new UsernamePasswordCredentialsProvider(accessToken, "");
    git.add().addFilepattern(".").call();
    git.commit().setMessage(COMMIT_MESSAGE).call();
    git.push().setCredentialsProvider(user).call();

    return repository;
}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * See {@link Git#add()}//from   ww w.  j a va2  s  .  c o  m
 */
public static DirCache doAdd(final File workspace, final String pattern) {
    try {
        final Git git = Git.open(workspace);
        return git.add().addFilepattern(pattern).call();
    } catch (final Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:com.chungkwong.jgitgui.GitTreeItem.java

License:Open Source License

private void gitAdd(TitledPane untrackedView, TitledPane modifiedView, TitledPane addedView,
        TitledPane changedView) {/* ww  w .  j  av  a2  s. c  o  m*/
    Git git = (Git) getValue();
    ListView<String> untracked = ((ListView<String>) untrackedView.getContent());
    ListView<String> modified = ((ListView<String>) modifiedView.getContent());
    ListView<String> added = ((ListView<String>) addedView.getContent());
    ListView<String> changed = ((ListView<String>) changedView.getContent());
    try {
        for (String item : untracked.getSelectionModel().getSelectedItems()) {
            git.add().addFilepattern(item).call();
            untracked.getItems().remove(item);
            added.getItems().add(item);
        }
        for (String item : modified.getSelectionModel().getSelectedItems()) {
            git.add().addFilepattern(item).call();
            modified.getItems().remove(item);
            changed.getItems().add(item);
        }
    } catch (Exception ex) {
        Logger.getLogger(GitTreeItem.class.getName()).log(Level.SEVERE, null, ex);
        Util.informUser(ex);
    }
}

From source file:com.cisco.step.jenkins.plugins.jenkow.JenkowWorkflowRepository.java

License:Open Source License

private void addAndCommit(Repository r, String filePattern, String msg) throws IOException {
    try {/*from w w  w.  j  av a  2 s . c  o  m*/
        Git git = new Git(r);
        AddCommand cmd = git.add();
        cmd.addFilepattern(filePattern);
        cmd.call();

        CommitCommand co = git.commit();
        co.setAuthor("Jenkow", "noreply@jenkins-ci.org");
        co.setMessage(msg);
        co.call();
    } catch (GitAPIException e) {
        LOGGER.log(Level.WARNING, "Adding seeded jenkow-repository content to Git failed", e);
    }
}

From source file:com.gitblit.servlet.GitServletTest.java

License:Apache License

@Test
public void testAnonymousPush() throws Exception {
    GitBlitSuite.close(ticgitFolder);//from   www.j a  va2s  .  com
    if (ticgitFolder.exists()) {
        FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE | FileUtils.RETRY);
    }

    RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.NONE;
    repositories().updateRepositoryModel(model.name, model, false);

    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
    clone.setDirectory(ticgitFolder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
    GitBlitSuite.close(clone.call());
    assertTrue(true);

    Git git = Git.open(ticgitFolder);
    File file = new File(ticgitFolder, "TODO");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// hellol " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage("test commit").call();
    Iterable<PushResult> results = git.push().setPushAll().call();
    GitBlitSuite.close(git);
    for (PushResult result : results) {
        for (RemoteRefUpdate update : result.getRemoteUpdates()) {
            assertEquals(Status.OK, update.getStatus());
        }
    }
}