Search in sources :

Example 1 with Change

use of org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change in project egit by eclipse.

the class TreeBuilder method build.

/**
 * @param root
 *            the root node of the tree to build, which will become the
 *            parent of the first level of children
 * @param repo
 * @param changes
 * @param fileFactory
 * @param treeFactory
 * @return the children of the root nodes
 */
public static GitModelObject[] build(final GitModelObjectContainer root, final Repository repo, final Map<String, Change> changes, final FileModelFactory fileFactory, final TreeModelFactory treeFactory) {
    if (changes == null || changes.isEmpty())
        return new GitModelObject[] {};
    final IPath rootPath = new Path(repo.getWorkTree().getAbsolutePath());
    final List<GitModelObject> rootChildren = new ArrayList<>();
    final Map<IPath, Node> nodes = new HashMap<>();
    for (Map.Entry<String, Change> entry : changes.entrySet()) {
        String repoRelativePath = entry.getKey();
        Change change = entry.getValue();
        GitModelObjectContainer parent = root;
        List<GitModelObject> children = rootChildren;
        IPath path = rootPath;
        // $NON-NLS-1$
        String[] segments = repoRelativePath.split("/");
        for (int i = 0; i < segments.length; i++) {
            path = path.append(segments[i]);
            // Changes represent files, so the last segment is the file name
            boolean fileNode = (i == segments.length - 1);
            if (!fileNode) {
                Node node = nodes.get(path);
                if (node == null) {
                    GitModelTree tree = treeFactory.createTreeModel(parent, path, change.getKind());
                    node = new Node(tree);
                    nodes.put(path, node);
                    children.add(tree);
                }
                parent = node.tree;
                children = node.children;
            } else {
                GitModelBlob file = fileFactory.createFileModel(parent, repo, change, path);
                children.add(file);
            }
        }
    }
    for (Node object : nodes.values()) {
        GitModelTree tree = object.tree;
        tree.setChildren(object.children);
    }
    return rootChildren.toArray(new GitModelObject[rootChildren.size()]);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Change(org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Change

use of org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change in project egit by eclipse.

the class GitModelCommit method createChildren.

private GitModelObject[] createChildren() {
    FileModelFactory fileModelFactory = new FileModelFactory() {

        @Override
        public GitModelBlob createFileModel(GitModelObjectContainer parent, Repository repository, Change change, IPath fullPath) {
            return new GitModelBlob(parent, repository, change, fullPath);
        }

        @Override
        public boolean isWorkingTree() {
            return false;
        }
    };
    TreeModelFactory treeModelFactory = new TreeModelFactory() {

        @Override
        public GitModelTree createTreeModel(GitModelObjectContainer parent, IPath fullPath, int kind) {
            return new GitModelTree(parent, fullPath, kind);
        }
    };
    return TreeBuilder.build(this, repo, commit.getChildren(), fileModelFactory, treeModelFactory);
}
Also used : TreeModelFactory(org.eclipse.egit.ui.internal.synchronize.model.TreeBuilder.TreeModelFactory) Repository(org.eclipse.jgit.lib.Repository) FileModelFactory(org.eclipse.egit.ui.internal.synchronize.model.TreeBuilder.FileModelFactory) IPath(org.eclipse.core.runtime.IPath) Change(org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change)

Example 3 with Change

use of org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change in project egit by eclipse.

the class WorkingTreeChangeCacheTest method shouldListSingleWorkspaceDeletion.

@Test
public void shouldListSingleWorkspaceDeletion() throws Exception {
    // given
    writeTrashFile(db, "a.txt", "trash");
    try (Git git = new Git(db)) {
        git.add().addFilepattern("a.txt").call();
    }
    deleteTrashFile(db, "a.txt");
    // when
    Map<String, Change> result = WorkingTreeChangeCache.build(db);
    // then
    assertThat(result.size(), is(1));
    assertFileDeletion(result, "a.txt", "a.txt");
}
Also used : Git(org.eclipse.jgit.api.Git) Change(org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change) Test(org.junit.Test)

Example 4 with Change

use of org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change in project egit by eclipse.

the class WorkingTreeChangeCacheTest method shouldListSingleWorkspaceChange.

@Test
public void shouldListSingleWorkspaceChange() throws Exception {
    // given
    writeTrashFile(db, "a.txt", "trash");
    try (Git git = new Git(db)) {
        git.add().addFilepattern("a.txt").call();
    }
    writeTrashFile(db, "a.txt", "modification");
    // when
    Map<String, Change> result = WorkingTreeChangeCache.build(db);
    // then
    assertThat(result.size(), is(1));
    assertFileChange(result, "a.txt", "a.txt");
}
Also used : Git(org.eclipse.jgit.api.Git) Change(org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change) Test(org.junit.Test)

Example 5 with Change

use of org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change in project egit by eclipse.

the class WorkingTreeChangeCacheTest method shouldListSingleWorkspaceDeletionInFolder.

@Test
public void shouldListSingleWorkspaceDeletionInFolder() throws Exception {
    // given
    writeTrashFile(db, "folder/a.txt", "trash");
    try (Git git = new Git(db)) {
        git.add().addFilepattern("folder/a.txt").call();
    }
    deleteTrashFile(db, "folder/a.txt");
    // when
    Map<String, Change> result = WorkingTreeChangeCache.build(db);
    // then
    assertThat(result.size(), is(1));
    assertFileDeletion(result, "folder/a.txt", "a.txt");
}
Also used : Git(org.eclipse.jgit.api.Git) Change(org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change) Test(org.junit.Test)

Aggregations

Change (org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change)35 Test (org.junit.Test)29 Git (org.eclipse.jgit.api.Git)21 HashMap (java.util.HashMap)3 Repository (org.eclipse.jgit.lib.Repository)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 IPath (org.eclipse.core.runtime.IPath)2 DirCacheIterator (org.eclipse.jgit.dircache.DirCacheIterator)2 MutableObjectId (org.eclipse.jgit.lib.MutableObjectId)2 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)2 Map (java.util.Map)1 Path (org.eclipse.core.runtime.Path)1 FileModelFactory (org.eclipse.egit.ui.internal.synchronize.model.TreeBuilder.FileModelFactory)1 TreeModelFactory (org.eclipse.egit.ui.internal.synchronize.model.TreeBuilder.TreeModelFactory)1 AbbreviatedObjectId (org.eclipse.jgit.lib.AbbreviatedObjectId)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 EmptyTreeIterator (org.eclipse.jgit.treewalk.EmptyTreeIterator)1