chiark / gitweb /
stgit.el: Add the stgit-file-toggle-index command
authorDavid Kågedal <david@virtutech.com>
Thu, 30 Jul 2009 14:54:50 +0000 (16:54 +0200)
committerDavid Kågedal <david@virtutech.com>
Thu, 30 Jul 2009 15:12:08 +0000 (17:12 +0200)
Based on a patch from Gustav Hållberg.

Signed-off-by: David Kågedal <david@virtutech.com>
Signed-off-by: Gustav Hållberg <gustav@virtutech.com>
contrib/stgit.el

index 0a1aefb1b3fe42ccf0d6abaa50be6ce463a9c0af..b47dbae00e69990ba38e907879040b79b48cdef7 100644 (file)
@@ -594,6 +594,7 @@ (unless stgit-mode-map
           ("U" .        stgit-uncommit)
           ("\r" .       stgit-select)
           ("o" .        stgit-find-file-other-window)
+          ("i" .        stgit-file-toggle-index)
           (">" .        stgit-push-next)
           ("<" .        stgit-pop-next)
           ("P" .        stgit-push-or-pop)
@@ -855,6 +856,35 @@ (defun stgit-show ()
       (goto-char (point-min))
       (diff-mode))))
 
+(defun stgit-move-change-to-index (file status)
+  "Copies the workspace state of FILE to index, using git add or git rm"
+  (let ((op (if (file-exists-p file) "add" "rm")))
+    (stgit-capture-output "*git output*"
+      (stgit-run-git op "--" file))))
+
+(defun stgit-remove-change-from-index (file status)
+  "Unstages the change in FILE from the index"
+  (stgit-capture-output "*git output*"
+    (stgit-run-git "reset" "-q" "--" file)))
+
+(defun stgit-file-toggle-index ()
+  "Move modified file in or out of the index."
+  (interactive)
+  (let ((patched-file (stgit-patched-file-at-point)))
+    (unless patched-file
+      (error "No file on the current line"))
+    (let ((patch-name (stgit-patch-name-at-point)))
+      (cond ((eq patch-name :work)
+             (stgit-move-change-to-index (stgit-file-file patched-file)
+                                         (stgit-file-status patched-file)))
+            ((eq patch-name :index)
+             (stgit-remove-change-from-index (stgit-file-file patched-file)
+                                             (stgit-file-status patched-file)))
+            (t
+             (error "Can only move files in the working tree to index")))))
+  ;; FIXME: invalidate ewoc
+  (stgit-reload))
+
 (defun stgit-edit ()
   "Edit the patch on the current line."
   (interactive)