Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.8 KiB

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