diff options
| author | Alex Blewitt | 2009-10-15 18:47:05 +0000 |
|---|---|---|
| committer | Shawn O. Pearce | 2009-12-01 01:57:51 +0000 |
| commit | 7fe41b7e3c0732c8f391a61d5feabfc14d2965ba (patch) | |
| tree | 8c5ad59c40c303bb85b3ded912d112682d5db447 | |
| parent | cb9adcf59464fc49a555516039435c45eb6ccb60 (diff) | |
| download | egit-7fe41b7e3c0732c8f391a61d5feabfc14d2965ba.tar.gz egit-7fe41b7e3c0732c8f391a61d5feabfc14d2965ba.tar.xz egit-7fe41b7e3c0732c8f391a61d5feabfc14d2965ba.zip | |
Add to '.gitignore' action
This adds .gitignore to the object contribution menu, and adds the
highlighted elements to the .gitignore file
Bug: 291133
Change-Id: Ic202fb67b80be93260974cde21d872a64a4561e6
Signed-off-by: Alex Blewitt <alex.blewitt@gmail.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
5 files changed, 131 insertions, 7 deletions
diff --git a/org.eclipse.egit.ui/plugin.properties b/org.eclipse.egit.ui/plugin.properties index d05a3b31aa..040c723a94 100644 --- a/org.eclipse.egit.ui/plugin.properties +++ b/org.eclipse.egit.ui/plugin.properties @@ -43,6 +43,9 @@ CompareWithRevisionAction_tooltip=Compare With a Git Revision CompareWithIndexAction_label=Git Index CompareWithIndexAction_tooltip=Compare with Git's index version +IgnoreAction_label=Add to .git&ignore... +IgnoreAction_tooltip=Ignore the selected resources + ShowResourceInHistoryAction_label=Show in Resource History ShowResourceInHistoryAction_tooltip=Show selected files in the resource history view. diff --git a/org.eclipse.egit.ui/plugin.xml b/org.eclipse.egit.ui/plugin.xml index d2936a54a3..8e2d8b4d5b 100644 --- a/org.eclipse.egit.ui/plugin.xml +++ b/org.eclipse.egit.ui/plugin.xml @@ -108,13 +108,18 @@ label="%CommitAction_label" menubarPath="team.main/group1" tooltip="%CommitAction_tooltip"/> - <action - class="org.eclipse.egit.ui.internal.actions.CompareWithIndexAction" - id="org.eclipse.egit.ui.internal.actions.CompareWithIndexAction" - label="%CompareWithIndexAction_label" - menubarPath="compareWithMenu/gitCompareWithGroup" - tooltip="%CompareWithIndexAction_tooltip"> - </action> + <action + class="org.eclipse.egit.ui.internal.actions.CompareWithIndexAction" + id="org.eclipse.egit.ui.internal.actions.CompareWithIndexAction" + label="%CompareWithIndexAction_label" + menubarPath="compareWithMenu/gitCompareWithGroup" + tooltip="%CompareWithIndexAction_tooltip"/> + <action + class="org.eclipse.egit.ui.internal.actions.IgnoreAction" + id="org.eclipse.egit.ui.internal.actions.IgnoreAction" + label="%IgnoreAction_label" + menubarPath="team.main/group1" + tooltip="%IgnoreAction_tooltip"/> </objectContribution> <objectContribution id="org.eclipse.egit.ui.resetto" diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java index 6cb986c9fd..8212cee87a 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java @@ -1082,6 +1082,15 @@ public class UIText extends NLS { /** */ public static String DecoratorPreferencesPage_iconsShowAssumeValid; + /** */ + public static String IgnoreAction_jobName; + + /** */ + public static String IgnoreAction_taskName; + + /** */ + public static String IgnoreAction_error; + static { initializeMessages("org.eclipse.egit.ui.uitext", UIText.class); //$NON-NLS-1$ } diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/IgnoreAction.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/IgnoreAction.java new file mode 100644 index 0000000000..1e2088ad6d --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/IgnoreAction.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (C) 2009, Alex Blewitt <alex.blewitt@gmail.com> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * See LICENSE for the full license text, also available. + *******************************************************************************/ +package org.eclipse.egit.ui.internal.actions; + +import java.io.ByteArrayInputStream; +import java.io.UnsupportedEncodingException; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.WorkspaceJob; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; +import org.eclipse.egit.ui.UIText; +import org.eclipse.jface.action.IAction; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.team.core.Team; + +/** Action for ignoring files via .gitignore. */ +public class IgnoreAction extends RepositoryAction { + @SuppressWarnings("restriction") + @Override + public void run(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(); + } + + @SuppressWarnings("restriction") + @Override + public boolean isEnabled() { + if (getProjectsInRepositoryOfSelectedResources().length == 0) + return false; + + IResource[] resources = getSelectedResources(); + for (IResource resource : resources) { + // NB This does the same thing in DecoratableResourceAdapter, but + // neither currently consult .gitignore + if (!Team.isIgnoredHint(resource)) + return true; + } + return false; + } +} diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties index 86023c81cb..4ad29aae17 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties @@ -395,3 +395,6 @@ DecoratorPreferencesPage_iconsShowAssumeValid=Assumed unchanged resources Decorator_exceptionMessage=Errors occurred while applying Git decorations to resources. +IgnoreAction_jobName=Ignore Git resources +IgnoreAction_taskName=Ignoring Git resources +IgnoreAction_error=Unable to ignore resources |
