git.fiddlerwoaroof.com
Browse code

feat: fix graphing

Edward Langley authored on 02/11/2023 07:42:10
Showing 1 changed files
... ...
@@ -1,21 +1,28 @@
1
-(in-package :fwoar.cl-git)
1
+(defpackage :co.fwoar.cl-git.graph
2
+  (:use :cl :fwoar.cl-git)
3
+  (:export ))
4
+(in-package :co.fwoar.cl-git.graph)
2 5
 
3 6
 (defclass git-graph ()
4 7
   ((%repo :initarg :repo :reader repo)
5 8
    (%depth :initarg :depth :reader depth)
9
+   (%stops :initarg :stops :reader stops :initform ())
6 10
    (%branches :reader branches)
7 11
    (%node-cache :reader node-cache :initform (make-hash-table :test 'equal))
8 12
    (%edge-cache :reader edge-cache :initform (make-hash-table :test 'equal))))
9 13
 
10 14
 (defmethod initialize-instance :after ((object git-graph) &key)
11
-  (setf (slot-value object '%branches)
12
-        (fw.lu:alist-string-hash-table
13
-         (funcall (data-lens:over
14
-                   (<>1 (data-lens:applying #'cons)
15
-                        (data-lens:transform-head
16
-                         (serapeum:op (subseq _1 0 (min (length _1) 7))))
17
-                        #'reverse))
18
-                  (branches (repo object))))))
15
+  (setf
16
+   (slot-value object '%branches)
17
+   (fw.lu:alist-string-hash-table
18
+    (funcall (data-lens:over
19
+              (data-lens:<>1 (data-lens:applying #'cons)
20
+                             (data-lens:transform-head
21
+                              (serapeum:op (subseq _1 0
22
+                                                   (min (length _1)
23
+                                                        8))))
24
+                             #'reverse))
25
+             (fwoar.cl-git::branches (repo object))))))
19 26
 
20 27
 (defun git-graph (repo)
21 28
   (fw.lu:new 'git-graph repo))
... ...
@@ -23,43 +30,51 @@
23 30
 (defun get-commit-parents (repository commit)
24 31
   #+lispworks
25 32
   (declare (notinline mismatch serapeum:string-prefix-p))
26
-  (map 'list 
27
-       (serapeum:op (second (partition #\space _)))
28
-       (remove-if-not (lambda (it)
29
-                        (serapeum:string-prefix-p "parent" it))
30
-                      (nth-value 1 (parse-commit
31
-                                    (split-object
32
-                                     (chipz:decompress nil (chipz:make-dstate 'chipz:zlib)
33
-                                                       (loose-object repository
34
-                                                                     commit))))))))
33
+  (when commit
34
+    (co.fwoar.git:with-repository (repository)
35
+      (alexandria:when-let*
36
+          ((ref (fwoar.cl-git:ensure-ref commit))
37
+           (direct-obj (fwoar.cl-git::extract-object
38
+                        ref))
39
+           (obj (etypecase direct-obj
40
+                  (fwoar.cl-git::delta
41
+                   (fwoar.cl-git::-extract-object-of-type
42
+                    :commit
43
+                    (fwoar.cl-git::trace-bases
44
+                     (fwoar.cl-git::packed-ref-pack
45
+                      ref)
46
+                     direct-obj)
47
+                    fwoar.cl-git::*git-repository*
48
+                    :hash (fwoar.cl-git::ref-hash ref)))
49
+                  (fwoar.cl-git::git-object
50
+                   direct-obj)))
51
+           (parents (fwoar.cl-git:component
52
+                     :parents
53
+                     obj)))
54
+        (when parents
55
+          parents)))))
35 56
 
36 57
 (defmethod cl-dot:graph-object-node ((graph git-graph) (commit string))
37
-  (alexandria:ensure-gethash commit
38
-                             (node-cache graph)
39
-                             (make-instance 'cl-dot:node
40
-                                            :attributes `(:label ,(gethash #1=(subseq commit 0 7)
41
-                                                                           (branches graph)
42
-                                                                           #1#)))))
58
+  (alexandria:ensure-gethash
59
+   commit
60
+   (node-cache graph)
61
+   (make-instance 'cl-dot:node
62
+                  :attributes `(:label ,(gethash #1=(subseq commit 0 8)
63
+                                                 (branches graph)
64
+                                                 #1#)))))
43 65
 
44
-(defmethod cl-dot:graph-object-points-to ((graph git-graph) (commit string))
45
-  #+nil
46
-  (loop
47
-    for cur = (list commit) then parents
48
-    for parents = (let ((f (get-commit-parents (repo graph) (car cur))))
49
-                    f)
50
-    until (or (not parents)
51
-              (cdr parents))
52
-    finally (return (or parents
53
-                        (when (not (equal commit (car cur)))
54
-                          cur))))
55
-
56
-  (funcall (data-lens:<>1 (data-lens:over (serapeum:op
57
-                                            (setf (gethash (list commit _1)
58
-                                                           (edge-cache graph))
59
-                                                  t)
60
-                                            _1))
61
-                          (data-lens:exclude (serapeum:op
62
-                                               (gethash (list commit _1)
63
-                                                        (edge-cache graph))))
64
-                          (data-lens:over (serapeum:op (subseq _ 0 7))))
65
-           (get-commit-parents (repo graph) commit)))
66
+(defmethod cl-dot:graph-object-points-to
67
+    ((graph git-graph) (commit string))
68
+  (unless (member commit (stops graph)
69
+                  :test 'serapeum:string-prefix-p)
70
+    (funcall (data-lens:<>1
71
+              (data-lens:over (serapeum:op
72
+                                (setf (gethash (list commit _1)
73
+                                               (edge-cache graph))
74
+                                      t)
75
+                                _1))
76
+              (data-lens:exclude (serapeum:op
77
+                                   (gethash (list commit _1)
78
+                                            (edge-cache graph))))
79
+              (data-lens:over (serapeum:op (subseq _ 0 8))))
80
+             (get-commit-parents (repo graph) commit))))