dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

magit-reset.el (5330B)


      1 ;;; magit-reset.el --- reset fuctionality  -*- lexical-binding: t -*-
      2 
      3 ;; Copyright (C) 2010-2021  The Magit Project Contributors
      4 ;;
      5 ;; You should have received a copy of the AUTHORS.md file which
      6 ;; lists all contributors.  If not, see http://magit.vc/authors.
      7 
      8 ;; Author: Jonas Bernoulli <jonas@bernoul.li>
      9 ;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
     10 
     11 ;; SPDX-License-Identifier: GPL-3.0-or-later
     12 
     13 ;; Magit is free software; you can redistribute it and/or modify it
     14 ;; under the terms of the GNU General Public License as published by
     15 ;; the Free Software Foundation; either version 3, or (at your option)
     16 ;; any later version.
     17 ;;
     18 ;; Magit is distributed in the hope that it will be useful, but WITHOUT
     19 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     20 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
     21 ;; License for more details.
     22 ;;
     23 ;; You should have received a copy of the GNU General Public License
     24 ;; along with Magit.  If not, see http://www.gnu.org/licenses.
     25 
     26 ;;; Commentary:
     27 
     28 ;; This library implements reset commands.
     29 
     30 ;;; Code:
     31 
     32 (require 'magit)
     33 
     34 ;;;###autoload (autoload 'magit-reset "magit" nil t)
     35 (transient-define-prefix magit-reset ()
     36   "Reset the `HEAD', index and/or worktree to a previous state."
     37   :man-page "git-reset"
     38   ["Reset"
     39    ("m" "mixed    (HEAD and index)"        magit-reset-mixed)
     40    ("s" "soft     (HEAD only)"             magit-reset-soft)
     41    ("h" "hard     (HEAD, index and files)" magit-reset-hard)
     42    ("k" "keep     (HEAD and index, keeping uncommitted)" magit-reset-keep)
     43    ("i" "index    (only)"                  magit-reset-index)
     44    ("w" "worktree (only)"                  magit-reset-worktree)
     45    ""
     46    ("f" "a file"                           magit-file-checkout)])
     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