Klimi's new dotfiles with stow.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

186 строки
6.8 KiB

4 лет назад
  1. ;;; magit-gitignore.el --- intentionally untracked files -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2008-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 gitignore commands.
  22. ;;; Code:
  23. (eval-when-compile
  24. (require 'subr-x))
  25. (require 'magit)
  26. ;;; Transient
  27. ;;;###autoload (autoload 'magit-gitignore "magit-gitignore" nil t)
  28. (define-transient-command magit-gitignore ()
  29. "Instruct Git to ignore a file or pattern."
  30. :man-page "gitignore"
  31. ["Gitignore"
  32. ("t" "shared at toplevel (.gitignore)"
  33. magit-gitignore-in-topdir)
  34. ("s" "shared in subdirectory (path/to/.gitignore)"
  35. magit-gitignore-in-subdir)
  36. ("p" "privately (.git/info/exclude)"
  37. magit-gitignore-in-gitdir)
  38. ("g" magit-gitignore-on-system
  39. :if (lambda () (magit-get "core.excludesfile"))
  40. :description (lambda ()
  41. (format "privately for all repositories (%s)"
  42. (magit-get "core.excludesfile"))))]
  43. ["Skip worktree"
  44. (7 "w" "do skip worktree" magit-skip-worktree)
  45. (7 "W" "do not skip worktree" magit-no-skip-worktree)]
  46. ["Assume unchanged"
  47. (7 "u" "do assume unchanged" magit-assume-unchanged)
  48. (7 "U" "do not assume unchanged" magit-no-assume-unchanged)])
  49. ;;; Gitignore Commands
  50. ;;;###autoload
  51. (defun magit-gitignore-in-topdir (rule)
  52. "Add the Git ignore RULE to the top-level \".gitignore\" file.
  53. Since this file is tracked, it is shared with other clones of the
  54. repository. Also stage the file."
  55. (interactive (list (magit-gitignore-read-pattern)))
  56. (magit-with-toplevel
  57. (magit--gitignore rule ".gitignore")
  58. (magit-run-git "add" ".gitignore")))
  59. ;;;###autoload
  60. (defun magit-gitignore-in-subdir (rule directory)
  61. "Add the Git ignore RULE to a \".gitignore\" file.
  62. Prompted the user for a directory and add the rule to the
  63. \".gitignore\" file in that directory. Since such files are
  64. tracked, they are shared with other clones of the repository.
  65. Also stage the file."
  66. (interactive (list (magit-gitignore-read-pattern)
  67. (read-directory-name "Limit rule to files in: ")))
  68. (magit-with-toplevel
  69. (let ((file (expand-file-name ".gitignore" directory)))
  70. (magit--gitignore rule file)
  71. (magit-run-git "add" ".gitignore"))))
  72. ;;;###autoload
  73. (defun magit-gitignore-in-gitdir (rule)
  74. "Add the Git ignore RULE to \"$GIT_DIR/info/exclude\".
  75. Rules in that file only affects this clone of the repository."
  76. (interactive (list (magit-gitignore-read-pattern)))
  77. (magit--gitignore rule (magit-git-dir "info/exclude"))
  78. (magit-refresh))
  79. ;;;###autoload
  80. (defun magit-gitignore-on-system (rule)
  81. "Add the Git ignore RULE to the file specified by `core.excludesFile'.
  82. Rules that are defined in that file affect all local repositories."
  83. (interactive (list (magit-gitignore-read-pattern)))
  84. (magit--gitignore rule
  85. (or (magit-get "core.excludesFile")
  86. (error "Variable `core.excludesFile' isn't set")))
  87. (magit-refresh))
  88. (defun magit--gitignore (rule file)
  89. (when-let ((directory (file-name-directory file)))
  90. (make-directory directory t))
  91. (with-temp-buffer
  92. (when (file-exists-p file)
  93. (insert-file-contents file))
  94. (goto-char (point-max))
  95. (unless (bolp)
  96. (insert "\n"))
  97. (insert (replace-regexp-in-string "\\(\\\\*\\)" "\\1\\1" rule))
  98. (insert "\n")
  99. (write-region nil nil file)))
  100. (defun magit-gitignore-read-pattern ()
  101. (let* ((default (magit-current-file))
  102. (choices
  103. (delete-dups
  104. (--mapcat
  105. (cons (concat "/" it)
  106. (when-let ((ext (file-name-extension it)))
  107. (list (concat "/" (file-name-directory "foo") "*." ext)
  108. (concat "*." ext))))
  109. (magit-untracked-files)))))
  110. (when default
  111. (setq default (concat "/" default))
  112. (unless (member default choices)
  113. (setq default (concat "*." (file-name-extension default)))
  114. (unless (member default choices)
  115. (setq default nil))))
  116. (magit-completing-read "File or pattern to ignore"
  117. choices nil nil nil nil default)))
  118. ;;; Skip Worktree Commands
  119. ;;;###autoload
  120. (defun magit-skip-worktree (file)
  121. "Call \"git update-index --skip-worktree -- FILE\"."
  122. (interactive
  123. (list (magit-read-file-choice "Skip worktree for"
  124. (magit-with-toplevel
  125. (cl-set-difference
  126. (magit-list-files)
  127. (magit-skip-worktree-files))))))
  128. (magit-with-toplevel
  129. (magit-run-git "update-index" "--skip-worktree" "--" file)))
  130. ;;;###autoload
  131. (defun magit-no-skip-worktree (file)
  132. "Call \"git update-index --no-skip-worktree -- FILE\"."
  133. (interactive
  134. (list (magit-read-file-choice "Do not skip worktree for"
  135. (magit-with-toplevel
  136. (magit-skip-worktree-files)))))
  137. (magit-with-toplevel
  138. (magit-run-git "update-index" "--no-skip-worktree" "--" file)))
  139. ;;; Assume Unchanged Commands
  140. ;;;###autoload
  141. (defun magit-assume-unchanged (file)
  142. "Call \"git update-index --assume-unchanged -- FILE\"."
  143. (interactive
  144. (list (magit-read-file-choice "Assume file to be unchanged"
  145. (magit-with-toplevel
  146. (cl-set-difference
  147. (magit-list-files)
  148. (magit-assume-unchanged-files))))))
  149. (magit-with-toplevel
  150. (magit-run-git "update-index" "--assume-unchanged" "--" file)))
  151. ;;;###autoload
  152. (defun magit-no-assume-unchanged (file)
  153. "Call \"git update-index --no-assume-unchanged -- FILE\"."
  154. (interactive
  155. (list (magit-read-file-choice "Do not assume file to be unchanged"
  156. (magit-with-toplevel
  157. (magit-assume-unchanged-files)))))
  158. (magit-with-toplevel
  159. (magit-run-git "update-index" "--no-assume-unchanged" "--" file)))
  160. ;;; _
  161. (provide 'magit-gitignore)
  162. ;;; magit-gitignore.el ends here