magit-reset.el (5268B)
1 ;;; magit-reset.el --- Reset functionality -*- lexical-binding:t -*- 2 3 ;; Copyright (C) 2008-2024 The Magit Project Contributors 4 5 ;; Author: Jonas Bernoulli <emacs.magit@jonas.bernoulli.dev> 6 ;; Maintainer: Jonas Bernoulli <emacs.magit@jonas.bernoulli.dev> 7 8 ;; SPDX-License-Identifier: GPL-3.0-or-later 9 10 ;; Magit is free software: you can redistribute it and/or modify it 11 ;; under the terms of the GNU General Public License as published by 12 ;; the Free Software Foundation, either version 3 of the License, or 13 ;; (at your option) any later version. 14 ;; 15 ;; Magit is distributed in the hope that it will be useful, but WITHOUT 16 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 17 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 18 ;; License for more details. 19 ;; 20 ;; You should have received a copy of the GNU General Public License 21 ;; along with Magit. If not, see <https://www.gnu.org/licenses/>. 22 23 ;;; Commentary: 24 25 ;; This library implements reset commands. 26 27 ;;; Code: 28 29 (require 'magit) 30 31 ;;; Commands 32 33 ;;;###autoload (autoload 'magit-reset "magit" nil t) 34 (transient-define-prefix magit-reset () 35 "Reset the `HEAD', index and/or worktree to a previous state." 36 :man-page "git-reset" 37 [["Reset" 38 ("b" "branch" magit-branch-reset) 39 ("f" "file" magit-file-checkout)] 40 ["Reset this" 41 ("m" "mixed (HEAD and index)" magit-reset-mixed) 42 ("s" "soft (HEAD only)" magit-reset-soft) 43 ("h" "hard (HEAD, index and worktree)" magit-reset-hard) 44 ("k" "keep (HEAD and index, keeping uncommitted)" magit-reset-keep) 45 ("i" "index (only)" magit-reset-index) 46 ("w" "worktree (only)" magit-reset-worktree)]]) 47 48 ;;;###autoload 49 (defun magit-reset-mixed (commit) 50 "Reset the `HEAD' and index to COMMIT, but not the working tree. 51 \n(git reset --mixed COMMIT)" 52 (interactive (list (magit-reset-read-branch-or-commit "Reset %s to"))) 53 (magit-reset-internal "--mixed" commit)) 54 55 ;;;###autoload 56 (defun magit-reset-soft (commit) 57 "Reset the `HEAD' to COMMIT, but not the index and working tree. 58 \n(git reset --soft REVISION)" 59 (interactive (list (magit-reset-read-branch-or-commit "Soft reset %s to"))) 60 (magit-reset-internal "--soft" commit)) 61 62 ;;;###autoload 63 (defun magit-reset-hard (commit) 64 "Reset the `HEAD', index, and working tree to COMMIT. 65 \n(git reset --hard REVISION)" 66 (interactive (list (magit-reset-read-branch-or-commit 67 (concat (magit--propertize-face "Hard" 'bold) 68 " reset %s to")))) 69 (magit-reset-internal "--hard" commit)) 70 71 ;;;###autoload 72 (defun magit-reset-keep (commit) 73 "Reset the `HEAD' and index to COMMIT, while keeping uncommitted changes. 74 \n(git reset --keep REVISION)" 75 (interactive (list (magit-reset-read-branch-or-commit "Reset %s to"))) 76 (magit-reset-internal "--keep" commit)) 77 78 ;;;###autoload 79 (defun magit-reset-index (commit) 80 "Reset the index to COMMIT. 81 Keep the `HEAD' and working tree as-is, so if COMMIT refers to the 82 head this effectively unstages all changes. 83 \n(git reset COMMIT .)" 84 (interactive (list (magit-read-branch-or-commit "Reset index to"))) 85 (magit-reset-internal nil commit ".")) 86 87 ;;;###autoload 88 (defun magit-reset-worktree (commit) 89 "Reset the worktree to COMMIT. 90 Keep the `HEAD' and index as-is." 91 (interactive (list (magit-read-branch-or-commit "Reset worktree to"))) 92 (magit-wip-commit-before-change nil " before reset") 93 (magit-with-temp-index commit nil 94 (magit-call-git "checkout-index" "--all" "--force")) 95 (magit-wip-commit-after-apply nil " after reset") 96 (magit-refresh)) 97 98 ;;;###autoload 99 (defun magit-reset-quickly (commit &optional hard) 100 "Reset the `HEAD' and index to COMMIT, and possibly the working tree. 101 With a prefix argument reset the working tree otherwise don't. 102 \n(git reset --mixed|--hard COMMIT)" 103 (interactive (list (magit-reset-read-branch-or-commit 104 (if current-prefix-arg 105 (concat (magit--propertize-face "Hard" 'bold) 106 " reset %s to") 107 "Reset %s to")) 108 current-prefix-arg)) 109 (magit-reset-internal (if hard "--hard" "--mixed") commit)) 110 111 (defun magit-reset-read-branch-or-commit (prompt) 112 "Prompt for and return a ref to reset HEAD to. 113 114 PROMPT is a format string, where either the current branch name 115 or \"detached head\" will be substituted for %s." 116 (magit-read-branch-or-commit 117 (format prompt (or (magit-get-current-branch) "detached head")))) 118 119 (defun magit-reset-internal (arg commit &optional path) 120 (when (and (not (member arg '("--hard" nil))) 121 (equal (magit-rev-parse commit) 122 (magit-rev-parse "HEAD~"))) 123 (with-temp-buffer 124 (magit-git-insert "show" "-s" "--format=%B" "HEAD") 125 (when git-commit-major-mode 126 (funcall git-commit-major-mode)) 127 (git-commit-setup-font-lock) 128 (git-commit-save-message))) 129 (let ((cmd (if (and (equal commit "HEAD") (not arg)) "unstage" "reset"))) 130 (magit-wip-commit-before-change nil (concat " before " cmd)) 131 (magit-run-git "reset" arg commit "--" path) 132 (when (equal cmd "unstage") 133 (magit-wip-commit-after-apply nil " after unstage")))) 134 135 ;;; _ 136 (provide 'magit-reset) 137 ;;; magit-reset.el ends here