Search in sources :

Example 21 with IndexDiffData

use of org.eclipse.egit.core.internal.indexdiff.IndexDiffData in project egit by eclipse.

the class IndexDiffCacheTest method testModifyFileInIgnoredDir.

@Test
public void testModifyFileInIgnoredDir() throws Exception {
    testRepository.connect(project.project);
    project.createFile(".gitignore", "ignore\n".getBytes("UTF-8"));
    project.createFolder("sub");
    project.createFile("sub/ignore", new byte[] {});
    testRepository.addToIndex(project.project);
    testRepository.createInitialCommit("testRemoveIgnoredFile\n\nfirst commit\n");
    prepareCacheEntry();
    IndexDiffData data1 = waitForListenerCalled();
    assertThat(data1.getIgnoredNotInIndex(), hasItem("Project-1/sub/ignore"));
    IFile file = project.getProject().getFile("sub/ignore");
    FileOutputStream str = new FileOutputStream(file.getLocation().toFile());
    try {
        str.write("other contents".getBytes("UTF-8"));
    } finally {
        str.close();
    }
    // no job should be triggered for that change.
    waitForListenerNotCalled();
}
Also used : IFile(org.eclipse.core.resources.IFile) FileOutputStream(java.io.FileOutputStream) IndexDiffData(org.eclipse.egit.core.internal.indexdiff.IndexDiffData) Test(org.junit.Test)

Example 22 with IndexDiffData

use of org.eclipse.egit.core.internal.indexdiff.IndexDiffData in project egit by eclipse.

the class StashCreateHandler method isEnabled.

static boolean isEnabled(Repository repository) {
    if (repository == null)
        return false;
    if (!repository.getRepositoryState().canCommit())
        return false;
    IndexDiffCacheEntry entry = Activator.getDefault().getIndexDiffCache().getIndexDiffCacheEntry(repository);
    if (entry == null)
        return false;
    IndexDiffData diff = entry.getIndexDiff();
    if (diff == null)
        return false;
    if (diff.getAdded().isEmpty() && diff.getChanged().isEmpty() && diff.getRemoved().isEmpty() && diff.getUntracked().isEmpty() && diff.getModified().isEmpty() && diff.getMissing().isEmpty())
        return false;
    return true;
}
Also used : IndexDiffCacheEntry(org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry) IndexDiffData(org.eclipse.egit.core.internal.indexdiff.IndexDiffData)

Example 23 with IndexDiffData

use of org.eclipse.egit.core.internal.indexdiff.IndexDiffData in project egit by eclipse.

the class CommitActionHandler method getIndexDiffData.

private IndexDiffData getIndexDiffData(@NonNull final Repository repository, @NonNull final Collection<IProject> projects) {
    IndexDiffCacheEntry diffCacheEntry = org.eclipse.egit.core.Activator.getDefault().getIndexDiffCache().getIndexDiffCacheEntry(repository);
    IndexDiffData diff = null;
    if (diffCacheEntry != null) {
        diff = diffCacheEntry.getIndexDiff();
    }
    if (diff != null) {
        return diff;
    }
    final IndexDiffData[] result = { null };
    try {
        PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    result[0] = new IndexDiffData(CommitUI.getIndexDiff(repository, projects.toArray(new IProject[projects.size()]), monitor));
                } catch (IOException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        Activator.handleError(UIText.CommitAction_errorComputingDiffs, e.getCause(), true);
        return null;
    } catch (InterruptedException e) {
        return null;
    }
    return result[0];
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IndexDiffCacheEntry(org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry) IOException(java.io.IOException) IndexDiffData(org.eclipse.egit.core.internal.indexdiff.IndexDiffData) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 24 with IndexDiffData

use of org.eclipse.egit.core.internal.indexdiff.IndexDiffData in project egit by eclipse.

the class CommitActionHandler method autoStage.

private void autoStage(@NonNull final Repository repository, boolean includeUntracked, IResource[] resourcesInScope) {
    if (resourcesInScope == null || resourcesInScope.length == 0) {
        return;
    }
    final Set<IProject> projects = new HashSet<>();
    for (IResource rsc : resourcesInScope) {
        projects.add(rsc.getProject());
    }
    IndexDiffData diff = getIndexDiffData(repository, projects);
    if (diff == null) {
        return;
    }
    Set<String> mayBeCommitted = new HashSet<>();
    mayBeCommitted.addAll(diff.getAdded());
    mayBeCommitted.addAll(diff.getChanged());
    mayBeCommitted.addAll(diff.getRemoved());
    mayBeCommitted.addAll(diff.getModified());
    mayBeCommitted.addAll(diff.getMissing());
    if (!includeUntracked) {
        mayBeCommitted.removeAll(diff.getUntracked());
    } else {
        mayBeCommitted.addAll(diff.getUntracked());
    }
    mayBeCommitted.removeAll(diff.getAssumeUnchanged());
    final Set<String> toBeStaged = CommitUI.getSelectedFiles(repository, mayBeCommitted, resourcesInScope);
    if (toBeStaged.isEmpty()) {
        return;
    }
    Job job = new Job(UIText.AddToIndexAction_addingFiles) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            SubMonitor progress = SubMonitor.convert(monitor, toBeStaged.size() + 1);
            try (Git git = new Git(repository)) {
                AddCommand add = git.add();
                for (String toStage : toBeStaged) {
                    add.addFilepattern(toStage);
                    if (progress.isCanceled()) {
                        return Status.CANCEL_STATUS;
                    }
                    progress.worked(1);
                }
                add.call();
                progress.worked(1);
            } catch (GitAPIException e) {
                return Activator.createErrorStatus(CoreText.AddToIndexOperation_failed, e);
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            return JobFamilies.ADD_TO_INDEX.equals(family) || super.belongsTo(family);
        }
    };
    job.setUser(true);
    job.setRule(RuleUtil.getRule(repository));
    job.schedule();
}
Also used : SubMonitor(org.eclipse.core.runtime.SubMonitor) IProject(org.eclipse.core.resources.IProject) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Git(org.eclipse.jgit.api.Git) Job(org.eclipse.core.runtime.jobs.Job) IResource(org.eclipse.core.resources.IResource) IndexDiffData(org.eclipse.egit.core.internal.indexdiff.IndexDiffData) HashSet(java.util.HashSet) AddCommand(org.eclipse.jgit.api.AddCommand)

Example 25 with IndexDiffData

use of org.eclipse.egit.core.internal.indexdiff.IndexDiffData in project egit by eclipse.

the class IndexDiffCacheTest method testAddFileFromUntrackedFolder.

@Test
public void testAddFileFromUntrackedFolder() throws Exception {
    testRepository.connect(project.project);
    testRepository.addToIndex(project.project);
    testRepository.createInitialCommit("testAddFileFromUntrackedFolder\n\nfirst commit\n");
    prepareCacheEntry();
    project.createFolder("folder");
    project.createFolder("folder/a");
    project.createFolder("folder/b");
    IFile fileA = project.createFile("folder/a/file", new byte[] {});
    project.createFile("folder/b/file", new byte[] {});
    IndexDiffData data1 = waitForListenerCalled();
    assertThat(data1.getUntrackedFolders(), hasItem("Project-1/folder/"));
    testRepository.track(fileA.getLocation().toFile());
    IndexDiffData data2 = waitForListenerCalled();
    assertThat(data2.getAdded(), hasItem("Project-1/folder/a/file"));
    assertThat(data2.getUntrackedFolders(), not(hasItem("Project-1/folder/")));
    assertThat(data2.getUntrackedFolders(), not(hasItem("Project-1/folder/a")));
    assertThat(data2.getUntrackedFolders(), hasItem("Project-1/folder/b/"));
}
Also used : IFile(org.eclipse.core.resources.IFile) IndexDiffData(org.eclipse.egit.core.internal.indexdiff.IndexDiffData) Test(org.junit.Test)

Aggregations

IndexDiffData (org.eclipse.egit.core.internal.indexdiff.IndexDiffData)47 Test (org.junit.Test)34 IResource (org.eclipse.core.resources.IResource)14 File (java.io.File)11 IFile (org.eclipse.core.resources.IFile)10 IndexDiffCacheEntry (org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)6 IFolder (org.eclipse.core.resources.IFolder)4 MergeResult (org.eclipse.jgit.api.MergeResult)4 Repository (org.eclipse.jgit.lib.Repository)4 IOException (java.io.IOException)3 CoreException (org.eclipse.core.runtime.CoreException)3 IndexDiffCache (org.eclipse.egit.core.internal.indexdiff.IndexDiffCache)3 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 IndexDiffChangedListener (org.eclipse.egit.core.internal.indexdiff.IndexDiffChangedListener)2