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:org.debian.dependency.sources.TestJGitSource.java

License:Apache License

/**
 * We should be able to create a git repo over top of other version control systems, public void testInitOtherVcs() throws
 * Exception { }/*from   ww  w  .  j  a v a 2s.  co  m*/
 *
 * /** When cleaning the source, we must take into account all types of dirty file states.
 */
@Test
public void testClean() throws Exception {
    Git git = Git.open(directory);

    File.createTempFile("unchanged", "file", directory);

    File subChanged = new File(new File(directory, "directory"), "changed");
    subChanged.getParentFile().mkdirs();
    subChanged.createNewFile();
    File changed = File.createTempFile("changed", "file", directory);

    git.add().addFilepattern(".").call();
    git.commit().setMessage("Prepare for test").call();

    source.initialize(directory, ORIGIN);

    Files.write("some data", subChanged, Charset.defaultCharset());
    Files.write("more data", changed, Charset.defaultCharset());
    File.createTempFile("new", "file", directory);

    File untrackedDir = new File(directory, "untracked");
    untrackedDir.mkdirs();
    File.createTempFile("another", "file", untrackedDir);

    source.clean();

    Status status = git.status().call();
    assertThat(status.getUntracked(), hasSize(0));
    assertThat(status.getUntrackedFolders(), hasSize(0));
    assertThat(status.getIgnoredNotInIndex(), hasSize(0));
    assertTrue("No changes", status.isClean());
}

From source file:org.eclipse.egit.core.op.AddToIndexOperation.java

License:Open Source License

private void addToCommand(IResource resource, Map<RepositoryMapping, AddCommand> addCommands) {
    IProject project = resource.getProject();
    RepositoryMapping map = RepositoryMapping.getMapping(project);
    AddCommand command = addCommands.get(map);
    if (command == null) {
        Repository repo = map.getRepository();
        Git git = new Git(repo);
        AdaptableFileTreeIterator it = new AdaptableFileTreeIterator(repo, resource.getWorkspace().getRoot());
        command = git.add().setWorkingTreeIterator(it);
        addCommands.put(map, command);//  w w w  .  jav a 2  s . c  o m
    }
    String filepattern = map.getRepoRelativePath(resource);
    if ("".equals(filepattern)) //$NON-NLS-1$
        filepattern = "."; //$NON-NLS-1$
    command.addFilepattern(filepattern);
}

From source file:org.eclipse.egit.core.storage.GitBlobStorageTest.java

License:Open Source License

@Test
public void testGitFileHistorySingleProjectOk() throws Exception {
    IProgressMonitor progress = new NullProgressMonitor();
    TestProject singleRepoProject = new TestProject(true, "Project-2");
    IProject proj = singleRepoProject.getProject();
    File singleProjectGitDir = new File(proj.getLocation().toFile(), Constants.DOT_GIT);
    if (singleProjectGitDir.exists())
        FileUtils.delete(singleProjectGitDir, FileUtils.RECURSIVE | FileUtils.RETRY);

    Repository singleProjectRepo = FileRepositoryBuilder.create(singleProjectGitDir);
    singleProjectRepo.create();//from   w ww. j  a  v  a 2s .c o  m

    // Repository must be mapped in order to test the GitFileHistory
    Activator.getDefault().getRepositoryUtil().addConfiguredRepository(singleProjectGitDir);
    ConnectProviderOperation connectOp = new ConnectProviderOperation(proj, singleProjectGitDir);
    connectOp.execute(progress);

    try {
        IFile file = proj.getFile("file");
        file.create(new ByteArrayInputStream("data".getBytes("UTF-8")), 0, progress);
        Git git = new Git(singleProjectRepo);
        git.add().addFilepattern(".").call();
        RevCommit commit = git.commit().setAuthor("JUnit", "junit@jgit.org").setAll(true)
                .setMessage("First commit").call();

        GitFileHistoryProvider fhProvider = new GitFileHistoryProvider();
        IFileHistory fh = fhProvider.getFileHistoryFor(singleRepoProject.getProject(), 0, null);
        assertNotNull(fh);
        assertEquals(fh.getFileRevisions().length, 1);
        assertNotNull(fh.getFileRevision(commit.getId().getName()));
    } finally {
        DisconnectProviderOperation disconnectOp = new DisconnectProviderOperation(
                Collections.singletonList(proj));
        disconnectOp.execute(progress);
        Activator.getDefault().getRepositoryUtil().removeDir(singleProjectGitDir);
        singleProjectRepo.close();
        singleRepoProject.dispose();
    }
}

From source file:org.eclipse.egit.core.synchronize.GitCommitsModelCacheTest.java

License:Open Source License

@Test
public void shouldListAdditionOrDeletionInCommit() throws Exception {
    // given/*from  www.j av a2s  .c om*/
    Git git = new Git(db);
    writeTrashFile(db, "a.txt", "content");
    git.add().addFilepattern("a.txt").call();
    RevCommit c = commit(git, "first commit");
    // when
    List<Commit> leftResult = GitCommitsModelCache.build(db, initialTagId(), c, null);
    List<Commit> rightResult = GitCommitsModelCache.build(db, c, initialTagId(), null);
    // then
    // left assertions
    assertThat(leftResult, notNullValue());
    assertCommit(leftResult.get(0), c, 1);
    assertFileDeletion(c, leftResult.get(0).getChildren().get("a.txt"), "a.txt", LEFT);
    // right asserts, after changing sides addition becomes deletion
    assertThat(rightResult, notNullValue());
    assertCommit(rightResult.get(0), c, 1);
    assertFileAddition(c, rightResult.get(0).getChildren().get("a.txt"), "a.txt", RIGHT);
}

From source file:org.eclipse.egit.core.synchronize.GitCommitsModelCacheTest.java

License:Open Source License

@Test
public void shouldListAdditionOrDeletionInsideFolderInCommit() throws Exception {
    // given//from ww w. jav  a  2  s  . co m
    Git git = new Git(db);
    writeTrashFile(db, "folder/a.txt", "content");
    git.add().addFilepattern("folder/a.txt").call();
    RevCommit c = commit(git, "first commit");
    // when
    List<Commit> leftResult = GitCommitsModelCache.build(db, initialTagId(), c, null);
    List<Commit> rightResult = GitCommitsModelCache.build(db, c, initialTagId(), null);
    // then
    // left assertions
    assertThat(leftResult, notNullValue());
    assertCommit(leftResult.get(0), c, 1);
    assertThat(leftResult.get(0).getChildren().size(), is(1));
    assertFileDeletion(c, leftResult.get(0).getChildren().get("folder/a.txt"), "a.txt", LEFT);
    // right asserts, after changing sides addition becomes deletion
    assertThat(rightResult, notNullValue());
    assertCommit(rightResult.get(0), c, 1);
    assertThat(rightResult.get(0).getChildren().size(), is(1));
    assertFileAddition(c, rightResult.get(0).getChildren().get("folder/a.txt"), "a.txt", RIGHT);
}

From source file:org.eclipse.egit.core.synchronize.GitCommitsModelCacheTest.java

License:Open Source License

@Test
public void shouldListAdditionsOrDeletionsInsideSeparateFoldersInCommit() throws Exception {
    // given/*from  w  w  w . j av a2  s  . co m*/
    Git git = new Git(db);
    writeTrashFile(db, "folder/a.txt", "content");
    writeTrashFile(db, "folder2/b.txt", "b content");
    git.add().addFilepattern("folder/a.txt").call();
    git.add().addFilepattern("folder2/b.txt").call();
    RevCommit c = commit(git, "first commit");
    // when
    List<Commit> leftResult = GitCommitsModelCache.build(db, initialTagId(), c, null);
    List<Commit> rightResult = GitCommitsModelCache.build(db, c, initialTagId(), null);
    // then
    // left assertions
    assertThat(leftResult, notNullValue());
    assertThat(Integer.valueOf(leftResult.size()), is(Integer.valueOf(1)));
    assertThat(leftResult.get(0).getShortMessage(), is("first commit"));
    assertThat(leftResult.get(0).getChildren(), notNullValue());
    assertThat(leftResult.get(0).getChildren().size(), is(2));
    assertFileDeletion(c, leftResult.get(0).getChildren().get("folder/a.txt"), "a.txt", LEFT);
    assertFileDeletion(c, leftResult.get(0).getChildren().get("folder2/b.txt"), "b.txt", LEFT);
    // right asserts, after changing sides addition becomes deletion
    assertThat(rightResult, notNullValue());
    assertThat(Integer.valueOf(rightResult.size()), is(Integer.valueOf(1)));
    assertThat(rightResult.get(0).getShortMessage(), is("first commit"));
    assertThat(rightResult.get(0).getChildren(), notNullValue());
    assertThat(rightResult.get(0).getChildren().size(), is(2));
    assertFileAddition(c, rightResult.get(0).getChildren().get("folder/a.txt"), "a.txt", RIGHT);
    assertFileAddition(c, rightResult.get(0).getChildren().get("folder2/b.txt"), "b.txt", RIGHT);
}

From source file:org.eclipse.egit.core.synchronize.GitCommitsModelCacheTest.java

License:Open Source License

@Test
public void shouldApplyPathFilter() throws Exception {
    // given/* www  . j a va 2  s. c  o  m*/
    Git git = new Git(db);
    writeTrashFile(db, "folder/a.txt", "content");
    writeTrashFile(db, "folder2/b.txt", "b content");
    git.add().addFilepattern("folder/a.txt").call();
    git.add().addFilepattern("folder2/b.txt").call();
    RevCommit c = commit(git, "first commit");

    // when
    PathFilter pathFilter = PathFilter.create("folder");
    List<Commit> leftResult = GitCommitsModelCache.build(db, initialTagId(), c, pathFilter);
    // then
    assertThat(leftResult, notNullValue());
    assertThat(Integer.valueOf(leftResult.size()), is(Integer.valueOf(1)));
    assertThat(leftResult.get(0).getShortMessage(), is("first commit"));
    assertThat(leftResult.get(0).getChildren(), notNullValue());
    assertThat(leftResult.get(0).getChildren().size(), is(1));
    assertFileDeletion(c, leftResult.get(0).getChildren().get("folder/a.txt"), "a.txt", LEFT);
}

From source file:org.eclipse.egit.core.synchronize.GitCommitsModelCacheTest.java

License:Open Source License

@Test
public void shouldListAdditionsOrDeletionsInsideFolderInCommit() throws Exception {
    // given//  w w  w .  j a  v  a2  s. c  o m
    Git git = new Git(db);
    writeTrashFile(db, "folder/a.txt", "content");
    writeTrashFile(db, "folder/b.txt", "b content");
    git.add().addFilepattern("folder").call();
    RevCommit c = commit(git, "first commit");
    // when
    List<Commit> leftResult = GitCommitsModelCache.build(db, initialTagId(), c, null);
    List<Commit> rightResult = GitCommitsModelCache.build(db, c, initialTagId(), null);
    // then
    // left assertions
    assertThat(leftResult, notNullValue());
    assertThat(Integer.valueOf(leftResult.size()), is(Integer.valueOf(1)));
    assertCommit(leftResult.get(0), c, 2);
    assertThat(leftResult.get(0).getChildren().size(), is(2));
    assertFileDeletion(c, leftResult.get(0).getChildren().get("folder/a.txt"), "a.txt", LEFT);
    assertFileDeletion(c, leftResult.get(0).getChildren().get("folder/b.txt"), "b.txt", LEFT);
    // right asserts, after changing sides addition becomes deletion
    assertThat(rightResult, notNullValue());
    assertThat(Integer.valueOf(rightResult.size()), is(Integer.valueOf(1)));
    assertCommit(rightResult.get(0), c, 2);
    assertThat(rightResult.get(0).getChildren().size(), is(2));
    assertFileAddition(c, rightResult.get(0).getChildren().get("folder/a.txt"), "a.txt", RIGHT);
    assertFileAddition(c, rightResult.get(0).getChildren().get("folder/b.txt"), "b.txt", RIGHT);
}

From source file:org.eclipse.egit.core.synchronize.GitCommitsModelCacheTest.java

License:Open Source License

@Test
public void shouldListChangeInCommit() throws Exception {
    // given/*from   ww w.  jav  a 2s .  c o  m*/
    Git git = new Git(db);
    writeTrashFile(db, "a.txt", "content");
    git.add().addFilepattern("a.txt").call();
    RevCommit c1 = commit(git, "first commit");
    writeTrashFile(db, "a.txt", "new content");
    RevCommit c2 = commit(git, "second commit");
    // when
    List<Commit> leftResult = GitCommitsModelCache.build(db, c1, c2, null);
    List<Commit> rightResult = GitCommitsModelCache.build(db, c2, c1, null);
    // then
    // left assertions
    assertThat(leftResult, notNullValue());
    assertCommit(leftResult.get(0), c2, 1);
    assertFileChange(c1, c2, leftResult.get(0).getChildren().get("a.txt"), "a.txt", LEFT);
    // right asserts
    assertThat(rightResult, notNullValue());
    assertCommit(rightResult.get(0), c2, 1);
    assertFileChange(c2, c1, rightResult.get(0).getChildren().get("a.txt"), "a.txt", RIGHT);
}

From source file:org.eclipse.egit.core.synchronize.GitCommitsModelCacheTest.java

License:Open Source License

@Test
public void shouldListChangeInsideFolderInCommit() throws Exception {
    // given// w  ww.  j ava  2 s .  c  o m
    Git git = new Git(db);
    writeTrashFile(db, "folder/a.txt", "content");
    git.add().addFilepattern("folder/a.txt").call();
    RevCommit c1 = commit(git, "first commit");
    writeTrashFile(db, "folder/a.txt", "new content");
    RevCommit c2 = commit(git, "second commit");
    // when
    List<Commit> leftResult = GitCommitsModelCache.build(db, c1, c2, null);
    List<Commit> rightResult = GitCommitsModelCache.build(db, c2, c1, null);
    // then
    // left assertions
    assertThat(leftResult, notNullValue());
    assertCommit(leftResult.get(0), c2, 1);
    assertFileChange(c1, c2, leftResult.get(0).getChildren().get("folder/a.txt"), "a.txt", LEFT);
    // right asserts
    assertThat(rightResult, notNullValue());
    assertCommit(rightResult.get(0), c2, 1);
    assertFileChange(c2, c1, rightResult.get(0).getChildren().get("folder/a.txt"), "a.txt", RIGHT);
}