Example usage for org.eclipse.jgit.lib Repository create

List of usage examples for org.eclipse.jgit.lib Repository create

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository create.

Prototype

public abstract void create(boolean bare) throws IOException;

Source Link

Document

Create a new Git repository initializing the necessary files and directories.

Usage

From source file:playRepository.GitRepositoryTest.java

License:Apache License

@Test
public void getPatch() throws IOException, GitAPIException {
    // given/*from   ww w .j  a  v  a 2 s. com*/
    String userName = "yobi";
    String projectName = "testProject";
    String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;

    String repoPath = wcPath + "/.git";
    File repoDir = new File(repoPath);
    Repository repo = new RepositoryBuilder().setGitDir(repoDir).build();
    repo.create(false);

    Git git = new Git(repo);
    String testFilePath = wcPath + "/readme.txt";
    BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));

    // when
    out.write("hello 1");
    out.flush();
    git.add().addFilepattern("readme.txt").call();
    git.commit().setMessage("commit 1").call();

    out.write("hello 2");
    out.flush();
    git.add().addFilepattern("readme.txt").call();
    RevCommit commit = git.commit().setMessage("commit 2").call();

    GitRepository gitRepo = new GitRepository(userName, projectName + "/");
    String patch = gitRepo.getPatch(commit.getId().getName());

    // then
    assertThat(patch).contains("-hello 1");
    assertThat(patch).contains("+hello 1hello 2");
}

From source file:playRepository.GitRepositoryTest.java

License:Apache License

@Test
public void getHistory() throws IOException, GitAPIException {
    // given//from   w w  w. j  ava2s  . c  o  m
    String userName = "yobi";
    String projectName = "testProject";
    String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;

    String repoPath = wcPath + "/.git";
    File repoDir = new File(repoPath);
    Repository repo = new RepositoryBuilder().setGitDir(repoDir).build();
    repo.create(false);

    Git git = new Git(repo);
    String testFilePath = wcPath + "/readme.txt";
    BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));

    // when
    out.write("hello 1");
    out.flush();
    git.add().addFilepattern("readme.txt").call();
    git.commit().setMessage("commit 1").call();

    out.write("hello 2");
    out.flush();
    git.add().addFilepattern("readme.txt").call();
    git.commit().setMessage("commit 2").call();

    git.tag().setName("tag").setAnnotated(true).call();

    out.write("hello 3");
    out.flush();
    git.add().addFilepattern("readme.txt").call();
    git.commit().setMessage("commit 3").call();

    GitRepository gitRepo = new GitRepository(userName, projectName + "/");

    List<Commit> history2 = gitRepo.getHistory(0, 2, "HEAD", null);
    List<Commit> history5 = gitRepo.getHistory(0, 5, null, null);
    List<Commit> tagHistory2 = gitRepo.getHistory(0, 2, "tag", null);

    // then
    assertThat(history2.size()).isEqualTo(2);
    assertThat(history2.get(0).getMessage()).isEqualTo("commit 3");
    assertThat(history2.get(1).getMessage()).isEqualTo("commit 2");

    assertThat(history5.size()).isEqualTo(3);
    assertThat(history5.get(0).getMessage()).isEqualTo("commit 3");
    assertThat(history5.get(1).getMessage()).isEqualTo("commit 2");
    assertThat(history5.get(2).getMessage()).isEqualTo("commit 1");

    assertThat(tagHistory2.size()).isEqualTo(2);
    assertThat(tagHistory2.get(0).getMessage()).isEqualTo("commit 2");
    assertThat(tagHistory2.get(1).getMessage()).isEqualTo("commit 1");
}

From source file:playRepository.GitRepositoryTest.java

License:Apache License

@Test
public void isFile() throws Exception {
    // Given/*from   w  w  w.  j a  v  a2s.co  m*/
    String userName = "yobi";
    String projectName = "mytest";
    String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;
    String repoPath = wcPath + "/.git";
    String dirName = "dir";
    String fileName = "file";

    Repository repository = new RepositoryBuilder().setGitDir(new File(repoPath)).build();
    repository.create(false);
    Git git = new Git(repository);
    FileUtils.forceMkdir(new File(wcPath + "/" + dirName));
    FileUtils.touch(new File(wcPath + "/" + fileName));
    git.add().addFilepattern(dirName).call();
    git.add().addFilepattern(fileName).call();
    git.commit().setMessage("test").call();
    repository.close();

    // When
    GitRepository gitRepository = new GitRepository(userName, projectName + "/");

    // Then
    assertThat(gitRepository.isFile(dirName)).isEqualTo(false);
    assertThat(gitRepository.isFile(fileName)).isEqualTo(true);
    assertThat(gitRepository.isFile("not_exist_file")).isEqualTo(false);
    assertThat(gitRepository.isFile(fileName, "not_exist_branch")).isEqualTo(false);
}

From source file:playRepository.GitRepositoryTest.java

License:Apache License

@Test
public void testGetAllBranches() throws IOException, GitAPIException {
    FakeApplication app = support.Helpers.makeTestApplication();
    Helpers.start(app);/*w  w w  .j a  v  a2s  .com*/

    // Given
    String userName = "wansoon";
    String projectName = "test";
    Project project = createProject(userName, projectName);
    project.save();

    String email = "test@email.com";
    String wcPath = GitRepository.getRepoPrefix() + userName + "/" + "clone-" + projectName + ".git";
    String repoPath = wcPath + "/.git";
    String dirName = "dir";
    String fileName = "file";

    GitRepository gitRepository = new GitRepository(userName, projectName);
    gitRepository.create();

    Repository repository = new RepositoryBuilder().setGitDir(new File(repoPath)).build();
    repository.create(false);

    Git git = new Git(repository);
    FileUtils.forceMkdir(new File(wcPath + "/" + dirName));
    FileUtils.touch(new File(wcPath + "/" + fileName));
    git.add().addFilepattern(dirName).call();
    git.add().addFilepattern(fileName).call();
    git.commit().setMessage("test").setAuthor(userName, email).call();

    String branchName = "testBranch";
    git.branchCreate().setName(branchName).setForce(true).call();

    git.push().setRemote(GitRepository.getGitDirectoryURL(project))
            .setRefSpecs(new RefSpec(branchName + ":" + branchName)).setForce(true).call();

    repository.close();

    // When
    List<GitBranch> gitBranches = gitRepository.getBranches();
    gitRepository.close();

    // Then
    assertThat(gitBranches.size()).isEqualTo(1);
    assertThat(gitBranches.get(0).getShortName()).isEqualTo(branchName);

    Helpers.stop(app);
}

From source file:playRepository.GitRepositoryTest.java

License:Apache License

@Test
public void getDiff_bigFile() throws IOException, GitAPIException {
    // given/*from ww  w  .j  a  v a 2 s. co  m*/
    String userName = "yobi";
    String projectName = "testProject";
    String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;

    String repoPath = wcPath + "/.git";
    File repoDir = new File(repoPath);
    Repository repo = new RepositoryBuilder().setGitDir(repoDir).build();
    repo.create(false);

    Git git = new Git(repo);
    String testFilePath = wcPath + "/readme.txt";
    BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));

    char[] cbuf = new char[FileDiff.SIZE_LIMIT + 1];
    java.util.Arrays.fill(cbuf, 'a');
    out.write(cbuf); // Add a big file
    out.flush();
    git.add().addFilepattern("readme.txt").call();
    RevCommit commit = git.commit().setMessage("commit 1").call();

    // When
    FileDiff diff = GitRepository.getDiff(repo, commit).get(0);

    // Then
    assertThat(diff.a).describedAs("a").isNull();
    assertThat(diff.b).describedAs("b").isNotNull();
    assertThat(diff.hasError(FileDiff.Error.A_SIZE_EXCEEDED)).describedAs("a exceeds the size limit.")
            .isFalse();
    assertThat(diff.hasError(FileDiff.Error.B_SIZE_EXCEEDED)).describedAs("b exceeds the size limit.").isTrue();
    assertThat(diff.hasError(FileDiff.Error.DIFF_SIZE_EXCEEDED)).describedAs("The diff exceeds the size limit.")
            .isFalse();
    assertThat(diff.hasError(FileDiff.Error.OTHERS_SIZE_EXCEEDED))
            .describedAs("The others exceeds the size limit.").isFalse();
}

From source file:playRepository.GitRepositoryTest.java

License:Apache License

@Test
public void getDiff_manyLines() throws IOException, GitAPIException {
    // given/*from ww w.  j  a  v  a2s .  c  om*/
    String userName = "yobi";
    String projectName = "testProject";
    String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;

    String repoPath = wcPath + "/.git";
    File repoDir = new File(repoPath);
    Repository repo = new RepositoryBuilder().setGitDir(repoDir).build();
    repo.create(false);

    Git git = new Git(repo);
    String testFilePath = wcPath + "/readme.txt";
    BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));

    for (int i = 0; i < FileDiff.LINE_LIMIT + 1; i++) {
        out.write("a\n"); // Add a big file
    }
    out.flush();
    git.add().addFilepattern("readme.txt").call();
    RevCommit commit = git.commit().setMessage("commit 1").call();

    // When
    FileDiff diff = GitRepository.getDiff(repo, commit).get(0);

    // Then
    assertThat(diff.hasError(FileDiff.Error.A_SIZE_EXCEEDED)).describedAs("a exceeds the size limit.")
            .isFalse();
    assertThat(diff.hasError(FileDiff.Error.B_SIZE_EXCEEDED)).describedAs("b exceeds the size limit.").isTrue();
    assertThat(diff.hasError(FileDiff.Error.DIFF_SIZE_EXCEEDED)).describedAs("The diff exceeds the size limit.")
            .isFalse();
    assertThat(diff.hasError(FileDiff.Error.OTHERS_SIZE_EXCEEDED))
            .describedAs("The others exceeds the size limit.").isFalse();

}

From source file:playRepository.GitRepositoryTest.java

License:Apache License

@Test
public void getDiff_smallChangeOfBigFile() throws IOException, GitAPIException {
    // given//w  w  w .j a v a2 s. co m
    String userName = "yobi";
    String projectName = "testProject";
    String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;

    String repoPath = wcPath + "/.git";
    File repoDir = new File(repoPath);
    Repository repo = new RepositoryBuilder().setGitDir(repoDir).build();
    repo.create(false);

    Git git = new Git(repo);
    String testFilePath = wcPath + "/readme.txt";
    BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));

    // Commit a big file
    for (int i = 0; i < FileDiff.LINE_LIMIT + 1; i++) {
        out.write("a\n"); // Add a big file
    }
    out.flush();
    git.add().addFilepattern("readme.txt").call();
    git.commit().setMessage("commit 1").call();

    // Modify the file
    out.write("b\n");
    out.flush();
    git.add().addFilepattern("readme.txt").call();
    RevCommit commit = git.commit().setMessage("commit 2").call();

    // When
    FileDiff diff = GitRepository.getDiff(repo, commit).get(0);

    // Then
    assertThat(diff.hasError(FileDiff.Error.A_SIZE_EXCEEDED)).describedAs("a exceeds the size limit.")
            .isFalse();
    assertThat(diff.hasError(FileDiff.Error.B_SIZE_EXCEEDED)).describedAs("b exceeds the size limit.")
            .isFalse();
    assertThat(diff.hasError(FileDiff.Error.DIFF_SIZE_EXCEEDED)).describedAs("The diff exceeds the size limit.")
            .isFalse();
    assertThat(diff.hasError(FileDiff.Error.OTHERS_SIZE_EXCEEDED))
            .describedAs("The others exceeds the size limit.").isFalse();
}

From source file:playRepository.GitRepositoryTest.java

License:Apache License

@Test
public void getDiff_manyFiles() throws IOException, GitAPIException {
    // given//from w ww  .j  av a 2 s .  c  o  m
    String userName = "yobi";
    String projectName = "testProject";
    String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;

    String repoPath = wcPath + "/.git";
    File repoDir = new File(repoPath);
    Repository repo = new RepositoryBuilder().setGitDir(repoDir).build();
    repo.create(false);

    Git git = new Git(repo);

    // Add four big files
    for (int i = 0; i < 4; i++) {
        String testFilePath = wcPath + "/" + i + ".txt";
        BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));
        char[] cbuf = new char[FileDiff.SIZE_LIMIT - 1];
        java.util.Arrays.fill(cbuf, 'a');
        out.write(cbuf);
        out.flush();
        git.add().addFilepattern(i + ".txt").call();
    }

    // Add a small file
    String testFilePath = wcPath + "/readme.txt";
    BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));
    out.write("hello");
    out.flush();
    git.add().addFilepattern("readme.txt").call();
    RevCommit commit = git.commit().setMessage("commit 1").call();

    // When
    List<FileDiff> diffs = GitRepository.getDiff(repo, commit);
    FileDiff diff = diffs.get(4);

    // Then
    assertThat(diff.hasError(FileDiff.Error.A_SIZE_EXCEEDED)).describedAs("a exceeds the size limit.")
            .isFalse();
    assertThat(diff.hasError(FileDiff.Error.B_SIZE_EXCEEDED)).describedAs("b exceeds the size limit.")
            .isFalse();
    assertThat(diff.hasError(FileDiff.Error.DIFF_SIZE_EXCEEDED)).describedAs("The diff exceeds the size limit.")
            .isFalse();
    assertThat(diff.hasError(FileDiff.Error.OTHERS_SIZE_EXCEEDED))
            .describedAs("The others exceeds the size limit.").isTrue();
}

From source file:support.Git.java

License:Apache License

public static Repository createRepository(String userName, String projectName, boolean bare)
        throws IOException {
    String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;
    String repoPath = wcPath + "/.git";
    File repoDir = new File(repoPath);
    Repository repository = new RepositoryBuilder().setGitDir(repoDir).build();
    repository.create(bare);
    return repository;
}