summaryrefslogtreecommitdiff
path: root/tissue/git.scm
blob: 70f0de996ada93d6b13c17d8c05840b5e69abdfd (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
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
;;; tissue --- Text based issue tracker
;;; Copyright © 2022, 2023 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of tissue.
;;;
;;; tissue 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.
;;;
;;; tissue 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 tissue.  If not, see <https://www.gnu.org/licenses/>.

(define-module (tissue git)
  #:use-module (rnrs arithmetic bitwise)
  #:use-module (rnrs conditions)
  #:use-module (rnrs exceptions)
  #:use-module (rnrs hashtables)
  #:use-module (rnrs io ports)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-171)
  #:use-module (ice-9 match)
  #:use-module (git)
  #:use-module (git types)
  #:use-module ((system foreign) #:select (%null-pointer
                                           dereference-pointer
                                           pointer->string
                                           string->pointer))
  #:use-module (bytestructures guile)
  #:use-module (tissue utils)
  #:export (reference-set-target!
            reference-symbolic-target
            condition-git-error
            git-top-level
            %current-git-repository
            current-git-repository
            commit-author-date
            git-tracked-file?
            git-tracked-files
            file-modification-table
            clone-options
            call-with-temporary-checkout))

;; We bind additional functions from libgit2 that are not already
;; bound in guile-git. TODO: Contribute them to guile-git.

(define diff-find-similar!
  (let ((proc (libgit2->procedure* "git_diff_find_similar" '(* *))))
    (lambda (diff)
      (proc (diff->pointer diff) %null-pointer)
      diff)))

(define reference-set-target!
  (let ((proc (libgit2->procedure* "git_reference_set_target" '(* * * *))))
    (lambda* (reference oid #:optional log-message)
      (let ((out (make-double-pointer)))
        (proc out
              (reference->pointer reference)
              (oid->pointer oid)
              (if log-message
                  (string->pointer log-message)
                  %null-pointer))
        (pointer->reference (dereference-pointer out))))))

(define reference-symbolic-target
  (let ((proc (libgit2->procedure '* "git_reference_symbolic_target" '(*))))
    (lambda (reference)
      (pointer->string
       (proc (reference->pointer reference))))))

(define (condition-git-error condition)
  "Return <git-error> object from CONDITION. If none, return #f."
  (and (irritants-condition? condition)
       (find git-error? (condition-irritants condition))))

(define %current-git-repository
  (make-parameter #f))

(define (current-git-repository)
  "Return the current git repository, either the repository specified by
the %current-git-repository parameter or the repository at the current
directory."
  (or (%current-git-repository)
      (repository-open-ext (getcwd) (list))))

(define (git-top-level)
  "Return the top-level directory of the current git repository."
  (let ((repository-directory
         (repository-directory (current-git-repository))))
    (if (repository-bare? (current-git-repository))
        repository-directory
        (dirname repository-directory))))

(define (head-tree repository)
  "Return tree of HEAD in REPOSITORY."
  (commit-tree
   (commit-lookup repository
                  (reference-name->oid repository "HEAD"))))

(define (commit-author-date commit)
  "Return the author date of COMMIT as an SRFI-19 date object."
  (let ((time (signature-when (commit-author commit))))
    (time-monotonic->date
     (make-time time-monotonic
                0
                (time-time time))
     (* 60 (time-offset time)))))

(define (git-tracked-file? path repository)
  "Return non-#f if PATH is a file tracked in REPOSITORY. Else, return
#f."
  (guard (c ((let ((git-error (condition-git-error c)))
               (and git-error
                    (= (git-error-code git-error)
                       GIT_ENOTFOUND)))
             #f))
    (tree-entry-bypath (head-tree repository)
                       path)))

(define* (git-tracked-files #:optional (repository (current-git-repository)))
  "Return a list of all files and directories tracked in
@var{repository}. The returned paths are relative to the top-level
directory of @var{repository} and do not have a leading slash."
  (tree-list (head-tree repository)))

(define (commit-file-changes repository commit)
  "Return a list of pairs describing files modified by COMMIT with
respect to its first parent in REPOSITORY. Each pair maps the old
filename before COMMIT to the new filename after COMMIT."
  (match (commit-parents commit)
    ((parent _ ...)
     (let ((diff (diff-tree-to-tree repository
                                    (commit-tree parent)
                                    (commit-tree commit))))
       (diff-find-similar! diff)
       (diff-fold (lambda (delta progress result)
                    (cons (cons (diff-file-path (diff-delta-old-file delta))
                                (diff-file-path (diff-delta-new-file delta)))
                          result))
                  (lambda (delta binary result)
                    result)
                  (lambda (delta hunk result)
                    result)
                  (lambda (delta hunk line result)
                    result)
                  (list)
                  diff)))
    (() (map (lambda (file)
               (cons file file))
             (tree-list (commit-tree commit))))))

(define (file-modification-table repository)
  "Return a hashtable mapping files to the list of commits in REPOSITORY
that modified them."
  (let ((result (make-hashtable string-hash string=?))
        (renames (make-hashtable string-hash string=?)))
    (fold-commits
     (lambda (commit _)
       (map (match-lambda
              ((old-file . new-file)
               ;; Map old filename to current filename if they are
               ;; different. Note that this manner of following renames
               ;; requires a linear git history and will not work with
               ;; branch merges.
               (unless (string=? old-file new-file)
                 (hashtable-set! renames old-file new-file))
               (hashtable-update! result
                                  ;; If necessary, translate old
                                  ;; filename to current filename.
                                  (hashtable-ref renames old-file old-file)
                                  (cut cons commit <>)
                                  (list))))
            (commit-file-changes repository commit)))
     #f
     repository)
    result))

(define* (clone-options #:key bare?)
  (let ((clone-options (make-clone-options)))
    (bytestructure-set!
     (clone-options-bytestructure clone-options)
     'bare
     (if bare? 1 0))
    clone-options))

(define (call-with-temporary-checkout repository proc)
  "Call PROC with a temporary checkout of REPOSITORY, and delete the
checkout when PROC returns or exits non-locally."
  (call-with-temporary-directory
   (lambda (temporary-checkout)
     (clone repository temporary-checkout)
     (proc temporary-checkout))
   ;; The system-dependent temporary directory
   (dirname (tmpnam))))