Example usage for org.eclipse.jgit.dircache DirCache read

List of usage examples for org.eclipse.jgit.dircache DirCache read

Introduction

In this page you can find the example usage for org.eclipse.jgit.dircache DirCache read.

Prototype

public static DirCache read(File indexLocation, FS fs) throws CorruptObjectException, IOException 

Source Link

Document

Create a new in-core index representation and read an index from disk.

Usage

From source file:org.eclipse.egit.core.GitMoveDeleteHookTest.java

License:Open Source License

@Test
public void testDeleteFile() throws Exception {
    TestProject project = initRepoInsideProjectInsideWorkspace();
    testUtils.addFileToProject(project.getProject(), "file.txt", "some text");
    testUtils.addFileToProject(project.getProject(), "file2.txt", "some  more text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(new IResource[] {
            project.getProject().getFile("file.txt"), project.getProject().getFile("file2.txt") });
    addToIndexOperation.execute(null);/*from   w w w .jav  a 2s .c  o  m*/

    // Validate pre-conditions
    DirCache dirCache = DirCache.read(repository.getIndexFile(), FS.DETECTED);
    assertEquals(2, dirCache.getEntryCount());
    assertNotNull(dirCache.getEntry("file.txt"));
    assertNotNull(dirCache.getEntry("file2.txt"));
    // Modify the content before the move
    testUtils.changeContentOfFile(project.getProject(), project.getProject().getFile("file.txt"), "other text");
    project.getProject().getFile("file.txt").delete(true, null);

    // Check index for the deleted file
    dirCache.read();
    assertEquals(1, dirCache.getEntryCount());
    assertNull(dirCache.getEntry("file.txt"));
    assertNotNull(dirCache.getEntry("file2.txt"));
    // Actual file is deleted
    assertFalse(project.getProject().getFile("file.txt").exists());
    // But a non-affected file remains
    assertTrue(project.getProject().getFile("file2.txt").exists());
}

From source file:org.eclipse.egit.core.GitMoveDeleteHookTest.java

License:Open Source License

@Test
public void testDeleteFolder() throws Exception {
    TestProject project = initRepoInsideProjectInsideWorkspace();
    testUtils.addFileToProject(project.getProject(), "folder/file.txt", "some text");
    testUtils.addFileToProject(project.getProject(), "folder2/file.txt", "some other text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
            new IResource[] { project.getProject().getFile("folder/file.txt"),
                    project.getProject().getFile("folder2/file.txt") });
    addToIndexOperation.execute(null);//from  w  w w  . j  av  a 2s  .c om

    DirCache dirCache = DirCache.read(repository.getIndexFile(), FS.DETECTED);
    assertNotNull(dirCache.getEntry("folder/file.txt"));
    assertNotNull(dirCache.getEntry("folder2/file.txt"));
    // Modify the content before the move
    testUtils.changeContentOfFile(project.getProject(), project.getProject().getFile("folder/file.txt"),
            "other text");
    project.getProject().getFolder("folder").delete(true, null);

    dirCache.read();
    // Unlike delete file, dircache is untouched... pretty illogical
    // TODO: Change the behavior of the hook.
    assertNotNull(dirCache.getEntry("folder/file.txt"));
    // Not moved file still there
    assertNotNull(dirCache.getEntry("folder2/file.txt"));
}

From source file:org.eclipse.egit.core.GitMoveDeleteHookTest.java

License:Open Source License

@Test
public void testDeleteProject() throws Exception {
    TestProject project = initRepoAboveProjectInsideWs("P/", "");
    testUtils.addFileToProject(project.getProject(), "file.txt", "some text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
            new IResource[] { project.getProject().getFile("file.txt") });
    addToIndexOperation.execute(null);//w ww  . j  a  v a2s  .c o m

    RepositoryMapping mapping = RepositoryMapping.getMapping(project.getProject());
    IPath gitDirAbsolutePath = mapping.getGitDirAbsolutePath();
    Repository db = new FileRepository(gitDirAbsolutePath.toFile());
    DirCache index = DirCache.read(db.getIndexFile(), db.getFS());
    assertNotNull(index.getEntry("P/Project-1/file.txt"));
    db.close();
    db = null;
    project.getProject().delete(true, null);
    assertNull(RepositoryMapping.getMapping(project.getProject()));
    // Check that the repo is still there. Being a bit paranoid we look for
    // a file
    assertTrue(gitDirAbsolutePath.toString(), gitDirAbsolutePath.append("HEAD").toFile().exists());

    db = new FileRepository(gitDirAbsolutePath.toFile());
    index = DirCache.read(db.getIndexFile(), db.getFS());
    // FIXME: Shouldn't we unstage deleted projects?
    assertNotNull(index.getEntry("P/Project-1/file.txt"));
    db.close();
}

From source file:org.eclipse.egit.core.GitMoveDeleteHookTest.java

License:Open Source License

@Test
public void testMoveFile() throws Exception {
    TestProject project = initRepoInsideProjectInsideWorkspace();
    testUtils.addFileToProject(project.getProject(), "file.txt", "some text");
    testUtils.addFileToProject(project.getProject(), "file2.txt", "some  more text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(new IResource[] {
            project.getProject().getFile("file.txt"), project.getProject().getFile("file2.txt") });
    addToIndexOperation.execute(null);/*from   w  w  w  . jav a2 s .co m*/

    // Validate pre-conditions
    DirCache dirCache = DirCache.read(repository.getIndexFile(), FS.DETECTED);
    assertNotNull(dirCache.getEntry("file.txt"));
    assertNotNull(dirCache.getEntry("file2.txt"));
    assertNull(dirCache.getEntry("data.txt"));
    assertFalse(project.getProject().getFile("data.txt").exists());
    ObjectId oldContentId = dirCache.getEntry("file.txt").getObjectId();
    // Modify the content before the move
    testUtils.changeContentOfFile(project.getProject(), project.getProject().getFile("file.txt"), "other text");
    project.getProject().getFile("file.txt").move(project.getProject().getFile("data.txt").getFullPath(), false,
            null);

    dirCache.read();
    assertTrue(project.getProject().getFile("data.txt").exists());
    assertNotNull(dirCache.getEntry("data.txt"));
    // Same content in index as before the move
    assertEquals(oldContentId, dirCache.getEntry("data.txt").getObjectId());

    // Not moved file still in its old place
    assertNotNull(dirCache.getEntry("file2.txt"));
}

From source file:org.eclipse.egit.core.GitMoveDeleteHookTest.java

License:Open Source License

/**
 * Rename "folder" to "dir"./*from  ww w  .  j  av a  2 s  .  c om*/
 *
 * @throws Exception
 */
@Test
public void testMoveFolder() throws Exception {
    TestProject project = initRepoInsideProjectInsideWorkspace();
    testUtils.addFileToProject(project.getProject(), "folder/file.txt", "some text");
    testUtils.addFileToProject(project.getProject(), "folder2/file.txt", "some other text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
            new IResource[] { project.getProject().getFile("folder/file.txt"),
                    project.getProject().getFile("folder2/file.txt") });
    addToIndexOperation.execute(null);

    DirCache dirCache = DirCache.read(repository.getIndexFile(), FS.DETECTED);
    assertNotNull(dirCache.getEntry("folder/file.txt"));
    assertNotNull(dirCache.getEntry("folder2/file.txt"));
    assertNull(dirCache.getEntry("dir/file.txt"));
    assertFalse(project.getProject().getFile("dir/file.txt").exists());
    ObjectId oldContentId = dirCache.getEntry("folder/file.txt").getObjectId();
    // Modify the content before the move
    testUtils.changeContentOfFile(project.getProject(), project.getProject().getFile("folder/file.txt"),
            "other text");
    project.getProject().getFolder("folder").move(project.getProject().getFolder("dir").getFullPath(), false,
            null);

    dirCache.read();
    assertTrue(project.getProject().getFile("dir/file.txt").exists());
    assertNull(dirCache.getEntry("folder/file.txt"));
    assertNotNull(dirCache.getEntry("dir/file.txt"));
    // Same content in index as before the move
    assertEquals(oldContentId, dirCache.getEntry("dir/file.txt").getObjectId());
    // Not moved file still there
    assertNotNull(dirCache.getEntry("folder2/file.txt"));
}

From source file:org.eclipse.egit.core.GitMoveDeleteHookTest.java

License:Open Source License

private void dotestMoveProjectWithinRepo(String srcParent, String srcProjectName, String dstParent,
        String dstProjecName, String gitDir, boolean sourceInsideWs) throws IOException, CoreException {
    String gdRelativeSrcParent = srcParent + srcProjectName + "/";
    if (gdRelativeSrcParent.startsWith(gitDir))
        gdRelativeSrcParent = gdRelativeSrcParent.substring(gitDir.length());
    String gdRelativeDstParent = dstParent + dstProjecName + "/";
    if (gdRelativeDstParent.startsWith(gitDir))
        gdRelativeDstParent = gdRelativeDstParent.substring(gitDir.length());

    registerWorkspaceRelativeTestDirProject(srcParent, srcProjectName);
    registerWorkspaceRelativeTestDirProject(dstParent, dstProjecName);

    // Old cruft may be laying around
    TestProject project = initRepoAboveProject(srcParent, gitDir, sourceInsideWs);
    IProject project0 = project.getProject().getWorkspace().getRoot().getProject(dstProjecName);
    project0.delete(true, null);//from   w  ww.  j  av  a 2  s  .  c o m

    testUtils.addFileToProject(project.getProject(), "file.txt", "some text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
            new IResource[] { project.getProject().getFile("file.txt") });
    addToIndexOperation.execute(null);

    // Check condition before move
    DirCache dirCache = DirCache.read(repository.getIndexFile(), FS.DETECTED);
    assertNotNull(dirCache.getEntry(gdRelativeSrcParent + "file.txt"));
    ObjectId oldContentId = dirCache.getEntry(gdRelativeSrcParent + "file.txt").getObjectId();

    // Modify the content before the move, we want to see the staged content
    // as it was before the move in the index
    testUtils.changeContentOfFile(project.getProject(), project.getProject().getFile("file.txt"), "other text");
    IProjectDescription description = project.getProject().getDescription();
    description.setName(dstProjecName);
    if (sourceInsideWs)
        if (dstParent.length() > 0)
            description.setLocationURI(URIUtil.toURI(project.getProject().getWorkspace().getRoot().getLocation()
                    .append(dstParent + dstProjecName)));
        else
            description.setLocationURI(null);
    else
        description.setLocationURI(
                URIUtil.toURI(new Path(workspaceSupplement + "/" + dstParent + "/" + dstProjecName)));
    project.getProject().move(description, IResource.FORCE | IResource.SHALLOW, null);
    IProject project2 = project.getProject().getWorkspace().getRoot().getProject(dstProjecName);
    assertTrue(project2.exists());
    assertNotNull(RepositoryMapping.getMapping(project2));

    // Check that our file exists on disk has a new location in the index
    dirCache.read();
    assertTrue(project2.getFile("file.txt").exists());
    assertNotNull(dirCache.getEntry(gdRelativeDstParent + "file.txt"));

    // Same content in index as before the move, i.e. not same as on disk
    assertEquals(oldContentId, dirCache.getEntry(gdRelativeDstParent + "file.txt").getObjectId());
}

From source file:org.eclipse.egit.core.test.TestRepository.java

License:Open Source License

public boolean inIndex(String path) throws IOException {
    String repoPath = getRepoRelativePath(path);
    DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS());

    return dc.getEntry(repoPath) != null;
}

From source file:org.eclipse.egit.core.test.TestRepository.java

License:Open Source License

public long lastModifiedInIndex(String path) throws IOException {
    String repoPath = getRepoRelativePath(path);
    DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS());

    return dc.getEntry(repoPath).getLastModified();
}

From source file:org.eclipse.egit.core.test.TestRepository.java

License:Open Source License

public int getDirCacheEntryLength(String path) throws IOException {
    String repoPath = getRepoRelativePath(path);
    DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS());

    return dc.getEntry(repoPath).getLength();
}

From source file:org.jboss.tools.openshift.egit.internal.test.util.TestRepository.java

License:Open Source License

private DirCacheEntry getDirCacheEntry(String path) throws IOException {
    String repoPath = getRepoRelativePath(path);
    DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS());

    return dc.getEntry(repoPath);
}