-
Notifications
You must be signed in to change notification settings - Fork 0
/
nix3-flake-lock.el
227 lines (199 loc) · 8.56 KB
/
nix3-flake-lock.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
;;; nix3-flake-lock.el --- flake.lock support -*- lexical-binding: t -*-
;; Copyright (C) 2022,2023 Akira Komamura
;; Author: Akira Komamura <[email protected]>
;; URL: https://github.com/emacs-twist/nix3.el
;; This file is not part of GNU Emacs.
;;; License:
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 2.1 of the License, or (at your option) any later version.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with this library; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;; Commentary:
;; FIXME
;;; Code:
(require 'nix3-flake)
(require 'nix3-utils)
(require 'seq)
(require 'map)
(defvar magit-buffer-typearg)
(defvar magit-buffer-revision-hash)
(defvar magit-buffer-range-hashed)
(declare-function text-property-search-backward "text-property-search")
(defconst nix3-flake-lock-range-re
(rx bol (group (+ hex))
".."
(group (+ hex))
eol))
;;;; Faces
(defface nix3-flake-lock-file-heading
'((t (:inherit magit-diff-file-heading)))
"Face for flake.lock file paths."
:group 'nix3-face)
(defface nix3-flake-lock-node-heading
'((t (:inherit magit-section-heading)))
"Face for flake.lock nodes."
:group 'nix3-face)
;;;;
(defun nix3-flake-lock--diff-entries (root filename base rev)
(pcase-exhaustive (mapcar (apply-partially #'nix3-flake-lock--entries-at
root
filename)
(list base rev))
(`(,locks1 ,locks2)
(let (added removed changed)
(dolist (id (seq-difference (mapcar #'car locks2)
(mapcar #'car locks1)))
(push (assq id locks2) added))
(dolist (id (seq-difference (mapcar #'car locks1)
(mapcar #'car locks2)))
(push (assq id locks1) removed))
(dolist (id (seq-intersection (mapcar #'car locks2)
(mapcar #'car locks1)))
(unless (equal (assq id locks2)
(assq id locks1))
(push (list id (assq id locks1) (assq id locks2)) changed)))
(list :added added
:removed removed
:changed changed)))))
(defun nix3-flake-lock--entries-at (root filename &optional rev)
(catch 'no-lock-file
(with-temp-buffer
(if rev
(if-let* ((default-directory root)
(blob (nix3-flake-lock--blob root rev filename)))
(unless (zerop (call-process nix3-git-executable nil (list t nil) nil
"cat-file" "blob" blob))
(error "git-cat-file failed on %s" blob))
(throw 'no-lock-file t))
(insert-file-contents (expand-file-name filename root)))
(goto-char (point-min))
(nix3-flake-lock--parse-buffer))))
(defun nix3-flake-lock--blob (root rev filename)
(let ((default-directory root))
(pcase-exhaustive rev
(`stage
(with-temp-buffer
(unless (zerop (call-process nix3-git-executable nil (list t nil) nil
"ls-files" "--stage" "--" filename))
(error "git-ls-files failed"))
(nth 1 (split-string (buffer-string) " "))))
((pred stringp)
(with-temp-buffer
(when (zerop (call-process nix3-git-executable nil (list t nil) nil
"rev-parse" (concat rev ":" filename)))
(string-chop-newline (buffer-string))))))))
(defun nix3-flake-lock--parse-buffer ()
"Parse nodes in the buffer."
(thread-last
(json-parse-buffer :object-type 'alist)
(alist-get 'nodes)
(seq-filter #'nix3-flake-lock--locked-p)))
(defun nix3-flake-lock--locked-p (alist)
(assq 'locked alist))
(defun nix3-flake-lock--range ()
(cond
(magit-buffer-range-hashed
(when magit-buffer-range-hashed
(if (string-match nix3-flake-lock-range-re magit-buffer-range-hashed)
(list (match-string 1 magit-buffer-range-hashed)
(match-string 2 magit-buffer-range-hashed))
(error "Failed to match a revision range: %s" magit-buffer-range-hashed))))
(magit-buffer-revision-hash
(list (concat magit-buffer-revision-hash "^")
magit-buffer-revision-hash))
((equal magit-buffer-typearg "--cached")
(list "HEAD" 'stage))
(t
(list "HEAD" nil))))
;;;; Magit integration
(defvar nix3-flake-lock-diff-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") #'nix3-flake-lock-source-at-point)
map))
;;;###autoload
(defun nix3-flake-lock-diff-section ()
(when-let* ((files (nix3-flake-lock-magit-sections)))
(cl-flet*
((format-mtime (locked)
(format-time-string "%Y-%m-%d" (alist-get 'lastModified locked)))
(insert-node (old-or-new node)
(insert (format " %s: %s\n"
old-or-new
(nix3-flake-ref-alist-to-url
(alist-get 'locked node)))))
(insert-node-change (event id &optional old new)
(let ((start (point)))
(magit-insert-section (flake-lock-node (list id (cdr old) (cdr new)) t)
(magit-insert-heading
(propertize (format "%-8s %s" event id)
'face 'nix3-flake-lock-node-heading)
(cond
((and new old)
(format " (%s -> %s)"
(format-mtime (alist-get 'locked old))
(format-mtime (alist-get 'locked new))))
(new
(format " (%s)"
(format-mtime (alist-get 'locked new))))))
(let ((ov (make-overlay start (point))))
(overlay-put ov 'keymap nix3-flake-lock-diff-section-map))
(when old
(insert-node "old" old))
(when new
(insert-node "new" new))))))
(pcase-exhaustive (nix3-flake-lock--range)
(`(,lrev ,rrev)
(dolist (file files)
(magit-insert-section (flake-lock file)
(magit-insert-heading (propertize file 'face 'nix3-flake-lock-file-heading))
(pcase-exhaustive (nix3-flake-lock--diff-entries default-directory
file lrev rrev)
((map :added :removed :changed)
(pcase-dolist (`(,id . ,new) added)
(insert-node-change "added" id nil new))
(pcase-dolist (`(,id . ,old) removed)
(insert-node-change "removed" id old))
(pcase-dolist (`(,id ,old ,new) changed)
(insert-node-change "changed" id old new)))))))))
(insert ?\n)))
(defun nix3-flake-lock-source-at-point ()
"Display the source at point."
(interactive)
(when-let* ((section (magit-current-section)))
(pcase (oref section value)
(`(,_id ,old ,new)
(cond
((and old new)
(nix3-flake-git-log-source (alist-get 'original new)
(list (concat (nix3-lookup-tree '(locked rev) old)
"..."
(nix3-lookup-tree '(locked rev) new)))))
(old
(nix3-flake-git-log-source (alist-get 'original old)
(list (nix3-lookup-tree '(locked rev) old))))
(new
(nix3-flake-git-log-source (alist-get 'original new)
(list (nix3-lookup-tree '(locked rev) new)))))))))
(defun nix3-flake-lock-magit-sections ()
"Return flake.lock files specified in magit sections."
(let (files)
(save-excursion
(while (text-property-search-backward 'magit-section)
(let ((section (magit-current-section)))
(when (and (eq (oref section type) 'file)
(string-match-p (rx (or bol "/")
"flake.lock" eol)
(oref section value)))
(push (oref section value) files)))))
(seq-uniq files #'equal)))
(provide 'nix3-flake-lock)
;;; nix3-flake-lock.el ends here