Commits

evhan committed 9ef88fe

updates for new libgit2 diff API

Comments (0)

Files changed (2)

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; diff.h
 
-(define-foreign-type diff-list (c-pointer "git_diff_list"))
+(define-foreign-type diff (c-pointer "git_diff"))
 (define-foreign-type diff-file-fn (c-pointer "git_diff_file_fn"))
 
 (define-foreign-enum-type (diff-line char)
   (unsigned-int16 mode   diff-file-mode))
 
 (define-foreign-record-type (diff-delta git_diff_delta)
-  ((struct diff-file) old_file   diff-delta-old-file)
-  ((struct diff-file) new_file   diff-delta-new-file)
   (delta              status     diff-delta-status)
-  (unsigned-int32     similarity diff-delta-similarity)
-  (unsigned-int32     flags      diff-delta-flags))
-
-(define-foreign-record-type (diff-range git_diff_range)
-  (int old_start diff-range-old-start)
-  (int old_lines diff-range-old-lines)
-  (int new_start diff-range-new-start)
-  (int new_lines diff-range-new-lines))
+  (unsigned-int32     flags      diff-delta-flags)
+  (unsigned-int16     similarity diff-delta-similarity)
+  (unsigned-int16     nfiles     diff-delta-nfiles)
+  ((struct diff-file) old_file   diff-delta-old-file)
+  ((struct diff-file) new_file   diff-delta-new-file))
 
-(define diff-list-free (foreign-lambda void git_diff_list_free diff-list))
-(define diff-merge     (foreign-lambda/retval git_diff_merge diff-list diff-list))
+(define diff-merge        (foreign-lambda/retval git_diff_merge diff diff))
+(define diff-find-similar (foreign-lambda/retval git_diff_find_similar diff c-pointer)) ; git_diff_find_options
+(define diff-num-deltas   (foreign-lambda size_t git_diff_num_deltas diff))
+(define diff-num-deltas-of-type (foreign-lambda size_t git_diff_num_deltas_of_type diff delta))
+(define diff-free         (foreign-lambda void git_diff_free diff))
 
 (define-syntax foreign-lambda/diff
   (lambda (e . _)
            (types (cddr e))
            (args  (map gensym types)))
       `(lambda (repo ,@args)
-         (let-location ((diffs diff-list))
+         (let-location ((diff diff))
            ((foreign-lambda/retval ,name
-            (c-pointer diff-list) repository ,@types diff-options)
-            (location diffs)      repo       ,@args  #f)
-           (set-finalizer! diffs diff-list-free))))))
+            (c-pointer diff) repository ,@types diff-options)
+            (location diff)  repo       ,@args  #f)
+           (set-finalizer! diff diff-free))))))
 
 (define diff-tree-to-tree     (foreign-lambda/diff git_diff_tree_to_tree tree tree))
 (define diff-tree-to-index    (foreign-lambda/diff git_diff_tree_to_index tree index))
 (define diff-index-to-workdir (foreign-lambda/diff git_diff_index_to_workdir index))
 (define diff-tree-to-workdir  (foreign-lambda/diff git_diff_tree_to_workdir tree))
 
-;; (define-foreign-type diff-file-cb (c-pointer "git_diff_file_cb"))
 (define-foreign-type diff-file-cb (function int ((const diff-delta) float c-pointer)))
 (define-external (diff_file_cb (diff-delta diff) (float progress) (c-pointer fn)) int
   ((callback-lookup fn) diff progress) 0)
 
-;; (define-foreign-type diff-hunk-cb (c-pointer "git_diff_hunk_cb"))
-(define-foreign-type diff-hunk-cb (function int ((const diff-delta) (const diff-range) (const c-string) size_t c-pointer)))
-(define-external (diff_hunk_cb (diff-delta diff) (diff-range range) (float progress) (c-pointer fn)) int
-  ((callback-lookup fn) diff range) 0)
-
-;; (define-foreign-type diff-data-cb (c-pointer "git_diff_data_cb"))
-(define-foreign-type diff-data-cb (function int ((const diff-delta) (const diff-range) char (const c-string) size_t c-pointer)))
-(define-external (diff_data_cb (diff-delta diff) (diff-range range) (char line) (c-string content) (size_t len) (c-pointer fn)) int
-  ((callback-lookup fn) diff range line content) 0)
-
 (define (diff-foreach fn diffs)
   (call-with-callback fn
    (lambda (callback)
      (guard-errors git_diff_foreach
       ((foreign-safe-lambda int git_diff_foreach
-        diff-list diff-file-cb            diff-hunk-cb diff-data-cb c-pointer)
-        diffs     (location diff_file_cb) #f           #f           callback)))))
+        diff  diff-file-cb            c-pointer c-pointer c-pointer)
+        diffs (location diff_file_cb) #f        #f        callback)))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; errors.h
    (or (diff-new-file diff)
        (diff-old-file diff))))
 
-;; Helper for `diff`, below.
-;;
-;; Adds a refcount to the diff-list, to delay GC until all its
-;; diff-deltas are unreachable.
-(define (build-diff-list diffs)
-  (let ((acc '()))
-    (git-diff-foreach
-     (lambda (delta* progress)
-       (set! acc (cons (pointer->diff-delta diffs delta*) acc)))
-     diffs)
-    acc))
-
 (define diff
-  (case-lambda
-    ((repo)
-     (diff repo (index-open repo)))
-    ((repo object)
-     (build-diff-list
-      (let ((repo* (repository->pointer repo)))
-        (cond ((index? object)
-               (git-diff-index-to-workdir repo* (index->pointer object)))
-              ((tree? object)
-               (git-diff-tree-to-workdir repo* (tree->pointer  object)))
-              (else
-               (git-error 'diff "Undiffable object" object))))))
-    ((repo tree object)
-     (build-diff-list
-      (let ((repo* (repository->pointer repo))
-            (tree* (tree->pointer tree)))
-        (cond ((tree? object)
-               (git-diff-tree-to-tree repo* tree* (tree->pointer object)))
-              ((index? object)
-               (git-diff-tree-to-index repo* tree* (index->pointer object)))
-              (else
-               (git-error 'diff "Undiffable object" object))))))))
+  (let ((build-diff-list
+         (lambda (diffs)
+           (begin0-let ((acc '()))
+             (git-diff-foreach
+              (lambda (delta* progress)
+                (set! acc (cons (pointer->diff-delta diffs delta*) acc)))
+              diffs)))))
+    (case-lambda
+      ((repo)
+       (diff repo (index-open repo)))
+      ((repo object)
+       (build-diff-list
+        (let ((repo* (repository->pointer repo)))
+          (cond ((index? object)
+                 (git-diff-index-to-workdir repo* (index->pointer object)))
+                ((tree? object)
+                 (git-diff-tree-to-workdir repo* (tree->pointer  object)))
+                (else
+                 (git-error 'diff "Undiffable object" object))))))
+      ((repo tree object)
+       (build-diff-list
+        (let ((repo* (repository->pointer repo))
+              (tree* (tree->pointer tree)))
+          (cond ((tree? object)
+                 (git-diff-tree-to-tree repo* tree* (tree->pointer object)))
+                ((index? object)
+                 (git-diff-tree-to-index repo* tree* (index->pointer object)))
+                (else
+                 (git-error 'diff "Undiffable object" object)))))))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;