diff options
| author | Bernard Leach | 2011-05-18 23:14:49 +0000 |
|---|---|---|
| committer | Bernard Leach | 2011-05-18 23:14:49 +0000 |
| commit | c8719051d5e2d85bd63dac8a6251432bf410fc3c (patch) | |
| tree | 4d165286f59fba14256ff6a1a716aa9c9c14506b | |
| parent | 0bd85b578c42020b31ad0a4333e3309786b092bc (diff) | |
| download | egit-c8719051d5e2d85bd63dac8a6251432bf410fc3c.tar.gz egit-c8719051d5e2d85bd63dac8a6251432bf410fc3c.tar.xz egit-c8719051d5e2d85bd63dac8a6251432bf410fc3c.zip | |
[RFC] StagingView: fix stage and unstage actions
Also includes I91ef02c9 fix
Still need to fix up error handling
Need to optimise away needless IndexDiff usage
Can refactor to use jgit change Id7cbce47 to perform unstage operation
Change-Id: I4377d460e19900c0686463e9038f9724d7d7783d
| -rw-r--r-- | org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java | 140 |
1 files changed, 137 insertions, 3 deletions
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java index 07b3016d1c..290213a9b5 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java @@ -13,6 +13,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -42,6 +43,13 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.OpenEvent; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jgit.api.AddCommand; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.RmCommand; +import org.eclipse.jgit.api.errors.NoFilepatternException; +import org.eclipse.jgit.dircache.DirCache; +import org.eclipse.jgit.dircache.DirCacheEditor; +import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.events.IndexChangedEvent; import org.eclipse.jgit.events.IndexChangedListener; import org.eclipse.jgit.events.ListenerHandle; @@ -51,10 +59,13 @@ import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.IndexDiff; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.PersonIdent; +import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryState; import org.eclipse.jgit.lib.UserConfig; +import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.WorkingTreeIterator; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.SashForm; @@ -147,7 +158,7 @@ public class StagingView extends ViewPart { new Transfer[] { LocalSelectionTransfer.getTransfer() }, new DragSourceAdapter() { public void dragStart(DragSourceEvent event) { - IStructuredSelection selection = (IStructuredSelection) stagedTableViewer + IStructuredSelection selection = (IStructuredSelection) unstagedTableViewer .getSelection(); event.doit = !selection.isEmpty(); } @@ -230,7 +241,7 @@ public class StagingView extends ViewPart { new Transfer[] { LocalSelectionTransfer.getTransfer() }, new DragSourceAdapter() { public void dragStart(DragSourceEvent event) { - IStructuredSelection selection = (IStructuredSelection) unstagedTableViewer + IStructuredSelection selection = (IStructuredSelection) stagedTableViewer .getSelection(); event.doit = !selection.isEmpty(); } @@ -348,10 +359,133 @@ public class StagingView extends ViewPart { } private void stage(IStructuredSelection selection) { - runCommand(ActionCommands.ADD_TO_INDEX, selection); + Git git = new Git(currentRepository); + AddCommand add = null; + RmCommand rm = null; + Iterator iterator = selection.iterator(); + while (iterator.hasNext()) { + StagingEntry entry = (StagingEntry) iterator.next(); + switch(entry.getState()) { + case ADDED: + case CHANGED: + case REMOVED: + // already staged + break; + case CONFLICTING: + case MODIFIED: + case PARTIALLY_MODIFIED: + case UNTRACKED: + if (add == null) + add = git.add(); + add.addFilepattern(entry.getPath()); + break; + case MISSING: + if (rm == null) + rm = git.rm(); + rm.addFilepattern(entry.getPath()); + break; + } + } + + if (add != null) + try { + add.call(); + } catch (NoFilepatternException e1) { + // cannot happen + } + if (rm != null) + try { + rm.call(); + } catch (NoFilepatternException e) { + // cannot happen + } + + reload(currentRepository); } private void unstage(IStructuredSelection selection) { + if (selection.isEmpty()) + return; + + final RevCommit headRev; + try { + final Ref head = currentRepository.getRef(Constants.HEAD); + headRev = new RevWalk(currentRepository).parseCommit(head.getObjectId()); + } catch (IOException e1) { + // TODO fix text + MessageDialog.openError(getSite().getShell(), + UIText.CommitAction_MergeHeadErrorTitle, + UIText.CommitAction_ErrorReadingMergeMsg); + return; + } + + final DirCache dirCache; + final DirCacheEditor edit; + try { + dirCache = currentRepository.lockDirCache(); + edit = dirCache.editor(); + } catch (IOException e) { + // TODO fix text + MessageDialog.openError(getSite().getShell(), + UIText.CommitAction_MergeHeadErrorTitle, + UIText.CommitAction_ErrorReadingMergeMsg); + return; + } + + try { + updateDirCache(selection, headRev, edit); + + try { + edit.commit(); + } catch (IOException e) { + // TODO fix text + MessageDialog.openError(getSite().getShell(), + UIText.CommitAction_MergeHeadErrorTitle, + UIText.CommitAction_ErrorReadingMergeMsg); + } + } finally { + dirCache.unlock(); + } + + reload(currentRepository); + } + + private void updateDirCache(IStructuredSelection selection, + final RevCommit headRev, final DirCacheEditor edit) { + Iterator iterator = selection.iterator(); + while (iterator.hasNext()) { + StagingEntry entry = (StagingEntry) iterator.next(); + switch(entry.getState()) { + case ADDED: + edit.add(new DirCacheEditor.DeletePath(entry.getPath())); + break; + case CHANGED: + case REMOVED: + // set the index object id/file mode back to our head revision + try { + final TreeWalk tw = TreeWalk.forPath(currentRepository, entry.getPath(), headRev.getTree()); + if (tw != null) { + edit.add(new DirCacheEditor.PathEdit(entry.getPath()) { + @Override + public void apply(DirCacheEntry ent) { + ent.setFileMode(tw.getFileMode(0)); + ent.setObjectId(tw.getObjectId(0)); + // for index & working tree compare + ent.setLastModified(0); + } + }); + } + } catch (IOException e) { + // TODO fix text + MessageDialog.openError(getSite().getShell(), + UIText.CommitAction_MergeHeadErrorTitle, + UIText.CommitAction_ErrorReadingMergeMsg); + } + break; + default: + // unstaged + } + } } private static boolean runCommand(String commandId, |
