On 2011-02-25 04:04 +0800, Phil Jackson wrote:
> Hey,
>
> On 24/02/11 19:52, Leo wrote:
>
>> Turns out I never use 'v' on uncommitted diff/hunk so that message is
>> misleading. Maybe this can be refined or make 'v' a no-op on uncommitted
>> stuff? WDYT?
>
> Personally I would be up for getting rid of the revert functionality
> on hunks/diffs altogether (just leaving 'k'). People are bound to use
> it though so I'd say change the message to be more context aware.
>
> Cheers,
> Phil

It looks to me the only case that cannot be undone is reverting unstaged
diff/hunk (in that sense it is discarding). Anybody sees any problem
with the following patch? Thanks. Leo

================

commit 21ebdeb6bd9f7eba7b7e7a5e637c7dcbb84c35bd (HEAD, refs/heads/master)
Date:   Fri Feb 25 11:54:25 2011 +0800

    File-tune magit-revert-item
    
    See discussion:
    http://thread.gmane.org/gmane.comp.version-control.git.magit/896
---
 magit.el |   35 ++++++++++++++++++++++-------------
 1 files changed, 22 insertions(+), 13 deletions(-)

        Modified magit.el
diff --git a/magit.el b/magit.el
index 8645d7ed..cba0bca6 100644
--- a/magit.el
+++ b/magit.el
@@ -3839,21 +3839,30 @@ (defun magit-cherry-pick-item ()
     ((stash)
      (magit-run-git "stash" "pop" info))))
 
+(defmacro magit-with-revert-confirmation (&rest body)
+  `(when (or (not magit-revert-item-confirm)
+             (yes-or-no-p "Really revert this item? "))
+     ,@body))
+
 (defun magit-revert-item ()
   (interactive)
-  (when (or (not magit-revert-item-confirm)
-           (yes-or-no-p
-            "Really revert this item (cannot be undone)? "))
-    (magit-section-action (item info "revert")
-      ((pending commit)
-       (magit-apply-commit info nil nil t)
-       (magit-rewrite-set-commit-property info 'used nil))
-      ((commit)
-       (magit-apply-commit info nil nil t))
-      ((hunk)
-       (magit-apply-hunk-item-reverse item))
-      ((diff)
-       (magit-apply-diff-item item "--reverse")))))
+  (magit-section-action (item info "revert")
+    ((pending commit)
+     (magit-with-revert-confirmation
+      (magit-apply-commit info nil nil t)
+      (magit-rewrite-set-commit-property info 'used nil)))
+    ((commit)
+     (magit-with-revert-confirmation
+      (magit-apply-commit info nil nil t)))
+    ;; Reverting unstaged changes cannot be undone
+    ((unstaged *)
+     (magit-discard-item))
+    ((hunk)
+     (magit-with-revert-confirmation
+      (magit-apply-hunk-item-reverse item)))
+    ((diff)
+     (magit-with-revert-confirmation
+      (magit-apply-diff-item item "--reverse")))))
 
 (defvar magit-have-graph 'unset)
 (defvar magit-have-decorate 'unset)


Reply via email to