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:info.debatty.jinu.Case.java

License:Open Source License

private void commitToGit(final String time_tag) {
    try {//ww w  .  jav a 2  s.c  om
        Repository repo = new FileRepositoryBuilder().findGitDir().build();

        Git git = new Git(repo);
        git.add().addFilepattern(".").call();
        git.commit().setAll(true).setMessage("Test case " + time_tag).call();
        git.tag().setName("T" + time_tag).call();
    } catch (Exception ex) {
        System.err.println("Could not commit GIT repo");
        System.err.println(ex.getMessage());
    }
}

From source file:info.plichta.maven.plugins.changelog.RepositoryProcessorTest.java

License:Apache License

private RevCommit commitWithFile(Git git, String commit) throws IOException, GitAPIException {
    writeTrashFile(commit, commit);//from   w  w  w. jav a2 s .c om
    git.add().addFilepattern(commit).call();
    return git.commit().setMessage(commit).setAll(true).call();
}

From source file:io.dpwspoon.github.utils.TravisCIUtils.java

public static void addTravisFileTo(Git localRepo, File dir, String orgName, String repoName)
        throws IOException, NoFilepatternException, GitAPIException {
    String travisFileName = ".travis.yml";
    try (FileWriter travisWriter = new FileWriter(new File(dir, travisFileName))) {
        travisWriter.write("language: node_js\n" + "node_js:\n" + "  - \"0.10\\n" + "before_install:\n"
                + "  - npm install -g bower\n" + "  - npm install -g grunt-cli\n" + "script: grunt\n");
        //         travisWriter.write("language: Java\n" + "jdk:\n" + "  - oraclejdk7\n" + "  - openjdk7\n"
        //               + "script: mvn verify\n");
    }/*  w w w  . j  a  v a  2 s  .c om*/
    localRepo.add().addFilepattern(travisFileName).call();

    String readmeFileName = "README.md";
    File readme = new File(dir, readmeFileName);
    if (readme.exists()) {

        String TEXT_TO_ADD = "[![Build Status][build-status-image]][build-status]\n\n"
                + String.format("[build-status-image]: https://travis-ci.org/%s/%s.svg?branch=develop\n",
                        orgName, repoName)
                + String.format("[build-status]: https://travis-ci.org/%s/%s\n", orgName, repoName);

        List<String> fileLines = new ArrayList<String>();
        Scanner scanner = null;
        try {
            scanner = new Scanner(readme);
            boolean linesAdded = false;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                fileLines.add(line);
                if (fileLines.size() == 2) {
                    linesAdded = true;
                    fileLines.add(TEXT_TO_ADD);
                }
            }
            if (!linesAdded) {
                fileLines.add(TEXT_TO_ADD);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        PrintWriter pw = null;
        try {
            pw = new PrintWriter(readme);
            for (String line : fileLines) {
                pw.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
        }

    } else {
        try (FileWriter readmeWriter = new FileWriter(readme)) {
            readmeWriter
                    .write("#" + repoName + "\n" + "[![Build Status][build-status-image]][build-status]\n\n");
            readmeWriter.write(
                    String.format("[build-status-image]: https://travis-ci.org/%s/%s.svg?branch=develop\n",
                            orgName, repoName));
            readmeWriter.write(String.format("[build-status]: https://travis-ci.org/%s/%s", orgName, repoName));
            readmeWriter.write("\n");
        }
        localRepo.add().addFilepattern(readmeFileName).call();
    }
}

From source file:io.fabric8.collector.git.GitHelpers.java

License:Apache License

public static void addFiles(Git git, File... files) throws GitAPIException, IOException {
    File rootDir = getRootGitDirectory(git);
    for (File file : files) {
        String relativePath = getFilePattern(rootDir, file);
        git.add().addFilepattern(relativePath).call();
    }/*from   w w w. j a va 2  s.co m*/
}

From source file:io.fabric8.collector.git.GitHelpers.java

License:Apache License

public static void doAddCommitAndPushFiles(Git git, UserDetails userDetails, PersonIdent personIdent,
        String branch, String origin, String message, boolean pushOnCommit) throws GitAPIException {
    git.add().addFilepattern(".").call();
    doCommitAndPush(git, message, userDetails, personIdent, branch, origin, pushOnCommit);
}

From source file:io.fabric8.forge.rest.client.ForgeTestSupport.java

License:Apache License

protected Build assertCodeChangeTriggersWorkingBuild(JenkinsServer jenkins, String jenkinsUrl,
        final String projectName, Build firstBuild, String folderName) throws Exception {
    File cloneDir = new File(getBasedir(), "target/projects/" + projectName);

    String gitUrl = asserGetAppGitCloneURL(forgeClient, projectName);
    Git git = ForgeClientAsserts.assertGitCloneRepo(gitUrl, cloneDir);

    // lets make a dummy commit...
    File readme = new File(cloneDir, "ReadMe.md");
    boolean mustAdd = false;
    String text = "";
    if (readme.exists()) {
        text = IOHelpers.readFully(readme);
    } else {/*ww  w .ja v a  2s .c o  m*/
        mustAdd = true;
    }
    text += "\nupdated at: " + new Date();
    Files.writeToFile(readme, text, Charset.defaultCharset());

    if (mustAdd) {
        AddCommand add = git.add().addFilepattern("*").addFilepattern(".");
        add.call();
    }

    LOG.info("Committing change to " + readme);

    CommitCommand commit = git.commit().setAll(true).setAuthor(forgeClient.getPersonIdent())
            .setMessage("dummy commit to trigger a rebuild");
    commit.call();
    PushCommand command = git.push();
    command.setCredentialsProvider(forgeClient.createCredentialsProvider());
    command.setRemote("origin").call();

    LOG.info("Git pushed change to " + readme);

    // now lets wait for the next build to start
    int nextBuildNumber = firstBuild.getNumber() + 1;

    Asserts.assertWaitFor(10 * 60 * 1000, new Block() {
        @Override
        public void invoke() throws Exception {
            JenkinsServer jenkins = createJenkinsServer(forgeClient.getKubernetesClient(), jenkinsNamespace);
            JobWithDetails job = assertJob(jenkins, folderName, projectName);
            Build lastBuild = job.getLastBuild();
            assertThat(lastBuild.getNumber())
                    .describedAs("Waiting for latest build for job " + projectName + " to start")
                    .isGreaterThanOrEqualTo(nextBuildNumber);
        }
    });

    return ForgeClientAsserts.assertBuildCompletes(jenkins, jenkinsUrl, folderName, projectName);
}

From source file:io.fabric8.forge.rest.git.RepositoryResource.java

License:Apache License

protected CommitInfo doCreateDirectory(Git git, String path) throws Exception {
    File file = getRelativeFile(path);
    if (file.exists()) {
        return null;
    }/*w w  w.  j av  a 2  s  .  c o m*/
    file.mkdirs();
    String filePattern = getFilePattern(path);
    AddCommand add = git.add().addFilepattern(filePattern).addFilepattern(".");
    add.call();

    CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(message);
    RevCommit revCommit = commitThenPush(git, commit);
    return createCommitInfo(revCommit);
}

From source file:io.fabric8.forge.rest.git.RepositoryResource.java

License:Apache License

protected CommitInfo doRename(Git git, String oldPath, String newPath) throws Exception {
    File file = getRelativeFile(oldPath);
    File newFile = getRelativeFile(newPath);
    if (file.exists()) {
        File parentFile = newFile.getParentFile();
        parentFile.mkdirs();//from w  w w.  ja v a2 s  .  com
        if (!parentFile.exists()) {
            throw new IOException("Could not create directory " + parentFile + " when trying to move " + file
                    + " to " + newFile + ". Maybe a file permission issue?");
        }
        file.renameTo(newFile);
        String filePattern = getFilePattern(newPath);
        git.add().addFilepattern(filePattern).call();
        CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(message);
        return createCommitInfo(commitThenPush(git, commit));
    } else {
        return null;
    }
}

From source file:io.fabric8.forge.rest.git.RepositoryResource.java

License:Apache License

protected CommitInfo doWrite(Git git, String path, byte[] contents, PersonIdent personIdent,
        String commitMessage) throws Exception {
    File file = getRelativeFile(path);
    file.getParentFile().mkdirs();//  www. ja  v  a 2s  .c o  m

    Files.writeToFile(file, contents);

    String filePattern = getFilePattern(path);
    AddCommand add = git.add().addFilepattern(filePattern).addFilepattern(".");
    add.call();

    CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(commitMessage);
    RevCommit revCommit = commitThenPush(git, commit);
    return createCommitInfo(revCommit);
}

From source file:io.fabric8.forge.rest.main.GitCommandCompletePostProcessor.java

License:Apache License

private void addFiles(Git git, File... files) throws GitAPIException, IOException {
    File rootDir = GitHelpers.getRootGitDirectory(git);
    for (File file : files) {
        String relativePath = getFilePattern(rootDir, file);
        git.add().addFilepattern(relativePath).call();
    }//from  w  w  w.  j  av a  2 s .c om
}