summaryrefslogtreecommitdiff
path: root/guix/scripts/git/log.scm
blob: c5338d43a842650c70c3cd516e743aa2a82ffe52 (plain)
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
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Magali Lemes <magalilemes00@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix 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.
;;;
;;; GNU Guix 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 GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix scripts git log)
  #:use-module (git)
  #:use-module (guix channels)
  #:use-module ((guix git) #:select (url-cache-directory))
  #:use-module (guix scripts)
  #:use-module (guix scripts pull)
  #:use-module (guix ui)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-37)
  #:export (guix-git-log))


(define %formats
  '("oneline" "medium" "full"))

(define %options
  (list (option '(#\h "help") #f #f
                (lambda args
                  (show-help)
                  (exit 0)))

        (option '("channel-cache-path") #f #t
                (lambda (opt name arg result)
                  (alist-cons 'channel-cache-path
                              (if arg (string->symbol arg) 'guix)
                              result)))
        (option '("format") #t #f
                (lambda (opt name arg result)
                  (unless (member arg %formats)
                    (leave (G_ "~a: invalid format~%") arg))
                  (alist-cons 'format (string->symbol arg) result)))
        (option '("oneline") #f #f
                (lambda (opt name arg result)
                  (alist-cons 'oneline? #t result)))))

(define %default-options
  '())

(define (show-help)
  (display (G_ "Usage: guix git log [OPTIONS...]
Show Guix commit logs.\n"))
  (display (G_ "
      --channel-cache-path[=CHANNEL]
                         show checkout path from CHANNEL"))
  (display (G_ "
      --format=FORMAT    show log according to FORMAT"))
  (display (G_ "
      --oneline          show short hash and summary of five first commits"))
  (display (G_ "
  -h, --help             display this help and exit"))
  (newline)
  (show-bug-report-information))

(define (show-channel-cache-path channel)
  (define channels (channel-list '()))

  (let ((found-channel (find (lambda (element)
                               (equal? channel (channel-name element)))
                             channels)))
    (if found-channel
        (format #t "~a~%" (url-cache-directory (channel-url found-channel)))
        (leave (G_ "~a: channel not found~%") (symbol->string channel)))))

(define commit-short-id
  (compose (cut string-take <> 7) oid->string commit-id))

;; --oneline = show-commit 'oneline #t
(define (show-commit commit fmt abbrev-commit)
  (match fmt
         ('oneline
          (format #t "~a ~a~%"
                  (if abbrev-commit
                      (commit-short-id commit)
                      (oid->string (commit-id commit)))
                  (commit-summary commit)))
         ('medium
          (let ((author (commit-author commit))
                (merge-commit (if (> (commit-parentcount commit) 1) #t #f)))
            (format #t "commit ~a~[~%Merge:~]~{ ~a~}~%Author: ~a <~a>~%Date:   ~a~%~%~a~%"
                    (if abbrev-commit
                        (commit-short-id commit)
                        (oid->string (commit-id commit)))
                    (if merge-commit 0 1) ;; show "Merge:"
                    (if merge-commit (map commit-short-id (commit-parents commit)) '())
                    (signature-name  author)
                    (signature-email author)
                    (date->string
                     (time-utc->date
                      (make-time time-utc 0
                                 (time-time (signature-when author)))
                      (* 60 (time-offset (signature-when author))))
                     "~a ~b ~e ~H:~M:~S ~Y ~z")
                    (commit-message commit))))
         ('full
          (let ((merge-commit (if (> (commit-parentcount commit) 1) #t #f))
                (author    (commit-author commit))
                (committer (commit-committer commit)))
            (format #t "commit ~a~[~%Merge:~]~{ ~a~}~%Author: ~a <~a>~%Commit: ~a <~a>~%~%~a~%"
                    (if abbrev-commit
                        (commit-short-id commit)
                        (oid->string (commit-id commit)))
                    (if merge-commit 0 1) ;; show "Merge:"
                    (if merge-commit (map commit-short-id (commit-parents commit)) '())
                    (signature-name  author)
                    (signature-email author)
                    (signature-name  committer)
                    (signature-email committer)
                    (commit-message commit))))))

;; returns a list of commits from path
(define (get-commits path)
  (let* ((repository (repository-open path))
         (latest-commit (commit-lookup repository (reference-target (repository-head repository)))))
    (define commits (let loop ((commit latest-commit)
                               (res (list latest-commit)))
                      (match (commit-parents commit)
                             (() (reverse res))
                             ((head . tail)
                              (loop head (cons head res))))))
    commits))

(define (guix-git-log . args)
  (define options
    (parse-command-line args %options (list %default-options)))

  (let ((channel-cache      (assoc-ref options 'channel-cache-path))
        (oneline?           (assoc-ref options 'oneline?))
        (format-type        (assoc-ref options 'format)))
    (with-error-handling
      (cond
       (channel-cache
        (show-channel-cache-path channel-cache))
       (oneline?
        (let ((cache (url-cache-directory (channel-url %default-guix-channel))))
          (for-each (lambda (commit-list)
                      (show-commit commit-list 'oneline #t))
                    (take (get-commits cache) 5))))
       (format-type
        (let ((cache (url-cache-directory (channel-url %default-guix-channel))))
          (for-each (lambda (commit-list)
                      (show-commit commit-list format-type #f))
                    (take (get-commits cache) 5))))))))