stgit.el: Add "U" for stgit-revert-file
authorGustav Hållberg <gustav@virtutech.com>
Thu, 30 Jul 2009 22:57:57 +0000 (00:57 +0200)
committerGustav Hållberg <gustav@virtutech.com>
Thu, 6 Aug 2009 20:49:07 +0000 (22:49 +0200)
Signed-off-by: Gustav Hållberg <gustav@virtutech.com>
contrib/stgit.el

index 2a72189..c475fc2 100644 (file)
@@ -616,6 +616,7 @@ at point."
             ("R" .        stgit-repair)
             ("\C-c\C-c" . stgit-commit)
             ("\C-c\C-u" . stgit-uncommit)
+            ("U" .        stgit-revert-file)
             ("\r" .       stgit-select)
             ("o" .        stgit-find-file-other-window)
             ("i" .        stgit-file-toggle-index)
@@ -792,6 +793,37 @@ Interactively, the prefix argument is used as COUNT."
   (stgit-capture-output nil (stgit-run "commit" "-n" count))
   (stgit-reload))
 
+(defun stgit-revert-file ()
+  "Revert the file at point, which must be in the index or the
+working tree."
+  (interactive)
+  (let* ((patched-file (or (stgit-patched-file-at-point)
+                           (error "No file on the current line")))
+         (patch-name   (stgit-patch-name-at-point))
+         (file-status  (stgit-file-status patched-file))
+         (rm-file      (cond ((stgit-file-copy-or-rename patched-file)
+                              (stgit-file-cr-to patched-file))
+                             ((eq file-status 'add)
+                              (stgit-file-file patched-file))))
+         (co-file      (cond ((eq file-status 'rename)
+                              (stgit-file-cr-from patched-file))
+                             ((not (memq file-status '(copy add)))
+                              (stgit-file-file patched-file)))))
+
+    (unless (memq patch-name '(:work :index))
+      (error "No index or working tree file on this line"))
+
+    (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
+      (when (yes-or-no-p (format "Revert %d file%s? "
+                                 nfiles
+                                 (if (= nfiles 1) "" "s")))
+        (stgit-capture-output nil
+          (when rm-file
+            (stgit-run-git "rm" "-f" "-q" "--" rm-file))
+          (when co-file
+            (stgit-run-git "checkout" "HEAD" co-file)))
+        (stgit-reload)))))
+
 (defun stgit-uncommit (count)
   "Run stg uncommit on COUNT commits.
 Interactively, the prefix argument is used as COUNT."