-
-
Notifications
You must be signed in to change notification settings - Fork 826
/
Copy pathmagit-log.el
2057 lines (1837 loc) · 81.3 KB
/
magit-log.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; magit-log.el --- Inspect Git history -*- lexical-binding:t; coding:utf-8 -*-
;; Copyright (C) 2008-2025 The Magit Project Contributors
;; Author: Jonas Bernoulli <emacs.magit@jonas.bernoulli.dev>
;; Maintainer: Jonas Bernoulli <emacs.magit@jonas.bernoulli.dev>
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Magit 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.
;;
;; Magit 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 Magit. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This library implements support for looking at Git logs, including
;; special logs like cherry-logs, as well as for selecting a commit
;; from a log.
;;; Code:
(require 'magit-core)
(require 'magit-diff)
(declare-function magit-blob-visit "magit-files" (blob-or-file))
(declare-function magit-cherry-apply "magit-sequence" (commit &optional args))
(declare-function magit-insert-head-branch-header "magit-status"
(&optional branch))
(declare-function magit-insert-upstream-branch-header "magit-status"
(&optional branch pull keyword))
(declare-function magit-read-file-from-rev "magit-files"
(rev prompt &optional default include-dirs))
(declare-function magit-rebase--get-state-lines "magit-sequence"
(file))
(declare-function magit-show-commit "magit-diff"
(arg1 &optional arg2 arg3 arg4))
(declare-function magit-reflog-format-subject "magit-reflog" (subject))
(defvar magit-refs-focus-column-width)
(defvar magit-refs-margin)
(defvar magit-refs-show-commit-count)
(defvar magit-buffer-margin)
(defvar magit-status-margin)
(defvar magit-status-sections-hook)
(require 'ansi-color)
(require 'crm)
(require 'which-func)
(make-obsolete-variable 'magit-log-highlight-keywords
'magit-log-wash-summary-hook
"Magit 4.3.0")
(make-obsolete-variable 'magit-log-format-message-function
'magit-log-wash-summary-hook
"Magit 4.3.0")
;;; Options
;;;; Log Mode
(defgroup magit-log nil
"Inspect and manipulate Git history."
:link '(info-link "(magit)Logging")
:group 'magit-commands
:group 'magit-modes)
(defcustom magit-log-mode-hook nil
"Hook run after entering Magit-Log mode."
:group 'magit-log
:type 'hook)
(defcustom magit-log-remove-graph-args '("--follow" "--grep" "-G" "-S" "-L")
"The log arguments that cause the `--graph' argument to be dropped.
The default value lists the arguments that are incompatible with
`--graph' and therefore must be dropped when that is used. You
can add additional arguments that are available in `magit-log',
but I recommend that you don't do that. Nowadays I would define
this as a constant, but I am preserving it as an option, in case
someone actually customized it."
:package-version '(magit . "2.3.0")
:group 'magit-log
:type '(repeat (string :tag "Argument"))
:options '("--follow" "--grep" "-G" "-S" "-L"))
(defcustom magit-log-revision-headers-format "\
%+b%+N
Author: %aN <%aE>
Committer: %cN <%cE>"
"Additional format string used with the `++header' argument."
:package-version '(magit . "3.2.0")
:group 'magit-log
:type 'string)
(defcustom magit-log-auto-more nil
"Insert more log entries automatically when moving past the last entry.
Only considered when moving past the last entry with
`magit-goto-*-section' commands."
:group 'magit-log
:type 'boolean)
(defcustom magit-log-margin '(t age magit-log-margin-width t 18)
"Format of the margin in `magit-log-mode' buffers.
The value has the form (INIT STYLE WIDTH AUTHOR AUTHOR-WIDTH).
If INIT is non-nil, then the margin is shown initially.
STYLE controls how to format the author or committer date.
It can be one of `age' (to show the age of the commit),
`age-abbreviated' (to abbreviate the time unit to a character),
or a string (suitable for `format-time-string') to show the
actual date. Option `magit-log-margin-show-committer-date'
controls which date is being displayed.
WIDTH controls the width of the margin. This exists for forward
compatibility and currently the value should not be changed.
AUTHOR controls whether the name of the author is also shown by
default.
AUTHOR-WIDTH has to be an integer. When the name of the author
is shown, then this specifies how much space is used to do so."
:package-version '(magit . "2.9.0")
:group 'magit-log
:group 'magit-margin
:type magit-log-margin--custom-type
:initialize #'magit-custom-initialize-reset
:set (apply-partially #'magit-margin-set-variable 'magit-log-mode))
(defcustom magit-log-margin-show-committer-date nil
"Whether to show the committer date in the margin.
This option only controls whether the committer date is displayed
instead of the author date. Whether some date is displayed in
the margin and whether the margin is displayed at all is
controlled by other options."
:package-version '(magit . "3.0.0")
:group 'magit-log
:group 'magit-margin
:type 'boolean)
(defcustom magit-log-show-refname-after-summary nil
"Whether to show refnames after commit summaries.
This is useful if you use really long branch names."
:package-version '(magit . "2.2.0")
:group 'magit-log
:type 'boolean)
(defcustom magit-log-wash-summary-hook
(list #'magit-highlight-squash-markers
#'magit-highlight-bracket-keywords)
"Functions used to highlight parts of each individual commit summary.
These functions are called in order, in a buffer that containing the
first line of the commit message. They should set text properties as
they see fit, usually just `font-lock-face'. Before each function is
called, point is at the beginning of the buffer.
See also the related `magit-revision-wash-message-hook'. You likely
want to use the same functions for both hooks."
:package-version '(magit . "4.3.0")
:group 'magit-log
:type 'hook
:options (list #'magit-highlight-squash-markers
#'magit-highlight-bracket-keywords))
(defcustom magit-log-header-line-function #'magit-log-header-line-sentence
"Function used to generate text shown in header line of log buffers."
:package-version '(magit . "2.12.0")
:group 'magit-log
:type `(choice (function-item ,#'magit-log-header-line-arguments)
(function-item ,#'magit-log-header-line-sentence)
function))
(defcustom magit-log-trace-definition-function #'magit-which-function
"Function used to determine the function at point.
This is used by the command `magit-log-trace-definition'.
You should prefer `magit-which-function' over `which-function'
because the latter may make use of Imenu's outdated cache."
:package-version '(magit . "3.0.0")
:group 'magit-log
:type `(choice (function-item ,#'magit-which-function)
(function-item ,#'which-function)
(function-item ,#'add-log-current-defun)
function))
(defcustom magit-log-color-graph-limit 256
"Number of commits over which log graphs are not colored.
When showing more commits than specified, then the `--color'
argument is silently dropped. This is necessary because the
`ansi-color' library, which is used to turn control sequences
into faces, is just too slow."
:package-version '(magit . "4.0.0")
:group 'magit-log
:type 'number)
(defcustom magit-log-show-signatures-limit 256
"Number of commits over which signatures are not verified.
When showing more commits than specified by this option, then the
`--show-signature' argument, if specified, is silently dropped.
This is necessary because checking the signature of a large
number of commits is just too slow."
:package-version '(magit . "4.0.0")
:group 'magit-log
:type 'number)
(defface magit-log-graph
'((((class color) (background light)) :foreground "grey30")
(((class color) (background dark)) :foreground "grey80"))
"Face for the graph part of the log output."
:group 'magit-faces)
(defface magit-log-author
'((((class color) (background light))
:foreground "firebrick"
:slant normal
:weight normal)
(((class color) (background dark))
:foreground "tomato"
:slant normal
:weight normal))
"Face for the author part of the log output."
:group 'magit-faces)
(defface magit-log-date
'((((class color) (background light))
:foreground "grey30"
:slant normal
:weight normal)
(((class color) (background dark))
:foreground "grey80"
:slant normal
:weight normal))
"Face for the date part of the log output."
:group 'magit-faces)
(defface magit-header-line-log-select
'((t :inherit bold))
"Face for the `header-line' in `magit-log-select-mode'."
:group 'magit-faces)
;;;; File Log
(defcustom magit-log-buffer-file-locked t
"Whether `magit-log-buffer-file-quick' uses a dedicated buffer."
:package-version '(magit . "2.7.0")
:group 'magit-commands
:group 'magit-log
:type 'boolean)
;;;; Select Mode
(defcustom magit-log-select-show-usage 'both
"Whether to show usage information when selecting a commit from a log.
The message can be shown in the `echo-area' or the `header-line', or in
`both' places. If the value isn't one of these symbols, then it should
be nil, in which case no usage information is shown."
:package-version '(magit . "2.1.0")
:group 'magit-log
:type '(choice (const :tag "In echo-area" echo-area)
(const :tag "In header-line" header-line)
(const :tag "In both places" both)
(const :tag "Nowhere")))
(defcustom magit-log-select-margin
(list (nth 0 magit-log-margin)
(nth 1 magit-log-margin)
'magit-log-margin-width t
(nth 4 magit-log-margin))
"Format of the margin in `magit-log-select-mode' buffers.
The value has the form (INIT STYLE WIDTH AUTHOR AUTHOR-WIDTH).
If INIT is non-nil, then the margin is shown initially.
STYLE controls how to format the author or committer date.
It can be one of `age' (to show the age of the commit),
`age-abbreviated' (to abbreviate the time unit to a character),
or a string (suitable for `format-time-string') to show the
actual date. Option `magit-log-margin-show-committer-date'
controls which date is being displayed.
WIDTH controls the width of the margin. This exists for forward
compatibility and currently the value should not be changed.
AUTHOR controls whether the name of the author is also shown by
default.
AUTHOR-WIDTH has to be an integer. When the name of the author
is shown, then this specifies how much space is used to do so."
:package-version '(magit . "2.9.0")
:group 'magit-log
:group 'magit-margin
:type magit-log-margin--custom-type
:initialize #'magit-custom-initialize-reset
:set-after '(magit-log-margin)
:set (apply-partially #'magit-margin-set-variable 'magit-log-select-mode))
;;;; Cherry Mode
(defcustom magit-cherry-sections-hook
(list #'magit-insert-cherry-headers
#'magit-insert-cherry-commits)
"Hook run to insert sections into the cherry buffer."
:package-version '(magit . "2.1.0")
:group 'magit-log
:type 'hook)
(defcustom magit-cherry-margin
(list (nth 0 magit-log-margin)
(nth 1 magit-log-margin)
'magit-log-margin-width t
(nth 4 magit-log-margin))
"Format of the margin in `magit-cherry-mode' buffers.
The value has the form (INIT STYLE WIDTH AUTHOR AUTHOR-WIDTH).
If INIT is non-nil, then the margin is shown initially.
STYLE controls how to format the author or committer date.
It can be one of `age' (to show the age of the commit),
`age-abbreviated' (to abbreviate the time unit to a character),
or a string (suitable for `format-time-string') to show the
actual date. Option `magit-log-margin-show-committer-date'
controls which date is being displayed.
WIDTH controls the width of the margin. This exists for forward
compatibility and currently the value should not be changed.
AUTHOR controls whether the name of the author is also shown by
default.
AUTHOR-WIDTH has to be an integer. When the name of the author
is shown, then this specifies how much space is used to do so."
:package-version '(magit . "2.9.0")
:group 'magit-log
:group 'magit-margin
:type magit-log-margin--custom-type
:initialize #'magit-custom-initialize-reset
:set-after '(magit-log-margin)
:set (apply-partially #'magit-margin-set-variable 'magit-cherry-mode))
;;;; Log Sections
(defcustom magit-log-section-commit-count 10
"How many recent commits to show in certain log sections.
How many recent commits `magit-insert-recent-commits' and
`magit-insert-unpulled-from-upstream-or-recent' (provided
the upstream isn't ahead of the current branch) show."
:package-version '(magit . "2.1.0")
:group 'magit-status
:type 'number)
(defcustom magit-log-merged-commit-count 20
"How many surrounding commits to show for `magit-log-merged'.
`magit-log-merged' will shows approximately half of this number
commits before and half after."
:package-version '(magit . "3.3.0")
:group 'magit-log
:type 'integer)
;;; Arguments
;;;; Prefix Classes
(defclass magit-log-prefix (transient-prefix)
((history-key :initform 'magit-log)
(major-mode :initform 'magit-log-mode)))
(defclass magit-log-refresh-prefix (magit-log-prefix)
((history-key :initform 'magit-log)
(major-mode :initform nil)))
;;;; Prefix Methods
(cl-defmethod transient-init-value ((obj magit-log-prefix))
(pcase-let ((`(,args ,files)
(magit-log--get-value 'magit-log-mode
magit-prefix-use-buffer-arguments)))
(when-let (((not (eq transient-current-command 'magit-dispatch)))
(file (magit-file-relative-name)))
(setq files (list file)))
(oset obj value (if files `(("--" ,@files) ,@args) args))))
(cl-defmethod transient-init-value ((obj magit-log-refresh-prefix))
(oset obj value (if magit-buffer-log-files
`(("--" ,@magit-buffer-log-files)
,@magit-buffer-log-args)
magit-buffer-log-args)))
(cl-defmethod transient-set-value ((obj magit-log-prefix))
(magit-log--set-value obj))
(cl-defmethod transient-save-value ((obj magit-log-prefix))
(magit-log--set-value obj 'save))
;;;; Argument Access
(defun magit-log-arguments (&optional mode)
"Return the current log arguments."
(if (memq transient-current-command '(magit-log magit-log-refresh))
(magit--transient-args-and-files)
(magit-log--get-value (or mode 'magit-log-mode))))
(defun magit-log--get-value (mode &optional use-buffer-args)
(unless use-buffer-args
(setq use-buffer-args magit-direct-use-buffer-arguments))
(let (args files)
(cond
((and (memq use-buffer-args '(always selected current))
(eq major-mode mode))
(setq args magit-buffer-log-args)
(setq files magit-buffer-log-files))
((when-let (((memq use-buffer-args '(always selected)))
(buffer (magit-get-mode-buffer
mode nil
(eq use-buffer-args 'selected))))
(setq args (buffer-local-value 'magit-buffer-log-args buffer))
(setq files (buffer-local-value 'magit-buffer-log-files buffer))
t))
((plist-member (symbol-plist mode) 'magit-log-current-arguments)
(setq args (get mode 'magit-log-current-arguments)))
((when-let ((elt (assq (intern (format "magit-log:%s" mode))
transient-values)))
(setq args (cdr elt))
t))
(t
(setq args (get mode 'magit-log-default-arguments))))
(list args files)))
(defun magit-log--set-value (obj &optional save)
(pcase-let* ((obj (oref obj prototype))
(mode (or (oref obj major-mode) major-mode))
(key (intern (format "magit-log:%s" mode)))
(`(,args ,files) (magit--transient-args-and-files)))
(put mode 'magit-log-current-arguments args)
(when save
(setf (alist-get key transient-values) args)
(transient-save-values))
(transient--history-push obj)
(setq magit-buffer-log-args args)
(unless (derived-mode-p 'magit-log-select-mode)
(setq magit-buffer-log-files files))
(magit-refresh)))
;;; Commands
;;;; Prefix Commands
(eval-and-compile
(defvar magit-log-infix-arguments
;; The grouping in git-log(1) appears to be guided by implementation
;; details, so our logical grouping only follows it to an extend.
;; Arguments that are "misplaced" here:
;; 1. From "Commit Formatting".
;; 2. From "Common Diff Options".
;; 3. From unnamed first group.
;; 4. Implemented by Magit.
[:class transient-subgroups
["Commit limiting"
(magit-log:-n)
(magit:--author)
(7 magit-log:--since)
(7 magit-log:--until)
(magit-log:--grep)
(7 "-i" "Search case-insensitive" ("-i" "--regexp-ignore-case"))
(7 "-I" "Invert search pattern" "--invert-grep")
(magit-log:-G) ;2
(magit-log:-S) ;2
(magit-log:-L) ;2
(7 "=m" "Omit merges" "--no-merges")
(7 "=p" "First parent" "--first-parent")]
["History simplification"
( "-D" "Simplify by decoration" "--simplify-by-decoration")
(magit:--)
( "-f" "Follow renames when showing single-file log" "--follow") ;3
(6 "/s" "Only commits changing given paths" "--sparse")
(7 "/d" "Only selected commits plus meaningful history" "--dense")
(7 "/a" "Only commits existing directly on ancestry path" "--ancestry-path")
(6 "/f" "Do not prune history" "--full-history")
(7 "/m" "Prune some history" "--simplify-merges")]
["Commit ordering"
(magit-log:--*-order)
("-r" "Reverse order" "--reverse")]
["Formatting"
("-g" "Show graph" "--graph") ;1
("-c" "Show graph in color" "--color") ;2
("-d" "Show refnames" "--decorate") ;3
("=S" "Show signatures" "--show-signature") ;1
("-h" "Show header" "++header") ;4
("-p" "Show diffs" ("-p" "--patch")) ;2
("-s" "Show diffstats" "--stat")] ;2
]))
;;;###autoload (autoload 'magit-log "magit-log" nil t)
(transient-define-prefix magit-log ()
"Show a commit or reference log."
:man-page "git-log"
:class 'magit-log-prefix
[magit-log-infix-arguments]
[["Log"
("l" "current" magit-log-current)
("h" "HEAD" magit-log-head)
("u" "related" magit-log-related)
("o" "other" magit-log-other)]
[""
("L" "local branches" magit-log-branches)
("b" "all branches" magit-log-all-branches)
("a" "all references" magit-log-all)
(7 "B" "matching branches" magit-log-matching-branches)
(7 "T" "matching tags" magit-log-matching-tags)
(7 "m" "merged" magit-log-merged)]
["Reflog"
("r" "current" magit-reflog-current)
("H" "HEAD" magit-reflog-head)
("O" "other" magit-reflog-other)]
[:if (lambda ()
(and (fboundp 'magit--any-wip-mode-enabled-p)
(magit--any-wip-mode-enabled-p)))
:description "Wiplog"
("i" "index" magit-wip-log-index)
("w" "worktree" magit-wip-log-worktree)]
["Other"
(5 "s" "shortlog" magit-shortlog)]])
;;;###autoload (autoload 'magit-log-refresh "magit-log" nil t)
(transient-define-prefix magit-log-refresh ()
"Change the arguments used for the log(s) in the current buffer."
:man-page "git-log"
:class 'magit-log-refresh-prefix
[:if-mode magit-log-mode
magit-log-infix-arguments]
[:if-not-mode magit-log-mode
:description "Arguments"
(magit-log:-n)
(magit-log:--*-order)
("-g" "Show graph" "--graph")
("-c" "Show graph in color" "--color")
("-d" "Show refnames" "--decorate")]
[["Refresh"
("g" "buffer" magit-log-refresh)
("s" "buffer and set defaults" transient-set-and-exit)
("w" "buffer and save defaults" transient-save-and-exit)]
["Margin"
(magit-toggle-margin)
(magit-cycle-margin-style)
(magit-toggle-margin-details)
(magit-toggle-log-margin-style)]
[:if-mode magit-log-mode
:description "Toggle"
("b" "buffer lock" magit-toggle-buffer-lock)]]
(interactive)
(cond
((not (eq transient-current-command 'magit-log-refresh))
(pcase major-mode
('magit-reflog-mode
(user-error "Cannot change log arguments in reflog buffers"))
('magit-cherry-mode
(user-error "Cannot change log arguments in cherry buffers")))
(transient-setup 'magit-log-refresh))
(t
(pcase-let ((`(,args ,files) (magit-log-arguments)))
(setq magit-buffer-log-args args)
(unless (derived-mode-p 'magit-log-select-mode)
(setq magit-buffer-log-files files)))
(magit-refresh))))
;;;; Infix Commands
(transient-define-argument magit-log:-n ()
:description "Limit number of commits"
:class 'transient-option
;; For historic reasons (and because it easy to guess what "-n"
;; stands for) this is the only argument where we do not use the
;; long argument ("--max-count").
:shortarg "-n"
:argument "-n"
:reader #'transient-read-number-N+)
(transient-define-argument magit:--author ()
:description "Limit to author"
:class 'transient-option
:key "-A"
:argument "--author="
:reader #'magit-transient-read-person)
(transient-define-argument magit-log:--since ()
:description "Limit to commits since"
:class 'transient-option
:key "=s"
:argument "--since="
:reader #'transient-read-date)
(transient-define-argument magit-log:--until ()
:description "Limit to commits until"
:class 'transient-option
:key "=u"
:argument "--until="
:reader #'transient-read-date)
(transient-define-argument magit-log:--*-order ()
:description "Order commits by"
:class 'transient-switches
:key "-o"
:argument-format "--%s-order"
:argument-regexp "\\(--\\(topo\\|author-date\\|date\\)-order\\)"
:choices '("topo" "author-date" "date"))
(transient-define-argument magit-log:--grep ()
:description "Search messages"
:class 'transient-option
:key "-F"
:argument "--grep=")
(transient-define-argument magit-log:-G ()
:description "Search changes"
:class 'transient-option
:argument "-G")
(transient-define-argument magit-log:-S ()
:description "Search occurrences"
:class 'transient-option
:argument "-S")
(transient-define-argument magit-log:-L ()
:description "Trace line evolution"
:class 'transient-option
:argument "-L"
:reader #'magit-read-file-trace)
(defun magit-read-file-trace (&rest _ignored)
(let ((file (magit-read-file-from-rev "HEAD" "File"))
(trace (magit-read-string "Trace")))
(concat trace ":" file)))
;;;; Setup Commands
(defvar-keymap magit-log-read-revs-map
:parent crm-local-completion-map
"SPC" #'self-insert-command)
(defun magit-log-read-revs (&optional use-current)
(or (and use-current (and-let* ((buf (magit-get-current-branch))) (list buf)))
(let ((crm-separator "\\(\\.\\.\\.?\\|[, ]\\)")
(crm-local-completion-map magit-log-read-revs-map))
(split-string (magit-completing-read-multiple
"Log rev,s: "
(magit-list-refnames nil t)
nil nil nil 'magit-revision-history
(or (magit-branch-or-commit-at-point)
(and (not use-current)
(magit-get-previous-branch)))
nil t)
"[, ]" t))))
(defun magit-log-read-pattern (option)
"Read a string from the user to pass as parameter to OPTION."
(magit-read-string (format "Type a pattern to pass to %s" option)))
;;;###autoload
(defun magit-log-current (revs &optional args files)
"Show log for the current branch.
When `HEAD' is detached or with a prefix argument show log for
one or more revs read from the minibuffer."
(interactive (cons (magit-log-read-revs t)
(magit-log-arguments)))
(magit-log-setup-buffer revs args files))
;;;###autoload
(defun magit-log-head (&optional args files)
"Show log for `HEAD'."
(interactive (magit-log-arguments))
(magit-log-setup-buffer (list "HEAD") args files))
;;;###autoload
(defun magit-log-related (revs &optional args files)
"Show log for the current branch, its upstream and its push target.
When the upstream is a local branch, then also show its own
upstream. When `HEAD' is detached, then show log for that, the
previously checked out branch and its upstream and push-target."
(interactive
(cons (let ((current (magit-get-current-branch))
head rebase target upstream upup)
(unless current
(setq rebase (magit-rebase--get-state-lines "head-name"))
(cond (rebase
(setq rebase (magit-ref-abbrev rebase))
(setq current rebase)
(setq head "HEAD"))
((setq current (magit-get-previous-branch)))))
(cond (current
(setq current
(magit--propertize-face current 'magit-branch-local))
(setq target (magit-get-push-branch current t))
(setq upstream (magit-get-upstream-branch current))
(when upstream
(setq upup (and (magit-local-branch-p upstream)
(magit-get-upstream-branch upstream)))))
((setq head "HEAD")))
(delq nil (list current head target upstream upup)))
(magit-log-arguments)))
(magit-log-setup-buffer revs args files))
;;;###autoload
(defun magit-log-other (revs &optional args files)
"Show log for one or more revs read from the minibuffer.
The user can input any revision or revisions separated by a
space, or even ranges, but only branches and tags, and a
representation of the commit at point, are available as
completion candidates."
(interactive (cons (magit-log-read-revs)
(magit-log-arguments)))
(magit-log-setup-buffer revs args files))
;;;###autoload
(defun magit-log-branches (&optional args files)
"Show log for all local branches and `HEAD'."
(interactive (magit-log-arguments))
(magit-log-setup-buffer (if (magit-get-current-branch)
(list "--branches")
(list "HEAD" "--branches"))
args files))
;;;###autoload
(defun magit-log-matching-branches (pattern &optional args files)
"Show log for all branches matching PATTERN and `HEAD'."
(interactive (cons (magit-log-read-pattern "--branches") (magit-log-arguments)))
(magit-log-setup-buffer
(list "HEAD" (format "--branches=%s" pattern))
args files))
;;;###autoload
(defun magit-log-matching-tags (pattern &optional args files)
"Show log for all tags matching PATTERN and `HEAD'."
(interactive (cons (magit-log-read-pattern "--tags") (magit-log-arguments)))
(magit-log-setup-buffer
(list "HEAD" (format "--tags=%s" pattern))
args files))
;;;###autoload
(defun magit-log-all-branches (&optional args files)
"Show log for all local and remote branches and `HEAD'."
(interactive (magit-log-arguments))
(magit-log-setup-buffer (if (magit-get-current-branch)
(list "--branches" "--remotes")
(list "HEAD" "--branches" "--remotes"))
args files))
;;;###autoload
(defun magit-log-all (&optional args files)
"Show log for all references and `HEAD'."
(interactive (magit-log-arguments))
(magit-log-setup-buffer (if (magit-get-current-branch)
(list "--all")
(list "HEAD" "--all"))
args files))
;;;###autoload
(defun magit-log-buffer-file (&optional follow beg end)
"Show log for the blob or file visited in the current buffer.
With a prefix argument or when `--follow' is an active log
argument, then follow renames. When the region is active,
restrict the log to the lines that the region touches."
(interactive (cons current-prefix-arg (magit-file-region-line-numbers)))
(require 'magit)
(if-let ((file (magit-file-relative-name)))
(magit-log-setup-buffer
(list (or magit-buffer-refname
(magit-get-current-branch)
"HEAD"))
(let ((args (car (magit-log-arguments))))
(when (and follow (not (member "--follow" args)))
(push "--follow" args))
(when (and beg end)
(setq args (cons (format "-L%s,%s:%s" beg end file)
(cl-delete "-L" args :test
#'string-prefix-p)))
(setq file nil))
args)
(and file (list file))
magit-log-buffer-file-locked)
(user-error "Buffer isn't visiting a file")))
;;;###autoload
(defun magit-log-trace-definition (file fn rev)
"Show log for the definition at point."
(interactive (list (or (magit-file-relative-name)
(user-error "Buffer isn't visiting a file"))
(or (funcall magit-log-trace-definition-function)
(user-error "No function at point found"))
(or magit-buffer-refname
(magit-get-current-branch)
"HEAD")))
(require 'magit)
(magit-log-setup-buffer
(list rev)
(cons (format "-L:%s%s:%s"
(string-replace ":" "\\:" (regexp-quote fn))
(if (derived-mode-p 'lisp-mode 'emacs-lisp-mode)
;; Git doesn't treat "-" the same way as
;; "_", leading to false-positives such as
;; "foo-suffix" being considered a match
;; for "foo". Wing it.
"\\( \\|$\\)"
;; We could use "\\b" here, but since Git
;; already does something equivalent, that
;; isn't necessary.
"")
file)
(cl-delete "-L" (car (magit-log-arguments))
:test #'string-prefix-p))
nil magit-log-buffer-file-locked))
(defun magit-diff-trace-definition ()
"Show log for the definition at point in a diff."
(interactive)
(pcase-let ((`(,buf ,pos) (magit-diff-visit-file--noselect)))
(magit--with-temp-position buf pos
(call-interactively #'magit-log-trace-definition))))
;;;###autoload
(defun magit-log-merged (commit branch &optional args files)
"Show log for the merge of COMMIT into BRANCH.
More precisely, find merge commit M that brought COMMIT into
BRANCH, and show the log of the range \"M^1..M\". If COMMIT is
directly on BRANCH, then show approximately
`magit-log-merged-commit-count' surrounding commits instead.
This command requires git-when-merged, which is available from
https://github.com/mhagger/git-when-merged."
(interactive
(append (let ((commit (magit-read-branch-or-commit "Log merge of commit")))
(list commit
(magit-read-other-branch "Merged into" commit)))
(magit-log-arguments)))
(unless (magit-git-executable-find "git-when-merged")
(user-error "This command requires git-when-merged (%s)"
"https://github.com/mhagger/git-when-merged"))
(let (exit m)
(with-temp-buffer
(save-excursion
(setq exit (magit-process-git t "when-merged" "-c"
(magit-abbrev-arg)
commit branch)))
(setq m (buffer-substring-no-properties (point) (line-end-position))))
(if (zerop exit)
(magit-log-setup-buffer (list (format "%s^1..%s" m m))
args files nil commit)
;; Output: "<ref><lots of spaces><message>".
;; This is not the same as `string-trim'.
(setq m (string-trim-left (substring m (string-match " " m))))
(if (equal m "Commit is directly on this branch.")
(let* ((from (format "%s~%d" commit
(/ magit-log-merged-commit-count 2)))
(to (- (car (magit-rev-diff-count branch commit t))
(/ magit-log-merged-commit-count 2)))
(to (if (<= to 0)
branch
(format "%s~%s" branch to))))
(unless (magit-rev-verify-commit from)
(setq from (magit-git-string "rev-list" "--max-parents=0"
commit)))
(magit-log-setup-buffer (list (concat from ".." to))
(cons "--first-parent" args)
files nil commit))
(user-error "Could not find when %s was merged into %s: %s"
commit branch m)))))
;;;; Limit Commands
(defun magit-log-toggle-commit-limit ()
"Toggle the number of commits the current log buffer is limited to.
If the number of commits is currently limited, then remove that
limit. Otherwise set it to 256."
(interactive)
(magit-log-set-commit-limit (lambda (&rest _) nil)))
(defun magit-log-double-commit-limit ()
"Double the number of commits the current log buffer is limited to."
(interactive)
(magit-log-set-commit-limit '*))
(defun magit-log-half-commit-limit ()
"Half the number of commits the current log buffer is limited to."
(interactive)
(magit-log-set-commit-limit '/))
(defun magit-log-set-commit-limit (fn)
(let* ((val magit-buffer-log-args)
(arg (seq-find (##string-match "^-n\\([0-9]+\\)?$" %) val))
(num (and arg (string-to-number (match-string 1 arg))))
(num (if num (funcall fn num 2) 256)))
(setq val (remove arg val))
(setq magit-buffer-log-args
(if (and num (> num 0))
(cons (format "-n%d" num) val)
val)))
(magit-refresh))
(defun magit-log-get-commit-limit (&optional args)
(and-let* ((str (seq-find (##string-match "^-n\\([0-9]+\\)?$" %)
(or args magit-buffer-log-args))))
(string-to-number (match-string 1 str))))
;;;; Mode Commands
(defun magit-log-bury-buffer (&optional arg)
"Bury the current buffer or the revision buffer in the same frame.
Like `magit-mode-bury-buffer' (which see) but with a negative
prefix argument instead bury the revision buffer, provided it
is displayed in the current frame."
(interactive "p")
(if (< arg 0)
(let* ((buf (magit-get-mode-buffer 'magit-revision-mode))
(win (and buf (get-buffer-window buf (selected-frame)))))
(if win
(with-selected-window win
(with-current-buffer buf
(magit-mode-bury-buffer (> (abs arg) 1))))
(user-error "No revision buffer in this frame")))
(magit-mode-bury-buffer (> arg 1))))
;;;###autoload
(defun magit-log-move-to-parent (&optional n)
"Move to the Nth parent of the current commit."
(interactive "p")
(when (and (derived-mode-p 'magit-log-mode)
(magit-section-match 'commit))
(let* ((section (magit-current-section))
(parent-rev (format "%s^%s" (oref section value) (or n 1))))
(if-let ((parent-hash (magit-rev-parse "--short" parent-rev)))
(if-let ((parent (seq-find (##equal (oref % value) parent-hash)
(magit-section-siblings section 'next))))
(magit-section-goto parent)
(user-error
(substitute-command-keys
(concat "Parent " parent-hash " not found. Try typing "
"\\[magit-log-double-commit-limit] first"))))
(user-error "Parent %s does not exist" parent-rev)))))
(defun magit-log-move-to-revision (rev)
"Read a revision and move to it in current log buffer.
If the chosen reference or revision isn't being displayed in
the current log buffer, then inform the user about that and do
nothing else.
If invoked outside any log buffer, then display the log buffer
of the current repository first; creating it if necessary."
(interactive
(list (or (magit-completing-read
"In log, jump to"
(magit-list-refnames nil t)
nil nil nil 'magit-revision-history
(or (and-let* ((rev (magit-commit-at-point)))
(magit-rev-fixup-target rev))
(magit-get-current-branch)))
(user-error "Nothing selected"))))
(with-current-buffer
(cond ((derived-mode-p 'magit-log-mode)
(current-buffer))
((and-let* ((buf (magit-get-mode-buffer 'magit-log-mode)))
(pop-to-buffer-same-window buf)))
(t
(apply #'magit-log-all-branches (magit-log-arguments))))
(unless (magit-log-goto-commit-section (magit-rev-abbrev rev))
(user-error "%s isn't visible in the current log buffer" rev))))
;;;; Shortlog Commands
;;;###autoload (autoload 'magit-shortlog "magit-log" nil t)
(transient-define-prefix magit-shortlog ()
"Show a history summary."
:man-page "git-shortlog"
:value '("--numbered" "--summary")
["Arguments"
("-n" "Sort by number of commits" ("-n" "--numbered"))
("-s" "Show commit count summary only" ("-s" "--summary"))
("-e" "Show email addresses" ("-e" "--email"))
("-g" "Group commits by" "--group="
:choices ("author" "committer" "trailer:"))
(7 "-f" "Format string" "--format=")
(7 "-w" "Linewrap" "-w" :class transient-option)]
["Shortlog"
("s" "since" magit-shortlog-since)
("r" "range" magit-shortlog-range)])
(defun magit-git-shortlog (rev args)
(let ((dir default-directory))
(with-current-buffer (get-buffer-create "*magit-shortlog*")
(setq default-directory dir)
(setq buffer-read-only t)
(let ((inhibit-read-only t))
(erase-buffer)
(save-excursion
(magit-git-insert "shortlog" args rev))
(switch-to-buffer-other-window (current-buffer))))))
;;;###autoload
(defun magit-shortlog-since (rev args)
"Show a history summary for commits since REV."
(interactive
(list (magit-read-branch-or-commit "Shortlog since" (magit-get-current-tag))
(transient-args 'magit-shortlog)))