File: diff.scm

package info (click to toggle)
guile-git 0.9.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: lisp: 6,231; makefile: 132; sh: 8
file content (219 lines) | stat: -rw-r--r-- 9,771 bytes parent folder | download | duplicates (2)
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
;;; Guile-Git --- GNU Guile bindings of libgit2
;;; Copyright © 2021 Julien Lepiller <julien@lepiller.eu>
;;;
;;; This file is part of Guile-Git.
;;;
;;; Guile-Git 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.
;;;
;;; Guile-Git 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 Guile-Git.  If not, see <http://www.gnu.org/licenses/>.

(define-module (git diff)
  #:use-module (system foreign)
  #:use-module (git errors)
  #:use-module (git bindings)
  #:use-module (git types)
  #:use-module (git structs)
  #:export (DIFF-OPTIONS-VERSION
            GIT-SUBMODULE-IGNORE-UNSPECIFIED
            GIT-SUBMODULE-IGNORE-NONE
            GIT-SUBMODULE-IGNORE-UNTRACKED
            GIT-SUBMODULE-IGNORE-DIRTY
            GIT-SUBMODULE-IGNORE-ALL
            GIT-DIFF-FORMAT-PATCH
            GIT-DIFF-FORMAT-PATCH-HEADER
            GIT-DIFF-FORMAT-RAW
            GIT-DIFF-FORMAT-NAME-ONLY
            GIT-DIFF-FORMAT-NAME-STATUS
            GIT-DIFF-FORMAT-PATCH-ID
            GIT-DELTA-UNMODIFIED
            GIT-DELTA-ADDED
            GIT-DELTA-DELETED
            GIT-DELTA-MODIFIED
            GIT-DELTA-RENAMED
            GIT-DELTA-COPIED
            GIT-DELTA-IGNORED
            GIT-DELTA-UNTRACKED
            GIT-DELTA-TYPECHANGE
            GIT-DELTA-UNREADABLE
            GIT-DELTA-CONFLICTED
            
            make-diff-options
            diff-index-to-index
            diff-index-to-workdir
            diff-tree-to-index
            diff-tree-to-tree
            diff-tree-to-workdir
            diff-print
            diff->string
            diff-foreach
            diff-fold))

;;; https://libgit2.org/libgit2/#HEAD/group/diff
(define DIFF-OPTIONS-VERSION 1)

(define GIT-SUBMODULE-IGNORE-UNSPECIFIED -1)
(define GIT-SUBMODULE-IGNORE-NONE 1)
(define GIT-SUBMODULE-IGNORE-UNTRACKED 2)
(define GIT-SUBMODULE-IGNORE-DIRTY 3)
(define GIT-SUBMODULE-IGNORE-ALL 4)

(define GIT-DIFF-FORMAT-PATCH 1)        ;; full git diff
(define GIT-DIFF-FORMAT-PATCH-HEADER 2) ;; just the file headers of patch
(define GIT-DIFF-FORMAT-RAW 3)          ;; like git diff --raw
(define GIT-DIFF-FORMAT-NAME-ONLY 4)    ;; like git diff --name-only
(define GIT-DIFF-FORMAT-NAME-STATUS 5)  ;; like git diff --name-status
(define GIT-DIFF-FORMAT-PATCH-ID 6)     ;; git diff as used by git patch-id

(define GIT-DELTA-UNMODIFIED 0)  ;; no changes
(define GIT-DELTA-ADDED 1)       ;; entry does not exist in old version
(define GIT-DELTA-DELETED 2)     ;; entry does not exist in new version
(define GIT-DELTA-MODIFIED 3)    ;; entry content changed between old and new
(define GIT-DELTA-RENAMED 4)     ;; entry was renamed between old and new
(define GIT-DELTA-COPIED 5)      ;; entry was copied from another old entry
(define GIT-DELTA-IGNORED 6)     ;; entry is ignored item in workdir
(define GIT-DELTA-UNTRACKED 7)   ;; entry is untracked item in workdir
(define GIT-DELTA-TYPECHANGE 8)  ;; type of entry changed between old and new
(define GIT-DELTA-UNREADABLE 9)  ;; entry is unreadable
(define GIT-DELTA-CONFLICTED 10) ;; entry in the index is conflicted

(define make-diff-options
  ;; Return a <diff-options> structure for use with DIFF procedures.
  (let ((proc (libgit2->procedure* "git_diff_options_init"
                                   `(* ,unsigned-int))))
    (lambda* (#:key (version DIFF-OPTIONS-VERSION))
      (let ((diff-options (make-diff-options-bytestructure)))
        (proc (diff-options->pointer diff-options) version)
        diff-options))))

(define* diff-index-to-index
  (let ((proc (libgit2->procedure* "git_diff_index_to_index" '(* * * * *))))
    (lambda* (repository old-index new-index #:optional (options (make-diff-options)))
      (let ((out (make-double-pointer)))
        (proc out
              (repository->pointer repository)
              (index->pointer old-index)
              (index->pointer new-index)
              (diff-options->pointer options))
        (pointer->diff (dereference-pointer out))))))

(define* diff-index-to-workdir
  (let ((proc (libgit2->procedure* "git_diff_index_to_workdir" '(* * * *))))
    (lambda* (repository index #:optional (options (make-diff-options)))
      (let ((out (make-double-pointer)))
        (proc out
              (repository->pointer repository)
              (index->pointer index)
              (diff-options->pointer options))
        (pointer->diff (dereference-pointer out))))))

(define* diff-tree-to-index
  (let ((proc (libgit2->procedure* "git_diff_tree_to_index" '(* * * * *))))
    (lambda* (repository old-tree index #:optional (options (make-diff-options)))
      (let ((out (make-double-pointer)))
        (proc out
              (repository->pointer repository)
              (tree->pointer old-tree)
              (index->pointer index)
              (diff-options->pointer options))
        (pointer->diff (dereference-pointer out))))))

(define* diff-tree-to-tree
  (let ((proc (libgit2->procedure* "git_diff_tree_to_tree" '(* * * * *))))
    (lambda* (repository old-tree new-tree #:optional (options (make-diff-options)))
      (let ((out (make-double-pointer)))
        (proc out
              (repository->pointer repository)
              (tree->pointer old-tree)
              (tree->pointer new-tree)
              (diff-options->pointer options))
        (pointer->diff (dereference-pointer out))))))

(define* diff-tree-to-workdir
  (let ((proc (libgit2->procedure* "git_diff_tree_to_workdir" '(* * * * ))))
    (lambda* (repository old-tree #:optional (options (make-diff-options)))
      (let ((out (make-double-pointer)))
        (proc out
              (repository->pointer repository)
              (tree->pointer old-tree)
              (diff-options->pointer options))
        (pointer->diff (dereference-pointer out))))))

(define* diff-print
  (let ((proc (libgit2->procedure* "git_diff_print" `(* ,int * *))))
    (lambda* (diff callback #:optional (format GIT-DIFF-FORMAT-PATCH))
      ;; Returning a non-zero value from the callbacks will terminate the
      ;; iteration and return the non-zero value to the caller.
      (let ((callback* (procedure->pointer int
                                           (lambda (delta hunk line _)
                                             (callback
                                               (pointer->diff-delta delta)
                                               (pointer->diff-hunk hunk)
                                               (pointer->diff-line line)))
                                           (list '* '* '* '*))))
        (proc (diff->pointer diff) format callback* %null-pointer)))))

(define* diff->string
  (let ((proc (libgit2->procedure* "git_diff_to_buf" `(* * ,int))))
    (lambda* (diff #:optional (format GIT-DIFF-FORMAT-PATCH))
      (let ((out (make-buffer)))
        (proc out (diff->pointer diff) format)
        (buffer-content/string out)))))

(define* diff-foreach
  (let ((proc (libgit2->procedure* "git_diff_foreach" '(* * * * * *))))
    (lambda* (diff file-cb binary-cb hunk-cb line-cb)
      ;; Returning a non-zero value from any of the callbacks will terminate
      ;; the iteration and return the value to the user.
      (let ((file-cb* (procedure->pointer int
                                          (lambda (delta progress _)
                                            (file-cb
                                              (pointer->diff-delta delta)
                                              progress))
                                          (list '* float '*)))
            (binary-cb* (procedure->pointer int
                                            (lambda (delta binary _)
                                              (binary-cb
                                                (pointer->diff-delta delta)
                                                (pointer->diff-binary binary)))
                                            (list '* '* '*)))
            (hunk-cb* (procedure->pointer int
                                          (lambda (delta hunk _)
                                            (hunk-cb
                                              (pointer->diff-delta delta)
                                              (pointer->diff-hunk hunk)))
                                          (list '* '* '*)))
            (line-cb* (procedure->pointer int
                                          (lambda (delta hunk line _)
                                            (line-cb
                                              (pointer->diff-delta delta)
                                              (pointer->diff-hunk hunk)
                                              (pointer->diff-line line)))
                                          (list '* '* '* '*))))
        (proc (diff->pointer diff) file-cb* binary-cb* hunk-cb* line-cb* %null-pointer)))))

(define (diff-fold file-proc binary-proc hunk-proc line-proc knil diff)
  (let ((out knil))
    (diff-foreach
      diff
      (lambda (delta progress)
        (set! out (file-proc delta progress out))
        0)
      (lambda (delta binary)
        (set! out (binary-proc delta binary out))
        0)
      (lambda (delta hunk)
        (set! out (hunk-proc delta hunk out))
        0)
      (lambda (delta hunk line)
        (set! out (line-proc delta hunk line out))
        0))
    out))