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

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

Introduction

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

Prototype

public AddCommand(Repository repo) 

Source Link

Document

Constructor for AddCommand

Usage

From source file:org.archicontribs.modelrepository.grafico.GraficoUtilsTests.java

License:Open Source License

@Test
public void getFileContents_IsCorrect() throws Exception {
    File localGitFolder = new File(getTempTestsFolder(), "testRepo");
    String contents = "Hello World!\nTesting.";

    try (Repository repo = GitHelper.createNewRepository(localGitFolder)) {
        File file = new File(localGitFolder, "test.txt");

        try (FileWriter fw = new FileWriter(file)) {
            fw.write(contents);/* w  ww  .j  ava 2  s. com*/
            fw.flush();
        }

        assertTrue(file.exists());

        // Add file to index
        AddCommand addCommand = new AddCommand(repo);
        addCommand.addFilepattern("."); //$NON-NLS-1$
        addCommand.setUpdate(false);
        addCommand.call();

        // Commit file
        CommitCommand commitCommand = Git.wrap(repo).commit();
        commitCommand.setAuthor("Test", "Test");
        commitCommand.setMessage("Message");
        commitCommand.call();

        assertEquals(contents, GraficoUtils.getFileContents(localGitFolder, "test.txt", "HEAD"));
    }
}

From source file:org.webcat.core.git.GitUtilities.java

License:Open Source License

/**
 * Sets up a new base Git repository for the specified object.
 *
 * @param object the object whose file store is desired
 * @param location the file system location for the repository
 * @return the newly created repository/*www. j  a  va2 s. com*/
 */
private static Repository setUpNewRepository(EOEnterpriseObject object, File location) throws IOException {
    // This method performs the following actions to set up a new
    // repository:
    //
    // 1) Creates a new bare repository in the location requested by the
    //    method argument.
    // 2) Creates a temporary non-bare repository in the system temp
    //    directory, which is then configured to use the bare repository
    //    as a remote repository.
    // 3) Creates a README.txt file in the temporary repository's working
    //    directory, which contains a welcome message determined by the
    //    type of object that created the repo.
    // 4) Adds the README.txt to the repository, commits the change, and
    //    then pushes the changes to the bare repository.
    // 5) Finally, the temporary repository is deleted.
    //
    // This results in a usable bare repository being created, which a user
    // can now clone locally in order to manage their Web-CAT file store.

    // Create the bare repository.
    InitCommand init = new InitCommand();
    init.setDirectory(location);
    init.setBare(true);
    Repository bareRepository = init.call().getRepository();

    // Create the temporary repository.
    File tempRepoDir = File.createTempFile("newgitrepo", null);
    tempRepoDir.delete();
    tempRepoDir.mkdirs();

    init = new InitCommand();
    init.setDirectory(tempRepoDir);
    Repository tempRepository = init.call().getRepository();

    // Create the welcome files in the temporary repo.

    if (object instanceof RepositoryProvider) {
        RepositoryProvider provider = (RepositoryProvider) object;

        try {
            provider.initializeRepositoryContents(tempRepoDir);
        } catch (Exception e) {
            log.error(
                    "The following exception occurred when trying to "
                            + "initialize the repository contents at " + location.getAbsolutePath()
                            + ", but I'm continuing " + "anyway to ensure that the repository isn't corrupt.",
                    e);
        }
    }

    // Make sure we created at least one file, since we can't do much with
    // an empty repository. If the object didn't put anything in the
    // staging area, we'll just create a dummy README file.

    File[] files = tempRepoDir.listFiles();
    boolean foundFile = false;

    for (File file : files) {
        String name = file.getName();
        if (!".".equals(name) && !"..".equals(name) && !".git".equalsIgnoreCase(name)) {
            foundFile = true;
            break;
        }
    }

    if (!foundFile) {
        PrintWriter writer = new PrintWriter(new File(tempRepoDir, "README.txt"));
        writer.println("This readme file is provided so that the initial repository\n"
                + "has some content. You may delete it when you push other files\n"
                + "into the repository, if you wish.");
        writer.close();
    }

    // Create an appropriate default .gitignore file.
    PrintWriter writer = new PrintWriter(new File(tempRepoDir, ".gitignore"));
    writer.println("~*");
    writer.println("._*");
    writer.println(".TemporaryItems");
    writer.println(".DS_Store");
    writer.println("Thumbs.db");
    writer.close();

    // Add the files to the temporary repository.
    AddCommand add = new AddCommand(tempRepository);
    add.addFilepattern(".");
    add.setUpdate(false);

    try {
        add.call();
    } catch (NoFilepatternException e) {
        log.error("An exception occurred when adding the welcome files " + "to the repository: ", e);
        return bareRepository;
    }

    // Commit the changes.
    String email = Application.configurationProperties().getProperty("coreAdminEmail");
    CommitCommand commit = new Git(tempRepository).commit();
    commit.setAuthor("Web-CAT", email);
    commit.setCommitter("Web-CAT", email);
    commit.setMessage("Initial repository setup.");

    try {
        commit.call();
    } catch (Exception e) {
        log.error("An exception occurred when committing the welcome files " + "to the repository: ", e);
        return bareRepository;
    }

    // Push the changes to the bare repository.
    PushCommand push = new Git(tempRepository).push();
    @SuppressWarnings("deprecation")
    String url = location.toURL().toString();
    push.setRemote(url);
    push.setRefSpecs(new RefSpec("master"), new RefSpec("master"));

    try {
        push.call();
    } catch (Exception e) {
        log.error("An exception occurred when pushing the welcome files " + "to the repository: ", e);
        return bareRepository;
    }

    // Cleanup after ourselves.
    FileUtilities.deleteDirectory(tempRepoDir);

    return bareRepository;
}