mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-17 c4001ccd1864293b64aa37d83a9d9457eb875e70
commit | author | age
5cb5f7 1 ;;; magit-gitignore.el --- intentionally untracked files  -*- lexical-binding: t -*-
C 2
3 ;; Copyright (C) 2008-2018  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 ;; Magit is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15 ;;
16 ;; Magit is distributed in the hope that it will be useful, but WITHOUT
17 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
19 ;; License for more details.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with Magit.  If not, see http://www.gnu.org/licenses.
23
24 ;;; Commentary:
25
26 ;; This library implements gitignore commands.
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'subr-x))
32
33 (require 'magit)
34
35 ;;; Commands
36
37 ;;;###autoload (autoload 'magit-gitignore-popup "magit-gitignore" nil t)
38 (magit-define-popup magit-gitignore-popup
39   "Popup console for gitignore commands."
40   :man-page "gitignore"
41   :actions '((?l "ignore locally"  magit-gitignore-locally)
42              (?g "ignore globally" magit-gitignore-globally))
43   :max-action-columns 1)
44
45 ;;;###autoload
46 (defun magit-gitignore-globally (file-or-pattern)
47   "Instruct Git to globally ignore FILE-OR-PATTERN."
48   (interactive (list (magit-gitignore-read-pattern nil)))
49   (magit--gitignore file-or-pattern nil))
50
51 ;;;###autoload
52 (defun magit-gitignore-locally (file-or-pattern)
53   "Instruct Git to locally ignore FILE-OR-PATTERN."
54   (interactive (list (magit-gitignore-read-pattern t)))
55   (magit--gitignore file-or-pattern t))
56
57 (defun magit--gitignore (file-or-pattern local)
58   (let ((gitignore
59          (if local
60              (magit-git-dir (convert-standard-filename "info/exclude"))
61            (expand-file-name ".gitignore" (magit-toplevel)))))
62     (make-directory (file-name-directory gitignore) t)
63     (with-temp-buffer
64       (when (file-exists-p gitignore)
65         (insert-file-contents gitignore))
66       (goto-char (point-max))
67       (unless (bolp)
68         (insert "\n"))
69       (insert (replace-regexp-in-string "\\(\\\\*\\)" "\\1\\1" file-or-pattern))
70       (insert "\n")
71       (write-region nil nil gitignore))
72     (if local
73         (magit-refresh)
74       (magit-run-git "add" ".gitignore"))))
75
76 (defun magit-gitignore-read-pattern (local)
77   (let* ((default (magit-current-file))
78          (choices
79           (delete-dups
80            (--mapcat
81             (cons (concat "/" it)
82                   (when-let ((ext (file-name-extension it)))
83                     (list (concat "/" (file-name-directory "foo") "*." ext)
84                           (concat "*." ext))))
85             (magit-untracked-files)))))
86     (when default
87       (setq default (concat "/" default))
88       (unless (member default choices)
89         (setq default (concat "*." (file-name-extension default)))
90         (unless (member default choices)
91           (setq default nil))))
92     (magit-completing-read (concat "File or pattern to ignore"
93                                    (and local " locally"))
94                            choices nil nil nil nil default)))
95
96 ;;; _
97 (provide 'magit-gitignore)
98 ;;; magit-gitignore.el ends here