-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagit-standup.el
More file actions
314 lines (276 loc) · 12.3 KB
/
magit-standup.el
File metadata and controls
314 lines (276 loc) · 12.3 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
;;; magit-standup.el --- Collect recent git commits for standup notes -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Function Artisans, Ltd.
;; Author: István Karaszi <ikaraszi@gmail.com>
;; Version: 0.1.0
;; Package-Requires: ((emacs "28.1") (magit "4.5.0") (transient "0.8.0"))
;; Keywords: tools, vc
;; URL: https://github.com/function-artisans/magit-standup
;; 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 3 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.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Collect recent git commits across multiple repositories and format
;; them as org-mode standup notes. On weekends and Mondays it
;; automatically looks back to Friday; on other weekdays it looks back
;; to the previous day. The list of repositories and the lookback
;; behavior are configurable.
;;
;; Usage:
;; M-x magit-standup
;;
;; Customize `magit-standup-repos' to specify which repositories to
;; scan. When nil, only the current repository is used.
;;; Code:
(require 'magit)
(require 'transient)
(defgroup magit-standup nil
"Collect recent git commits for standup notes."
:group 'magit
:prefix "magit-standup-")
(defcustom magit-standup-repos nil
"List of directory paths to collect commits from.
When nil, only the current repository is used. Entries that are
not git repositories are searched recursively for nested repos,
up to `magit-standup-repos-max-depth' levels deep."
:type '(repeat directory)
:group 'magit-standup)
(defcustom magit-standup-repos-max-depth 1
"Maximum depth to search for git repositories in non-repo directories.
When nil, search with unlimited depth."
:type '(choice (const :tag "Unlimited" nil)
(integer :tag "Max depth"))
:group 'magit-standup)
(defcustom magit-standup-author nil
"Author name or email to filter commits by.
When nil, the result of `git config user.email' is used."
:type '(choice (const :tag "From git config" nil)
(string :tag "Author name/email"))
:group 'magit-standup)
(defcustom magit-standup-link-package nil
"Package to use for linking commit hashes in org output.
When nil, auto-detect by checking if `orgit' or `org-git-link'
is loaded. When `orgit', use orgit-rev links. When
`org-git-link', use git links. When `none', plain text
without links."
:type '(choice (const :tag "Auto-detect" nil)
(const :tag "orgit" orgit)
(const :tag "org-git-link" org-git-link)
(const :tag "Plain text" none))
:group 'magit-standup)
(defcustom magit-standup-since-days-ago nil
"Override for how many days back to look.
When nil, automatic weekday-aware logic is used: on Monday look
back to Friday (3 days), otherwise look back 1 day."
:type '(choice (const :tag "Automatic" nil)
(integer :tag "Days ago"))
:group 'magit-standup)
(defconst magit-standup--buffer-name "*magit-standup*")
(defun magit-standup--since-date ()
"Return the \"since\" date string for filtering commits.
If `magit-standup-since-days-ago' is set, use it. Otherwise,
on weekends look back to Friday; on Monday look back to Friday
\(3 days); on other weekdays look back 1 day."
(let* ((now (current-time))
(day (string-to-number (format-time-string "%u" now)))
(days-ago (cond (magit-standup-since-days-ago magit-standup-since-days-ago)
((<= 6 day) (- day 5))
((= 1 day) 3)
(t 1))))
(format-time-string "%Y-%m-%d"
(time-subtract now
(days-to-time days-ago)))))
(defun magit-standup--find-repos (dir depth)
"Recursively find git repositories under DIR up to DEPTH levels.
When DEPTH is nil, search with unlimited depth. Hidden
directories are skipped."
(cond
((magit-git-repo-p dir) (list dir))
((and depth (<= depth 0)) nil)
(t (mapcan (lambda (child)
(when (and (file-directory-p child)
(not (string-prefix-p "." (file-name-nondirectory child))))
(magit-standup--find-repos child (and depth (1- depth)))))
(directory-files dir t nil t)))))
(defun magit-standup--resolve-repos (dirs)
"Expand DIRS to a list of git repository paths.
Entries that are already git repos are kept as-is. Others are
searched recursively up to `magit-standup-repos-max-depth'."
(mapcan (lambda (dir)
(magit-standup--find-repos dir magit-standup-repos-max-depth))
dirs))
(defun magit-standup--detect-link-package ()
"Detect which git-link package is available.
Returns `orgit' if orgit is loaded, `org-git-link' if
org-git-link is loaded, or nil if neither is available."
(cond
((featurep 'orgit) 'orgit)
((featurep 'org-git-link) 'org-git-link)))
(defun magit-standup--link-prefix (package)
"Return the org link type string for PACKAGE.
PACKAGE should be `orgit', `org-git-link', or nil."
(pcase package
('orgit "orgit-rev")
('org-git-link "git")
(_ nil)))
(defun magit-standup--format-commit (repo-path line &optional link-prefix)
"Format a commit LINE, optionally as an org link.
REPO-PATH is the repository directory. LINE is expected to have
the hash separated from the rest by a null byte. LINK-PREFIX is
the org link prefix string, or nil for plain text."
(let* ((parts (split-string line "\0"))
(hash (car parts))
(rest (cadr parts)))
(format "%s %s"
(if link-prefix
(format "[[%s:%s::%s][%s]]" link-prefix repo-path hash hash)
hash)
rest)))
(defun magit-standup--resolve-author (repo-path)
"Return the author string to filter commits by in REPO-PATH.
Uses `magit-standup-author' if set, otherwise falls back to
`git config user.email' in REPO-PATH."
(let ((default-directory (file-name-as-directory repo-path)))
(or magit-standup-author
(magit-git-string "config" "user.email")
(user-error "Cannot determine author for %s; set `magit-standup-author' or git config user.email" repo-path))))
(defun magit-standup--collect-commits (repo-path since-date)
"Collect commits from REPO-PATH since SINCE-DATE.
Returns an alist of (BRANCH-NAME . COMMITS) where COMMITS is a
list of raw commit strings with hash and message separated by a
null byte."
(let* ((default-directory (file-name-as-directory repo-path))
(author (magit-standup--resolve-author repo-path))
(branches (magit-git-lines "branch" "--format=%(refname:short)")))
(mapcar (lambda (branch)
(cons branch
(magit-git-lines "log"
"--no-merges"
"--format=%h%x00%s <%ai> - %aN"
(concat "--after=" since-date)
(concat "--author=" author)
branch)))
branches)))
(defun magit-standup--format-branch-commits (repo-path branch-commits &optional link-prefix)
"Format BRANCH-COMMITS for REPO-PATH as org text.
BRANCH-COMMITS is a cons of (BRANCH-NAME . COMMITS).
LINK-PREFIX is the org link prefix string, or nil for plain text.
Returns nil when BRANCH-COMMITS has no commits."
(when (cdr branch-commits)
(format "** ~%s~\n%s\n"
(car branch-commits)
(mapconcat
(lambda (c)
(format "- %s" (magit-standup--format-commit
repo-path c link-prefix)))
(cdr branch-commits) "\n"))))
(defun magit-standup--format-org (repo-commits &optional link-prefix)
"Format REPO-COMMITS as `org-mode' text.
REPO-COMMITS is an alist of (REPO-PATH . BRANCH-COMMITS) where
BRANCH-COMMITS is an alist of (BRANCH-NAME . COMMITS).
LINK-PREFIX is the org link prefix string, or nil for plain text."
(mapconcat
(lambda (entry)
(let* ((repo-path (car entry))
(repo-name (file-name-nondirectory
(directory-file-name repo-path)))
(formatted (delq nil
(mapcar
(lambda (bc)
(magit-standup--format-branch-commits
repo-path bc link-prefix))
(cdr entry)))))
(when formatted
(format "* [[file:%s][%s]]\n\n%s\n"
repo-path repo-name
(mapconcat #'identity formatted "\n")))))
repo-commits ""))
(defun magit-standup--gather (&optional since-date repos)
"Gather recent commits across all configured repositories.
SINCE-DATE, when non-nil, overrides the computed since date.
REPOS, when non-nil, overrides `magit-standup-repos'.
Returns an alist of (REPO-PATH . BRANCH-COMMITS) suitable for
`magit-standup--format-org'."
(let* ((since-date (or since-date (magit-standup--since-date)))
(repos (or repos
(magit-standup--resolve-repos magit-standup-repos)
(list (magit-toplevel)))))
(mapcar (lambda (repo)
(cons repo
(magit-standup--collect-commits repo since-date)))
repos)))
(defun magit-standup-quit ()
"Quit the `*magit-standup*' buffer and kill it."
(interactive)
(when-let ((win (get-buffer-window magit-standup--buffer-name)))
(quit-window t win)))
(defun magit-standup--display (repo-commits)
"Display REPO-COMMITS in the `*magit-standup*' buffer.
REPO-COMMITS is an alist as returned by `magit-standup--gather'."
(let* ((link-package (or magit-standup-link-package
(magit-standup--detect-link-package)))
(buf (get-buffer-create magit-standup--buffer-name))
(link-prefix (magit-standup--link-prefix link-package)))
(with-current-buffer buf
(let ((inhibit-read-only t))
(erase-buffer)
(insert (magit-standup--format-org repo-commits link-prefix)))
(goto-char (point-min))
(org-mode)
(read-only-mode 1)
(when (bound-and-true-p evil-mode)
(evil-local-set-key 'normal "q" #'magit-standup-quit)))
(pop-to-buffer buf)))
(defun magit-standup--default-repos ()
"Return resolved repo paths as a list of normalized directory names."
(mapcar (lambda (d) (directory-file-name (expand-file-name d)))
(or (magit-standup--resolve-repos magit-standup-repos)
(when-let ((top (magit-toplevel)))
(list top)))))
(defun magit-standup--read-repos (prompt _initial-input _history)
"Read repo directories with `completing-read-multiple'.
PROMPT is shown to the user. Returns a comma-separated string."
(string-join (completing-read-multiple prompt (magit-standup--default-repos)) ","))
(transient-define-infix magit-standup--since ()
:description "Since date"
:class 'transient-option
:shortarg "-d"
:argument "--since="
:reader #'transient-read-date
:init-value (lambda (obj)
(unless (oref obj value)
(oset obj value (magit-standup--since-date)))))
(transient-define-infix magit-standup--repos ()
:description "Repositories"
:class 'transient-option
:shortarg "-r"
:argument "--repos="
:reader #'magit-standup--read-repos)
(transient-define-suffix magit-standup--run ()
"Run the standup with the selected options."
:key "s"
:description "Show standup"
(interactive)
(let* ((args (transient-args 'magit-standup))
(since (transient-arg-value "--since=" args))
(repos-str (transient-arg-value "--repos=" args))
(repos (when repos-str (split-string repos-str ","))))
(magit-standup--display
(magit-standup--gather since repos))))
;;;###autoload (autoload 'magit-standup "magit-standup" nil t)
(transient-define-prefix magit-standup ()
"Show a menu for generating standup notes."
["Options"
(magit-standup--since)
(magit-standup--repos)]
["Actions"
("s" "Show standup" magit-standup--run)])
(provide 'magit-standup)
;;; magit-standup.el ends here