diff options
| author | Roland Grunberg | 2010-04-07 08:16:57 +0000 |
|---|---|---|
| committer | Stefan Lay | 2010-04-07 08:16:57 +0000 |
| commit | eeed2a866c079d2c2b56e991b31eba49a22d9f52 (patch) | |
| tree | 5815a29e200efa9cac81bb863daf86529fcf82ce | |
| parent | b56662e4d430752f87b7c27eacb40fcd3c7a7599 (diff) | |
| download | egit-eeed2a866c079d2c2b56e991b31eba49a22d9f52.tar.gz egit-eeed2a866c079d2c2b56e991b31eba49a22d9f52.tar.xz egit-eeed2a866c079d2c2b56e991b31eba49a22d9f52.zip | |
Add option to replace selected files with version in the git index.
This is the equivalent of 'git checkout' on a list of files from the
command-line.
Change-Id: I8958ddcfc1d2a09f52eff7534db6942195eaf5ef
5 files changed, 181 insertions, 1 deletions
diff --git a/org.eclipse.egit.ui/plugin.properties b/org.eclipse.egit.ui/plugin.properties index 88d29fb5d8..0b1c9152b7 100644 --- a/org.eclipse.egit.ui/plugin.properties +++ b/org.eclipse.egit.ui/plugin.properties @@ -31,6 +31,9 @@ TrackAction_tooltip=Track selected files/folders with Git. UntrackAction_label=&Remove from Version Control UntrackAction_tooltip=Stop tracking selected files/folders with Git. +DiscardChangesAction_label=&File in Git Index +DiscardChangesAction_tooltip=Replace selected files with Git Index. + UpdateAction_label=Update Index (Refresh) UpdateAction_tooltip=Update Git's index with current version diff --git a/org.eclipse.egit.ui/plugin.xml b/org.eclipse.egit.ui/plugin.xml index 936488d09b..a2e3ad7b75 100644 --- a/org.eclipse.egit.ui/plugin.xml +++ b/org.eclipse.egit.ui/plugin.xml @@ -134,6 +134,12 @@ label="%AddToIndexAction_label" menubarPath="team.main/group1" tooltip="%AddToIndexAction_tooltip"/> + <action + class="org.eclipse.egit.ui.internal.actions.DiscardChangesAction" + id="org.eclipse.egit.ui.internal.actions.DiscardChangesAction" + label="%DiscardChangesAction_label" + menubarPath="replaceWithMenu/gitReplaceWithGroup" + tooltip="%DiscardChangesAction_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 f126cd7138..6b54e47c48 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 @@ -1329,6 +1329,27 @@ public class UIText extends NLS { /** */ public static String RepositoriesView_WorkingDir_treenode; + /** */ + public static String DiscardChangesAction_confirmActionTitle; + + /** */ + public static String DiscardChangesAction_confirmActionMessage; + + /** */ + public static String DiscardChangesAction_unexpectedErrorTitle; + + /** */ + public static String DiscardChangesAction_unexpectedErrorMessage; + + /** */ + public static String DiscardChangesAction_unexpectedIndexErrorMessage; + + /** */ + public static String DiscardChangesAction_refreshErrorTitle; + + /** */ + public static String DiscardChangesAction_refreshErrorMessage; + 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/DiscardChangesAction.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/DiscardChangesAction.java new file mode 100644 index 0000000000..8eced5f062 --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/DiscardChangesAction.java @@ -0,0 +1,142 @@ +/******************************************************************************* + * Copyright (C) 2010, Roland Grunberg <rgrunber@redhat.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.io.IOException; +import java.util.ArrayList; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.egit.core.project.RepositoryMapping; +import org.eclipse.egit.ui.UIText; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.RepositoryState; +import org.eclipse.jgit.lib.GitIndex.Entry; + +/** + * Checkout all selected dirty files. + */ +public class DiscardChangesAction extends RepositoryAction{ + + @Override + public void run(IAction action) { + + boolean performAction = MessageDialog.openConfirm(getShell(), + UIText.DiscardChangesAction_confirmActionTitle, + UIText.DiscardChangesAction_confirmActionMessage); + if (performAction) { + performDiscardChanges(); + } + } + + private void performDiscardChanges() { + ArrayList<IResource> allFiles = new ArrayList<IResource>(); + + // find all files + for (IResource res : getSelectedResources()) { + allFiles.addAll(getAllMembers(res)); + } + + for (IResource res : allFiles) { + try { + discardChange(res); + + } catch (IOException e1) { + MessageDialog.openError(getShell(), + UIText.DiscardChangesAction_unexpectedErrorTitle, + UIText.DiscardChangesAction_unexpectedErrorMessage); + }catch (RuntimeException e2) { + MessageDialog.openError(getShell(), + UIText.DiscardChangesAction_unexpectedErrorTitle, + UIText.DiscardChangesAction_unexpectedIndexErrorMessage); + } + } + + } + + private void discardChange(IResource res) throws IOException { + IProject[] proj = new IProject[] { res.getProject() }; + Repository repository = getRepositoriesFor(proj)[0]; + + String resRelPath = RepositoryMapping.getMapping(res).getRepoRelativePath(res); + Entry e = repository.getIndex().getEntry(resRelPath); + + // resource must exist in the index and be dirty + if (e != null && e.getStage() == 0 && e.isModified(repository.getWorkDir())) { + repository.getIndex().checkoutEntry(repository.getWorkDir(), e); + + try { + res.refreshLocal(0, new NullProgressMonitor()); + } catch (CoreException e1) { + MessageDialog.openError(getShell(), + UIText.DiscardChangesAction_refreshErrorTitle, + UIText.DiscardChangesAction_refreshErrorMessage); + } + + repository.getIndex().write(); + } + } + + @Override + public boolean isEnabled() { + for (IResource res : getSelectedResources()) { + IProject[] proj = new IProject[] { res.getProject() }; + Repository repository = getRepositoriesFor(proj)[0]; + if (! repository.getRepositoryState().equals(RepositoryState.SAFE)){ + return false; + } + } + return true; + } + + /** + * @param res an IResource + * @return An ArrayList with all members of this IResource + * of arbitrary depth. This will return just the argument + * res if it is a file. + */ + private ArrayList<IResource> getAllMembers(IResource res) { + ArrayList<IResource> ret = new ArrayList<IResource>(); + if (res.getLocation().toFile().isFile()) { + ret.add(res); + } else { + getAllMembersHelper(res, ret); + } + return ret; + } + + + private void getAllMembersHelper(IResource res, ArrayList<IResource> ret) { + ArrayList<IResource> tmp = new ArrayList<IResource> (); + if (res instanceof IContainer) { + IContainer cont = (IContainer) res; + try { + for (IResource r : cont.members()) { + if (r.getLocation().toFile().isFile()) { + tmp.add(r); + } else { + getAllMembersHelper(r, tmp); + } + } + } catch (CoreException e) { + // thrown by members() + // ignore children in case parent resource no longer accessible + return; + } + + ret.addAll(tmp); + } + } + +} 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 fbe03d6f22..958884986a 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 @@ -478,4 +478,12 @@ RepositoriesView_Refresh_Button=Refresh RepositoriesView_RemotesNodeText=Remotes RepositoriesView_Remove_MenuItem=Remove RepositoriesView_RemoveRemoteMenu=Remove remote... -RepositoriesView_WorkingDir_treenode=Working directory
\ No newline at end of file +RepositoriesView_WorkingDir_treenode=Working directory + +DiscardChangesAction_confirmActionTitle=Discard Local Changes +DiscardChangesAction_confirmActionMessage=This will discard all local changes for the selected resources. Are you sure you want to do this ? +DiscardChangesAction_unexpectedErrorTitle=Unexpected Error +DiscardChangesAction_unexpectedErrorMessage=An unexpected error occured while attempting to checkout resources. +DiscardChangesAction_unexpectedIndexErrorMessage=An unexpected error occured while attempting to interact with the Git Index. +DiscardChangesAction_refreshErrorTitle=Error During Refresh +DiscardChangesAction_refreshErrorMessage=Some of the selected resources could not be refreshed.
\ No newline at end of file |
