Skip to content

Commit bef7a50

Browse files
committed
Initial version
0 parents  commit bef7a50

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.elc

git-attr-linguist.el

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
;;; git-attr-linguist.el --- Support git attributes from linguist -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2018 Arne Jørgensen
4+
;; URL: https://github.com/arnested/emacs-git-attr
5+
;; Version: 0.0.1
6+
;; Package-Requires: ((emacs "24.3"))
7+
8+
;; Author: Arne Jørgensen <arne@arnested.dk>
9+
;; Keywords: vc
10+
11+
;; This program is free software; you can redistribute it and/or modify
12+
;; it under the terms of the GNU General Public License as published by
13+
;; the Free Software Foundation, either version 3 of the License, or
14+
;; (at your option) any later version.
15+
16+
;; This program is distributed in the hope that it will be useful,
17+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
;; GNU General Public License for more details.
20+
21+
;; You should have received a copy of the GNU General Public License
22+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
24+
;;; Commentary:
25+
26+
;; This adds some functions for the git attributes linguist-generated
27+
;; and linguist-vendored.
28+
29+
;; It adds a `find-file-hook' and upon visiting a file puts the buffer
30+
;; into `git-attr-linguist-generated-mode' and/or
31+
;; `git-attr-linguist-vendored-mode' minor modes.
32+
33+
;; Both minor modes just puts the buffer into `read-only-mode'.
34+
35+
;;; Code:
36+
37+
(require 'git-attr)
38+
39+
(defun git-attr-linguist-generated-p ()
40+
"Check if current buffer is a generated file."
41+
(let ((value (git-attr-get "linguist-generated")))
42+
(string= value "true")))
43+
44+
(defun git-attr-linguist-vendored-p ()
45+
"Check if current buffer is a vendored file."
46+
(let ((value (git-attr-get "linguist-vendored")))
47+
(string= value "true")))
48+
49+
(define-minor-mode git-attr-linguist-generated-mode
50+
nil
51+
:lighter " Generated"
52+
(if git-attr-linguist-generated-mode
53+
(read-only-mode 1)
54+
(read-only-mode 0)))
55+
56+
(define-minor-mode git-attr-linguist-vendored-mode
57+
nil
58+
:lighter " Vendored"
59+
(if git-attr-linguist-vendored-mode
60+
(read-only-mode 1)
61+
(read-only-mode 0)))
62+
63+
(defun git-attr-linguist ()
64+
"Make vendored and generated files read only."
65+
(when (git-attr-linguist-generated-p)
66+
(git-attr-linguist-generated-mode 1))
67+
(when (git-attr-linguist-vendored-p)
68+
(git-attr-linguist-vendored-mode 1)))
69+
70+
;;;###autoload
71+
(add-hook 'find-file-hook 'git-attr-linguist)
72+
73+
(provide 'git-attr-linguist)
74+
;;; git-attr-linguist.el ends here

git-attr.el

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
;;; git-attr.el --- Git attributes of buffer file -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2018 Arne Jørgensen
4+
;; URL: https://github.com/arnested/emacs-git-attr
5+
;; Version: 0.0.1
6+
;; Package-Requires: ((emacs "24.3"))
7+
8+
;; Author: Arne Jørgensen <arne@arnested.dk>
9+
;; Keywords: vc
10+
11+
;; This program is free software; you can redistribute it and/or modify
12+
;; it under the terms of the GNU General Public License as published by
13+
;; the Free Software Foundation, either version 3 of the License, or
14+
;; (at your option) any later version.
15+
16+
;; This program is distributed in the hope that it will be useful,
17+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
;; GNU General Public License for more details.
20+
21+
;; You should have received a copy of the GNU General Public License
22+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
24+
;;; Commentary:
25+
26+
;; This tool will let you use git attributes
27+
;; (https://git-scm.com/docs/gitattributes) in Emacs buffers.
28+
29+
;; In example the following will get the value of a `foo' git
30+
;; attribute for the file associated with the current buffer:
31+
32+
;; (git-attr-get "foo")
33+
34+
;; The `git-attr-get' function will return
35+
36+
;; * t for git attributes with the value `set'
37+
;; * nil for git attributes with the value `unset'
38+
;; * 'undecided for git attributes that are `unspecified'
39+
;; * and the value if the git attribute is set to a value
40+
41+
;;; Code:
42+
43+
(defvar-local git-attr 'undecided "Git attributtes for current buffer.")
44+
45+
(defun git-attr-check ()
46+
"Get git attributes for current buffer file."
47+
(let ((file buffer-file-name)
48+
(git (executable-find "git")))
49+
(when (and file
50+
git)
51+
(let ((attr-list (split-string (with-output-to-string
52+
(with-current-buffer standard-output
53+
(call-process git nil (list t nil) nil "check-attr" "-z" "-a" file)
54+
)
55+
) "\000" t))
56+
result)
57+
(while attr-list
58+
(push `(,(cadr attr-list) . ,(caddr attr-list)) result)
59+
(setq attr-list (cdddr attr-list)))
60+
result))))
61+
62+
(defun git-attr ()
63+
"Get git attributes for current buffer file and set in buffer local variable `git-attr'."
64+
(interactive)
65+
(if (eq git-attr 'undecided)
66+
(setq git-attr (git-attr-check))
67+
git-attr))
68+
69+
(defun git-attr-raw (attr)
70+
"Get the raw git attribute named ATTR for the file in current buffer.
71+
72+
This is the raw value as returned from `git check-attr -a' (if specified).
73+
74+
You probably want to use `git-attr-get' instead."
75+
(cdr (assoc attr (git-attr))))
76+
77+
(defun git-attr-get (attr)
78+
"Get the git attribute named ATTR for the file in current buffer.
79+
80+
* t for git attributes with the value `set'
81+
* nil for git attributes with the value `unset'
82+
* 'undecided for git attributes that are `unspecified'
83+
* and the value if the git attribute is set to a value"
84+
(let ((value (git-attr-raw attr)))
85+
(cond ((string= value "set") t)
86+
((string= value "unset") nil)
87+
((eq nil value) 'undecided)
88+
(t value))))
89+
90+
(provide 'git-attr)
91+
;;; git-attr.el ends here

0 commit comments

Comments
 (0)