+(defun stgit-revert ()
+ "Revert the change at point, which must be the index, the work
+tree, or a single change in either."
+ (interactive)
+ (let ((patched-file (stgit-patched-file-at-point)))
+ (if patched-file
+ (stgit-revert-file)
+ (let* ((patch-name (or (stgit-patch-name-at-point)
+ (error "No patch or file at point")))
+ (patch-desc (case patch-name
+ (:index "index")
+ (:work "work tree")
+ (t (error (substitute-command-keys
+ "Use \\[stgit-delete] to delete a patch"))))))
+ (when (if (eq patch-name :work)
+ (stgit-work-tree-empty-p)
+ (stgit-index-empty-p))
+ (error (format "There are no changes in the %s to revert"
+ patch-desc)))
+ (and (eq patch-name :index)
+ (not (stgit-work-tree-empty-p))
+ (error "Cannot revert index as work tree contains unstaged changes"))
+
+ (when (yes-or-no-p (format "Revert all changes in the %s? "
+ patch-desc))
+ (if (eq patch-name :index)
+ (stgit-run-git-silent "reset" "--hard" "-q")
+ (stgit-run-git-silent "checkout" "--" "."))
+ (stgit-refresh-index)
+ (stgit-refresh-worktree)
+ (stgit-goto-patch patch-name))))))
+