forked from Malabarba/paradox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paradox-commit-list.el
235 lines (203 loc) · 8.36 KB
/
paradox-commit-list.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
228
229
230
231
232
233
234
;;; paradox-commit-list.el --- listing commits for a package's repository -*- lexical-binding:t -*-
;; Copyright (C) 2014-2015 Artur Malabarba <[email protected]>
;; Author: Artur Malabarba <[email protected]>
;; Prefix: paradox
;; Separator: -
;;; License:
;;
;; This file is NOT part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.
;;
;; This program 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 General Public License for more details.
;;
;;; Code:
(require 'subr-x)
(require 'cl-lib)
(require 'package)
(require 'paradox-github)
(defgroup paradox-commit-list nil
"Buffer used by paradox to list commits for a package."
:prefix "paradox-"
:package-version '(paradox . "2.0")
:group 'paradox)
;;; Variables
(defcustom paradox-commit-list-query-max-pages 1
"Max number of pages we read from github when fetching the commit-list.
Each page lists 100 commits, so 1 page should be more than enough
for most repositories.
Increasing this number consequently multiplies the time it takes
to load the commit list on repos which actually use that many
pages."
:type 'integer
:group 'paradox-commit-list
:package-version '(paradox . "1.2.3"))
(defcustom paradox-date-format "%Y-%m-%d"
"Format used for the date displayed on the commit list.
See `format-time-string' for more information.
Set it to \"%x\" for a more \"human\" date format."
:type 'string
:group 'paradox-commit-list
:package-version '(paradox . "1.2.3"))
(defface paradox-commit-tag-face
'((t :foreground "goldenrod4"
:background "LemonChiffon1"
:box 1))
"Face used for tags on the commit list."
:group 'paradox-commit-list)
;;; Variables
(defvar paradox--commit-message-face nil
"Face currently being used on commit messages.
Gets dynamically changed to `font-lock-comment-face' on old commits.
nil means `default'.")
(defvar-local paradox--package-repo nil
"Repo of the package in a commit-list buffer.")
(defvar-local paradox--package-name nil
"Name of the package in a commit-list buffer.")
(defvar-local paradox--package-version nil
"Installed version of the package in a commit-list buffer.")
(defvar-local paradox--package-tag-commit-alist nil
"Alist of (COMMIT-SHA . TAG) for this package's repo.")
;;; Functions
(defun paradox--get-tag-commit-alist (repo)
"Get REPO's tag list and associate them to commit hashes."
(require 'json)
(mapcar
(lambda (x)
(cons
(cdr (assoc 'sha (cdr (assoc 'commit x))))
(cdr (assoc 'name x))))
(let ((json-array-type 'list))
(paradox--github-action
(format "repos/%s/tags?per_page=100" repo)
:reader #'json-read
:max-pages paradox-commit-list-query-max-pages))))
(defun paradox--get-installed-version (pkg)
"Return the installed version of PKG.
- If PKG isn't installed, return '(0).
- If it has a Melpa-like version (YYYYMMDD HHMM), return it as a
time value.
- If it has a regular version number, return it as a string."
(let ((desc (cadr (assoc pkg package-alist))))
(if desc
(let ((version (package-desc-version desc)))
(if (> (car version) 19000000)
(date-to-time
(format "%8dT%02d:%02d"
(car version)
(/ (cadr version) 100)
(% (cadr version) 100)))
;; Regular version numbers.
(mapconcat 'int-to-string version ".")))
'(0 0))))
(defun paradox--commit-tabulated-list (repo)
"Return the tabulated list for REPO's commit list."
(require 'json)
(let* ((paradox--commit-message-face nil)
(json-array-type 'list)
(feed (paradox--github-action
(format "repos/%s/commits?per_page=100" repo)
:reader #'json-read
:max-pages paradox-commit-list-query-max-pages)))
(apply 'append (mapcar 'paradox--commit-print-info feed))))
(defun paradox--commit-print-info (x)
"Parse json in X into a tabulated list entry."
(let* ((commit (cdr (assoc 'commit x)))
(date (date-to-time (cdr (assoc 'date (cdr (assoc 'committer commit))))))
(title (split-string (cdr (assoc 'message commit)) "[\n\r][ \t]*" t))
;; (url (cdr (assoc 'html_url commit)))
(cc (cdr (assoc 'comment_count commit)))
(sha (cdr (assoc 'sha x)))
(tag (cdr (assoc-string sha paradox--package-tag-commit-alist))))
;; Have we already crossed the installed commit, or is it not even installed?
(unless (or paradox--commit-message-face
(equal '(0) paradox--package-version))
;; Is this where we cross to old commits?
(when (paradox--version<= date tag)
(setq paradox--commit-message-face 'paradox-comment-face)))
;; Return the tabulated list entry.
(cons
;; The ID
(list `((is-old . ,(null paradox--commit-message-face))
(lisp-date . ,date)
,@x)
;; The actual displayed data
(vector
(propertize (format-time-string paradox-date-format date)
'button t
'follow-link t
'action 'paradox-commit-list-visit-commit
'face (or paradox--commit-message-face 'link))
(concat (if (> cc 0)
(propertize (format "(%s comments) " cc)
'face 'font-lock-function-name-face)
"")
(if (stringp tag)
(propertize tag 'face 'paradox-commit-tag-face)
"")
(if (stringp tag) " " "")
(propertize (or (car-safe title) "")
'face paradox--commit-message-face))))
(mapcar (lambda (m) (list x (vector "" (propertize m 'face paradox--commit-message-face))))
(cdr title)))))
(defun paradox--version<= (date version)
"Non-nil if commit at DATE tagged with VERSION is older or equal to `paradox--package-version'."
;; Melpa date-like versions
(if (listp paradox--package-version)
;; Installed date >= to commit date
(null (time-less-p paradox--package-version date))
;; Regular version numbers.
(and version
(ignore-errors (version<= version paradox--package-version)))))
(defun paradox--commit-list-update-entries ()
"Update entries of current commit-list."
(setq tabulated-list-entries
(paradox--commit-tabulated-list paradox--package-repo)))
;;; Commands
(defun paradox-commit-list-visit-commit (&optional _)
"Visit this commit on GitHub.
IGNORE is ignored."
(interactive)
(when (derived-mode-p 'paradox-commit-list-mode)
(browse-url (cdr (assoc 'html_url (tabulated-list-get-id))))))
(defun paradox-previous-commit (&optional n)
"Move to previous commit, which might not be the previous line.
With prefix N, move to the N-th previous commit."
(interactive "p")
(paradox-next-commit (- n)))
(defun paradox-next-commit (&optional n)
"Move to next commit, which might not be the next line.
With prefix N, move to the N-th next commit."
(interactive "p")
(dotimes (_ (abs n))
(let ((d (cl-signum n)))
(forward-line d)
(while (looking-at " +")
(forward-line d)))))
;;; Mode definition
(define-derived-mode paradox-commit-list-mode
tabulated-list-mode "Paradox Commit List"
"Major mode for browsing a list of commits.
Letters do not insert themselves; instead, they are commands.
\\<paradox-commit-list-mode-map>
\\{paradox-commit-list-mode-map}"
(hl-line-mode 1)
(setq tabulated-list-format
`[("Date" ,(length (format-time-string paradox-date-format (current-time))) nil)
("Message" 0 nil)])
(setq tabulated-list-padding 1)
(setq tabulated-list-sort-key nil)
(add-hook 'tabulated-list-revert-hook 'paradox--commit-list-update-entries nil t)
(tabulated-list-init-header))
(define-key paradox-commit-list-mode-map "" #'paradox-commit-list-visit-commit)
(define-key paradox-commit-list-mode-map "p" #'paradox-previous-commit)
(define-key paradox-commit-list-mode-map "n" #'paradox-next-commit)
(provide 'paradox-commit-list)
;;; paradox-commit-list.el ends here.