diff options
| author | Magali Lemes <magalilemes00@gmail.com> | 2020-12-23 21:31:55 -0300 |
|---|---|---|
| committer | Ricardo Wurmus <rekado@elephly.net> | 2022-07-04 10:34:52 +0200 |
| commit | 38f088544ca87737e62f35de9072dd15ffafbd7f (patch) | |
| tree | a016cf67e7f9bed7e313f6c3ff5946bc55b46510 | |
| parent | fa61849b40c7e94772833316d4eff20853831893 (diff) | |
scripts: git: log: Add '--format'.
* guix/scripts/git/log.scm (%formats): New variable.
(show-help, %options): Add '--format' option.
(show-commit): Adjust adding new arguments.
(get-commits): Return a list of all commits.
| -rw-r--r-- | guix/scripts/git/log.scm | 116 |
1 files changed, 91 insertions, 25 deletions
diff --git a/guix/scripts/git/log.scm b/guix/scripts/git/log.scm index 63f1913e786..c5338d43a84 100644 --- a/guix/scripts/git/log.scm +++ b/guix/scripts/git/log.scm | |||
| @@ -18,29 +18,39 @@ | |||
| 18 | 18 | ||
| 19 | (define-module (guix scripts git log) | 19 | (define-module (guix scripts git log) |
| 20 | #:use-module (git) | 20 | #:use-module (git) |
| 21 | #:use-module ((guix channels) | 21 | #:use-module (guix channels) |
| 22 | #:select (%default-guix-channel | ||
| 23 | channel-url)) | ||
| 24 | #:use-module ((guix git) #:select (url-cache-directory)) | 22 | #:use-module ((guix git) #:select (url-cache-directory)) |
| 25 | #:use-module (guix scripts) | 23 | #:use-module (guix scripts) |
| 24 | #:use-module (guix scripts pull) | ||
| 26 | #:use-module (guix ui) | 25 | #:use-module (guix ui) |
| 27 | #:use-module (ice-9 format) | 26 | #:use-module (ice-9 format) |
| 28 | #:use-module (ice-9 match) | 27 | #:use-module (ice-9 match) |
| 29 | #:use-module (srfi srfi-1) | 28 | #:use-module (srfi srfi-1) |
| 29 | #:use-module (srfi srfi-19) | ||
| 30 | #:use-module (srfi srfi-26) | 30 | #:use-module (srfi srfi-26) |
| 31 | #:use-module (srfi srfi-37) | 31 | #:use-module (srfi srfi-37) |
| 32 | #:export (guix-git-log)) | 32 | #:export (guix-git-log)) |
| 33 | 33 | ||
| 34 | 34 | ||
| 35 | (define %formats | ||
| 36 | '("oneline" "medium" "full")) | ||
| 37 | |||
| 35 | (define %options | 38 | (define %options |
| 36 | (list (option '(#\h "help") #f #f | 39 | (list (option '(#\h "help") #f #f |
| 37 | (lambda args | 40 | (lambda args |
| 38 | (show-help) | 41 | (show-help) |
| 39 | (exit 0))) | 42 | (exit 0))) |
| 40 | 43 | ||
| 41 | (option '("checkout-path") #f #f | 44 | (option '("channel-cache-path") #f #t |
| 45 | (lambda (opt name arg result) | ||
| 46 | (alist-cons 'channel-cache-path | ||
| 47 | (if arg (string->symbol arg) 'guix) | ||
| 48 | result))) | ||
| 49 | (option '("format") #t #f | ||
| 42 | (lambda (opt name arg result) | 50 | (lambda (opt name arg result) |
| 43 | (alist-cons 'checkout-path? #t result))) | 51 | (unless (member arg %formats) |
| 52 | (leave (G_ "~a: invalid format~%") arg)) | ||
| 53 | (alist-cons 'format (string->symbol arg) result))) | ||
| 44 | (option '("oneline") #f #f | 54 | (option '("oneline") #f #f |
| 45 | (lambda (opt name arg result) | 55 | (lambda (opt name arg result) |
| 46 | (alist-cons 'oneline? #t result))))) | 56 | (alist-cons 'oneline? #t result))))) |
| @@ -52,7 +62,10 @@ | |||
| 52 | (display (G_ "Usage: guix git log [OPTIONS...] | 62 | (display (G_ "Usage: guix git log [OPTIONS...] |
| 53 | Show Guix commit logs.\n")) | 63 | Show Guix commit logs.\n")) |
| 54 | (display (G_ " | 64 | (display (G_ " |
| 55 | --checkout-path show checkout path")) | 65 | --channel-cache-path[=CHANNEL] |
| 66 | show checkout path from CHANNEL")) | ||
| 67 | (display (G_ " | ||
| 68 | --format=FORMAT show log according to FORMAT")) | ||
| 56 | (display (G_ " | 69 | (display (G_ " |
| 57 | --oneline show short hash and summary of five first commits")) | 70 | --oneline show short hash and summary of five first commits")) |
| 58 | (display (G_ " | 71 | (display (G_ " |
| @@ -60,39 +73,92 @@ Show Guix commit logs.\n")) | |||
| 60 | (newline) | 73 | (newline) |
| 61 | (show-bug-report-information)) | 74 | (show-bug-report-information)) |
| 62 | 75 | ||
| 63 | (define (show-checkout-path) | 76 | (define (show-channel-cache-path channel) |
| 64 | (display (url-cache-directory (channel-url %default-guix-channel))) | 77 | (define channels (channel-list '())) |
| 65 | (newline)) | 78 | |
| 79 | (let ((found-channel (find (lambda (element) | ||
| 80 | (equal? channel (channel-name element))) | ||
| 81 | channels))) | ||
| 82 | (if found-channel | ||
| 83 | (format #t "~a~%" (url-cache-directory (channel-url found-channel))) | ||
| 84 | (leave (G_ "~a: channel not found~%") (symbol->string channel))))) | ||
| 66 | 85 | ||
| 67 | (define commit-short-id | 86 | (define commit-short-id |
| 68 | (compose (cut string-take <> 7) oid->string commit-id)) | 87 | (compose (cut string-take <> 7) oid->string commit-id)) |
| 69 | 88 | ||
| 70 | (define (show-commit commit) | 89 | ;; --oneline = show-commit 'oneline #t |
| 71 | (format #t (G_ "~a ~a~%") (commit-short-id commit) (commit-summary commit))) | 90 | (define (show-commit commit fmt abbrev-commit) |
| 91 | (match fmt | ||
| 92 | ('oneline | ||
| 93 | (format #t "~a ~a~%" | ||
| 94 | (if abbrev-commit | ||
| 95 | (commit-short-id commit) | ||
| 96 | (oid->string (commit-id commit))) | ||
| 97 | (commit-summary commit))) | ||
| 98 | ('medium | ||
| 99 | (let ((author (commit-author commit)) | ||
| 100 | (merge-commit (if (> (commit-parentcount commit) 1) #t #f))) | ||
| 101 | (format #t "commit ~a~[~%Merge:~]~{ ~a~}~%Author: ~a <~a>~%Date: ~a~%~%~a~%" | ||
| 102 | (if abbrev-commit | ||
| 103 | (commit-short-id commit) | ||
| 104 | (oid->string (commit-id commit))) | ||
| 105 | (if merge-commit 0 1) ;; show "Merge:" | ||
| 106 | (if merge-commit (map commit-short-id (commit-parents commit)) '()) | ||
| 107 | (signature-name author) | ||
| 108 | (signature-email author) | ||
| 109 | (date->string | ||
| 110 | (time-utc->date | ||
| 111 | (make-time time-utc 0 | ||
| 112 | (time-time (signature-when author))) | ||
| 113 | (* 60 (time-offset (signature-when author)))) | ||
| 114 | "~a ~b ~e ~H:~M:~S ~Y ~z") | ||
| 115 | (commit-message commit)))) | ||
| 116 | ('full | ||
| 117 | (let ((merge-commit (if (> (commit-parentcount commit) 1) #t #f)) | ||
| 118 | (author (commit-author commit)) | ||
| 119 | (committer (commit-committer commit))) | ||
| 120 | (format #t "commit ~a~[~%Merge:~]~{ ~a~}~%Author: ~a <~a>~%Commit: ~a <~a>~%~%~a~%" | ||
| 121 | (if abbrev-commit | ||
| 122 | (commit-short-id commit) | ||
| 123 | (oid->string (commit-id commit))) | ||
| 124 | (if merge-commit 0 1) ;; show "Merge:" | ||
| 125 | (if merge-commit (map commit-short-id (commit-parents commit)) '()) | ||
| 126 | (signature-name author) | ||
| 127 | (signature-email author) | ||
| 128 | (signature-name committer) | ||
| 129 | (signature-email committer) | ||
| 130 | (commit-message commit)))))) | ||
| 72 | 131 | ||
| 73 | ;; currently showing 5 latest commits | 132 | ;; returns a list of commits from path |
| 74 | (define (get-commits path) | 133 | (define (get-commits path) |
| 75 | (let* ((repository (repository-open path)) | 134 | (let* ((repository (repository-open path)) |
| 76 | (latest-commit (commit-lookup repository (reference-target (repository-head repository))))) | 135 | (latest-commit (commit-lookup repository (reference-target (repository-head repository))))) |
| 77 | (for-each show-commit (take | 136 | (define commits (let loop ((commit latest-commit) |
| 78 | (let loop ((commit latest-commit) | 137 | (res (list latest-commit))) |
| 79 | (res (list latest-commit))) | 138 | (match (commit-parents commit) |
| 80 | (match (commit-parents commit) | 139 | (() (reverse res)) |
| 81 | (() (reverse res)) | 140 | ((head . tail) |
| 82 | ((head . tail) | 141 | (loop head (cons head res)))))) |
| 83 | (loop head (cons head res))))) | 142 | commits)) |
| 84 | 5)))) | ||
| 85 | 143 | ||
| 86 | (define (guix-git-log . args) | 144 | (define (guix-git-log . args) |
| 87 | (define options | 145 | (define options |
| 88 | (parse-command-line args %options (list %default-options))) | 146 | (parse-command-line args %options (list %default-options))) |
| 89 | 147 | ||
| 90 | (let ((checkout-path? (assoc-ref options 'checkout-path?)) | 148 | (let ((channel-cache (assoc-ref options 'channel-cache-path)) |
| 91 | (oneline? (assoc-ref options 'oneline?))) | 149 | (oneline? (assoc-ref options 'oneline?)) |
| 150 | (format-type (assoc-ref options 'format))) | ||
| 92 | (with-error-handling | 151 | (with-error-handling |
| 93 | (cond | 152 | (cond |
| 94 | (checkout-path? | 153 | (channel-cache |
| 95 | (show-checkout-path)) | 154 | (show-channel-cache-path channel-cache)) |
| 96 | (oneline? | 155 | (oneline? |
| 97 | (let ((cache (url-cache-directory (channel-url %default-guix-channel)))) | 156 | (let ((cache (url-cache-directory (channel-url %default-guix-channel)))) |
| 98 | (get-commits cache))))))) | 157 | (for-each (lambda (commit-list) |
| 158 | (show-commit commit-list 'oneline #t)) | ||
| 159 | (take (get-commits cache) 5)))) | ||
| 160 | (format-type | ||
| 161 | (let ((cache (url-cache-directory (channel-url %default-guix-channel)))) | ||
| 162 | (for-each (lambda (commit-list) | ||
| 163 | (show-commit commit-list format-type #f)) | ||
| 164 | (take (get-commits cache) 5)))))))) | ||
