diff options
author | Jens Baumgart | 2010-08-09 12:16:08 +0000 |
---|---|---|
committer | Chris Aniszczyk | 2010-08-09 15:10:25 +0000 |
commit | c4c4e797dca3ddefdc562ab471d7142b0b79bcfb (patch) | |
tree | c7b60a52c48ae13f73b737717960b1695998dedd | |
parent | 3a487d70629183699891aaa84da6a22c378ef65a (diff) | |
download | egit-c4c4e797dca3ddefdc562ab471d7142b0b79bcfb.tar.gz egit-c4c4e797dca3ddefdc562ab471d7142b0b79bcfb.tar.xz egit-c4c4e797dca3ddefdc562ab471d7142b0b79bcfb.zip |
Support Team->Ignore on projects
Fix not working Team->ignore on project node.
Move logic in IgnoreActionHandler to new class IgnoreOperation.
Bug: 309589
Change-Id: Iafbd4e5b3575960fbb02357a896cde2893731366
Signed-off-by: Jens Baumgart <jens.baumgart@sap.com>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
6 files changed, 230 insertions, 64 deletions
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java index 3a45085014..52b4b1c3ac 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java @@ -286,6 +286,21 @@ public class CoreText extends NLS { /** */ public static String OperationNotYetExecuted; + /** */ + public static String IgnoreOperation_error; + + /** */ + public static String IgnoreOperation_parentOutsideRepo; + + /** */ + public static String IgnoreOperation_creatingFailed; + + /** */ + public static String IgnoreOperation_taskName; + + /** */ + public static String IgnoreOperation_updatingFailed; + static { initializeMessages("org.eclipse.egit.core.coretext", //$NON-NLS-1$ CoreText.class); diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties b/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties index a00fbaa470..8d0552b3a2 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties @@ -119,3 +119,9 @@ GitBranchResourceVariantTreeSubscriber_gitRepository=Git without local changes OperationAlreadyExecuted=Operation has already been executed and can not be executed again OperationNotYetExecuted=Operation has not yet been executed and can not return a result RemoteRefUpdateCantBeReused=The RemoteRefUpdate instance can not be re-used + +IgnoreOperation_error=Unable to ignore resources +IgnoreOperation_parentOutsideRepo=Can not add to gitignore. The parent of {0} is outside the Git repsository {1} +IgnoreOperation_creatingFailed=creating {0} failed +IgnoreOperation_taskName=Adding resources to gitignore +IgnoreOperation_updatingFailed=updating {0} failed diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java new file mode 100644 index 0000000000..85b065fefd --- /dev/null +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java @@ -0,0 +1,190 @@ +/******************************************************************************* + * Copyright (C) 2009, Alex Blewitt <alex.blewitt@gmail.com> + * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.egit.core.op; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceRuleFactory; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.jobs.ISchedulingRule; +import org.eclipse.core.runtime.jobs.MultiRule; +import org.eclipse.egit.core.Activator; +import org.eclipse.egit.core.CoreText; +import org.eclipse.egit.core.project.RepositoryMapping; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.osgi.util.NLS; +import org.eclipse.team.core.Team; + +/** + * IgnoreOperation adds resources to a .gitignore file + * + */ +public class IgnoreOperation implements IEGitOperation { + + private IResource[] resources; + + private boolean gitignoreOutsideWSChanged; + + private ISchedulingRule schedulingRule; + + /** + * construct an IgnoreOperation + * + * @param resources + */ + public IgnoreOperation(IResource[] resources) { + this.resources = resources; + gitignoreOutsideWSChanged = false; + calcSchedulingRule(); + } + + public void execute(IProgressMonitor monitor) throws CoreException { + monitor.beginTask(CoreText.IgnoreOperation_taskName, resources.length); + try { + for (IResource resource : resources) { + if (monitor.isCanceled()) + break; + // 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(Activator.error( + CoreText.IgnoreOperation_error, e)); + } + } + + /** + * @return true if a gitignore file outside the workspace was changed. In + * this case the caller may need to perform manual UI refreshes + * because there was no ResourceChanged event. + */ + public boolean isGitignoreOutsideWSChanged() { + return gitignoreOutsideWSChanged; + } + + public ISchedulingRule getSchedulingRule() { + return schedulingRule; + } + + 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); + } + 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); + } + } + + private void updateGitIgnore(File gitIgnore, String entry) + throws CoreException { + try { + if (!gitIgnore.exists()) + if (!gitIgnore.createNewFile()) { + String error = NLS.bind( + CoreText.IgnoreOperation_creatingFailed, + gitIgnore.getAbsolutePath()); + throw new CoreException(Activator.error(error, null)); + } + + FileOutputStream os = new FileOutputStream(gitIgnore, true); + try { + os.write(entry.getBytes()); + } finally { + os.close(); + } + } catch (IOException e) { + String error = NLS.bind(CoreText.IgnoreOperation_updatingFailed, + gitIgnore.getAbsolutePath()); + throw new CoreException(Activator.error(error, e)); + } + } + + private ByteArrayInputStream asStream(String entry) + throws UnsupportedEncodingException { + return new ByteArrayInputStream( + entry.getBytes(Constants.CHARACTER_ENCODING)); + } + + private ISchedulingRule calcSchedulingRule() { + List<ISchedulingRule> rules = new ArrayList<ISchedulingRule>(); + IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace() + .getRuleFactory(); + for (IResource resource : resources) { + IContainer container = resource.getParent(); + if (!(container instanceof IWorkspaceRoot)) { + ISchedulingRule rule = ruleFactory.modifyRule(container); + if (rule != null) + rules.add(rule); + } + } + if (rules.size() == 0) + return null; + else + return new MultiRule(rules.toArray(new IResource[rules.size()])); + } +} 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 41cb34cd7f..73929443f4 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 @@ -1630,13 +1630,7 @@ 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; + public static String IgnoreActionHandler_addToGitignore; /** */ public static String RepositoriesView_ActionCanceled_Message; diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/IgnoreActionHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/IgnoreActionHandler.java index ee10820291..b549300605 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/IgnoreActionHandler.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/IgnoreActionHandler.java @@ -1,5 +1,6 @@ /******************************************************************************* * Copyright (C) 2009, Alex Blewitt <alex.blewitt@gmail.com> + * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com> * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -7,23 +8,18 @@ *******************************************************************************/ package org.eclipse.egit.ui.internal.actions; -import java.io.ByteArrayInputStream; -import java.io.UnsupportedEncodingException; - import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; -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.core.runtime.jobs.Job; +import org.eclipse.egit.core.op.IgnoreOperation; import org.eclipse.egit.ui.Activator; import org.eclipse.egit.ui.UIText; -import org.eclipse.jgit.lib.Constants; +import org.eclipse.egit.ui.internal.decorators.GitLightweightDecorator; import org.eclipse.team.core.Team; /** Action for ignoring files via .gitignore. */ @@ -33,58 +29,24 @@ public class IgnoreActionHandler extends RepositoryActionHandler { final IResource[] resources = getSelectedResources(event); if (resources.length == 0) return null; - - WorkspaceJob job = new WorkspaceJob(UIText.IgnoreAction_jobName) { - public IStatus runInWorkspace(IProgressMonitor monitor) - throws CoreException { - monitor.beginTask(UIText.IgnoreAction_taskName, - resources.length); + final IgnoreOperation operation = new IgnoreOperation(resources); + String jobname = UIText.IgnoreActionHandler_addToGitignore; + Job job = new Job(jobname) { + @Override + protected IStatus run(IProgressMonitor monitor) { 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(); + operation.execute(monitor); } 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 Activator.createErrorStatus(e.getStatus() + .getMessage(), e); } + if (operation.isGitignoreOutsideWSChanged()) + GitLightweightDecorator.refresh(); 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.setUser(true); + job.setRule(operation.getSchedulingRule()); job.schedule(); return null; } 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 738bd2eab9..800e8ebaef 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 @@ -595,9 +595,8 @@ 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 +IgnoreActionHandler_addToGitignore=Add to gitignore + RepositoriesView_ActionCanceled_Message=Action was canceled RepositoriesView_Add_Button=Add... RepositoriesView_AddRepository_MenuItem=&Add Git Repository... |