Check for unsaved changes before Commit
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / IgnoreAction.java
blob1383c2ebe54cda3f204c9893cc2aff71505c53dc
1 /*******************************************************************************
2 * Copyright (C) 2009, Alex Blewitt <alex.blewitt@gmail.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * See LICENSE for the full license text, also available.
7 *******************************************************************************/
8 package org.eclipse.egit.ui.internal.actions;
10 import java.io.ByteArrayInputStream;
11 import java.io.UnsupportedEncodingException;
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.WorkspaceJob;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.egit.ui.UIText;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jgit.lib.Constants;
25 import org.eclipse.team.core.Team;
27 /** Action for ignoring files via .gitignore. */
28 public class IgnoreAction extends RepositoryAction {
29 @SuppressWarnings("restriction")
30 @Override
31 public void execute(IAction action) {
32 final IResource[] resources = getSelectedResources();
33 if (resources.length == 0)
34 return;
36 WorkspaceJob job = new WorkspaceJob(UIText.IgnoreAction_jobName) {
37 public IStatus runInWorkspace(IProgressMonitor monitor)
38 throws CoreException {
39 monitor.beginTask(UIText.IgnoreAction_taskName, resources.length);
40 try {
41 for (IResource resource : resources) {
42 // TODO This is pretty inefficient; multiple ignores in
43 // the same directory cause multiple writes.
45 // NB This does the same thing in
46 // DecoratableResourceAdapter, but neither currently
47 // consult .gitignore
49 if (!Team.isIgnoredHint(resource)) {
50 addIgnore(monitor, resource);
52 monitor.worked(1);
54 monitor.done();
55 } catch (CoreException e) {
56 throw e;
57 } catch (Exception e) {
58 throw new CoreException(
59 new Status(
60 IStatus.ERROR,
61 "org.eclipse.egit.ui", UIText.IgnoreAction_error, e)); //$NON-NLS-1$
63 return Status.OK_STATUS;
66 private void addIgnore(IProgressMonitor monitor, IResource resource)
67 throws UnsupportedEncodingException, CoreException {
68 IContainer container = resource.getParent();
69 IFile gitignore = container.getFile(new Path(
70 Constants.GITIGNORE_FILENAME));
71 String entry = "/" + resource.getName() + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
72 ByteArrayInputStream entryBytes = asStream(entry);
74 if (gitignore.exists())
75 gitignore.appendContents(entryBytes, true, true, monitor);
76 else
77 gitignore.create(entryBytes, true, monitor);
80 private ByteArrayInputStream asStream(String entry)
81 throws UnsupportedEncodingException {
82 return new ByteArrayInputStream(entry
83 .getBytes(Constants.CHARACTER_ENCODING));
86 job.schedule();
89 @SuppressWarnings("restriction")
90 @Override
91 public boolean isEnabled() {
92 if (getProjectsInRepositoryOfSelectedResources().length == 0)
93 return false;
95 IResource[] resources = getSelectedResources();
96 for (IResource resource : resources) {
97 // NB This does the same thing in DecoratableResourceAdapter, but
98 // neither currently consult .gitignore
99 if (!Team.isIgnoredHint(resource))
100 return true;
102 return false;