diff options
| author | Jens Baumgart | 2010-03-11 10:02:48 +0000 |
|---|---|---|
| committer | Stefan Lay | 2010-03-11 15:21:58 +0000 |
| commit | 374af9a268a289429ae1b0c64e14116cff5d3916 (patch) | |
| tree | 76bea407695884d9f41c02f901dc7dd19d646be3 | |
| parent | 86df94e89fcf3d308d04c348f840be116c55ead0 (diff) | |
| download | egit-374af9a268a289429ae1b0c64e14116cff5d3916.tar.gz egit-374af9a268a289429ae1b0c64e14116cff5d3916.tar.xz egit-374af9a268a289429ae1b0c64e14116cff5d3916.zip | |
Implement Team->Add to Index action
An initial implementation for the Team->Add to Index action is
provided. GitIndex is used in the implementation (similar to Add
To Index in CommitDialog).
The action does not modify the index if the index contains staged
content. Later GitIndex should be replaced by using DirCache.
Change-Id: Ibc36704257caa3109098bf523ac1ff5f37d8e096
Signed-off-by: Jens Baumgart <jens.baumgart@sap.com>
8 files changed, 230 insertions, 0 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 6e82b9b573..47b11b8856 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 @@ -100,6 +100,9 @@ public class CoreText extends NLS { /** */ public static String PushOperation_taskNameNormalRun; + /** */ + public static String AddToIndexOperation_failed; + static { initializeMessages("org.eclipse.egit.core.coretext", 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 9b7ed82bad..7c3981bef1 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 @@ -49,3 +49,5 @@ PushOperation_resultTransportError=Transport error occured during push operation PushOperation_resultNoServiceError=Push service is not available: {0} PushOperation_taskNameDryRun=Trying pushing to remote repositories PushOperation_taskNameNormalRun=Pushing to remote repositories + +AddToIndexOperation_failed=Failed to add resource to index diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/AddToIndexOperation.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/AddToIndexOperation.java new file mode 100644 index 0000000000..d7b422e351 --- /dev/null +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/AddToIndexOperation.java @@ -0,0 +1,147 @@ +/******************************************************************************* + * 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.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.IdentityHashMap; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspaceRunnable; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.egit.core.Activator; +import org.eclipse.egit.core.CoreText; +import org.eclipse.egit.core.project.RepositoryMapping; +import org.eclipse.jgit.lib.GitIndex; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.GitIndex.Entry; + +/** + */ +public class AddToIndexOperation implements IWorkspaceRunnable { + private final Collection rsrcList; + private final Collection<IFile> notAddedFiles; + + private final IdentityHashMap<RepositoryMapping, Object> mappings; + + /** + * Create a new operation to add files to the Git index + * + * @param rsrcs + * collection of {@link IResource}s which should be added to the + * relevant Git repositories. + */ + public AddToIndexOperation(final Collection rsrcs) { + rsrcList = rsrcs; + mappings = new IdentityHashMap<RepositoryMapping, Object>(); + notAddedFiles = new ArrayList<IFile>(); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime + * .IProgressMonitor) + */ + public void run(IProgressMonitor m) throws CoreException { + IProgressMonitor monitor; + if (m == null) + monitor = new NullProgressMonitor(); + else + monitor = m; + Collection<GitIndex> changedIndexes = new ArrayList<GitIndex>(); + // GitIndex can not be updated if it contains staged entries + Collection<GitIndex> indexesWithStagedEntries = new ArrayList<GitIndex>(); + try { + for (Object obj : rsrcList) { + obj = ((IAdaptable) obj).getAdapter(IResource.class); + if (obj instanceof IFile) + addToIndex((IFile) obj, changedIndexes, + indexesWithStagedEntries); + monitor.worked(200); + } + if (!changedIndexes.isEmpty()) { + for (GitIndex idx : changedIndexes) { + idx.write(); + } + + } + } catch (RuntimeException e) { + throw Activator.error(CoreText.AddToIndexOperation_failed, e); + } catch (IOException e) { + throw Activator.error(CoreText.AddToIndexOperation_failed, e); + } finally { + for (final RepositoryMapping rm : mappings.keySet()) + rm.fireRepositoryChanged(); + mappings.clear(); + monitor.done(); + } + } + + /** + * @return returns the files that could not be added to the index + * because there are unmerged entries + */ + public Collection<IFile> getNotAddedFiles() { + return notAddedFiles; + } + + private void addToIndex(IFile file, + Collection<GitIndex> changedIndexes, + Collection<GitIndex> indexesWithUnmergedEntries) throws IOException { + IProject project = file.getProject(); + RepositoryMapping map = RepositoryMapping.getMapping(project); + Repository repo = map.getRepository(); + GitIndex index = null; + index = repo.getIndex(); + Entry entry = index.getEntry(map.getRepoRelativePath(file)); + if (entry == null) + return; + if (indexesWithUnmergedEntries.contains(index)) { + notAddedFiles.add(file); + return; + } else { + if (!canUpdateIndex(index)) { + indexesWithUnmergedEntries.add(index); + notAddedFiles.add(file); + return; + } + } + if (entry.isModified(map.getWorkDir())) { + entry.update(new File(map.getWorkDir(), entry.getName())); + if (!changedIndexes.contains(index)) + changedIndexes.add(index); + } + } + + /** + * The method checks if the given index can be updated. The index can be + * updated if it does not contain entries with stage !=0. + * + * @param index + * @return true if the given index can be updated + */ + private static boolean canUpdateIndex(GitIndex index) { + Entry[] members = index.getMembers(); + for (int i = 0; i < members.length; i++) { + if (members[i].getStage() != 0) + return false; + } + return true; + } + +} diff --git a/org.eclipse.egit.ui/plugin.properties b/org.eclipse.egit.ui/plugin.properties index e1d9331e24..f456561ed1 100644 --- a/org.eclipse.egit.ui/plugin.properties +++ b/org.eclipse.egit.ui/plugin.properties @@ -46,6 +46,9 @@ CompareWithIndexAction_tooltip=Compare with Git's index version IgnoreAction_label=Add to .git&ignore IgnoreAction_tooltip=Ignore the selected resources +AddToIndexAction_label=Add to Index +AddToIndexAction_tooltip=Add files to the Git Index + 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 2c1cbbe224..50a3185466 100644 --- a/org.eclipse.egit.ui/plugin.xml +++ b/org.eclipse.egit.ui/plugin.xml @@ -120,6 +120,12 @@ label="%IgnoreAction_label" menubarPath="team.main/group1" tooltip="%IgnoreAction_tooltip"/> + <action + class="org.eclipse.egit.ui.internal.actions.AddToIndexAction" + id="org.eclipse.egit.ui.internal.actions.AddToIndexAction" + label="%AddToIndexAction_label" + menubarPath="team.main/group1" + tooltip="%AddToIndexAction_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 0073e09b9d..6d1546461b 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 @@ -22,6 +22,12 @@ public class UIText extends NLS { public static String WizardProjectsImportPage_filterText; /** */ + public static String AddToIndexAction_addingFilesFailed; + + /** */ + public static String AddToIndexAction_indexesWithUnmergedEntries; + + /** */ public static String WizardProjectsImportPage_ImportProjectsTitle; /** */ diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/AddToIndexAction.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/AddToIndexAction.java new file mode 100644 index 0000000000..7aa768d755 --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/AddToIndexAction.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * 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.ui.internal.actions; + +import java.util.Collection; +import java.util.List; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IWorkspaceRunnable; +import org.eclipse.egit.core.op.AddToIndexOperation; +import org.eclipse.egit.ui.UIText; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.dialogs.MessageDialog; + +/** + * An action to add files to a Git index. + * + * @see AddToIndexOperation + */ +public class AddToIndexAction extends AbstractOperationAction { + private AddToIndexOperation operation = null; + + protected IWorkspaceRunnable createOperation(final IAction act, + final List sel) { + if (sel.isEmpty()) { + return null; + } else { + operation = new AddToIndexOperation(sel); + return operation; + } + } + + @Override + protected void postOperation() { + Collection<IFile> notAddedFiles = operation.getNotAddedFiles(); + if (notAddedFiles.size()==0) + return; + String title = UIText.AddToIndexAction_addingFilesFailed; + String message = UIText.AddToIndexAction_indexesWithUnmergedEntries; + message += "\n\n"; //$NON-NLS-1$ + message += getFileList(notAddedFiles); + MessageDialog.openWarning(wp.getSite().getShell(), title, message); + } + + private static String getFileList(Collection<IFile> notAddedFiles) { + String result = ""; //$NON-NLS-1$ + for (IFile file : notAddedFiles) { + result += file.getName(); + result += "\n"; //$NON-NLS-1$ + } + return result; + } + + +} 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 191dccd3c3..01e95f29f0 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 @@ -7,6 +7,8 @@ # ############################################################################### +AddToIndexAction_indexesWithUnmergedEntries=The indexes of the following files contain unmerged entries: +AddToIndexAction_addingFilesFailed=Adding Files to the Git Index failed WizardProjectsImportPage_projectLabel={0} ({1}) WizardProjectsImportPage_ImportProjectsTitle=Import Projects WizardProjectsImportPage_ImportProjectsDescription=Import projects from cloned repository into workbench |
