Search in sources :

Example 1 with WorkingTreeIterator

use of org.eclipse.jgit.treewalk.WorkingTreeIterator in project VoxelGamesLibv2 by VoxelGamesLib.

the class CustomAddCommand method call.

/**
 * Executes the {@code Add} command. Each instance of this class should only be used for one invocation of the
 * command. Don't call this method twice on an instance.
 *
 * @return the DirCache after Add
 */
@Override
@Nonnull
public DirCache call() throws GitAPIException {
    if (filepatterns.isEmpty())
        throw new NoFilepatternException(JGitText.get().atLeastOnePatternIsRequired);
    checkCallable();
    DirCache dc = null;
    // $NON-NLS-1$
    boolean addAll = filepatterns.contains(".");
    try (ObjectInserter inserter = repo.newObjectInserter();
        NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
        tw.setOperationType(OperationType.CHECKIN_OP);
        dc = repo.lockDirCache();
        DirCacheBuilder builder = dc.builder();
        tw.addTree(new DirCacheBuildIterator(builder));
        if (workingTreeIterator == null)
            workingTreeIterator = new FileTreeIterator(repo);
        workingTreeIterator.setDirCacheIterator(tw, 0);
        tw.addTree(workingTreeIterator);
        if (!addAll)
            tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));
        byte[] lastAdded = null;
        while (tw.next()) {
            DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
            WorkingTreeIterator f = tw.getTree(1, WorkingTreeIterator.class);
            // if (c == null && f != null && f.isEntryIgnored()) {
            // // file is not in index but is ignored, do nothing
            // continue;
            // } else if (c == null && update) {
            // // Only update of existing entries was requested.
            // continue;
            // }
            // JUST FUCK YOU
            DirCacheEntry entry = c != null ? c.getDirCacheEntry() : null;
            if (entry != null && entry.getStage() > 0 && lastAdded != null && lastAdded.length == tw.getPathLength() && tw.isPathPrefix(lastAdded, lastAdded.length) == 0) {
                // new DirCacheEntry per path.
                continue;
            }
            if (tw.isSubtree() && !tw.isDirectoryFileConflict()) {
                tw.enterSubtree();
                continue;
            }
            if (f == null) {
                // working tree file does not exist
                if (entry != null && (!update || GITLINK == entry.getFileMode())) {
                    builder.add(entry);
                }
                continue;
            }
            if (entry != null && entry.isAssumeValid()) {
                // Index entry is marked assume valid. Even though
                // the user specified the file to be added JGit does
                // not consider the file for addition.
                builder.add(entry);
                continue;
            }
            if ((f.getEntryRawMode() == TYPE_TREE && f.getIndexFileMode(c) != FileMode.GITLINK) || (f.getEntryRawMode() == TYPE_GITLINK && f.getIndexFileMode(c) == FileMode.TREE)) {
                // Index entry exists and is symlink, gitlink or file,
                // otherwise the tree would have been entered above.
                // Replace the index entry by diving into tree of files.
                tw.enterSubtree();
                continue;
            }
            byte[] path = tw.getRawPath();
            if (entry == null || entry.getStage() > 0) {
                entry = new DirCacheEntry(path);
            }
            FileMode mode = f.getIndexFileMode(c);
            entry.setFileMode(mode);
            if (GITLINK != mode) {
                entry.setLength(f.getEntryLength());
                entry.setLastModified(f.getEntryLastModified());
                long len = f.getEntryContentLength();
                // inserting. TODO: Fix this by using Buffers.
                try (InputStream in = f.openEntryStream()) {
                    ObjectId id = inserter.insert(OBJ_BLOB, len, in);
                    entry.setObjectId(id);
                }
            } else {
                entry.setLength(0);
                entry.setLastModified(0);
                entry.setObjectId(f.getEntryObjectId());
            }
            builder.add(entry);
            lastAdded = path;
        }
        inserter.flush();
        builder.commit();
        setCallable(false);
    } catch (IOException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause instanceof FilterFailedException)
            throw (FilterFailedException) cause;
        throw new JGitInternalException(JGitText.get().exceptionCaughtDuringExecutionOfAddCommand, e);
    } finally {
        if (dc != null)
            dc.unlock();
    }
    return dc;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) WorkingTreeIterator(org.eclipse.jgit.treewalk.WorkingTreeIterator) ObjectId(org.eclipse.jgit.lib.ObjectId) InputStream(java.io.InputStream) NoFilepatternException(org.eclipse.jgit.api.errors.NoFilepatternException) DirCacheBuildIterator(org.eclipse.jgit.dircache.DirCacheBuildIterator) IOException(java.io.IOException) NameConflictTreeWalk(org.eclipse.jgit.treewalk.NameConflictTreeWalk) DirCache(org.eclipse.jgit.dircache.DirCache) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) FilterFailedException(org.eclipse.jgit.api.errors.FilterFailedException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) Nonnull(javax.annotation.Nonnull)

Example 2 with WorkingTreeIterator

use of org.eclipse.jgit.treewalk.WorkingTreeIterator in project egit by eclipse.

the class IndexDiffCacheEntry method calcIndexDiffDataIncremental.

private IndexDiffData calcIndexDiffDataIncremental(IProgressMonitor monitor, String jobName, Repository repository, Collection<String> filesToUpdate, Collection<IResource> resourcesToUpdate) throws IOException {
    if (indexDiffData == null)
        // -> do full refresh instead
        return calcIndexDiffDataFull(monitor, jobName, repository);
    EclipseGitProgressTransformer jgitMonitor = new EclipseGitProgressTransformer(monitor);
    List<String> treeFilterPaths = calcTreeFilterPaths(filesToUpdate);
    WorkingTreeIterator iterator = IteratorService.createInitialIterator(repository);
    if (iterator == null)
        // workspace is closed
        return null;
    IndexDiff diffForChangedResources = new IndexDiff(repository, Constants.HEAD, iterator);
    diffForChangedResources.setFilter(PathFilterGroup.createFromStrings(treeFilterPaths));
    diffForChangedResources.diff(jgitMonitor, 0, 0, jobName);
    IndexDiffData previous = indexDiffData;
    if (previous == null) {
        // but the updateJob is still running (and about to cancel).
        return null;
    }
    return new IndexDiffData(previous, filesToUpdate, resourcesToUpdate, diffForChangedResources);
}
Also used : WorkingTreeIterator(org.eclipse.jgit.treewalk.WorkingTreeIterator) EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) IndexDiff(org.eclipse.jgit.lib.IndexDiff)

Example 3 with WorkingTreeIterator

use of org.eclipse.jgit.treewalk.WorkingTreeIterator in project egit by eclipse.

the class RepositoryUtil method canBeAutoIgnored.

/**
 * Checks if the existing resource with given path can be automatically
 * added to the .gitignore file.
 *
 * @param path
 *            Path to be checked, file or directory must exist on the disk
 * @return true if the file or directory at given path exists, is inside
 *         known git repository and does not match any existing ignore rule,
 *         false otherwise
 * @throws IOException
 * @since 4.1.0
 */
public static boolean canBeAutoIgnored(IPath path) throws IOException {
    Repository repository = Activator.getDefault().getRepositoryCache().getRepository(path);
    if (repository == null || repository.isBare()) {
        return false;
    }
    WorkingTreeIterator treeIterator = IteratorService.createInitialIterator(repository);
    if (treeIterator == null) {
        return false;
    }
    String repoRelativePath = path.makeRelativeTo(new Path(repository.getWorkTree().getAbsolutePath())).toString();
    if (repoRelativePath.length() == 0 || repoRelativePath.equals(path.toString())) {
        return false;
    }
    try (TreeWalk walk = new TreeWalk(repository)) {
        walk.addTree(treeIterator);
        walk.setFilter(PathFilterGroup.createFromStrings(repoRelativePath));
        while (walk.next()) {
            WorkingTreeIterator workingTreeIterator = walk.getTree(0, WorkingTreeIterator.class);
            if (walk.getPathString().equals(repoRelativePath)) {
                return !workingTreeIterator.isEntryIgnored();
            }
            if (workingTreeIterator.getEntryFileMode().equals(FileMode.TREE)) {
                walk.enterSubtree();
            }
        }
    }
    // path not found in tree, we should not automatically ignore it
    return false;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Repository(org.eclipse.jgit.lib.Repository) WorkingTreeIterator(org.eclipse.jgit.treewalk.WorkingTreeIterator) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 4 with WorkingTreeIterator

use of org.eclipse.jgit.treewalk.WorkingTreeIterator in project egit by eclipse.

the class LinkedResourcesTest method testLinkedResourcesIgnoredByContainerTreeIterator.

@Test
public void testLinkedResourcesIgnoredByContainerTreeIterator() throws Exception {
    // Create linked folder in project1 that points to project2
    IFolder folder = project1.getFolder("link2project2");
    folder.createLink(project2.getLocation(), IResource.ALLOW_MISSING_LOCAL, null);
    // Create linked file in project1 that points to a file in project2
    IFile file = project1.getFile("link2project2folder1file1.txt");
    file.createLink(project2.getFile("project2folder1/project2folder1file1.txt").getLocation(), IResource.ALLOW_MISSING_LOCAL, null);
    // Test iterator
    WorkingTreeIterator iterator = IteratorService.createInitialIterator(repository1.repository);
    assertTrue(iterator instanceof FileTreeIterator);
    while (!iterator.eof()) {
        assertFalse(iterator.getEntryPathString().startsWith("link2"));
        iterator.next(1);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) WorkingTreeIterator(org.eclipse.jgit.treewalk.WorkingTreeIterator) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) IFolder(org.eclipse.core.resources.IFolder) Test(org.junit.Test)

Example 5 with WorkingTreeIterator

use of org.eclipse.jgit.treewalk.WorkingTreeIterator in project egit by eclipse.

the class RepositoryUtil method isIgnored.

/**
 * Checks if existing resource with given path is to be ignored.
 * <p>
 * <b>Note:</b>The check makes sense only for files which exists in the
 * working directory. This method returns false for paths to not existing
 * files or directories.
 *
 * @param path
 *            Path to be checked, file or directory must exist on the disk
 * @return true if the path is either not inside git repository or exists
 *         and matches an ignore rule
 * @throws IOException
 * @since 2.3
 */
public static boolean isIgnored(IPath path) throws IOException {
    RepositoryMapping mapping = RepositoryMapping.getMapping(path);
    if (mapping == null) {
        // Linked resources may not be mapped
        return true;
    }
    Repository repository = mapping.getRepository();
    WorkingTreeIterator treeIterator = IteratorService.createInitialIterator(repository);
    if (treeIterator == null) {
        return true;
    }
    String repoRelativePath = mapping.getRepoRelativePath(path);
    if (repoRelativePath == null || repoRelativePath.isEmpty()) {
        return true;
    }
    try (TreeWalk walk = new TreeWalk(repository)) {
        walk.addTree(treeIterator);
        walk.setFilter(PathFilterGroup.createFromStrings(repoRelativePath));
        while (walk.next()) {
            WorkingTreeIterator workingTreeIterator = walk.getTree(0, WorkingTreeIterator.class);
            if (walk.getPathString().equals(repoRelativePath)) {
                return workingTreeIterator.isEntryIgnored();
            }
            if (workingTreeIterator.getEntryFileMode().equals(FileMode.TREE)) {
                walk.enterSubtree();
            }
        }
    }
    return false;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) WorkingTreeIterator(org.eclipse.jgit.treewalk.WorkingTreeIterator) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Aggregations

WorkingTreeIterator (org.eclipse.jgit.treewalk.WorkingTreeIterator)8 EclipseGitProgressTransformer (org.eclipse.egit.core.EclipseGitProgressTransformer)3 IndexDiff (org.eclipse.jgit.lib.IndexDiff)3 Repository (org.eclipse.jgit.lib.Repository)3 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)3 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 FileTreeIterator (org.eclipse.jgit.treewalk.FileTreeIterator)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Nonnull (javax.annotation.Nonnull)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 RepositoryMapping (org.eclipse.egit.core.project.RepositoryMapping)1