Example usage for org.eclipse.jgit.lib Constants GITIGNORE_FILENAME

List of usage examples for org.eclipse.jgit.lib Constants GITIGNORE_FILENAME

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants GITIGNORE_FILENAME.

Prototype

String GITIGNORE_FILENAME

To view the source code for org.eclipse.jgit.lib Constants GITIGNORE_FILENAME.

Click Source Link

Document

A gitignore file name

Usage

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

License:Open Source License

private void addIgnore(IProgressMonitor monitor, IResource resource)
        throws UnsupportedEncodingException, CoreException {
    IContainer container = resource.getParent();
    String entry = "/" + resource.getName() + "\n"; //$NON-NLS-1$  //$NON-NLS-2$
    ByteArrayInputStream entryBytes = asStream(entry);

    if (container instanceof IWorkspaceRoot) {
        Repository repository = RepositoryMapping.getMapping(resource.getProject()).getRepository();
        // .gitignore is not accessible as resource
        IPath gitIgnorePath = resource.getLocation().removeLastSegments(1).append(Constants.GITIGNORE_FILENAME);
        IPath repoPath = new Path(repository.getWorkTree().getAbsolutePath());
        if (!repoPath.isPrefixOf(gitIgnorePath)) {
            String message = NLS.bind(CoreText.IgnoreOperation_parentOutsideRepo,
                    resource.getLocation().toOSString(), repoPath.toOSString());
            IStatus status = Activator.error(message, null);
            throw new CoreException(status);
        }/*from   w  w w  .  j a  va  2 s .co  m*/
        File gitIgnore = new File(gitIgnorePath.toOSString());
        updateGitIgnore(gitIgnore, entry);
        // no resource change event when updating .gitignore outside
        // workspace => trigger manual decorator refresh
        gitignoreOutsideWSChanged = true;
    } else {
        IFile gitignore = container.getFile(new Path(Constants.GITIGNORE_FILENAME));
        IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
        if (gitignore.exists())
            gitignore.appendContents(entryBytes, true, true, subMonitor);
        else
            gitignore.create(entryBytes, true, subMonitor);
    }
}

From source file:org.eclipse.egit.core.test.op.IgnoreOperationTest.java

License:Open Source License

@After
public void tearDown() throws Exception {
    testRepository.dispose();//from   w w w  .j av a2 s  .c o  m
    // delete gitignore file in workspace folder
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    File rootFile = root.getRawLocation().toFile();
    File ignoreFile = new File(rootFile, Constants.GITIGNORE_FILENAME);
    if (ignoreFile.exists()) {
        FileUtils.delete(ignoreFile, FileUtils.RETRY);
        assert !ignoreFile.exists();
    }
    super.tearDown();
}

From source file:org.eclipse.egit.core.test.op.IgnoreOperationTest.java

License:Open Source License

@Test
public void testIgnoreFolder() throws Exception {
    IFolder binFolder = project.getProject().getFolder("bin");
    IgnoreOperation operation = new IgnoreOperation(new IResource[] { binFolder });
    operation.execute(new NullProgressMonitor());

    String content = project.getFileContent(Constants.GITIGNORE_FILENAME);
    assertEquals("/bin\n", content);
    assertFalse(operation.isGitignoreOutsideWSChanged());
}

From source file:org.eclipse.egit.core.test.op.IgnoreOperationTest.java

License:Open Source License

@Test
public void testIgnoreFileCancel() throws Exception {
    IFolder binFolder = project.getProject().getFolder("bin");
    IgnoreOperation operation = new IgnoreOperation(new IResource[] { binFolder });
    NullProgressMonitor monitor = new NullProgressMonitor();
    monitor.setCanceled(true);/*from w w  w . jav  a 2 s . co m*/
    operation.execute(monitor);

    assertFalse(project.getProject().getFile(Constants.GITIGNORE_FILENAME).exists());
}

From source file:org.eclipse.egit.core.test.op.IgnoreOperationTest.java

License:Open Source License

@Test
public void testIgnoreMultiFile() throws Exception {
    project.createSourceFolder();// w w w. j a v a  2  s .co m
    IFolder binFolder = project.getProject().getFolder("bin");
    IFolder srcFolder = project.getProject().getFolder("src");
    IgnoreOperation operation = new IgnoreOperation(new IResource[] { binFolder });
    operation.execute(new NullProgressMonitor());

    String content = project.getFileContent(Constants.GITIGNORE_FILENAME);
    assertEquals("/bin\n", content);

    operation = new IgnoreOperation(new IResource[] { srcFolder });
    operation.execute(new NullProgressMonitor());

    content = project.getFileContent(Constants.GITIGNORE_FILENAME);
    assertEquals("/bin\n/src\n", content);
}

From source file:org.eclipse.egit.core.test.op.IgnoreOperationTest.java

License:Open Source License

@Test
public void testIgnoreProject() throws Exception {
    IgnoreOperation operation = new IgnoreOperation(new IResource[] { project.getProject() });
    operation.execute(new NullProgressMonitor());

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    File rootFile = root.getRawLocation().toFile();
    File ignoreFile = new File(rootFile, Constants.GITIGNORE_FILENAME);
    String content = testUtils.slurpAndClose(ignoreFile.toURL().openStream());
    assertEquals("/" + project.getProject().getName() + "\n", content);
    assertTrue(operation.isGitignoreOutsideWSChanged());
}

From source file:org.eclipse.egit.ui.internal.actions.IgnoreAction.java

License:Open Source License

@SuppressWarnings("restriction")
@Override/*from  w ww .  j av  a2s .c  o m*/
public void execute(IAction action) {
    final IResource[] resources = getSelectedResources();
    if (resources.length == 0)
        return;

    WorkspaceJob job = new WorkspaceJob(UIText.IgnoreAction_jobName) {
        public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
            monitor.beginTask(UIText.IgnoreAction_taskName, resources.length);
            try {
                for (IResource resource : resources) {
                    // TODO This is pretty inefficient; multiple ignores in
                    // the same directory cause multiple writes.

                    // NB This does the same thing in
                    // DecoratableResourceAdapter, but neither currently
                    // consult .gitignore

                    if (!Team.isIgnoredHint(resource)) {
                        addIgnore(monitor, resource);
                    }
                    monitor.worked(1);
                }
                monitor.done();
            } catch (CoreException e) {
                throw e;
            } catch (Exception e) {
                throw new CoreException(
                        new Status(IStatus.ERROR, "org.eclipse.egit.ui", UIText.IgnoreAction_error, e)); //$NON-NLS-1$
            }
            return Status.OK_STATUS;
        }

        private void addIgnore(IProgressMonitor monitor, IResource resource)
                throws UnsupportedEncodingException, CoreException {
            IContainer container = resource.getParent();
            IFile gitignore = container.getFile(new Path(Constants.GITIGNORE_FILENAME));
            String entry = "/" + resource.getName() + "\n"; //$NON-NLS-1$  //$NON-NLS-2$
            ByteArrayInputStream entryBytes = asStream(entry);

            if (gitignore.exists())
                gitignore.appendContents(entryBytes, true, true, monitor);
            else
                gitignore.create(entryBytes, true, monitor);
        }

        private ByteArrayInputStream asStream(String entry) throws UnsupportedEncodingException {
            return new ByteArrayInputStream(entry.getBytes(Constants.CHARACTER_ENCODING));
        }
    };
    job.schedule();
}

From source file:org.eclipse.egit.ui.internal.decorators.GitLightweightDecorator.java

License:Open Source License

/**
 * Callback for IResourceChangeListener events
 *
 * Schedules a refresh of the changed resource
 *
 * If the preference for computing deep dirty states has been set we walk
 * the ancestor tree of the changed resource and update all parents as well.
 *
 * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
 *///from  www . j  ava  2  s  .c om
public void resourceChanged(IResourceChangeEvent event) {
    final Set<IResource> resourcesToUpdate = new HashSet<IResource>();

    try { // Compute the changed resources by looking at the delta
        event.getDelta().accept(new IResourceDeltaVisitor() {
            public boolean visit(IResourceDelta delta) throws CoreException {

                // If the file has changed but not in a way that we care
                // about (e.g. marker changes to files) then ignore
                if (delta.getKind() == IResourceDelta.CHANGED
                        && (delta.getFlags() & INTERESTING_CHANGES) == 0) {
                    return true;
                }

                final IResource resource = delta.getResource();

                // If the resource is not part of a project under Git
                // revision control
                final RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
                if (mapping == null) {
                    // Ignore the change
                    return true;
                }

                if (resource.getType() == IResource.ROOT) {
                    // Continue with the delta
                    return true;
                }

                if (resource.getType() == IResource.PROJECT) {
                    // If the project is not accessible, don't process it
                    if (!resource.isAccessible())
                        return false;
                }

                // All seems good, schedule the resource for update
                if (Constants.GITIGNORE_FILENAME.equals(resource.getName())) {
                    // re-decorate all container members when .gitignore changes
                    IContainer parent = resource.getParent();
                    if (parent.exists())
                        resourcesToUpdate.addAll(Arrays.asList(parent.members()));
                    else
                        return false;
                } else {
                    resourcesToUpdate.add(resource);
                }

                if (delta.getKind() == IResourceDelta.CHANGED && (delta.getFlags() & IResourceDelta.OPEN) > 1)
                    return false; // Don't recurse when opening projects
                else
                    return true;
            }
        }, true /* includePhantoms */);
    } catch (final CoreException e) {
        handleException(null, e);
    }

    if (resourcesToUpdate.isEmpty())
        return;

    // If ancestor-decoration is enabled in the preferences we walk
    // the ancestor tree of each of the changed resources and add
    // their parents to the update set
    final IPreferenceStore store = Activator.getDefault().getPreferenceStore();
    if (store.getBoolean(UIPreferences.DECORATOR_RECOMPUTE_ANCESTORS)) {
        final IResource[] changedResources = resourcesToUpdate.toArray(new IResource[resourcesToUpdate.size()]);
        for (IResource current : changedResources) {
            while (current.getType() != IResource.ROOT) {
                current = current.getParent();
                resourcesToUpdate.add(current);
            }
        }
    }

    postLabelEvent(new LabelProviderChangedEvent(this, resourcesToUpdate.toArray()));
}

From source file:org.jboss.tools.openshift.egit.core.GitIgnore.java

License:Open Source License

public GitIgnore(IProject project) throws IOException, CoreException {
    this(project.getFile(Constants.GITIGNORE_FILENAME));
}

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

License:Open Source License

private IFile createGitFile(IProject project, String... gitIgnoreEntries) throws CoreException {
    IFile gitFile = project.getFile(Constants.GITIGNORE_FILENAME);
    StringBuilder builder = new StringBuilder();
    for (String entry : gitIgnoreEntries) {
        builder.append(entry);/*from w  w  w .  j  av  a 2 s .c om*/
        builder.append(NL);
    }
    gitFile.create(new ByteArrayInputStream(builder.toString().getBytes()), IResource.FORCE, null);
    return gitFile;
}